assoc.c 16 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2007, 2009-2012, 2014-2017 Free Software Foundation,
   Inc.

   GNU Mailutils is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   GNU Mailutils is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with GNU Mailutils.  If not, see <http://www.gnu.org/licenses/>. */

#if HAVE_CONFIG_H
# include <config.h>
#endif

#include <stdlib.h>
#include <string.h>
#include <mailutils/types.h>
#include <mailutils/assoc.h>
#include <mailutils/errno.h>
#include <mailutils/error.h>
#include <mailutils/iterator.h>
#include <mailutils/util.h>
#include <mailutils/cstr.h>
#include <mailutils/sys/iterator.h>

/* |hash_size| defines a sequence of symbol table sizes. These are prime
   numbers, the distance between each pair of them grows exponentially,
   starting from 64. Hardly someone will need more than 16411 symbols, and
   even if someone will, it is easy enough to add more numbers to the
   sequence. */

static unsigned int hash_size[] = {
    37,   101,  229,  487, 1009, 2039, 4091, 8191, 16411
};

/* |max_rehash| keeps the number of entries in |hash_size| table. */
static unsigned int max_rehash = sizeof (hash_size) / sizeof (hash_size[0]);

struct _mu_assoc_elem
{
  char *name;
  struct _mu_assoc_elem *next, *prev;
  char *data;
};

struct _mu_assoc
{
  int flags;
  unsigned int hash_num;  /* Index to hash_size table */
  struct _mu_assoc_elem **tab;
  struct _mu_assoc_elem *head, *tail;
  mu_deallocator_t free;
  mu_iterator_t itr;
};

static void
assoc_elem_unlink (mu_assoc_t assoc, int idx)
{
  struct _mu_assoc_elem *p;

  p = assoc->tab[idx]->prev;
  if (p)
    p->next = assoc->tab[idx]->next;
  else
    assoc->head = assoc->tab[idx]->next;

  p = assoc->tab[idx]->next;
  if (p)
    p->prev = assoc->tab[idx]->prev;
  else
    assoc->tail = assoc->tab[idx]->prev;

  assoc->tab[idx]->prev = assoc->tab[idx]->next = NULL;
}

static void
assoc_elem_link (mu_assoc_t assoc, int idx)
{
  assoc->tab[idx]->next = NULL;
  assoc->tab[idx]->prev = assoc->tail;

  if (assoc->tail)
    assoc->tail->next = assoc->tab[idx];
  else
    assoc->head = assoc->tab[idx];
  assoc->tail = assoc->tab[idx];
}

static unsigned
hash (const char *name, unsigned long hash_num)
{
  unsigned i;
	
  for (i = 0; *name; name++)
    {
      i <<= 1;
      i ^= *(unsigned char*) name;
    }
  return i % hash_size[hash_num];
};

static int assoc_find_slot (mu_assoc_t assoc, const char *name,
			    int *install, unsigned *slot);

static int
assoc_rehash (mu_assoc_t assoc)
{
  struct _mu_assoc_elem **old_tab = assoc->tab;
  struct _mu_assoc_elem **new_tab;
  unsigned int i;
  unsigned int hash_num = assoc->hash_num + 1;
  
  if (hash_num >= max_rehash)
    return MU_ERR_BUFSPACE;

  new_tab = calloc (hash_size[hash_num], sizeof (new_tab[0]));
  if (!new_tab)
    return errno;
  assoc->tab = new_tab;
  if (old_tab)
    {
      assoc->hash_num = hash_num;
      for (i = 0; i < hash_size[hash_num-1]; i++)
	{
	  if (old_tab[i])
	    {
	      int tmp;
	      unsigned slot;
	      int rc = assoc_find_slot (assoc, old_tab[i]->name,
					&tmp, &slot);
	      if (rc)
		return rc;
	      assoc->tab[slot] = old_tab[i];
	    }
	}
      free (old_tab);
    }
  return 0;
}

