-
Notifications
You must be signed in to change notification settings - Fork 0
/
simclist.c
1525 lines (1225 loc) · 47 KB
/
simclist.c
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
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 2007,2008,2009,2010,2011 Mij <mij@bitchx.it>
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
/*
* SimCList library. See http://mij.oltrelinux.com/devel/simclist
*/
/* SimCList implementation, version 1.6 */
#include <stdlib.h>
#include <string.h>
#include <errno.h> /* for setting errno */
#include <sys/types.h>
#ifndef _WIN32
/* not in Windows! */
# include <unistd.h>
# include <stdint.h>
#endif
#ifndef SIMCLIST_NO_DUMPRESTORE
/* includes for dump/restore */
# include <time.h>
# include <sys/uio.h> /* for READ_ERRCHECK() and write() */
# include <fcntl.h> /* for open() etc */
# ifndef _WIN32
# include <arpa/inet.h> /* for htons() on UNIX */
# else
# include <winsock2.h> /* for htons() on Windows */
# endif
#endif
/* disable asserts */
#ifndef SIMCLIST_DEBUG
#define NDEBUG
#endif
#include <assert.h>
#include <sys/stat.h> /* for open()'s access modes S_IRUSR etc */
#include <limits.h>
#if defined(_MSC_VER) || defined(__MINGW32__)
/* provide gettimeofday() missing in Windows */
int gettimeofday(struct timeval *tp, void *tzp) {
DWORD t;
/* XSI says: "If tzp is not a null pointer, the behavior is unspecified" */
assert(tzp == NULL);
t = timeGetTime();
tp->tv_sec = t / 1000;
tp->tv_usec = t % 1000;
return 0;
}
#endif
/* work around lack of inttypes.h support in broken Microsoft Visual Studio compilers */
#if !defined(_WIN32) || !defined(_MSC_VER)
# include <inttypes.h> /* (u)int*_t */
#else
# include <basetsd.h>
typedef UINT8 uint8_t;
typedef UINT16 uint16_t;
typedef ULONG32 uint32_t;
typedef UINT64 uint64_t;
typedef INT8 int8_t;
typedef INT16 int16_t;
typedef LONG32 int32_t;
typedef INT64 int64_t;
#endif
/* define some commodity macros for Dump/Restore functionality */
#ifndef SIMCLIST_NO_DUMPRESTORE
/* write() decorated with error checking logic */
#define WRITE_ERRCHECK(fd, msgbuf, msglen) do { \
if (write(fd, msgbuf, msglen) < 0) return -1; \
} while (0);
/* READ_ERRCHECK() decorated with error checking logic */
#define READ_ERRCHECK(fd, msgbuf, msglen) do { \
if (read(fd, msgbuf, msglen) != msglen) { \
/*errno = EPROTO;*/ \
return -1; \
} \
} while (0);
/* convert 64bit integers from host to network format */
#define hton64(x) (\
htons(1) == 1 ? \
(uint64_t)x /* big endian */ \
: /* little endian */ \
((uint64_t)((((uint64_t)(x) & 0xff00000000000000ULL) >> 56) | \
(((uint64_t)(x) & 0x00ff000000000000ULL) >> 40) | \
(((uint64_t)(x) & 0x0000ff0000000000ULL) >> 24) | \
(((uint64_t)(x) & 0x000000ff00000000ULL) >> 8) | \
(((uint64_t)(x) & 0x00000000ff000000ULL) << 8) | \
(((uint64_t)(x) & 0x0000000000ff0000ULL) << 24) | \
(((uint64_t)(x) & 0x000000000000ff00ULL) << 40) | \
(((uint64_t)(x) & 0x00000000000000ffULL) << 56))) \
)
/* convert 64bit integers from network to host format */
#define ntoh64(x) (hton64(x))
#endif
/* some OSes don't have EPROTO (eg OpenBSD) */
#ifndef EPROTO
#define EPROTO EIO
#endif
#ifdef SIMCLIST_WITH_THREADS
/* limit (approx) to the number of threads running
* for threaded operations. Only meant when
* SIMCLIST_WITH_THREADS is defined */
#define SIMCLIST_MAXTHREADS 2
#endif
/*
* how many elems to keep as spare. During a deletion, an element
* can be saved in a "free-list", not free()d immediately. When
* latter insertions are performed, spare elems can be used instead
* of malloc()ing new elems.
*
* about this param, some values for appending
* 10 million elems into an empty list:
* (#, time[sec], gain[%], gain/no[%])
* 0 2,164 0,00 0,00 <-- feature disabled
* 1 1,815 34,9 34,9
* 2 1,446 71,8 35,9 <-- MAX gain/no
* 3 1,347 81,7 27,23
* 5 1,213 95,1 19,02
* 8 1,064 110,0 13,75
* 10 1,015 114,9 11,49 <-- MAX gain w/ likely sol
* 15 1,019 114,5 7,63
* 25 0,985 117,9 4,72
* 50 1,088 107,6 2,15
* 75 1,016 114,8 1,53
* 100 0,988 117,6 1,18
* 150 1,022 114,2 0,76
* 200 0,939 122,5 0,61 <-- MIN time
*/
#ifndef SIMCLIST_MAX_SPARE_ELEMS
#define SIMCLIST_MAX_SPARE_ELEMS 5
#endif
#ifdef SIMCLIST_WITH_THREADS
#include <pthread.h>
#endif
#include <PCSC/simclist.h>
/* minumum number of elements for sorting with quicksort instead of insertion */
#define SIMCLIST_MINQUICKSORTELS 24
/* list dump declarations */
#define SIMCLIST_DUMPFORMAT_VERSION 1 /* (short integer) version of fileformat managed by _dump* and _restore* functions */
#define SIMCLIST_DUMPFORMAT_HEADERLEN 30 /* length of the header */
/* header for a list dump */
struct list_dump_header_s {
uint16_t ver; /* version */
int32_t timestamp_sec; /* dump timestamp, seconds since UNIX Epoch */
int32_t timestamp_usec; /* dump timestamp, microseconds since timestamp_sec */
int32_t rndterm; /* random value terminator -- terminates the data sequence */
uint32_t totlistlen; /* sum of every element' size, bytes */
uint32_t numels; /* number of elements */
uint32_t elemlen; /* bytes length of an element, for constant-size lists, <= 0 otherwise */
int32_t listhash; /* hash of the list at the time of dumping, or 0 if to be ignored */
};
/* deletes tmp from list, with care wrt its position (head, tail, middle) */
static int list_drop_elem(list_t *restrict l, struct list_entry_s *tmp, unsigned int pos);
/* set default values for initialized lists */
static int list_attributes_setdefaults(list_t *restrict l);
#ifndef NDEBUG
/* check whether the list internal REPresentation is valid -- Costs O(n) */
static int list_repOk(const list_t *restrict l);
/* check whether the list attribute set is valid -- Costs O(1) */
static int list_attrOk(const list_t *restrict l);
#endif
/* do not inline, this is recursive */
static void list_sort_quicksort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel);
static inline void list_sort_selectionsort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel);
static void *list_get_minmax(const list_t *restrict l, int versus);
static inline struct list_entry_s *list_findpos(const list_t *restrict l, int posstart);
/*
* Random Number Generator
*
* The user is expected to seed the RNG (ie call srand()) if
* SIMCLIST_SYSTEM_RNG is defined.
*
* Otherwise, a self-contained RNG based on LCG is used; see
* http://en.wikipedia.org/wiki/Linear_congruential_generator .
*
* Facts pro local RNG:
* 1. no need for the user to call srand() on his own
* 2. very fast, possibly faster than OS
* 3. avoid interference with user's RNG
*
* Facts pro system RNG:
* 1. may be more accurate (irrelevant for SimCList randno purposes)
* 2. why reinvent the wheel
*
* Default to local RNG for user's ease of use.
*/
#ifdef SIMCLIST_SYSTEM_RNG
/* keep track whether we initialized already (non-0) or not (0) */
static unsigned random_seed = 0;
/* use local RNG */
static inline void seed_random(void) {
if (random_seed == 0)
random_seed = (unsigned)getpid() ^ (unsigned)time(NULL);
}
static inline long get_random(void) {
random_seed = (1664525 * random_seed + 1013904223);
return random_seed;
}
#else
/* use OS's random generator */
# define seed_random()
# define get_random() (rand())
#endif
/* list initialization */
int list_init(list_t *restrict l) {
if (l == NULL) return -1;
seed_random();
l->numels = 0;
/* head/tail sentinels and mid pointer */
l->head_sentinel = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
l->tail_sentinel = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
l->head_sentinel->next = l->tail_sentinel;
l->tail_sentinel->prev = l->head_sentinel;
l->head_sentinel->prev = l->tail_sentinel->next = l->mid = NULL;
l->head_sentinel->data = l->tail_sentinel->data = NULL;
/* iteration attributes */
l->iter_active = 0;
l->iter_pos = 0;
l->iter_curentry = NULL;
/* free-list attributes */
l->spareels = (struct list_entry_s **)malloc(SIMCLIST_MAX_SPARE_ELEMS * sizeof(struct list_entry_s *));
l->spareelsnum = 0;
#ifdef SIMCLIST_WITH_THREADS
l->threadcount = 0;
#endif
list_attributes_setdefaults(l);
assert(list_repOk(l));
assert(list_attrOk(l));
return 0;
}
void list_destroy(list_t *restrict l) {
unsigned int i;
list_clear(l);
for (i = 0; i < l->spareelsnum; i++) {
free(l->spareels[i]);
}
free(l->spareels);
free(l->head_sentinel);
free(l->tail_sentinel);
}
int list_attributes_setdefaults(list_t *restrict l) {
l->attrs.comparator = NULL;
l->attrs.seeker = NULL;
/* also free() element data when removing and element from the list */
l->attrs.meter = NULL;
l->attrs.copy_data = 0;
l->attrs.hasher = NULL;
/* serializer/unserializer */
l->attrs.serializer = NULL;
l->attrs.unserializer = NULL;
assert(list_attrOk(l));
return 0;
}
/* setting list properties */
int list_attributes_comparator(list_t *restrict l, element_comparator comparator_fun) {
if (l == NULL) return -1;
l->attrs.comparator = comparator_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_seeker(list_t *restrict l, element_seeker seeker_fun) {
if (l == NULL) return -1;
l->attrs.seeker = seeker_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_copy(list_t *restrict l, element_meter metric_fun, int copy_data) {
if (l == NULL || (metric_fun == NULL && copy_data != 0)) return -1;
l->attrs.meter = metric_fun;
l->attrs.copy_data = copy_data;
assert(list_attrOk(l));
return 0;
}
int list_attributes_hash_computer(list_t *restrict l, element_hash_computer hash_computer_fun) {
if (l == NULL) return -1;
l->attrs.hasher = hash_computer_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_serializer(list_t *restrict l, element_serializer serializer_fun) {
if (l == NULL) return -1;
l->attrs.serializer = serializer_fun;
assert(list_attrOk(l));
return 0;
}
int list_attributes_unserializer(list_t *restrict l, element_unserializer unserializer_fun) {
if (l == NULL) return -1;
l->attrs.unserializer = unserializer_fun;
assert(list_attrOk(l));
return 0;
}
int list_append(list_t *restrict l, const void *data) {
return list_insert_at(l, data, l->numels);
}
int list_prepend(list_t *restrict l, const void *data) {
return list_insert_at(l, data, 0);
}
void *list_fetch(list_t *restrict l) {
return list_extract_at(l, 0);
}
void *list_get_at(const list_t *restrict l, unsigned int pos) {
struct list_entry_s *tmp;
tmp = list_findpos(l, pos);
return (tmp != NULL ? tmp->data : NULL);
}
void *list_get_max(const list_t *restrict l) {
return list_get_minmax(l, +1);
}
void *list_get_min(const list_t *restrict l) {
return list_get_minmax(l, -1);
}
/* REQUIRES {list->numels >= 1}
* return the min (versus < 0) or max value (v > 0) in l */
static void *list_get_minmax(const list_t *restrict l, int versus) {
void *curminmax;
struct list_entry_s *s;
if (l->attrs.comparator == NULL || l->numels == 0)
return NULL;
curminmax = l->head_sentinel->next->data;
for (s = l->head_sentinel->next->next; s != l->tail_sentinel; s = s->next) {
if (l->attrs.comparator(curminmax, s->data) * versus > 0)
curminmax = s->data;
}
return curminmax;
}
/* set tmp to point to element at index posstart in l */
static inline struct list_entry_s *list_findpos(const list_t *restrict l, int posstart) {
struct list_entry_s *ptr;
float x;
int i;
/* accept 1 slot overflow for fetching head and tail sentinels */
if (posstart < -1 || posstart > (int)l->numels) return NULL;
x = (float)(posstart+1) / l->numels;
if (x <= 0.25) {
/* first quarter: get to posstart from head */
for (i = -1, ptr = l->head_sentinel; i < posstart; ptr = ptr->next, i++);
} else if (x < 0.5) {
/* second quarter: get to posstart from mid */
for (i = (l->numels-1)/2, ptr = l->mid; i > posstart; ptr = ptr->prev, i--);
} else if (x <= 0.75) {
/* third quarter: get to posstart from mid */
for (i = (l->numels-1)/2, ptr = l->mid; i < posstart; ptr = ptr->next, i++);
} else {
/* fourth quarter: get to posstart from tail */
for (i = l->numels, ptr = l->tail_sentinel; i > posstart; ptr = ptr->prev, i--);
}
return ptr;
}
void *list_extract_at(list_t *restrict l, unsigned int pos) {
struct list_entry_s *tmp;
void *data;
if (l->iter_active || pos >= l->numels) return NULL;
tmp = list_findpos(l, pos);
data = tmp->data;
tmp->data = NULL; /* save data from list_drop_elem() free() */
list_drop_elem(l, tmp, pos);
l->numels--;
assert(list_repOk(l));
return data;
}
int list_insert_at(list_t *restrict l, const void *data, unsigned int pos) {
struct list_entry_s *lent, *succ, *prec;
if (l->iter_active || pos > l->numels) return -1;
/* this code optimizes malloc() with a free-list */
if (l->spareelsnum > 0) {
lent = l->spareels[l->spareelsnum-1];
l->spareelsnum--;
} else {
lent = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
if (lent == NULL)
return -1;
}
if (l->attrs.copy_data) {
/* make room for user' data (has to be copied) */
size_t datalen = l->attrs.meter(data);
lent->data = (struct list_entry_s *)malloc(datalen);
memcpy(lent->data, data, datalen);
} else {
lent->data = (void*)data;
}
/* actually append element */
prec = list_findpos(l, pos-1);
succ = prec->next;
prec->next = lent;
lent->prev = prec;
lent->next = succ;
succ->prev = lent;
l->numels++;
/* fix mid pointer */
if (l->numels == 1) { /* first element, set pointer */
l->mid = lent;
} else if (l->numels % 2) { /* now odd */
if (pos >= (l->numels-1)/2) l->mid = l->mid->next;
} else { /* now even */
if (pos <= (l->numels-1)/2) l->mid = l->mid->prev;
}
assert(list_repOk(l));
return 1;
}
int list_delete(list_t *restrict l, const void *data) {
int pos, r;
pos = list_locate(l, data);
if (pos < 0)
return -1;
r = list_delete_at(l, pos);
if (r < 0)
return -1;
assert(list_repOk(l));
return 0;
}
int list_delete_at(list_t *restrict l, unsigned int pos) {
struct list_entry_s *delendo;
if (l->iter_active || pos >= l->numels) return -1;
delendo = list_findpos(l, pos);
list_drop_elem(l, delendo, pos);
l->numels--;
assert(list_repOk(l));
return 0;
}
int list_delete_range(list_t *restrict l, unsigned int posstart, unsigned int posend) {
struct list_entry_s *lastvalid, *tmp, *tmp2;
unsigned int numdel, midposafter, i;
int movedx;
if (l->iter_active || posend < posstart || posend >= l->numels) return -1;
numdel = posend - posstart + 1;
if (numdel == l->numels) return list_clear(l);
tmp = list_findpos(l, posstart); /* first el to be deleted */
lastvalid = tmp->prev; /* last valid element */
midposafter = (l->numels-1-numdel)/2;
midposafter = midposafter < posstart ? midposafter : midposafter+numdel;
movedx = midposafter - (l->numels-1)/2;
if (movedx > 0) { /* move right */
for (i = 0; i < (unsigned int)movedx; l->mid = l->mid->next, i++);
} else { /* move left */
movedx = -movedx;
for (i = 0; i < (unsigned int)movedx; l->mid = l->mid->prev, i++);
}
assert(posstart == 0 || lastvalid != l->head_sentinel);
i = posstart;
if (l->attrs.copy_data) {
/* also free element data */
for (; i <= posend; i++) {
tmp2 = tmp;
tmp = tmp->next;
if (tmp2->data != NULL) free(tmp2->data);
if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
l->spareels[l->spareelsnum++] = tmp2;
} else {
free(tmp2);
}
}
} else {
/* only free containers */
for (; i <= posend; i++) {
tmp2 = tmp;
tmp = tmp->next;
if (l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS) {
l->spareels[l->spareelsnum++] = tmp2;
} else {
free(tmp2);
}
}
}
assert(i == posend+1 && (posend != l->numels || tmp == l->tail_sentinel));
lastvalid->next = tmp;
tmp->prev = lastvalid;
l->numels -= posend - posstart + 1;
assert(list_repOk(l));
return numdel;
}
int list_clear(list_t *restrict l) {
struct list_entry_s *s;
unsigned int numels;
/* will be returned */
numels = l->numels;
if (l->iter_active) return -1;
if (l->attrs.copy_data) { /* also free user data */
/* spare a loop conditional with two loops: spareing elems and freeing elems */
for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
/* move elements as spares as long as there is room */
if (s->data != NULL) free(s->data);
l->spareels[l->spareelsnum++] = s;
}
while (s != l->tail_sentinel) {
/* free the remaining elems */
if (s->data != NULL) free(s->data);
s = s->next;
free(s->prev);
}
l->head_sentinel->next = l->tail_sentinel;
l->tail_sentinel->prev = l->head_sentinel;
} else { /* only free element containers */
/* spare a loop conditional with two loops: spareing elems and freeing elems */
for (s = l->head_sentinel->next; l->spareelsnum < SIMCLIST_MAX_SPARE_ELEMS && s != l->tail_sentinel; s = s->next) {
/* move elements as spares as long as there is room */
l->spareels[l->spareelsnum++] = s;
}
while (s != l->tail_sentinel) {
/* free the remaining elems */
s = s->next;
free(s->prev);
}
l->head_sentinel->next = l->tail_sentinel;
l->tail_sentinel->prev = l->head_sentinel;
}
l->numels = 0;
l->mid = NULL;
assert(list_repOk(l));
return numels;
}
unsigned int list_size(const list_t *restrict l) {
return l->numels;
}
int list_empty(const list_t *restrict l) {
return (l->numels == 0);
}
int list_locate(const list_t *restrict l, const void *data) {
struct list_entry_s *el;
int pos = 0;
if (l->attrs.comparator != NULL) {
/* use comparator */
for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
if (l->attrs.comparator(data, el->data) == 0) break;
}
} else {
/* compare references */
for (el = l->head_sentinel->next; el != l->tail_sentinel; el = el->next, pos++) {
if (el->data == data) break;
}
}
if (el == l->tail_sentinel) return -1;
return pos;
}
void *list_seek(list_t *restrict l, const void *indicator) {
const struct list_entry_s *iter;
if (l->attrs.seeker == NULL) return NULL;
for (iter = l->head_sentinel->next; iter != l->tail_sentinel; iter = iter->next) {
if (l->attrs.seeker(iter->data, indicator) != 0) return iter->data;
}
return NULL;
}
int list_contains(const list_t *restrict l, const void *data) {
return (list_locate(l, data) >= 0);
}
int list_concat(const list_t *l1, const list_t *l2, list_t *restrict dest) {
struct list_entry_s *el, *srcel;
unsigned int cnt;
int err;
if (l1 == NULL || l2 == NULL || dest == NULL || l1 == dest || l2 == dest)
return -1;
list_init(dest);
dest->numels = l1->numels + l2->numels;
if (dest->numels == 0)
return 0;
/* copy list1 */
srcel = l1->head_sentinel->next;
el = dest->head_sentinel;
while (srcel != l1->tail_sentinel) {
el->next = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
el->next->prev = el;
el = el->next;
el->data = srcel->data;
srcel = srcel->next;
}
dest->mid = el; /* approximate position (adjust later) */
/* copy list 2 */
srcel = l2->head_sentinel->next;
while (srcel != l2->tail_sentinel) {
el->next = (struct list_entry_s *)malloc(sizeof(struct list_entry_s));
el->next->prev = el;
el = el->next;
el->data = srcel->data;
srcel = srcel->next;
}
el->next = dest->tail_sentinel;
dest->tail_sentinel->prev = el;
/* fix mid pointer */
err = l2->numels - l1->numels;
if ((err+1)/2 > 0) { /* correct pos RIGHT (err-1)/2 moves */
err = (err+1)/2;
for (cnt = 0; cnt < (unsigned int)err; cnt++) dest->mid = dest->mid->next;
} else if (err/2 < 0) { /* correct pos LEFT (err/2)-1 moves */
err = -err/2;
for (cnt = 0; cnt < (unsigned int)err; cnt++) dest->mid = dest->mid->prev;
}
assert(!(list_repOk(l1) && list_repOk(l2)) || list_repOk(dest));
return 0;
}
int list_sort(list_t *restrict l, int versus) {
if (l->iter_active || l->attrs.comparator == NULL) /* cannot modify list in the middle of an iteration */
return -1;
if (l->numels <= 1)
return 0;
list_sort_quicksort(l, versus, 0, l->head_sentinel->next, l->numels-1, l->tail_sentinel->prev);
assert(list_repOk(l));
return 0;
}
#ifdef SIMCLIST_WITH_THREADS
struct list_sort_wrappedparams {
list_t *restrict l;
int versus;
unsigned int first, last;
struct list_entry_s *fel, *lel;
};
static void *list_sort_quicksort_threadwrapper(void *wrapped_params) {
struct list_sort_wrappedparams *wp = (struct list_sort_wrappedparams *)wrapped_params;
list_sort_quicksort(wp->l, wp->versus, wp->first, wp->fel, wp->last, wp->lel);
free(wp);
pthread_exit(NULL);
return NULL;
}
#endif
static inline void list_sort_selectionsort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel) {
struct list_entry_s *cursor, *toswap, *firstunsorted;
void *tmpdata;
if (last <= first) /* <= 1-element lists are always sorted */
return;
for (firstunsorted = fel; firstunsorted != lel; firstunsorted = firstunsorted->next) {
/* find min or max in the remainder of the list */
for (toswap = firstunsorted, cursor = firstunsorted->next; cursor != lel->next; cursor = cursor->next)
if (l->attrs.comparator(toswap->data, cursor->data) * -versus > 0) toswap = cursor;
if (toswap != firstunsorted) { /* swap firstunsorted with toswap */
tmpdata = firstunsorted->data;
firstunsorted->data = toswap->data;
toswap->data = tmpdata;
}
}
}
static void list_sort_quicksort(list_t *restrict l, int versus,
unsigned int first, struct list_entry_s *fel,
unsigned int last, struct list_entry_s *lel) {
unsigned int pivotid;
unsigned int i;
register struct list_entry_s *pivot;
struct list_entry_s *left, *right;
void *tmpdata;
#ifdef SIMCLIST_WITH_THREADS
pthread_t tid;
int traised;
#endif
if (last <= first) /* <= 1-element lists are always sorted */
return;
if (last - first+1 <= SIMCLIST_MINQUICKSORTELS) {
list_sort_selectionsort(l, versus, first, fel, last, lel);
return;
}
/* base of iteration: one element list */
if (! (last > first)) return;
pivotid = (get_random() % (last - first + 1));
/* pivotid = (last - first + 1) / 2; */
/* find pivot */
if (pivotid < (last - first + 1)/2) {
for (i = 0, pivot = fel; i < pivotid; pivot = pivot->next, i++);
} else {
for (i = last - first, pivot = lel; i > pivotid; pivot = pivot->prev, i--);
}
/* smaller PIVOT bigger */
left = fel;
right = lel;
/* iterate --- left ---> PIV <--- right --- */
while (left != pivot && right != pivot) {
for (; left != pivot && (l->attrs.comparator(left->data, pivot->data) * -versus <= 0); left = left->next);
/* left points to a smaller element, or to pivot */
for (; right != pivot && (l->attrs.comparator(right->data, pivot->data) * -versus >= 0); right = right->prev);
/* right points to a bigger element, or to pivot */
if (left != pivot && right != pivot) {
/* swap, then move iterators */
tmpdata = left->data;
left->data = right->data;
right->data = tmpdata;
left = left->next;
right = right->prev;
}
}
/* now either left points to pivot (end run), or right */
if (right == pivot) { /* left part longer */
while (left != pivot) {
if (l->attrs.comparator(left->data, pivot->data) * -versus > 0) {
tmpdata = left->data;
left->data = pivot->prev->data;
pivot->prev->data = pivot->data;
pivot->data = tmpdata;
pivot = pivot->prev;
pivotid--;
if (pivot == left) break;
} else {
left = left->next;
}
}
} else { /* right part longer */
while (right != pivot) {
if (l->attrs.comparator(right->data, pivot->data) * -versus < 0) {
/* move current right before pivot */
tmpdata = right->data;
right->data = pivot->next->data;
pivot->next->data = pivot->data;
pivot->data = tmpdata;
pivot = pivot->next;
pivotid++;
if (pivot == right) break;
} else {
right = right->prev;
}
}
}
/* sort sublists A and B : |---A---| pivot |---B---| */
#ifdef SIMCLIST_WITH_THREADS
traised = 0;
if (pivotid > 0) {
/* prepare wrapped args, then start thread */
if (l->threadcount < SIMCLIST_MAXTHREADS-1) {
struct list_sort_wrappedparams *wp = (struct list_sort_wrappedparams *)malloc(sizeof(struct list_sort_wrappedparams));
l->threadcount++;
traised = 1;
wp->l = l;
wp->versus = versus;
wp->first = first;
wp->fel = fel;
wp->last = first+pivotid-1;
wp->lel = pivot->prev;
if (pthread_create(&tid, NULL, list_sort_quicksort_threadwrapper, wp) != 0) {
free(wp);
traised = 0;
list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
}
} else {
list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
}
}
if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
if (traised) {
pthread_join(tid, (void **)NULL);
l->threadcount--;
}
#else
if (pivotid > 0) list_sort_quicksort(l, versus, first, fel, first+pivotid-1, pivot->prev);
if (first + pivotid < last) list_sort_quicksort(l, versus, first+pivotid+1, pivot->next, last, lel);
#endif
}
int list_iterator_start(list_t *restrict l) {
if (l->iter_active) return 0;
l->iter_pos = 0;
l->iter_active = 1;
l->iter_curentry = l->head_sentinel->next;
return 1;
}
void *list_iterator_next(list_t *restrict l) {
void *toret;
if (! l->iter_active) return NULL;
toret = l->iter_curentry->data;
l->iter_curentry = l->iter_curentry->next;
l->iter_pos++;
return toret;
}
int list_iterator_hasnext(const list_t *restrict l) {
if (! l->iter_active) return 0;
return (l->iter_pos < l->numels);
}
int list_iterator_stop(list_t *restrict l) {
if (! l->iter_active) return 0;
l->iter_pos = 0;
l->iter_active = 0;
return 1;
}
int list_hash(const list_t *restrict l, list_hash_t *restrict hash) {
struct list_entry_s *x;
list_hash_t tmphash;
assert(hash != NULL);
tmphash = l->numels * 2 + 100;
if (l->attrs.hasher == NULL) {
#ifdef SIMCLIST_ALLOW_LOCATIONBASED_HASHES
/* ENABLE WITH CARE !! */
#warning "Memlocation-based hash is consistent only for testing modification in the same program run."
int i;
/* only use element references */
for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
for (i = 0; i < sizeof(x->data); i++) {
tmphash += (tmphash ^ (uintptr_t)x->data);
}
tmphash += tmphash % l->numels;
}
#else
return -1;
#endif
} else {
/* hash each element with the user-given function */
for (x = l->head_sentinel->next; x != l->tail_sentinel; x = x->next) {
tmphash += tmphash ^ l->attrs.hasher(x->data);
tmphash += tmphash % l->numels;
}
}