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 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854
|
/*******************************************************************************
*
* MODULE: list
*
********************************************************************************
*
* DESCRIPTION: Generic routines for a doubly linked ring list
*
********************************************************************************
*
* Copyright (c) 2002-2024 Marcus Holland-Moritz. All rights reserved.
* This program is free software; you can redistribute it and/or modify
* it under the same terms as Perl itself.
*
*******************************************************************************/
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "ccattr.h"
#include "memalloc.h"
#include "list.h"
/*----------*/
/* Typedefs */
/*----------*/
typedef struct _link Link;
struct _link {
void *pObj;
Link *prev;
Link *next;
};
struct _linkedList {
Link link;
int size;
#ifdef DEBUG_UTIL_LIST
unsigned state;
#endif
};
#ifdef DEBUG_UTIL_LIST
# define CHANGE_STATE(list) (list)->state++
#else
# define CHANGE_STATE(list) (void) 0
#endif
/*------------------*/
/* Static Functions */
/*------------------*/
static inline Link *GetLink( LinkedList list, int item );
static inline void *Extract( LinkedList list, Link *pLink );
static inline Link *Insert( LinkedList list, Link *pLink, void *pObj );
static void QuickSort( Link *l, Link *r, int size, LLCompareFunc cmp );
/************************************************************
*
* S T A T I C F U N C T I O N S
*
************************************************************/
/*
* GetLink
*
* Get a link by item number.
*
* 0 <= item < list->size
* returns a pointer to the (item)th link
*
* -(list->size) <= item < 0
* returns a pointer to the (list->size+item)th link
*
* otherwise
* return NULL
*/
static inline Link *GetLink( LinkedList list, int item )
{
Link *pLink = &list->link;
if( item < 0 ) {
if( -item > list->size ) /* -1 is last item */
return NULL;
while( item++ < 0 )
pLink = pLink->prev;
}
else { /* item > 0 */
if( item >= list->size ) /* 0 is first item */
return NULL;
while( item-- >= 0 )
pLink = pLink->next;
}
return pLink;
}
/*
* Extract
*
* Extracts a link from its list, frees its
* resources and returns a pointer to the
* associated object.
*/
static inline void *Extract( LinkedList list, Link *pLink )
{
void *pObj = pLink->pObj;
pLink->prev->next = pLink->next;
pLink->next->prev = pLink->prev;
list->size--;
Free( pLink );
return pObj;
}
/*
* Insert
*
* Inserts a new link associated with pObj _before_
* the link pointed to by pLink and returns a pointer
* to the inserted link.
*/
static inline Link *Insert( LinkedList list, Link *pLink, void *pObj )
{
Link *pLinkNew;
AllocF( Link *, pLinkNew, sizeof( Link ) );
pLinkNew->pObj = pObj;
pLinkNew->prev = pLink->prev;
pLinkNew->next = pLink;
pLink->prev->next = pLinkNew;
pLink->prev = pLinkNew;
list->size++;
return pLinkNew;
}
/*
* QuickSort
*
* Adapted quick sort algorithm.
*/
static void QuickSort( Link *l, Link *r, int size, LLCompareFunc cmp )
{
Link *i, *j;
void *p, *t;
int lp, rp;
/* determine pivot */
lp = size / 2;
for( i=l; --lp > 0; i=i->next );
p = i->pObj;
/* initialize vars */
i = l; j = r;
lp = 0; rp = size-1;
/* sort */
for(;;) {
while( cmp( i->pObj, p ) < 0 )
i = i->next, lp++;
if( lp > rp ) break;
while( cmp( j->pObj, p ) > 0 )
j = j->prev, rp--;
if( lp > rp ) break;
/* swap elements */
t = i->pObj;
i->pObj = j->pObj;
j->pObj = t;
i = i->next; lp++;
j = j->prev; rp--;
}
if( rp+1 > 1 )
QuickSort( l, j, rp+1, cmp );
if( size-lp > 1 )
QuickSort( i, r, size-lp, cmp );
}
/************************************************************
*
* G L O B A L F U N C T I O N S
*
************************************************************/
/**
* Constructor
*
* Using the LL_new() function you create an empty linked
* list. If the term linked list scares you, just think of
* it as a flexible array, because the Linked List Library
* won't let you deal with links at all.
*
* \return A handle to the newly created linked list.
*
* \see LL_delete() and LL_destroy()
*/
LinkedList LL_new( void )
{
LinkedList list;
AllocF( LinkedList, list, sizeof( struct _linkedList ) );
list->link.prev = list->link.next = &list->link;
list->link.pObj = NULL;
list->size = 0;
#ifdef DEBUG_UTIL_LIST
list->state = 0;
#endif
return list;
}
/**
* Destructor
*
* LL_delete() will free the resources occupied by a
* linked list. The function will fail silently if the
* associated list is not empty.
* You can also delete a list that is not empty by
* using the LL_destroy() function.
*
* \param list Handle to an existing linked list.
*
* \see LL_new() and LL_destroy()
*/
void LL_delete( LinkedList list )
{
if( list == NULL || list->size )
return;
CHANGE_STATE(list);
Free( list );
}
/**
* Remove all elements from a list
*
* LL_flush() will remove all elements from a linked list,
* optionally calling a destructor function. It will not
* free the resources occupied by the list itself.
*
* \param list Handle to an existing linked list.
*
* \param destroy Pointer to the destructor function
* of the objects contained in the list.
* You can pass NULL if you don't want
* LL_flush() to call object destructors.
*
* \see LL_destroy()
*/
void LL_flush( LinkedList list, LLDestroyFunc destroy )
{
void *pObj;
if( list == NULL )
return;
CHANGE_STATE(list);
while( (pObj = LL_shift( list )) != NULL )
if( destroy ) destroy( pObj );
}
/**
* Extended Destructor
*
* LL_destroy() will, like LL_delete(), free the resources
* occupied by a linked list. However, it will empty the
* the list prior to deleting it, like LL_flush().
*
* \param list Handle to an existing linked list.
*
* \param destroy Pointer to the destructor function
* of the objects contained in the list.
* You can pass NULL if you don't want
* LL_destroy() to call object destructors.
*
* \see LL_new(), LL_delete() and LL_flush()
*/
void LL_destroy( LinkedList list, LLDestroyFunc destroy )
{
if( list == NULL )
return;
CHANGE_STATE(list);
LL_flush( list, destroy );
LL_delete( list );
}
/**
* Cloning a linked list
*
* Using the LL_clone() function to create an exact copy
* of a linked list. If the objects stored in the list
* need to be cloned as well, you can pass a pointer to
* a function that clones each element.
*
* \param list Handle to an existing linked list.
*
* \param func Pointer to the cloning function of
* the objects contained in the list.
* If you pass NULL, the original
* object is stored in the cloned list
* instead of a cloned object.
*
* \return A handle to the cloned linked list.
*
* \see LL_new()
*/
LinkedList LL_clone( ConstLinkedList list, LLCloneFunc func )
{
ListIterator li;
LinkedList clone;
void *pObj;
if( list == NULL )
return NULL;
clone = LL_new();
LL_foreach(pObj, li, list)
LL_push(clone, func ? func(pObj) : pObj);
return clone;
}
/**
* Current size of a list
*
* LL_count() will return the the number of objects that
* a linked list contains.
*
* \param list Handle to an existing linked list.
*
* \return The size of the list or -1 if an invalid handle
* was passed.
*/
int LL_count( ConstLinkedList list )
{
if( list == NULL )
return -1;
AssertValidPtr( list );
return list->size;
}
/**
* Add element to the end of a list.
*
* LL_push() will add a new element to the end of a list.
* If you think of the list as a stack, the function pushes
* a new element on top of the stack.
*
* \param list Handle to an existing linked list.
*
* \param pObj Pointer to an object associated with
* the new list element. The function
* will not add a new element if this
* is NULL.
*
* \see LL_pop()
*/
void LL_push( LinkedList list, void *pObj )
{
if( list == NULL || pObj == NULL )
return;
AssertValidPtr( list );
CHANGE_STATE(list);
(void) Insert( list, &list->link, pObj );
}
/**
* Remove element from the end of a list.
*
* LL_pop() will remove the last element from a list.
* If you think of the list as a stack, the function pops
* an element of the stack.
*
* \param list Handle to an existing linked list.
*
* \return Pointer to the object that was associated with
* the element removed from the list. If the list
* is empty, NULL will be returned.
*
* \see LL_push()
*/
void *LL_pop( LinkedList list )
{
if( list == NULL || list->size == 0 )
return NULL;
AssertValidPtr( list );
CHANGE_STATE(list);
return Extract( list, list->link.prev );
}
/**
* Add element to the start of a list.
*
* LL_unshift() will add a new element to the beginning of a
* list, right before the first element. For an empty list
* this is equivalent to calling LL_push().
*
* \param list Handle to an existing linked list.
*
* \param pObj Pointer to an object associated with
* the new list element. The function
* will not add a new element if this
* is NULL.
*
* \see LL_shift()
*/
void LL_unshift( LinkedList list, void *pObj )
{
if( list == NULL || pObj == NULL )
return;
AssertValidPtr( list );
CHANGE_STATE(list);
(void) Insert( list, list->link.next, pObj );
}
/**
* Remove element from the start of a list.
*
* LL_shift() will remove the first element from a list.
* If the list contains only a single element, this is
* equivalent to calling LL_pop().
*
* \param list Handle to an existing linked list.
*
* \return Pointer to the object that was associated with
* the element removed from the list. If the list
* is empty, NULL will be returned.
*
* \see LL_unshift()
*/
void *LL_shift( LinkedList list )
{
if( list == NULL || list->size == 0 )
return NULL;
AssertValidPtr( list );
CHANGE_STATE(list);
return Extract( list, list->link.next );
}
/**
* Insert a new element into a list.
*
* Using LL_insert(), you can insert a new element at an
* arbitrary position in the list.
* If \a item is out of the valid range, the element will
* not be added.
*
* \param list Handle to an existing linked list.
*
* \param item Position where the new element should
* be inserted.\n
* A value of 0 will insert
* the new element at the start of the
* list, like LL_unshift() would do. A
* value of LL_count() would insert the
* element at the end of the list, like
* LL_push() would do. A negative value
* will count backwards from the end of
* the list. So a value of -1 would also
* add the new element to the end of the
* list.
*
* \param pObj Pointer to an object associated with
* the new list element. The function
* will not add a new element if this
* is NULL.
*
* \see LL_extract()
*/
void LL_insert( LinkedList list, int item, void *pObj )
{
Link *pLink;
if( list == NULL || pObj == NULL )
return;
AssertValidPtr( list );
CHANGE_STATE(list);
/*
* We have to do some faking here because adding to the end
* of the list is a more natural result for item == -1 than
* adding to the position _before_ the last element would be
*/
if( item < 0 )
pLink = item == -1 ? &list->link : GetLink( list, item+1 );
else
pLink = item == list->size ? &list->link : GetLink( list, item );
if( pLink == NULL )
return;
(void) Insert( list, pLink, pObj );
}
/**
* Extract an element from a list.
*
* LL_extract() will remove an arbitrary element from the
* list and return a pointer to the associated object.
*
* \param list Handle to an existing linked list.
*
* \param item Position of the element that should
* be extracted.\n
* A value of 0 will extract the first
* element, like LL_shift(). A negative
* value will count backwards from the
* end of the list. So a value of -1
* will extract the last element, which
* will be equivalent to LL_pop().
*
* \return Pointer to the object that was associated with
* the element removed from the list. If the list
* is empty or \a item is out of range, NULL will
* be returned.
*
* \see LL_insert()
*/
void *LL_extract( LinkedList list, int item )
{
Link *pLink;
if( list == NULL || list->size == 0 )
return NULL;
AssertValidPtr( list );
CHANGE_STATE(list);
pLink = GetLink( list, item );
if( pLink == NULL )
return NULL;
return Extract( list, pLink );
}
/**
* Get the element of a list.
*
* LL_get() will simply return a pointer to the object
* associated with a certain list element.
*
* \param list Handle to an existing linked list.
*
* \param item Position of the element. Negative
* positions count backwards from the
* end of the list, so -1 would refer
* to the last element.
*
* \return Pointer to the object that is associated with
* the element. If the list is empty or \a item
* is out of range, NULL will be returned.
*/
void *LL_get( ConstLinkedList list, int item )
{
Link *pLink;
if( list == NULL || list->size == 0 )
return NULL;
AssertValidPtr( list );
pLink = GetLink( (LinkedList) list, item );
return pLink ? pLink->pObj : NULL;
}
/**
* Perform different list transformations.
*
* LL_splice() can be used for a variety of list transformations
* and is similar to Perl's splice builtin. In brief,
* LL_splice() will extract \a length elements starting at
* \a offset from \a list, replace them by the elements in
* \a rlist and return a new list holding the extracted elements.
*
* \param list Handle to an existing linked list.
*
* \param offset Offset of the first element to extract.
* If negative, counts backwards from the
* end.
*
* \param length Length of the list to extract. If negative,
* all remaining elements will be extracted.
* If \a length is larger than the number of
* remaining elements, only the remaining
* elements will be extracted. If this is 0,
* no elements will be extracted. However,
* an empty list will still be returned.
*
* \param rlist List that will replace the extracted
* elements. If no elements were extracted,
* the elements of \a rlist will just be
* inserted at \a offset. If \a rlist is
* NULL, no replacement elements will be
* inserted. The list will be automatically
* destroyed after the elements have been
* inserted into \a list.
*
* \return Handle to a new list holding the extracted elements,
* if any. NULL if LL_splice() fails for some reason.
*/
LinkedList LL_splice( LinkedList list, int offset, int length, LinkedList rlist )
{
LinkedList nlist;
Link *pLink, *pLast;
if( list == NULL )
return NULL;
AssertValidPtr( list );
CHANGE_STATE(list);
pLink = offset == list->size ? &list->link : GetLink( list, offset );
if( pLink == NULL )
return NULL;
nlist = LL_new();
if( nlist == NULL )
return NULL;
if( length < 0 )
length = offset < 0 ? -offset : list->size - offset;
if( length > 0 ) {
pLast = pLink;
while( ++nlist->size < length && pLast->next->pObj )
pLast = pLast->next;
pLink->prev->next = pLast->next;
pLast->next->prev = pLink->prev;
nlist->link.next = pLink;
nlist->link.prev = pLast;
pLink->prev = &nlist->link;
pLink = pLast->next;
pLast->next = &nlist->link;
list->size -= nlist->size;
}
if( rlist ) {
pLast = pLink;
pLink = pLink->prev;
rlist->link.next->prev = pLink;
rlist->link.prev->next = pLast;
pLink->next = rlist->link.next;
pLast->prev = rlist->link.prev;
list->size += rlist->size;
Free( rlist );
}
return nlist;
}
/**
* Initialize list iterator.
*
* LI_init() will initialize a list iterator object.
* Keep in mind that modifying the list invalidates all
* list iterators.
*
* \param it Pointer to a list iterator object.
*
* \param list Handle to an existing linked list.
*
* \see LI_next(), LI_prev() and LI_curr()
*/
void LI_init(ListIterator *it, ConstLinkedList list)
{
it->list = list;
if (list)
{
AssertValidPtr(list);
it->cur = &list->link;
#ifdef DEBUG_UTIL_LIST
it->orig_state = list->state;
#endif
}
}
/**
* Move iterator to next list element.
*
* LI_next() will advance to the next element in the list.
*
* \param it Pointer to a list iterator object.
*
* \return Nonzero as long as the next element is valid,
* zero at the end of the list.
*
* \see LI_init(), LI_prev() and LI_curr()
*/
int LI_next(ListIterator *it)
{
if (it == NULL || it->list == NULL)
return 0;
AssertValidPtr(it->list);
#ifdef DEBUG_UTIL_LIST
assert(it->orig_state == it->list->state);
#endif
it->cur = it->cur->next;
return it->cur != &it->list->link;
}
/**
* Move iterator to previous list element.
*
* LI_prev() will advance to the previous element in the list.
*
* \param it Pointer to a list iterator object.
*
* \return Nonzero as long as the previous element is valid,
* zero at the beginning of the list.
*
* \see LI_init(), LI_next() and LI_curr()
*/
int LI_prev(ListIterator *it)
{
if (it == NULL || it->list == NULL)
return 0;
AssertValidPtr(it->list);
it->cur = it->cur->prev;
return it->cur != &it->list->link;
}
/**
* Return the object associated with the current list element.
*
* LI_curr() will return a pointer to the current object.
*
* \param it Pointer to a list iterator object.
*
* \return Pointer to the current object in the list.
*
* \see LI_init(), LI_next() and LI_prev()
*/
void *LI_curr(const ListIterator *it)
{
if (it == NULL || it->list == NULL)
return NULL;
AssertValidPtr(it->list);
return it->cur->pObj;
}
/**
* Sort list elements.
*
* LL_sort() will sort a list using a quicksort algorithm.
* The sorted list will be in ascending order.
*
* \param list Handle to an existing linked list.
*
* \param cmp Pointer to a comparison function.
* This function is called with a pair
* of pointers to objects in the list
* and must return
* - a negative value if the first
* argument is less than the second
* - a positive value if the first
* argument is greater than the second
* - zero if the first both arguments
* are considered to be equal
*/
void LL_sort( LinkedList list, LLCompareFunc cmp )
{
if( list == NULL || list->size <= 1 )
return;
AssertValidPtr( list );
QuickSort( list->link.next, list->link.prev, list->size, cmp );
}
|