Debian Codegraph @

cextern/astrometry.net/bl.c

raw
1 /*
2 # This file is part of the Astrometry.net suite.
3 # Licensed under a 3-clause BSD style license - see LICENSE
4 */
5
6 #define _GNU_SOURCE /* for GNU extension vasprintf() */
7 #include <stdio.h>
8 #include <string.h>
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <assert.h>
12
13 #include "bl.h"
14
15 #include "keywords.h"
16
17 #include "bl.ph"
18
19 static bl_node* bl_new_node(bl* list);
20 static void bl_remove_from_node(bl* list, bl_node* node,
21 bl_node* prev, int index_in_node);
22
23 // NOTE: this should be replaced by a proper implementation!
24 #ifdef _MSC_VER
25 int vasprintf(char **strp, const char *fmt, va_list ap) {return -1;}
26 #endif
27
28 // Defined in bl.ph (private header):
29 // free_node
30 // NODE_DATA
31 // NODE_CHARDATA
32 // NODE_INTDATA
33 // NODE_DOUBLEDATA
34
35 // Defined in bl.inc (inlined functions):
36 // bl_size
37 // bl_access
38 // il_size
39 // il_get
40
41 // NOTE!, if you make changes here, also see bl-sort.c !
42
43 //#define DEFINE_SORT 1
44 #define DEFINE_SORT 0
45 #define nl il
46 #define number int
47 #define NL_PRINT(x) printf("%i", x)
48 #include "bl-nl.c"
49 #undef nl
50 #undef number
51 #undef NL_PRINT
52
53 #define nl ll
54 #define number int64_t
55 #define NL_PRINT(x) printf("%lli", (long long int)x)
56 #include "bl-nl.c"
57 #undef nl
58 #undef number
59 #undef NL_PRINT
60
61 #define nl fl
62 #define number float
63 #define NL_PRINT(x) printf("%f", (float)x)
64 #include "bl-nl.c"
65 #undef nl
66 #undef number
67 #undef NL_PRINT
68
69 #define nl dl
70 #define number double
71 #define NL_PRINT(x) printf("%g", x)
72 #include "bl-nl.c"
73 #undef nl
74 #undef number
75 #undef NL_PRINT
76
77 #undef DEFINE_SORT
78 #define DEFINE_SORT 0
79 #define nl pl
80 #define number void*
81 #define NL_PRINT(x) printf("%p", x)
82 #include "bl-nl.c"
83 #undef nl
84 #undef number
85 #undef NL_PRINT
86 #undef DEFINE_SORT
87
88
89
90 Pure int bl_datasize(const bl* list) {
91 if (!list)
92 return 0;
93 return list->datasize;
94 }
95
96
97 void bl_split(bl* src, bl* dest, size_t split) {
98 bl_node* node;
99 size_t nskipped;
100 size_t ind;
101 size_t ntaken = src->N - split;
102 node = find_node(src, split, &nskipped);
103 ind = split - nskipped;
104 if (ind == 0) {
105 // this whole node belongs to "dest".
106 if (split) {
107 // we need to get the previous node...
108 bl_node* last = find_node(src, split-1, NULL);
109 last->next = NULL;
110 src->tail = last;
111 } else {
112 // we've removed everything from "src".
113 src->head = NULL;
114 src->tail = NULL;
115 }
116 } else {
117 // create a new node to hold the second half of the items in "node".
118 bl_node* newnode = bl_new_node(dest);
119 newnode->N = (node->N - ind);
120 newnode->next = node->next;
121 memcpy(NODE_CHARDATA(newnode),
122 NODE_CHARDATA(node) + (ind * src->datasize),
123 newnode->N * src->datasize);
124 node->N -= (node->N - ind);
125 node->next = NULL;
126 src->tail = node;
127 // to make the code outside this block work...
128 node = newnode;
129 }
130
131 // append it to "dest".
132 if (dest->tail) {
133 dest->tail->next = node;
134 dest->N += ntaken;
135 } else {
136 dest->head = node;
137 dest->tail = node;
138 dest->N += ntaken;
139 }
140
141 // adjust "src".
142 src->N -= ntaken;
143 src->last_access = NULL;
144 }
145
146 void bl_init(bl* list, int blocksize, int datasize) {
147 list->head = NULL;
148 list->tail = NULL;
149 list->N = 0;
150 list->blocksize = blocksize;
151 list->datasize = datasize;
152 list->last_access = NULL;
153 list->last_access_n = 0;
154 }
155
156 bl* bl_new(int blocksize, int datasize) {
157 bl* rtn;
158 rtn = malloc(sizeof(bl));
159 if (!rtn) {
160 printf("Couldn't allocate memory for a bl.\n");
161 return NULL;
162 }
163 bl_init(rtn, blocksize, datasize);
164 return rtn;
165 }
166
167 void bl_free(bl* list) {
168 if (!list) return;
169 bl_remove_all(list);
170 free(list);
171 }
172
173 void bl_remove_all(bl* list) {
174 bl_node *n, *lastnode;
175 lastnode = NULL;
176 for (n=list->head; n; n=n->next) {
177 if (lastnode)
178 bl_free_node(lastnode);
179 lastnode = n;
180 }
181 if (lastnode)
182 bl_free_node(lastnode);
183 list->head = NULL;
184 list->tail = NULL;
185 list->N = 0;
186 list->last_access = NULL;
187 list->last_access_n = 0;
188 }
189
190 void bl_remove_all_but_first(bl* list) {
191 bl_node *n, *lastnode;
192 lastnode = NULL;
193
194 if (list->head) {
195 for (n=list->head->next; n; n=n->next) {
196 if (lastnode)
197 bl_free_node(lastnode);
198 lastnode = n;
199 }
200 if (lastnode)
201 bl_free_node(lastnode);
202 list->head->next = NULL;
203 list->head->N = 0;
204 list->tail = list->head;
205 } else {
206 list->head = NULL;
207 list->tail = NULL;
208 }
209 list->N = 0;
210 list->last_access = NULL;
211 list->last_access_n = 0;
212 }
213
214 static void bl_remove_from_node(bl* list, bl_node* node,
215 bl_node* prev, int index_in_node) {
216 // if we're removing the last element at this node, then
217 // remove this node from the linked list.
218 if (node->N == 1) {
219 // if we're removing the first node...
220 if (prev == NULL) {
221 list->head = node->next;
222 // if it's the first and only node...
223 if (list->head == NULL) {
224 list->tail = NULL;
225 }
226 } else {
227 // if we're removing the last element from
228 // the tail node...
229 if (node == list->tail) {
230 list->tail = prev;
231 }
232 prev->next = node->next;
233 }
234 bl_free_node(node);
235 } else {
236 int ncopy;
237 // just remove this element...
238 ncopy = node->N - index_in_node - 1;
239 if (ncopy > 0) {
240 memmove(NODE_CHARDATA(node) + index_in_node * list->datasize,
241 NODE_CHARDATA(node) + (index_in_node+1) * list->datasize,
242 ncopy * list->datasize);
243 }
244 node->N--;
245 }
246 list->N--;
247 }
248
249 void bl_remove_index(bl* list, size_t index) {
250 // find the node (and previous node) at which element 'index'
251 // can be found.
252 bl_node *node, *prev;
253 size_t nskipped = 0;
254 for (node=list->head, prev=NULL;
255 node;
256 prev=node, node=node->next) {
257
258 if (index < (nskipped + node->N))
259 break;
260 nskipped += node->N;
261 }
262 assert(node);
263 bl_remove_from_node(list, node, prev, index-nskipped);
264 list->last_access = NULL;
265 list->last_access_n = 0;
266 }
267
268 void bl_remove_index_range(bl* list, size_t start, size_t length) {
269 // find the node (and previous node) at which element 'start'
270 // can be found.
271 bl_node *node, *prev;
272 size_t nskipped = 0;
273 list->last_access = NULL;
274 list->last_access_n = 0;
275 for (node=list->head, prev=NULL;
276 node;
277 prev=node, node=node->next) {
278
279 if (start < (nskipped + node->N))
280 break;
281
282 nskipped += node->N;
283 }
284
285 // begin by removing any indices that are at the end of a block.
286 if (start > nskipped) {
287 // we're not removing everything at this node.
288 size_t istart;
289 size_t n;
290 istart = start - nskipped;
291 if ((istart + length) < node->N) {
292 // we're removing a chunk of elements from the middle of this
293 // block. move elements from the end into the removed chunk.
294 memmove(NODE_CHARDATA(node) + istart * list->datasize,
295 NODE_CHARDATA(node) + (istart + length) * list->datasize,
296 (node->N - (istart + length)) * list->datasize);
297 // we're done!
298 node->N -= length;
299 list->N -= length;
300 return;
301 } else {
302 // we're removing everything from 'istart' to the end of this
303 // block. just change the "N" values.
304 n = (node->N - istart);
305 node->N -= n;
306 list->N -= n;
307 length -= n;
308 start += n;
309 nskipped = start;
310 prev = node;
311 node = node->next;
312 }
313 }
314
315 // remove complete blocks.
316 for (;;) {
317 size_t n;
318 bl_node* todelete;
319 if (length == 0 || length < node->N)
320 break;
321 // we're skipping this whole block.
322 n = node->N;
323 length -= n;
324 start += n;
325 list->N -= n;
326 nskipped += n;
327 todelete = node;
328 node = node->next;
329 bl_free_node(todelete);
330 }
331 if (prev)
332 prev->next = node;
333 else
334 list->head = node;
335
336 if (!node)
337 list->tail = prev;
338
339 // remove indices from the beginning of the last block.
340 // note that we may have removed everything from the tail of the list,
341 // no "node" may be null.
342 if (node && length>0) {
343 //printf("removing %i from end.\n", length);
344 memmove(NODE_CHARDATA(node),
345 NODE_CHARDATA(node) + length * list->datasize,
346 (node->N - length) * list->datasize);
347 node->N -= length;
348 list->N -= length;
349 }
350 }
351
352 static void clear_list(bl* list) {
353 list->head = NULL;
354 list->tail = NULL;
355 list->N = 0;
356 list->last_access = NULL;
357 list->last_access_n = 0;
358 }
359
360 void bl_append_list(bl* list1, bl* list2) {
361 list1->last_access = NULL;
362 list1->last_access_n = 0;
363 if (list1->datasize != list2->datasize) {
364 printf("Error: cannot append bls with different data sizes!\n");
365 assert(0);
366 exit(0);
367 }
368 if (list1->blocksize != list2->blocksize) {
369 printf("Error: cannot append bls with different block sizes!\n");
370 assert(0);
371 exit(0);
372 }
373
374 // if list1 is empty, then just copy over list2's head and tail.
375 if (list1->head == NULL) {
376 list1->head = list2->head;
377 list1->tail = list2->tail;
378 list1->N = list2->N;
379 // remove everything from list2 (to avoid sharing nodes)
380 clear_list(list2);
381 return;
382 }
383
384 // if list2 is empty, then do nothing.
385 if (list2->head == NULL)
386 return;
387
388 // otherwise, append list2's head to list1's tail.
389 list1->tail->next = list2->head;
390 list1->tail = list2->tail;
391 list1->N += list2->N;
392 // remove everything from list2 (to avoid sharing nodes)
393 clear_list(list2);
394 }
395
396 static bl_node* bl_new_node(bl* list) {
397 bl_node* rtn;
398 // merge the mallocs for the node and its data into one malloc.
399 rtn = malloc(sizeof(bl_node) + list->datasize * list->blocksize);
400 if (!rtn) {
401 printf("Couldn't allocate memory for a bl node!\n");
402 return NULL;
403 }
404 //rtn->data = (char*)rtn + sizeof(bl_node);
405 rtn->N = 0;
406 rtn->next = NULL;
407 return rtn;
408 }
409
410 static void bl_append_node(bl* list, bl_node* node) {
411 node->next = NULL;
412 if (!list->head) {
413 // first node to be added.
414 list->head = node;
415 list->tail = node;
416 } else {
417 list->tail->next = node;
418 list->tail = node;
419 }
420 list->N += node->N;
421 }
422
423 /*
424 * Append an item to this bl node. If this node is full, then create a new
425 * node and insert it into the list.
426 *
427 * Returns the location where the new item was copied.
428 */
429 void* bl_node_append(bl* list, bl_node* node, const void* data) {
430 void* dest;
431 if (node->N == list->blocksize) {
432 // create a new node and insert it after the current node.
433 bl_node* newnode;
434 newnode = bl_new_node(list);
435 newnode->next = node->next;
436 node->next = newnode;
437 if (list->tail == node)
438 list->tail = newnode;
439 node = newnode;
440 }
441 // space remains at this node. add item.
442 dest = NODE_CHARDATA(node) + node->N * list->datasize;
443 if (data)
444 memcpy(dest, data, list->datasize);
445 node->N++;
446 list->N++;
447 return dest;
448 }
449
450 void* bl_append(bl* list, const void* data) {
451 if (!list->tail)
452 // empty list; create a new node.
453 bl_append_node(list, bl_new_node(list));
454 // append the item to the tail. if the tail node is full, a new tail node may be created.
455 return bl_node_append(list, list->tail, data);
456 }
457
458 void* bl_push(bl* list, const void* data) {
459 return bl_append(list, data);
460 }
461
462 void bl_pop(bl* list, void* into) {
463 assert(list->N > 0);
464 bl_get(list, list->N-1, into);
465 bl_remove_index(list, list->N-1);
466 }
467
468 void bl_print_structure(bl* list) {
469 bl_node* n;
470 printf("bl: head %p, tail %p, N %zu\n", list->head, list->tail, list->N);
471 for (n=list->head; n; n=n->next) {
472 printf("[N=%i] ", n->N);
473 }
474 printf("\n");
475 }
476
477 void bl_get(bl* list, size_t n, void* dest) {
478 char* src;
479 assert(list->N > 0);
480 src = bl_access(list, n);
481 memcpy(dest, src, list->datasize);
482 }
483
484 static void bl_find_ind_and_element(bl* list, const void* data,
485 int (*compare)(const void* v1, const void* v2),
486 void** presult, ptrdiff_t* pindex) {
487 ptrdiff_t lower, upper;
488 int cmp = -2;
489 void* result;
490 lower = -1;
491 upper = list->N;
492 while (lower < (upper-1)) {
493 ptrdiff_t mid;
494 mid = (upper + lower) / 2;
495 cmp = compare(data, bl_access(list, mid));
496 if (cmp >= 0) {
497 lower = mid;
498 } else {
499 upper = mid;
500 }
501 }
502 if (lower == -1 || compare(data, (result = bl_access(list, lower)))) {
503 *presult = NULL;
504 if (pindex)
505 *pindex = -1;
506 return;
507 }
508 *presult = result;
509 if (pindex)
510 *pindex = lower;
511 }
512
513 /**
514 * Finds a node for which the given compare() function
515 * returns zero when passed the given 'data' pointer
516 * and elements from the list.
517 */
518 void* bl_find(bl* list, const void* data,
519 int (*compare)(const void* v1, const void* v2)) {
520 void* rtn;
521 bl_find_ind_and_element(list, data, compare, &rtn, NULL);
522 return rtn;
523 }
524
525 ptrdiff_t bl_find_index(bl* list, const void* data,
526 int (*compare)(const void* v1, const void* v2)) {
527 void* val;
528 ptrdiff_t ind;
529 bl_find_ind_and_element(list, data, compare, &val, &ind);
530 return ind;
531 }
532
533 size_t bl_insert_sorted(bl* list, const void* data,
534 int (*compare)(const void* v1, const void* v2)) {
535 ptrdiff_t lower, upper;
536 lower = -1;
537 upper = list->N;
538 while (lower < (upper-1)) {
539 ptrdiff_t mid;
540 int cmp;
541 mid = (upper + lower) / 2;
542 cmp = compare(data, bl_access(list, mid));
543 if (cmp >= 0) {
544 lower = mid;
545 } else {
546 upper = mid;
547 }
548 }
549 bl_insert(list, lower+1, data);
550 return lower+1;
551 }
552
553 ptrdiff_t bl_insert_unique_sorted(bl* list, const void* data,
554 int (*compare)(const void* v1, const void* v2)) {
555 // This is just straightforward binary search - really should
556 // use the block structure...
557 ptrdiff_t lower, upper;
558 lower = -1;
559 upper = list->N;
560 while (lower < (upper-1)) {
561 ptrdiff_t mid;
562 int cmp;
563 mid = (upper + lower) / 2;
564 cmp = compare(data, bl_access(list, mid));
565 if (cmp >= 0) {
566 lower = mid;
567 } else {
568 upper = mid;
569 }
570 }
571
572 if (lower >= 0) {
573 if (compare(data, bl_access(list, lower)) == 0) {
574 return BL_NOT_FOUND;
575 }
576 }
577 bl_insert(list, lower+1, data);
578 return lower+1;
579 }
580
581 void bl_set(bl* list, size_t index, const void* data) {
582 bl_node* node;
583 size_t nskipped;
584 void* dataloc;
585
586 node = find_node(list, index, &nskipped);
587 dataloc = NODE_CHARDATA(node) + (index - nskipped) * list->datasize;
588 memcpy(dataloc, data, list->datasize);
589 // update the last_access member...
590 list->last_access = node;
591 list->last_access_n = nskipped;
592 }
593
594 /**
595 * Insert the element "data" into the list, such that its index is "index".
596 * All elements that previously had indices "index" and above are moved
597 * one position to the right.
598 */
599 void bl_insert(bl* list, size_t index, const void* data) {
600 bl_node* node;
601 size_t nskipped;
602
603 if (list->N == index) {
604 bl_append(list, data);
605 return;
606 }
607
608 node = find_node(list, index, &nskipped);
609
610 list->last_access = node;
611 list->last_access_n = nskipped;
612
613 // if the node is full:
614 // if we're inserting at the end of this node, then create a new node.
615 // else, shift all but the last element, add in this element, and
616 // add the last element to a new node.
617 if (node->N == list->blocksize) {
618 int localindex, nshift;
619 bl_node* next = node->next;
620 bl_node* destnode;
621 localindex = index - nskipped;
622
623 // if the next node exists and is not full, then insert the overflowing
624 // element at the front. otherwise, create a new node.
625 if (next && (next->N < list->blocksize)) {
626 // shift the existing elements up by one position...
627 memmove(NODE_CHARDATA(next) + list->datasize,
628 NODE_CHARDATA(next),
629 next->N * list->datasize);
630 destnode = next;
631 } else {
632 // create and insert a new node.
633 bl_node* newnode = bl_new_node(list);
634 newnode->next = next;
635 node->next = newnode;
636 if (!newnode->next)
637 list->tail = newnode;
638 destnode = newnode;
639 }
640
641 if (localindex == node->N) {
642 // the new element becomes the first element in the destination node.
643 memcpy(NODE_CHARDATA(destnode), data, list->datasize);
644 } else {
645 // the last element in this node is added to the destination node.
646 memcpy(NODE_CHARDATA(destnode), NODE_CHARDATA(node) + (node->N-1)*list->datasize, list->datasize);
647 // shift the end portion of this node up by one...
648 nshift = node->N - localindex - 1;
649 memmove(NODE_CHARDATA(node) + (localindex+1) * list->datasize,
650 NODE_CHARDATA(node) + localindex * list->datasize,
651 nshift * list->datasize);
652 // insert the new element...
653 memcpy(NODE_CHARDATA(node) + localindex * list->datasize, data, list->datasize);
654 }
655
656 destnode->N++;
657 list->N++;
658
659 } else {
660 // shift...
661 int localindex, nshift;
662 localindex = index - nskipped;
663 nshift = node->N - localindex;
664 memmove(NODE_CHARDATA(node) + (localindex+1) * list->datasize,
665 NODE_CHARDATA(node) + localindex * list->datasize,
666 nshift * list->datasize);
667 // insert...
668 memcpy(NODE_CHARDATA(node) + localindex * list->datasize,
669 data, list->datasize);
670 node->N++;
671 list->N++;
672 }
673 }
674
675 void* bl_access_const(const bl* list, size_t n) {
676 bl_node* node;
677 size_t nskipped;
678 node = find_node(list, n, &nskipped);
679 // grab the element.
680 return NODE_CHARDATA(node) + (n - nskipped) * list->datasize;
681 }
682
683 void bl_copy(bl* list, size_t start, size_t length, void* vdest) {
684 bl_node* node;
685 size_t nskipped;
686 char* dest;
687 if (length <= 0)
688 return;
689 node = find_node(list, start, &nskipped);
690
691 // we've found the node containing "start". keep copying elements and
692 // moving down the list until we've copied all "length" elements.
693 dest = vdest;
694 while (length > 0) {
695 size_t take, avail;
696 char* src;
697 // number of elements we want to take.
698 take = length;
699 // number of elements available at this node.
700 avail = node->N - (start - nskipped);
701 if (take > avail)
702 take = avail;
703 src = NODE_CHARDATA(node) + (start - nskipped) * list->datasize;
704 memcpy(dest, src, take * list->datasize);
705
706 dest += take * list->datasize;
707 start += take;
708 length -= take;
709 nskipped += node->N;
710 node = node->next;
711 }
712 // update the last_access member...
713 list->last_access = node;
714 list->last_access_n = nskipped;
715 }
716
717 int bl_check_consistency(bl* list) {
718 bl_node* node;
719 size_t N;
720 int tailok = 1;
721 int nempty = 0;
722 int nnull = 0;
723
724 // if one of head or tail is NULL, they had both better be NULL!
725 if (!list->head)
726 nnull++;
727 if (!list->tail)
728 nnull++;
729 if (nnull == 1) {
730 fprintf(stderr, "bl_check_consistency: head is %p, and tail is %p.\n",
731 list->head, list->tail);
732 return 1;
733 }
734
735 N = 0;
736 for (node=list->head; node; node=node->next) {
737 N += node->N;
738 if (!node->N) {
739 // this block is empty.
740 nempty++;
741 }
742 // are we at the last node?
743 if (!node->next) {
744 tailok = (list->tail == node) ? 1 : 0;
745 }
746 }
747 if (!tailok) {
748 fprintf(stderr, "bl_check_consistency: tail pointer is wrong.\n");
749 return 1;
750 }
751 if (nempty) {
752 fprintf(stderr, "bl_check_consistency: %i empty blocks.\n", nempty);
753 return 1;
754 }
755 if (N != list->N) {
756 fprintf(stderr, "bl_check_consistency: list->N is %zu, but sum of blocks is %zu.\n",
757 list->N, N);
758 return 1;
759 }
760 return 0;
761 }
762
763 int bl_check_sorted(bl* list,
764 int (*compare)(const void* v1, const void* v2),
765 int isunique) {
766 size_t i, N;
767 size_t nbad = 0;
768 void* v2 = NULL;
769 N = bl_size(list);
770 if (N)
771 v2 = bl_access(list, 0);
772 for (i=1; i<N; i++) {
773 void* v1;
774 int cmp;
775 v1 = v2;
776 v2 = bl_access(list, i);
777 cmp = compare(v1, v2);
778 if (isunique) {
779 if (cmp >= 0) {
780 nbad++;
781 }
782 } else {
783 if (cmp > 0) {
784 nbad++;
785 }
786 }
787 }
788 if (nbad) {
789 fprintf(stderr, "bl_check_sorted: %zu are out of order.\n", nbad);
790 return 1;
791 }
792 return 0;
793 }
794
795 static void memswap(void* v1, void* v2, int len) {
796 unsigned char tmp;
797 unsigned char* c1 = v1;
798 unsigned char* c2 = v2;
799 int i;
800 for (i=0; i<len; i++) {
801 tmp = *c1;
802 *c1 = *c2;
803 *c2 = tmp;
804 c1++;
805 c2++;
806 }
807 }
808
809 void bl_reverse(bl* list) {
810 // reverse each block, and reverse the order of the blocks.
811 pl* blocks;
812 bl_node* node;
813 bl_node* lastnode;
814 int i;
815
816 // reverse each block
817 blocks = pl_new(256);
818 for (node=list->head; node; node=node->next) {
819 for (i=0; i<(node->N/2); i++) {
820 memswap(NODE_CHARDATA(node) + i * list->datasize,
821 NODE_CHARDATA(node) + (node->N - 1 - i) * list->datasize,
822 list->datasize);
823 }
824 pl_append(blocks, node);
825 }
826
827 // reverse the blocks
828 lastnode = NULL;
829 for (i=pl_size(blocks)-1; i>=0; i--) {
830 node = pl_get(blocks, i);
831 if (lastnode)
832 lastnode->next = node;
833 lastnode = node;
834 }
835 if (lastnode)
836 lastnode->next = NULL;
837 pl_free(blocks);
838
839 // swap head and tail
840 node = list->head;
841 list->head = list->tail;
842 list->tail = node;
843
844 list->last_access = NULL;
845 list->last_access_n = 0;
846 }
847
848 void* bl_extend(bl* list) {
849 return bl_append(list, NULL);
850 }
851
852
853
854 // special-case pointer list accessors...
855 int bl_compare_pointers_ascending(const void* v1, const void* v2) {
856 void* p1 = *(void**)v1;
857 void* p2 = *(void**)v2;
858 if (p1 > p2) return 1;
859 else if (p1 < p2) return -1;
860 else return 0;
861 }
862
863 void pl_free_elements(pl* list) {
864 size_t i;
865 for (i=0; i<pl_size(list); i++) {
866 free(pl_get(list, i));
867 }
868 }
869
870 size_t pl_insert_sorted(pl* list, const void* data, int (*compare)(const void* v1, const void* v2)) {
871 // we don't just call bl_insert_sorted because then we end up passing
872 // "void**" rather than "void*" args to the compare function, which
873 // violates the principle of least surprise.
874 ptrdiff_t lower, upper;
875 lower = -1;
876 upper = list->N;
877 while (lower < (upper-1)) {
878 ptrdiff_t mid;
879 int cmp;
880 mid = (upper + lower) / 2;
881 cmp = compare(data, pl_get(list, mid));
882 if (cmp >= 0) {
883 lower = mid;
884 } else {
885 upper = mid;
886 }
887 }
888 bl_insert(list, lower+1, &data);
889 return lower+1;
890 }
891
892 /*
893 void pl_set(pl* list, int index, void* data) {
894 int i;
895 int nadd = (index+1) - list->N;
896 if (nadd > 0) {
897 // enlarge the list to hold 'nadd' more entries.
898 for (i=0; i<nadd; i++) {
899 pl_append(list, NULL);
900 }
901 }
902 bl_set(list, index, &data);
903 }
904 */
905
906 void sl_remove_duplicates(sl* lst) {
907 size_t i, j;
908 for (i=0; i<sl_size(lst); i++) {
909 const char* s1 = sl_get(lst, i);
910 for (j=i+1; j<sl_size(lst); j++) {
911 const char* s2 = sl_get(lst, j);
912 if (strcmp(s1, s2) == 0) {
913 sl_remove(lst, j);
914 j--;
915 }
916 }
917 }
918 }
919
920 sl* sl_new(int blocksize) {
921 pl* lst = pl_new(blocksize);
922 assert(lst);
923 return lst;
924 }
925
926 void sl_init2(sl* list, int blocksize) {
927 pl_init(list, blocksize);
928 }
929
930 void sl_free2(sl* list) {
931 size_t i;
932 if (!list) return;
933 for (i=0; i<sl_size(list); i++)
934 free(sl_get(list, i));
935 bl_free(list);
936 }
937
938 sl* sl_split(sl* lst, const char* str, const char* sepstring) {
939 int seplen;
940 const char* s;
941 char* next_sep;
942 if (!lst)
943 lst = sl_new(4);
944 seplen = strlen(sepstring);
945 s = str;
946 while (s && *s) {
947 next_sep = strstr(s, sepstring);
948 if (!next_sep) {
949 sl_append(lst, s);
950 break;
951 }
952 //logverb("Appending: '%.*s'\n", (int)(next_sep - s), s);
953 sl_appendf(lst, "%.*s", (int)(next_sep - s), s);
954 s = next_sep + seplen;
955 }
956 return lst;
957 }
958
959 void sl_free_nonrecursive(sl* list) {
960 bl_free(list);
961 }
962
963 void sl_append_contents(sl* dest, sl* src) {
964 size_t i;
965 if (!src)
966 return;
967 for (i=0; i<sl_size(src); i++) {
968 char* str = sl_get(src, i);
969 sl_append(dest, str);
970 }
971 }
972
973 ptrdiff_t sl_index_of(sl* lst, const char* str) {
974 size_t i;
975 for (i=0; i<sl_size(lst); i++) {
976 char* s = sl_get(lst, i);
977 if (strcmp(s, str) == 0)
978 return i;
979 }
980 return BL_NOT_FOUND;
981 }
982
983 ptrdiff_t sl_last_index_of(sl* lst, const char* str) {
984 ptrdiff_t i;
985 for (i=sl_size(lst)-1; i>=0; i--) {
986 char* s = sl_get(lst, i);
987 if (strcmp(s, str) == 0)
988 return i;
989 }
990 return BL_NOT_FOUND;
991 }
992
993 // Returns 0 if the string is not in the sl, 1 otherwise.
994 // (same as sl_index_of(lst, str) > -1)
995 int sl_contains(sl* lst, const char* str) {
996 return (sl_index_of(lst, str) > -1);
997 }
998
999 void sl_reverse(sl* list) {
1000 bl_reverse(list);
1001 }
1002
1003 char* sl_append(sl* list, const char* data) {
1004 char* copy;
1005 if (data) {
1006 copy = strdup(data);
1007 assert(copy);
1008 } else
1009 copy = NULL;
1010 pl_append(list, copy);
1011 return copy;
1012 }
1013
1014 void sl_append_array(sl* list, const char**strings, size_t n) {
1015 size_t i;
1016 for (i=0; i<n; i++)
1017 sl_append(list, strings[i]);
1018 }
1019
1020 void sl_append_nocopy(sl* list, const char* data) {
1021 pl_append(list, data);
1022 }
1023
1024 char* sl_push(sl* list, const char* data) {
1025 char* copy = strdup(data);
1026 pl_push(list, copy);
1027 return copy;
1028 }
1029
1030 char* sl_pop(sl* list) {
1031 return pl_pop(list);
1032 }
1033
1034 char* sl_get(sl* list, size_t n) {
1035 return pl_get(list, n);
1036 }
1037
1038 char* sl_get_const(const sl* list, size_t n) {
1039 return pl_get_const(list, n);
1040 }
1041
1042 char* sl_set(sl* list, size_t index, const char* value) {
1043 char* copy;
1044 assert(index >= 0);
1045 copy = strdup(value);
1046 if (index < list->N) {
1047 // we're replacing an existing value - free it!
1048 free(sl_get(list, index));
1049 bl_set(list, index, &copy);
1050 } else {
1051 // pad
1052 size_t i;
1053 for (i=list->N; i<index; i++)
1054 sl_append_nocopy(list, NULL);
1055 sl_append(list, copy);
1056 }
1057 return copy;
1058 }
1059
1060 int sl_check_consistency(sl* list) {
1061 return bl_check_consistency(list);
1062 }
1063
1064 char* sl_insert(sl* list, size_t indx, const char* data) {
1065 char* copy = strdup(data);
1066 bl_insert(list, indx, &copy);
1067 return copy;
1068 }
1069
1070 void sl_insert_nocopy(sl* list, size_t indx, const char* str) {
1071 bl_insert(list, indx, &str);
1072 }
1073
1074 void sl_remove_from(sl* list, size_t start) {
1075 sl_remove_index_range(list, start, sl_size(list) - start);
1076 }
1077
1078 ptrdiff_t sl_remove_string(sl* list, const char* string) {
1079 return pl_remove_value(list, string);
1080 }
1081
1082 char* sl_remove_string_bycaseval(sl* list, const char* string) {
1083 size_t N = sl_size(list);
1084 size_t i;
1085 for (i=0; i<N; i++) {
1086 char* str = sl_get(list, i);
1087 if (strcasecmp(str, string) == 0) {
1088 char* s = sl_get(list, i);
1089 sl_remove(list, i);
1090 return s;
1091 }
1092 }
1093 return NULL;
1094 }
1095
1096 ptrdiff_t sl_remove_string_byval(sl* list, const char* string) {
1097 size_t N = sl_size(list);
1098 size_t i;
1099 for (i=0; i<N; i++) {
1100 char* str = sl_get(list, i);
1101 if (strcmp(str, string) == 0) {
1102 sl_remove(list, i);
1103 return i;
1104 }
1105 }
1106 return BL_NOT_FOUND;
1107 }
1108
1109 void sl_remove_index_range(sl* list, size_t start, size_t length) {
1110 size_t i;
1111 assert(list);
1112 assert(start + length <= list->N);
1113 assert(start >= 0);
1114 assert(length >= 0);
1115 for (i=0; i<length; i++) {
1116 char* str = sl_get(list, start + i);
1117 free(str);
1118 }
1119 bl_remove_index_range(list, start, length);
1120 }
1121
1122 void sl_remove(sl* list, size_t index) {
1123 bl_remove_index(list, index);
1124 }
1125
1126 void sl_remove_all(sl* list) {
1127 size_t i;
1128 if (!list) return;
1129 for (i=0; i<sl_size(list); i++)
1130 free(pl_get(list, i));
1131 bl_remove_all(list);
1132 }
1133
1134 void sl_merge_lists(sl* list1, sl* list2) {
1135 bl_append_list(list1, list2);
1136 }
1137
1138 void sl_print(sl* list) {
1139 bl_node* n;
1140 int i;
1141 for (n=list->head; n; n=n->next) {
1142 printf("[\n");
1143 for (i=0; i<n->N; i++)
1144 printf(" \"%s\"\n", ((char**)NODE_DATA(n))[i]);
1145 printf("]\n");
1146 }
1147 }
1148
1149 static char* sljoin(sl* list, const char* join, int forward) {
1150 size_t start, end, inc;
1151
1152 size_t len = 0;
1153 size_t i, N;
1154 char* rtn;
1155 size_t offset;
1156 size_t JL;
1157
1158 if (sl_size(list) == 0)
1159 return strdup("");
1160
1161 // step through the list forward or backward?
1162 if (forward) {
1163 start = 0;
1164 end = sl_size(list);
1165 inc = 1;
1166 } else {
1167 start = sl_size(list) - 1;
1168 end = -1;
1169 inc = -1;
1170 }
1171
1172 JL = strlen(join);
1173 N = sl_size(list);
1174 for (i=0; i<N; i++)
1175 len += strlen(sl_get(list, i));
1176 len += ((N-1) * JL);
1177 rtn = malloc(len + 1);
1178 if (!rtn)
1179 return rtn;
1180 offset = 0;
1181 for (i=start; i!=end; i+= inc) {
1182 char* str = sl_get(list, i);
1183 size_t L = strlen(str);
1184 if (i != start) {
1185 memcpy(rtn + offset, join, JL);
1186 offset += JL;
1187 }
1188 memcpy(rtn + offset, str, L);
1189 offset += L;
1190 }
1191 assert(offset == len);
1192 rtn[offset] = '\0';
1193 return rtn;
1194 }
1195
1196 char* sl_join(sl* list, const char* join) {
1197 return sljoin(list, join, 1);
1198 }
1199
1200 char* sl_join_reverse(sl* list, const char* join) {
1201 return sljoin(list, join, 0);
1202 }
1203
1204 char* sl_implode(sl* list, const char* join) {
1205 return sl_join(list, join);
1206 }
1207
1208 char* sl_appendf(sl* list, const char* format, ...) {
1209 char* str;
1210 va_list lst;
1211 va_start(lst, format);
1212 str = sl_appendvf(list, format, lst);
1213 va_end(lst);
1214 return str;
1215 }
1216
1217 char* sl_appendvf(sl* list, const char* format, va_list va) {
1218 char* str;
1219 if (vasprintf(&str, format, va) == -1)
1220 return NULL;
1221 sl_append_nocopy(list, str);
1222 return str;
1223 }
1224
1225 char* sl_insert_sortedf(sl* list, const char* format, ...) {
1226 va_list lst;
1227 char* str;
1228 va_start(lst, format);
1229 if (vasprintf(&str, format, lst) == -1)
1230 return NULL;
1231 sl_insert_sorted_nocopy(list, str);
1232 va_end(lst);
1233 return str;
1234 }
1235
1236 char* sl_insertf(sl* list, size_t index, const char* format, ...) {
1237 va_list lst;
1238 char* str;
1239 va_start(lst, format);
1240 if (vasprintf(&str, format, lst) == -1)
1241 return NULL;
1242 sl_insert_nocopy(list, index, str);
1243 va_end(lst);
1244 return str;
1245 }
1246
1247 int bl_compare_strings_ascending(const void* v1, const void* v2) {
1248 const char* str1 = v1;
1249 const char* str2 = v2;
1250 return strcoll(str1, str2);
1251 }
1252
1253 void sl_insert_sorted_nocopy(sl* list, const char* string) {
1254 pl_insert_sorted(list, string, bl_compare_strings_ascending);
1255 }
1256
1257 char* sl_insert_sorted(sl* list, const char* string) {
1258 char* copy = strdup(string);
1259 pl_insert_sorted(list, copy, bl_compare_strings_ascending);
1260 return copy;
1261 }
1262
1263 #define InlineDefine InlineDefineC
1264 #include "bl.inc"
1265 #undef InlineDefine