static void
assoc_free_elem (mu_assoc_t assoc, unsigned idx)
{
  if (assoc->tab[idx])
    {
      assoc_elem_unlink (assoc, idx);
      if (assoc->free)
	assoc->free (assoc->tab[idx]->data);
      if (!(assoc->flags & MU_ASSOC_COPY_KEY))
	free (assoc->tab[idx]->name);
      assoc->tab[idx] = NULL;
    }
}

static int
assoc_remove (mu_assoc_t assoc, unsigned idx)
{
  unsigned int i, j, r;

  if (!(idx < hash_size[assoc->hash_num]))
    return EINVAL;

  mu_iterator_delitem (assoc->itr, assoc->tab[idx]);
  assoc_free_elem (assoc, idx);
  
  for (i = idx;;)
    {
      assoc->tab[i] = NULL;
      j = i;

      do
	{
	  if (++i >= hash_size[assoc->hash_num])
	    i = 0;
	  if (!assoc->tab[i])
	    return 0;
	  r = hash (assoc->tab[i]->name, assoc->hash_num);
	}
      while ((j < r && r <= i) || (i < j && j < r) || (r <= i && i < j));

      if (j != i)
	assoc->tab[j] = assoc->tab[i];
    }
  return 0;
}

#define name_cmp(assoc,a,b) (((assoc)->flags & MU_ASSOC_ICASE) ? \
                             mu_c_strcasecmp(a,b) : strcmp(a,b))

static int
assoc_find_slot (mu_assoc_t assoc, const char *name,
		 int *install, unsigned *slot)
{
  int rc;
  unsigned i, pos;
  struct _mu_assoc_elem *elem;
  
  if (!assoc->tab)
    {
      if (install)
	{
	  rc = assoc_rehash (assoc);
	  if (rc)
	    return rc;
	}
      else
	return MU_ERR_NOENT;
    }

  pos = hash (name, assoc->hash_num);

  for (i = pos; (elem = assoc->tab[i]);)
    {
      if (name_cmp (assoc, elem->name, name) == 0)
	{
	  if (install)
	    *install = 0;
	  *slot = i;
	  return 0;
	}
      
      if (++i >= hash_size[assoc->hash_num])
	i = 0;
      if (i == pos)
	break;
    }

  if (!install)
    return MU_ERR_NOENT;
  
  if (!elem)
    {
      *slot = i;
      *install = 1;
      return 0; 
    }

  if ((rc = assoc_rehash (assoc)) != 0)
    return rc;

  return assoc_find_slot (assoc, name, install, slot);
}

int
mu_assoc_create (mu_assoc_t *passoc, int flags)
{
  mu_assoc_t assoc = calloc (1, sizeof (*assoc));
  if (!assoc)
    return ENOMEM;
  assoc->flags = flags;
  *passoc = assoc;
  return 0;
}

void
mu_assoc_clear (mu_assoc_t assoc)
{
  unsigned i, hs;
  
  if (!assoc || !assoc->tab)
    return;

  hs = hash_size[assoc->hash_num];
  for (i = 0; i < hs; i++)
    assoc_free_elem (assoc, i);
}

void
mu_assoc_destroy (mu_assoc_t *passoc)
{
  mu_assoc_t assoc;
  if (passoc && (assoc = *passoc) != NULL)
    {
      mu_assoc_clear (assoc);
      free (assoc->tab);
      free (assoc);
      *passoc = NULL;
    }
}

int
mu_assoc_set_destroy_item (mu_assoc_t assoc, mu_deallocator_t fn)
{
  if (!assoc)
    return EINVAL;
  assoc->free = fn;
  return 0;
}

int
mu_assoc_lookup (mu_assoc_t assoc, const char *name, void *dataptr)
{
  int rc;
  unsigned idx;
  
  if (!assoc || !name)
    return EINVAL;

  rc = assoc_find_slot (assoc, name, NULL, &idx);
  if (rc == 0)
    {
      if (dataptr)
	*(void**)dataptr = assoc->tab[idx]->data;
    }
  return rc;
}


