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 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923
|
/** \file shared/LL.c
* Define routines to deal with doubly linked lists
*/
/* This file is part of LCDproc.
*
* This file is released under the GNU General Public License.
* Refer to the COPYING file distributed with this package.
*
* Copyright(c) 1994, Selene Scriven
* (c) 1999, William Ferrell
* (c) 2000, Guillaume Filion
* (c) 2001, Joris Robijn
* (c) 2008, Peter Marschall
*
*/
#include <stdlib.h>
#include <stdio.h>
#include "LL.h"
#ifdef DEBUG
#undef DEBUG
#endif
//TODO: Test everything?
/** Create new linked list.
* \return Pointer to freshly created list object; \c NULL on error.
*/
LinkedList *
LL_new(void)
{
LinkedList *list;
list = malloc(sizeof(LinkedList));
if (list == NULL)
return NULL;
list->head.data = NULL;
list->head.prev = NULL;
list->head.next = &list->tail;
list->tail.data = NULL;
list->tail.prev = &list->head;
list->tail.next = NULL;
list->current = &list->head;
return list;
}
/** Destroy the entire list.
*
* \note
* This does not free the data, only the list itself.
*
* \param list List object to be destroyed.
* \retval <0 error
* \retval 0 success
*/
int
LL_Destroy(LinkedList *list)
{
LL_node *prev, *next;
LL_node *node;
if (!list)
return -1;
node = &list->head;
for (node = node->next; node && node->next; node = next) {
// Avoid accessing "node" after it's freed.. :)
next = node->next;
prev = node->prev;
if (next != NULL)
next->prev = prev;
if (prev != NULL)
prev->next = next;
node->next = NULL;
node->prev = NULL;
free(node);
}
free(list);
return 0;
}
/** Move to another entry in the list.
* Set list's \c current pointer to the node denoted to by \c whereto.
* \param list List object.
* \param whereto Direction where to set the list's \c current pointer
* \return New value of list's \c current pointer;
* \c NULL on error or when moving beyond ends.
*/
LL_node *
LL_GoTo(LinkedList *list, Direction whereto)
{
if (!list)
return NULL;
switch (whereto) {
case HEAD: list->current = (list->head.next != &list->tail)
? list->head.next
: NULL;
break;
case PREV: if (list->current->prev == &list->head)
return NULL;
list->current = list->current->prev;
case CURRENT: break;
case NEXT: if (list->current->next == &list->tail)
return NULL;
list->current = list->current->next;
break;
case TAIL: list->current = (list->tail.prev != &list->head)
? list->tail.prev
: NULL;
break;
}
return list->current;
}
/** Return to the beginning of the list.
* Set list's \c current pointer to the first node in the list.
* \param list List object.
* \retval <0 error: no list given
* \retval 0 success
*/
int
LL_Rewind(LinkedList *list)
{
if (!list)
return -1;
list->current = (list->head.next != &list->tail)
? list->head.next
: &list->head;
return 0;
}
/** Jump to the end of the list.
* Set list's \c current pointer to the last node in the list.
* \param list List object.
* \retval <0 error: no list given
* \retval 0 success
*/
int
LL_End(LinkedList *list)
{
if (!list)
return -1;
list->current = (list->tail.prev != &list->head)
? list->tail.prev
: &list->tail;
return 0;
}
/** Go to the next node of the list.
* Advance list's \c current pointer to the next node in the list.
* \param list List object.
* \retval <0 error: no list given or no next node
* \retval 0 success
*/
int
LL_Next(LinkedList *list)
{
if (!list)
return -1;
if (!list->current)
return -1;
if (list->current->next == &list->tail)
return -1;
list->current = list->current->next;
return 0;
}
/** Go to the previous node of the list.
* Set list's \c current pointer to the previous node in the list.
* \param list List object.
* \retval <0 error: no list given or no previous node
* \retval 0 success
*/
int
LL_Prev(LinkedList *list)
{
if (!list)
return -1;
if (!list->current)
return -1;
if (list->current->prev == &list->head)
return -1;
list->current = list->current->prev;
return 0;
}
/** Access current node's data.
* Return pointer to list's \c current node's data.
* \param list List object.
* \return Pointer to \c current node's payload data;
* \c NULL may be empty payload or an error.
*/
void *
LL_Get(LinkedList *list)
{
if (!list)
return NULL;
if (!list->current)
return NULL;
return list->current->data;
}
/** Set/change current node's data.
* \param list List object.
* \param data Pointer to data to be set.
* \retval <0 error: no list given, or no current node
* \retval 0 success
*/
int
LL_Put(LinkedList *list, void *data)
{
if (!list)
return -1;
if (!list->current)
return -1;
list->current->data = data;
return 0;
}
/** Get current node in list.
* \param list List object.
* \return Pointer to current node.
*/
LL_node *
LL_GetNode(LinkedList *list)
{
if (!list)
return NULL;
return list->current;
}
/** Set list's \c current pointer to a specific node.
*
* \warning
* Don't use this unless you know what you're doing.
*
* \param list List object.
* \param node Node to become new \c current.
* \retval <0 error
* \retval 0 success
*/
int
LL_PutNode(LinkedList *list, LL_node *node)
{
if (!list)
return -1;
if (!node)
return -1;
list->current = node;
return 0;
}
/** Access list's first node's data.
* Set list's \c current pointer to the first node and return its data.
* \param list List object.
* \return Pointer to first node's data; \c NULL on error.
*/
void *
LL_GetFirst(LinkedList *list)
{
if (!list)
return NULL;
if (0 > LL_Rewind(list))
return NULL;
return LL_Get(list);
}
/** Access next node's data.
* Advance list's \c current pointer to the next node and return its data.
* \param list List object.
* \return Pointer to next node's data; \c NULL on error.
*/
void *
LL_GetNext(LinkedList *list)
{
if (!list)
return NULL;
if (0 > LL_Next(list))
return NULL;
return LL_Get(list);
}
/** Access previous node's data.
* Set list's \c current pointer to the previous node, and return its data.
* \param list List object.
* \return Pointer to previous node's data; \c NULL on error.
*/
void *
LL_GetPrev(LinkedList *list)
{
if (!list)
return NULL;
if (0 > LL_Prev(list))
return NULL;
return LL_Get(list);
}
/** Access list's last node's data.
* Set list's \c current pointer to the last node and return its data.
* \param list List object.
* \return Pointer to last node's data; \c NULL on error.
*/
void *
LL_GetLast(LinkedList *list)
{
if (!list)
return NULL;
if (0 > LL_End(list))
return NULL;
return LL_Get(list);
}
/** Add/append a new node after current one in the list.
* Update the list's \c current pointer to point to the freshly created node.
* \param list List object.
* \param add Pointer to new node's data.
* \retval <0 error
* \retval 0 success
*/
int
LL_AddNode(LinkedList *list, void *add)
{
LL_node *node;
if (!list)
return -1;
if (!list->current)
return -1;
node = malloc(sizeof(LL_node));
if (node == NULL)
return -1;
// we're behind the list's end, go to previous node
if (list->current == &list->tail)
list->current = list->current->prev;
// Set node data
node->next = list->current->next;
node->prev = list->current;
node->data = add;
// Re-link
if (node->next)
node->next->prev = node;
list->current->next = node;
list->current = node;
return 0;
}
/** Add/insert a new node before current one in the list.
* Update the list's \c current pointer to point to the freshly created node.
* \param list List object.
* \param add Pointer to new node's data.
* \retval <0 error
* \retval 0 success
*/
int
LL_InsertNode(LinkedList *list, void *add)
{
LL_node *node;
if (!list)
return -1;
if (!add)
return -1;
if (!list->current)
return -1;
node = malloc(sizeof(LL_node));
if (node == NULL)
return -1;
// we're before the list's start, go to next node
if (list->current == &list->head)
list->current = list->current->next;
node->next = list->current;
node->prev = list->current->prev;
node->data = add;
if (list->current->prev)
list->current->prev->next = node;
list->current->prev = node;
list->current = node;
return 0;
}
/** Remove current node from the list.
* Set the list's \c current pointer to the one denoted by \c whereto.
* \param list List object.
* \param whereto Direction where to set the list's \c current pointer
* \return Pointer to data of deleted node; \c NULL on error.
*/
void *
LL_DeleteNode(LinkedList *list, Direction whereto)
{
LL_node *next, *prev;
void *data;
if (!list)
return NULL;
if (!list->current)
return NULL;
if (list->current == &list->head)
return NULL;
if (list->current == &list->tail)
return NULL;
next = list->current->next;
prev = list->current->prev;
data = list->current->data;
if (prev)
prev->next = next;
if (next)
next->prev = prev;
list->current->prev = NULL;
list->current->next = NULL;
// This should not free things; the user should do it explicitly.
//if(list->current->data) free(list->current->data);
list->current->data = NULL;
free(list->current);
switch (whereto) {
case HEAD: list->current = list->head.next;
break;
case TAIL: list->current = list->tail.prev;
break;
case PREV: list->current = prev;
break;
default:
case NEXT: list->current = next;
}
return data;
}
/** Remove a specific node from the list.
* Find a node by a pointer to its data and remove it.
* Set the list's \c current pointer to the one denoted by \c whereto.
* \param list List object.
* \param data Pointer to data of node to delete.
* \param whereto Direction where to set the list's \c current pointer
* \return Pointer to data of deleted node; \c NULL on error.
*/
void *
LL_Remove(LinkedList *list, void *data, Direction whereto)
{
if (!list)
return NULL;
LL_Rewind(list);
do {
void *find = LL_Get(list);
if (find == data)
return LL_DeleteNode(list, whereto);
} while (LL_Next(list) == 0);
return NULL;
}
/** Add/append a new node after the last one in the list.
* Jump to the last node in the list, append a new node
* and make this new one the list's \c current one.
* \param list List object.
* \param add Pointer to new node's data.
* \retval <0 error
* \retval 0 success
*/
int
LL_Push(LinkedList *list, void *add)
{
if (!list)
return -1;
if (!add)
return -1;
LL_End(list);
return LL_AddNode(list, add);
}
/** Remove the last node from the list, and return its data.
* Jump to the last node in the list, remove it from the list
* and return its data.
* \param list List object.
* \return Pointer to data of deleted node; \c NULL on error.
*/
void *
LL_Pop(LinkedList *list)
{
if (!list)
return NULL;
if (0 > LL_End(list))
return NULL;
return LL_DeleteNode(list, PREV);
}
/** Access list's last node's data.
* Set list's \c current pointer to the last node and return its data.
* \param list List object.
* \return Pointer to last node's data; \c NULL on error.
*/
void *
LL_Top(LinkedList *list)
{
return LL_GetLast(list);
}
/** Remove the first node from the list, and return its data.
* Jump to the first node in the list, remove it from the list and return its data.
* \param list List object.
* \return Pointer to data of deleted node; \c NULL on error.
*/
void *
LL_Shift(LinkedList *list)
{
if (!list)
return NULL;
if (0 > LL_Rewind(list))
return NULL;
return LL_DeleteNode(list, NEXT);
}
/** Access list's first node's data.
* Set list's \c current pointer to the first node and return its data.
* \param list List object.
* \return Pointer to first node's data; \c NULL on error.
*/
void *
LL_Look(LinkedList *list)
{
return LL_GetFirst(list);
}
/** Add/insert a new node before the first one in the list.
* Jump to the first node in the list and insert a new node before that one.
* \param list List object.
* \param add Pointer to new node's data.
* \retval <0 error
* \retval 0 success
*/
int
LL_Unshift(LinkedList *list, void *add)
{
if (!list)
return -1;
if (!add)
return -1;
LL_Rewind(list);
return LL_InsertNode(list, add);
}
/** Add an item to the end of its "priority group"
* The list is assumed to be sorted already.
* \param list List object.
* \param add Pointer to new node's data.
* \param compare Pointer to a comparison function.
* \retval <0 error
* \retval 0 success
*/
int
LL_PriorityEnqueue(LinkedList *list, void *add, int (*compare)(void *, void *))
{
if (!list)
return -1;
if (!add)
return -1;
if (!compare)
return -1;
// From the end of the list, keep searching while we're "less than"
// the given nodes...
LL_End(list);
do {
void *data = LL_Get(list);
if (data) {
int i = compare(add, data);
if (i >= 0) { // If we're in the right place, add it and exit
LL_AddNode(list, add);
return 0;
}
}
} while (LL_Prev(list) == 0);
// If we're less than *everything*, put it at the beginning
LL_Unshift(list, add);
return 0;
}
/** Switch two nodes positions.
* \param one First list object.
* \param two Second list object.
* \return -1 on error, 0 on success
*/
int
LL_SwapNodes(LL_node *one, LL_node *two)
{
LL_node *firstprev, *firstnext;
LL_node *secondprev, *secondnext;
if (!one || !two)
return -1;
if (one == two)
return 0; // Do nothing
firstprev = one->prev; // Look up the nodes neighbors...
firstnext = one->next;
secondprev = two->prev;
secondnext = two->next;
if (firstprev != NULL)
firstprev->next = two; // Swap the neighboring
if (firstnext != NULL)
firstnext->prev = two; // nodes pointers...
if (secondprev != NULL)
secondprev->next = one;
if (secondprev != NULL)
secondnext->prev = one;
one->next = secondnext; // Swap the nodes pointers
one->prev = secondprev;
two->next = firstnext;
two->prev = firstprev;
if (firstnext == two)
one->prev = two; // Fix things in case
if (firstprev == two)
one->next = two; // they were next to
if (secondprev == one)
two->next = one; // each other...
if (secondnext == one)
two->prev = one;
return 0;
}
/** Calculate the length of a list.
* \param list List object.
* \return Number of nodes in the list; \c -1 on error.
*/
int
LL_Length(LinkedList *list)
{
LL_node *node;
int num = 0;
if (!list)
return -1;
node = &list->head;
for (num = -1; node != &list->tail; num++)
node = node->next;
return num;
}
/** Find a node by giving a comparison function and a value.
* Go to to the list node whose data matches the given value
* and return the data.
*
* \note
* This does \em not rewind the list first!
* Do it yourself if you want to start from the beginning!
*
* \param list List object.
* \param compare Pointer to a comparison function, that takes to void pointers
* as arguments and returns an int. If must return \c 0 exactly
* when the node's data matches \c value.
* \param value Pointer to the value used for matching.
* \return The found node's data pointer; \c NULL otherwise
*/
void *
LL_Find(LinkedList *list, int (*compare)(void *, void *), void *value)
{
if (!list)
return NULL;
if (!compare)
return NULL;
if (!value)
return NULL;
for (void *data = LL_Get(list); data; data = LL_GetNext(list))
if (0 == compare(data, value))
return data;
return NULL;
}
/** Perform an action for the all list elements.
* Execute a function on the data of each node in the list.
* Depending on the result of the function, new nodes may get added,
* nodes may get deleted or simply changed by the function itself.
*
* The \c action() function is in turn called with each node's data
* pointer as the first argument and \c value as its second argument.
* If it returns \c NULL, the node will be deleted from the list,
* otherwise, if the pointer returned from the function differs from
* data, a new node gets added after the current node.
* If the result equals the payload data, no addition or deletio happens.
*
* \note
* Removing the client payload in case of deletion, or creation of
* the payload for the new node in case of addition is up to the
* \c action() function.
*
* \note
* \c value can be used to report errors, pass additional information, ...
*
* \param list List object.
* \param action Pointer to the action function that takes two void pointers
* as arguments and returns a void pointer.
* \param value Pointer to data that is used as second argument to
* \c action().
*/
void
LL_ForAll(LinkedList *list, void *(*action)(void *, void *), void *value)
{
if (!list)
return;
if (!action)
return;
LL_Rewind(list);
if (list->current != NULL) {
do {
void *data = LL_Get(list);
void *result = action(data, value);
if (result != data) {
if (result != NULL)
LL_AddNode(list, result);
else
LL_DeleteNode(list, PREV);
}
} while (LL_Next(list) == 0);
}
}
/** Go to the n-th node in the list and return its data.
* Go to to the list node with the given \c index
* and return the data.
* \param list List object.
* \param index Index of the node whose data we want.
* \return The found node's data pointer; \c NULL otherwise
*/
void *
LL_GetByIndex(LinkedList *list, int index)
{
LL_node *node;
int num = 0;
if (!list)
return NULL;
if (index < 0)
return NULL;
for (node = list->head.next; node != &list->tail; node = node->next) {
if (num == index)
return node->data;
num++;
}
return NULL; // got past the end
}
/** Sort list by its contents.
* The list gets sorted using a comparison function for the data of its nodes.
* After the sorting, the list's current pointer is set to the first node.
* \param list List object.
* \param compare Pointer to a comparison function, that takes to void pointers
* as arguments and returns an int > \c 0 when the first argument
* is considered greater than the second.
* \retval <0 error
* \retval 0 success.
*/
int
LL_Sort(LinkedList *list, int (*compare)(void *, void *))
{
int i, j; // Junk / loop variables
int numnodes; // number of nodes in list
LL_node *best, *last; // best match and last node in the list
LL_node *current;
if (!list)
return -1;
if (!compare)
return -1;
numnodes = LL_Length(list); // get the number of nodes...
if (0 > LL_End(list))
return -1; // Find the last node.
last = LL_GetNode(list);
if (numnodes < 2)
return 0;
for (i = numnodes - 1; i > 0; i--) {
LL_Rewind(list); // get the first node again
best = last; // reset our "best" node
for (j = 0; j < i; j++) {
current = LL_GetNode(list);
// If we found a better match...
if (compare(current->data, best->data) > 0) {
best = current; // keep track of the "best" match
}
LL_Next(list); // Go to the next node.
}
LL_SwapNodes(last, best); // Switch two nodes...
if (best)
last = best->prev;
else
return -1;
}
LL_Rewind(list);
return 0;
}
void
LL_dprint(LinkedList *list)
{
LL_node *current;
current = &list->head;
printf("Head: prev:\t0x%p\taddr:\t0x%p\tnext:\t0x%p\n", list->head.prev, &list->head, list->head.next);
for (current = current->next; current != &list->tail; current = current->next) {
printf("node: prev:\t0x%p\taddr:\t0x%p\tnext:\t0x%p\n", current->prev, current, current->next);
}
printf("Tail: prev:\t0x%p\taddr:\t0x%p\tnext:\t0x%p\n", list->tail.prev, &list->tail, list->tail.next);
}
|