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 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982
|
static const char rcsid[] = "#(@) $Id$";
/*
* Date last modified: Jan 2001
* Modifications by Dan Libby (dan@libby.com), including:
* - various fixes, null checks, etc
* - addition of Q_Iter funcs, macros
*/
/*-**************************************************************
*
* File : q.c
*
* Author: Peter Yard [1993.01.02] -- 02 Jan 1993
*
* Disclaimer: This code is released to the public domain.
*
* Description:
* Generic double ended queue (Deque pronounced DEK) for handling
* any data types, with sorting.
*
* By use of various functions in this module the caller
* can create stacks, queues, lists, doubly linked lists,
* sorted lists, indexed lists. All lists are dynamic.
*
* It is the responsibility of the caller to malloc and free
* memory for insertion into the queue. A pointer to the object
* is used so that not only can any data be used but various kinds
* of data can be pushed on the same queue if one so wished e.g.
* various length string literals mixed with pointers to structures
* or integers etc.
*
* Enhancements:
* A future improvement would be the option of multiple "cursors"
* so that multiple locations could occur in the one queue to allow
* placemarkers and additional flexibility. Perhaps even use queue
* itself to have a list of cursors.
*
* Usage:
*
* /x init queue x/
* queue q;
* Q_Init(&q);
*
* To create a stack :
*
* Q_PushHead(&q, &mydata1); /x push x/
* Q_PushHead(&q, &mydata2);
* .....
* data_ptr = Q_PopHead(&q); /x pop x/
* .....
* data_ptr = Q_Head(&q); /x top of stack x/
*
* To create a FIFO:
*
* Q_PushHead(&q, &mydata1);
* .....
* data_ptr = Q_PopTail(&q);
*
* To create a double list:
*
* data_ptr = Q_Head(&q);
* ....
* data_ptr = Q_Next(&q);
* data_ptr = Q_Tail(&q);
* if (Q_IsEmpty(&q)) ....
* .....
* data_ptr = Q_Previous(&q);
*
* To create a sorted list:
*
* Q_PushHead(&q, &mydata1); /x push x/
* Q_PushHead(&q, &mydata2);
* .....
* if (!Q_Sort(&q, MyFunction))
* .. error ..
*
* /x fill in key field of mydata1.
* * NB: Q_Find does linear search
* x/
*
* if (Q_Find(&q, &mydata1, MyFunction))
* {
* /x found it, queue cursor now at correct record x/
* /x can retrieve with x/
* data_ptr = Q_Get(&q);
*
* /x alter data , write back with x/
* Q_Put(&q, data_ptr);
* }
*
* /x Search with binary search x/
* if (Q_Seek(&q, &mydata, MyFunction))
* /x etc x/
*
*
****************************************************************/
#ifdef _WIN32
#include "xmlrpc_win32.h"
#endif
#include <stdlib.h>
#include "queue.h"
static void QuickSort(void *list[], int low, int high,
int (*Comp)(const void *, const void *));
static int Q_BSearch(queue *q, void *key,
int (*Comp)(const void *, const void *));
/* The index: a pointer to pointers */
static void **index;
static datanode **posn_index;
/***
*
** function : Q_Init
*
** purpose : Initialise queue object and pointers.
*
** parameters : 'queue' pointer.
*
** returns : True_ if init successful else False_
*
** comments :
***/
int Q_Init(queue *q)
{
if(q) {
q->head = q->tail = NULL;
q->cursor = q->head;
q->size = 0;
q->sorted = False_;
}
return True_;
}
/***
*
** function : Q_AtHead
*
** purpose : tests if cursor is at head of queue
*
** parameters : 'queue' pointer.
*
** returns : boolean - True_ is at head else False_
*
** comments :
*
***/
int Q_AtHead(queue *q)
{
return(q && q->cursor == q->head);
}
/***
*
** function : Q_AtTail
*
** purpose : boolean test if cursor at tail of queue
*
** parameters : 'queue' pointer to test.
*
** returns : True_ or False_
*
** comments :
*
***/
int Q_AtTail(queue *q)
{
return(q && q->cursor == q->tail);
}
/***
*
** function : Q_IsEmpty
*
** purpose : test if queue has nothing in it.
*
** parameters : 'queue' pointer
*
** returns : True_ if IsEmpty queue, else False_
*
** comments :
*
***/
inline int Q_IsEmpty(queue *q)
{
return(!q || q->size == 0);
}
/***
*
** function : Q_Size
*
** purpose : return the number of elements in the queue
*
** parameters : queue pointer
*
** returns : number of elements
*
** comments :
*
***/
int Q_Size(queue *q)
{
return q ? q->size : 0;
}
/***
*
** function : Q_Head
*
** purpose : position queue cursor to first element (head) of queue.
*
** parameters : 'queue' pointer
*
** returns : pointer to data at head. If queue is IsEmpty returns NULL
*
** comments :
*
***/
void *Q_Head(queue *q)
{
if(Q_IsEmpty(q))
return NULL;
q->cursor = q->head;
return q->cursor->data;
}
/***
*
** function : Q_Tail
*
** purpose : locate cursor at tail of queue.
*
** parameters : 'queue' pointer
*
** returns : pointer to data at tail , if queue IsEmpty returns NULL
*
** comments :
*
***/
void *Q_Tail(queue *q)
{
if(Q_IsEmpty(q))
return NULL;
q->cursor = q->tail;
return q->cursor->data;
}
/***
*
** function : Q_PushHead
*
** purpose : put a data pointer at the head of the queue
*
** parameters : 'queue' pointer, void pointer to the data.
*
** returns : True_ if success else False_ if unable to push data.
*
** comments :
*
***/
int Q_PushHead(queue *q, void *d)
{
if(q && d) {
node *n;
datanode *p;
p = malloc(sizeof(datanode));
if(p == NULL)
return False_;
n = q->head;
q->head = (node*)p;
q->head->prev = NULL;
if(q->size == 0) {
q->head->next = NULL;
q->tail = q->head;
}
else {
q->head->next = (datanode*)n;
n->prev = q->head;
}
q->head->data = d;
q->size++;
q->cursor = q->head;
q->sorted = False_;
return True_;
}
return False_;
}
/***
*
** function : Q_PushTail
*
** purpose : put a data element pointer at the tail of the queue
*
** parameters : queue pointer, pointer to the data
*
** returns : True_ if data pushed, False_ if data not inserted.
*
** comments :
*
***/
int Q_PushTail(queue *q, void *d)
{
if(q && d) {
node *p;
datanode *n;
n = malloc(sizeof(datanode));
if(n == NULL)
return False_;
p = q->tail;
q->tail = (node *)n;
if(q->size == 0) {
q->tail->prev = NULL;
q->head = q->tail;
}
else {
q->tail->prev = (datanode *)p;
p->next = q->tail;
}
q->tail->next = NULL;
q->tail->data = d;
q->cursor = q->tail;
q->size++;
q->sorted = False_;
return True_;
}
return False_;
}
/***
*
** function : Q_PopHead
*
** purpose : remove and return the top element at the head of the
* queue.
*
** parameters : queue pointer
*
** returns : pointer to data element or NULL if queue is IsEmpty.
*
** comments :
*
***/
void *Q_PopHead(queue *q)
{
datanode *n;
void *d;
if(Q_IsEmpty(q))
return NULL;
d = q->head->data;
n = q->head->next;
free(q->head);
q->size--;
if(q->size == 0)
q->head = q->tail = q->cursor = NULL;
else {
q->head = (node *)n;
q->head->prev = NULL;
q->cursor = q->head;
}
q->sorted = False_;
return d;
}
/***
*
** function : Q_PopTail
*
** purpose : remove element from tail of queue and return data.
*
** parameters : queue pointer
*
** returns : pointer to data element that was at tail. NULL if queue
* IsEmpty.
*
** comments :
*
***/
void *Q_PopTail(queue *q)
{
datanode *p;
void *d;
if(Q_IsEmpty(q))
return NULL;
d = q->tail->data;
p = q->tail->prev;
free(q->tail);
q->size--;
if(q->size == 0)
q->head = q->tail = q->cursor = NULL;
else {
q->tail = (node *)p;
q->tail->next = NULL;
q->cursor = q->tail;
}
q->sorted = False_;
return d;
}
/***
*
** function : Q_Next
*
** purpose : Move to the next element in the queue without popping
*
** parameters : queue pointer.
*
** returns : pointer to data element of new element or NULL if end
* of the queue.
*
** comments : This uses the cursor for the current position. Q_Next
* only moves in the direction from the head of the queue
* to the tail.
***/
void *Q_Next(queue *q)
{
if(!q)
return NULL;
if(!q->cursor || q->cursor->next == NULL)
return NULL;
q->cursor = (node *)q->cursor->next;
return q->cursor->data ;
}
/***
*
** function : Q_Previous
*
** purpose : Opposite of Q_Next. Move to next element closer to the
* head of the queue.
*
** parameters : pointer to queue
*
** returns : pointer to data of new element else NULL if queue IsEmpty
*
** comments : Makes cursor move towards the head of the queue.
*
***/
void *Q_Previous(queue *q)
{
if(!q)
return NULL;
if(q->cursor->prev == NULL)
return NULL;
q->cursor = (node *)q->cursor->prev;
return q->cursor->data;
}
void *Q_Iter_Del(queue *q, q_iter iter)
{
void *d;
datanode *n, *p;
if(!q)
return NULL;
if(iter == NULL)
return NULL;
if(iter == (q_iter)q->head)
return Q_PopHead(q);
if(iter == (q_iter)q->tail)
return Q_PopTail(q);
n = ((node*)iter)->next;
p = ((node*)iter)->prev;
d = ((node*)iter)->data;
free(iter);
if(p) {
p->next = n;
}
if (q->cursor == (node*)iter) {
if (p) {
q->cursor = p;
} else {
q->cursor = n;
}
}
if (n != NULL) {
n->prev = p;
}
q->size--;
q->sorted = False_;
return d;
}
/***
*
** function : Q_DelCur
*
** purpose : Delete the current queue element as pointed to by
* the cursor.
*
** parameters : queue pointer
*
** returns : pointer to data element.
*
** comments : WARNING! It is the responsibility of the caller to
* free any memory. Queue cannot distinguish between
* pointers to literals and malloced memory.
*
***/
void *Q_DelCur(queue* q) {
if(q) {
return Q_Iter_Del(q, (q_iter)q->cursor);
}
return 0;
}
/***
*
** function : Q_Destroy
*
** purpose : Free all queue resources
*
** parameters : queue pointer
*
** returns : null.
*
** comments : WARNING! It is the responsibility of the caller to
* free any memory. Queue cannot distinguish between
* pointers to literals and malloced memory.
*
***/
void Q_Destroy(queue *q)
{
while(!Q_IsEmpty(q)) {
Q_PopHead(q);
}
}
/***
*
** function : Q_Get
*
** purpose : get the pointer to the data at the cursor location
*
** parameters : queue pointer
*
** returns : data element pointer
*
** comments :
*
***/
void *Q_Get(queue *q)
{
if(!q)
return NULL;
if(q->cursor == NULL)
return NULL;
return q->cursor->data;
}
/***
*
** function : Q_Put
*
** purpose : replace pointer to data with new pointer to data.
*
** parameters : queue pointer, data pointer
*
** returns : boolean- True_ if successful, False_ if cursor at NULL
*
** comments :
*
***/
int Q_Put(queue *q, void *data)
{
if(q && data) {
if(q->cursor == NULL)
return False_;
q->cursor->data = data;
return True_;
}
return False_;
}
/***
*
** function : Q_Find
*
** purpose : Linear search of queue for match with key in *data
*
** parameters : queue pointer q, data pointer with data containing key
* comparison function here called Comp.
*
** returns : True_ if found , False_ if not in queue.
*
** comments : Useful for small queues that are constantly changing
* and would otherwise need constant sorting with the
* Q_Seek function.
* For description of Comp see Q_Sort.
* Queue cursor left on position found item else at end.
*
***/
int Q_Find(queue *q, void *data,
int (*Comp)(const void *, const void *))
{
void *d;
if (q == NULL) {
return False_;
}
d = Q_Head(q);
do {
if(Comp(d, data) == 0)
return True_;
d = Q_Next(q);
} while(!Q_AtTail(q));
if(Comp(d, data) == 0)
return True_;
return False_;
}
/*======== Sorted Queue and Index functions ========= */
static void QuickSort(void *list[], int low, int high,
int (*Comp)(const void *, const void *))
{
int flag = 1, i, j;
void *key, *temp;
if(low < high) {
i = low;
j = high + 1;
key = list[ low ];
while(flag) {
i++;
while(Comp(list[i], key) < 0)
i++;
j--;
while(Comp(list[j], key) > 0)
j--;
if(i < j) {
temp = list[i];
list[i] = list[j];
list[j] = temp;
}
else flag = 0;
}
temp = list[low];
list[low] = list[j];
list[j] = temp;
QuickSort(list, low, j-1, Comp);
QuickSort(list, j+1, high, Comp);
}
}
/***
*
** function : Q_Sort
*
** purpose : sort the queue and allow index style access.
*
** parameters : queue pointer, comparison function compatible with
* with 'qsort'.
*
** returns : True_ if sort succeeded. False_ if error occurred.
*
** comments : Comp function supplied by caller must return
* -1 if data1 < data2
* 0 if data1 == data2
* +1 if data1 > data2
*
* for Comp(data1, data2)
*
* If queue is already sorted it frees the memory of the
* old index and starts again.
*
***/
int Q_Sort(queue *q, int (*Comp)(const void *, const void *))
{
int i;
void *d;
datanode *dn;
/* if already sorted free memory for tag array */
if(q->sorted) {
free(index);
free(posn_index);
q->sorted = False_;
}
/* Now allocate memory of array, array of pointers */
index = malloc(q->size * sizeof(q->cursor->data));
if(index == NULL)
return False_;
posn_index = malloc(q->size * sizeof(q->cursor));
if(posn_index == NULL) {
free(index);
return False_;
}
/* Walk queue putting pointers into array */
d = Q_Head(q);
for(i=0; i < q->size; i++) {
index[i] = d;
posn_index[i] = q->cursor;
d = Q_Next(q);
}
/* Now sort the index */
QuickSort(index, 0, q->size - 1, Comp);
/* Rearrange the actual queue into correct order */
dn = q->head;
i = 0;
while(dn != NULL) {
dn->data = index[i++];
dn = dn->next;
}
/* Re-position to original element */
if(d != NULL)
Q_Find(q, d, Comp);
else Q_Head(q);
q->sorted = True_;
return True_;
}
/***
*
** function : Q_BSearch
*
** purpose : binary search of queue index for node containing key
*
** parameters : queue pointer 'q', data pointer of key 'key',
* Comp comparison function.
*
** returns : integer index into array of node pointers,
* or -1 if not found.
*
** comments : see Q_Sort for description of 'Comp' function.
*
***/
static int Q_BSearch( queue *q, void *key,
int (*Comp)(const void *, const void*))
{
int low, mid, hi, val;
low = 0;
hi = q->size - 1;
while(low <= hi) {
mid = (low + hi) / 2;
val = Comp(key, index[ mid ]);
if(val < 0)
hi = mid - 1;
else if(val > 0)
low = mid + 1;
else /* Success */
return mid;
}
/* Not Found */
return -1;
}
/***
*
** function : Q_Seek
*
** purpose : use index to locate data according to key in 'data'
*
** parameters : queue pointer 'q', data pointer 'data', Comp comparison
* function.
*
** returns : pointer to data or NULL if could not find it or could
* not sort queue.
*
** comments : see Q_Sort for description of 'Comp' function.
*
***/
void *Q_Seek(queue *q, void *data, int (*Comp)(const void *, const void *))
{
int idx;
if (q == NULL) {
return NULL;
}
if(!q->sorted) {
if(!Q_Sort(q, Comp))
return NULL;
}
idx = Q_BSearch(q, data, Comp);
if(idx < 0)
return NULL;
q->cursor = posn_index[idx];
return index[idx];
}
/***
*
** function : Q_Insert
*
** purpose : Insert an element into an indexed queue
*
** parameters : queue pointer 'q', data pointer 'data', Comp comparison
* function.
*
** returns : pointer to data or NULL if could not find it or could
* not sort queue.
*
** comments : see Q_Sort for description of 'Comp' function.
* WARNING! This code can be very slow since each new
* element means a new Q_Sort. Should only be used for
* the insertion of the odd element ,not the piecemeal
* building of an entire queue.
***/
int Q_Insert(queue *q, void *data, int (*Comp)(const void *, const void *))
{
if (q == NULL) {
return False_;
}
Q_PushHead(q, data);
if(!Q_Sort(q, Comp))
return False_;
return True_;
}
/* read only funcs for iterating through queue. above funcs modify queue */
q_iter Q_Iter_Head(queue *q) {
return q ? (q_iter)q->head : NULL;
}
q_iter Q_Iter_Tail(queue *q) {
return q ? (q_iter)q->tail : NULL;
}
q_iter Q_Iter_Next(q_iter qi) {
return qi ? (q_iter)((node*)qi)->next : NULL;
}
q_iter Q_Iter_Prev(q_iter qi) {
return qi ? (q_iter)((node*)qi)->prev : NULL;
}
void * Q_Iter_Get(q_iter qi) {
return qi ? ((node*)qi)->data : NULL;
}
int Q_Iter_Put(q_iter qi, void* data) {
if(qi) {
((node*)qi)->data = data;
return True_;
}
return False_;
}
|