void *
mu_assoc_get (mu_assoc_t assoc, const char *name)
{
  int rc;
  unsigned idx;
  
  if (!assoc || !name)
    return NULL;

  rc = assoc_find_slot (assoc, name, NULL, &idx);
  if (rc == 0)
    return assoc->tab[idx]->data;
  return NULL;
}

int
mu_assoc_install (mu_assoc_t assoc, const char *name, void *value)
{
  int rc;
  int inst;
  unsigned idx;
  struct _mu_assoc_elem *elem;
  
  if (!assoc || !name)
    return EINVAL;

  rc = assoc_find_slot (assoc, name, &inst, &idx);
  if (rc)
    return rc;
  if (!inst)
    return MU_ERR_EXISTS;

  elem = malloc (sizeof *elem);
  if (!elem)
    return errno;
      
  if (assoc->flags & MU_ASSOC_COPY_KEY)
    elem->name = (char *) name;
  else
    {
      elem->name = strdup (name);
      if (!elem->name)
	{
	  int rc = errno;
	  free (elem);
	  return rc;
	}
    }
  elem->data = value;
  assoc->tab[idx] = elem;
  assoc_elem_link (assoc, idx);
  return 0;
}

int
mu_assoc_lookup_ref (mu_assoc_t assoc, const char *name, void *dataptr)
{
  int rc;
  unsigned idx;
  
  if (!assoc || !name)
    return EINVAL;

  rc = assoc_find_slot (assoc, name, NULL, &idx);
  if (rc == 0)
    {
      if (dataptr)
	*(void**)dataptr = &assoc->tab[idx]->data;
    }
  return rc;
}

int
mu_assoc_install_ref (mu_assoc_t assoc, const char *name, void *pval)
{
  int rc;
  int inst;
  unsigned idx;

  if (!assoc || !name)
    return EINVAL;
  
  rc = assoc_find_slot (assoc, name, &inst, &idx);
  if (rc)
    return rc;

  if (inst)
    {
      struct _mu_assoc_elem *elem;
      
      elem = malloc (sizeof *elem);
      if (!elem)
	return errno;
      
      if (assoc->flags & MU_ASSOC_COPY_KEY)
	elem->name = (char *) name;
      else
	{
	  elem->name = strdup (name);
	  if (!elem->name)
	    {
	      int rc = errno;
	      free (elem);
	      return rc;
	    }
	}
      elem->data = NULL;
      assoc->tab[idx] = elem;
      assoc_elem_link (assoc, idx);
    }
  
  *(void**)pval = &assoc->tab[idx]->data;
  return inst ? 0 : MU_ERR_EXISTS;
}  

int
mu_assoc_remove (mu_assoc_t assoc, const char *name)
{
  int rc;
  unsigned idx;

  if (!assoc || !name)
    return EINVAL;
  rc = assoc_find_slot (assoc, name, NULL, &idx);
  if (rc)
    return rc;
  return assoc_remove (assoc, idx);
}

/* Iterator interface */

struct assoc_iterator
{
  mu_assoc_t assoc;
  struct _mu_assoc_elem *elem;
  int backwards; /* true if iterating backwards */
};

static int
itrctl (void *owner, enum mu_itrctl_req req, void *arg)
{
  struct assoc_iterator *itr = owner;
  mu_assoc_t assoc = itr->assoc;

  switch (req)
    {
    case mu_itrctl_tell:
      /* Return current position in the object */
      {
	size_t n = 0;
	struct _mu_assoc_elem *elem;

	for (elem = itr->elem; elem; elem = elem->prev)
	  n++;
	*(size_t*)arg = n;
      }
      break;

    case mu_itrctl_delete:
    case mu_itrctl_delete_nd:
      /* Delete current element */
      if (itr->elem)
	{
	  unsigned i;

	  for (i = 0; i < hash_size[assoc->hash_num]; i++)
	    {
	      if (assoc->tab[i] == itr->elem)
		{
		  if (req == mu_itrctl_delete_nd)
		    assoc->tab[i]->data = NULL;
		  assoc_remove (assoc, i);
		  return 0;
		}
	    }
	}
      return MU_ERR_NOENT;

    case mu_itrctl_replace:
    case mu_itrctl_replace_nd:
      /* Replace current element */
      if (itr->elem == NULL)
	return MU_ERR_NOENT;
      if (req == mu_itrctl_replace && assoc->free)
	assoc->free (itr->elem->data);
      itr->elem->data = arg;
      break;
      
    case mu_itrctl_qry_direction:
      if (!arg)
	return EINVAL;
      else
	*(int*)arg = itr->backwards;
      break;

    case mu_itrctl_set_direction:
      if (!arg)
	return EINVAL;
      else
	itr->backwards = !!*(int*)arg;
      break;

    case mu_itrctl_count:
      if (!arg)
	return EINVAL;
      return mu_assoc_count (assoc, arg);
      
    default:
      return ENOSYS;
    }
  return 0;
}

static int
first (void *owner)
{
  struct assoc_iterator *itr = owner;
  mu_assoc_t assoc = itr->assoc;
  itr->elem = itr->backwards ? assoc->tail : assoc->head;
  return 0;
}

static int
next (void *owner)
{
  struct assoc_iterator *itr = owner;
  itr->elem = itr->backwards ? itr->elem->prev : itr->elem->next;
  return 0;
}

static int
getitem (void *owner, void **pret, const void **pkey)
{
  struct assoc_iterator *itr = owner;

  if (!itr->elem)
    return EINVAL;
  *pret = itr->elem->data;
  if (pkey)
    *pkey = itr->elem->name;
  return 0;
}

static int
finished_p (void *owner)
{
  struct assoc_iterator *itr = owner;
  return itr->elem == NULL;
}

static int
destroy (mu_iterator_t iterator, void *data)
{
  struct assoc_iterator *itr = data;
  mu_iterator_detach (&itr->assoc->itr, iterator);
  free (data);
  return 0;
}

static int
delitem (void *owner, void *item)
{
  struct assoc_iterator *itr = owner;
  return itr->elem == item ? MU_ITR_DELITEM_NEXT : MU_ITR_DELITEM_NOTHING;
}

static int
assoc_data_dup (void **ptr, void *owner)
{
  *ptr = malloc (sizeof (struct assoc_iterator));
  if (*ptr == NULL)
    return ENOMEM;
  memcpy (*ptr, owner, sizeof (struct assoc_iterator));
  return 0;
}

int
mu_assoc_get_iterator (mu_assoc_t assoc, mu_iterator_t *piterator)
{
  mu_iterator_t iterator;
  int status;
  struct assoc_iterator *itr;

  if (!assoc)
    return EINVAL;

  itr = calloc (1, sizeof *itr);
  if (!itr)
    return ENOMEM;
  itr->assoc = assoc;
  itr->elem = NULL;

  status = mu_iterator_create (&iterator, itr);
  if (status)
    {
      free (itr);
      return status;
    }

  mu_iterator_set_first (iterator, first);
  mu_iterator_set_next (iterator, next);
  mu_iterator_set_getitem (iterator, getitem);
  mu_iterator_set_finished_p (iterator, finished_p);
  mu_iterator_set_delitem (iterator, delitem);
  mu_iterator_set_destroy (iterator, destroy);
  mu_iterator_set_dup (iterator, assoc_data_dup);
  mu_iterator_set_itrctl (iterator, itrctl);
  
  mu_iterator_attach (&assoc->itr, iterator);

  *piterator = iterator;
  return 0;
}  

int
mu_assoc_count (mu_assoc_t assoc, size_t *pcount)
{
  size_t length = 0;

  if (!pcount)
    return MU_ERR_OUT_PTR_NULL;
  if (assoc)
    {
      struct _mu_assoc_elem *elt;
      for (elt = assoc->head; elt; elt = elt->next)
	length++;
    }
  *pcount = length;
  return 0;
}

int
mu_assoc_is_empty (mu_assoc_t assoc)
{
  return assoc == NULL || assoc->head == NULL;
}

int
mu_assoc_foreach (mu_assoc_t assoc, mu_assoc_action_t action, void *data)
{
  mu_iterator_t itr;
  int rc;
  
  if (!assoc || !action)
    return EINVAL;
  rc = mu_assoc_get_iterator (assoc, &itr);
  if (rc)
    return rc;
  for (mu_iterator_first (itr); !mu_iterator_is_done (itr);
       mu_iterator_next (itr))
    {
      char *name;
      void *value;

      rc = mu_iterator_current_kv (itr, (const void **)&name, (void**)&value);
      if (rc)
	break;
      
      rc = action (name, value, data);
      if (rc)
	break;
    }
  mu_iterator_destroy (&itr);
  return rc;
}

/* Merges the two NULL-terminated lists, LEFT and RIGHT, using CMP for
   element comparison.  DATA supplies call-specific data for CMP.

   Both LEFT and RIGHT are treated as singly-linked lists, with NEXT pointing
   to the successive element.  PREV pointers are ignored.
   
   Returns the resulting list.
 */
static struct _mu_assoc_elem *
merge (struct _mu_assoc_elem *left, struct _mu_assoc_elem *right,
       mu_assoc_comparator_t cmp, void *data)
{
  struct _mu_assoc_elem *head = NULL, **tailptr = &head, *tmp;

  while (left && right)
    {
      if (cmp (left->name, left->data, right->name, right->data, data) <= 0)
	{
	  tmp = left->next;
	  *tailptr = left;
	  tailptr = &left->next;
	  left = tmp;
	}
      else
	{
	  tmp = right->next;
	  *tailptr = right;
	  tailptr = &right->next;
	  right = tmp;
	}
    }

  *tailptr = left ? left : right;

  return head;
}

/* Sort the singly-linked LIST of LENGTH elements using merge sort algorithm.
   Elements are compared using the CMP function with DATA pointing to
   call-specific data.
   The elements of LIST are linked by the NEXT pointer.  The NEXT pointer of
   the last element (LIST[LENGTH], 1-based) must be NULL.

   Returns the resulting list.

   Side-effects: PREV pointers in the returned list are messed up.
*/
static struct _mu_assoc_elem *
merge_sort (struct _mu_assoc_elem *list, size_t length,
	    mu_assoc_comparator_t cmp, void *data)
{
  struct _mu_assoc_elem *left, *right;
  size_t left_len, right_len, i;
  struct _mu_assoc_elem *elt;
  
  if (length == 1)
    return list;

  if (length == 2)
    {
      elt = list->next;
      if (cmp (list->name, list->data, elt->name, elt->data, data) > 0)
	{
	  elt->next = list;
	  list->next = NULL;
	  return elt;
	}
      return list;
    }

  left = list;
  left_len = (length + 1) / 2;

  right_len = length / 2;
  for (elt = list, i = left_len - 1; i; i--)
    elt = elt->next;

  right = elt->next;
  elt->next = NULL;

  left = merge_sort (left, left_len, cmp, data);
  right = merge_sort (right, right_len, cmp, data);

  return merge (left, right, cmp, data);
}

/* Sort the linked list of elements in ASSOC using merge sort.  CMP points
   to the function to use for comparing two elements.  DATA supplies call-
   specific data for CMP.
*/
int
mu_assoc_sort_r (mu_assoc_t assoc, mu_assoc_comparator_t cmp, void *data)
{
  struct _mu_assoc_elem *head, *prev, *p;
  size_t length;
  
  if (!assoc)
    return EINVAL;
  if (!cmp)
    return 0;

  mu_assoc_count (assoc, &length);
  head = merge_sort (assoc->head, length, cmp, data);
  /* The above call leaves PREV pointers in inconsistent state.  Fix them
     up: */
  for (prev = NULL, p = head; p; prev = p, p = p->next)
    p->prev = prev;

  /* Update list head and tail */
  assoc->head = head;
  assoc->tail = prev;

  return 0;
}