-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathkeylist.c
More file actions
743 lines (621 loc) · 19 KB
/
keylist.c
File metadata and controls
743 lines (621 loc) · 19 KB
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
/*####COPYRIGHTBEGIN####
-------------------------------------------
Copyright (C) 2003 Steve Karg
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
As a special exception, if other files instantiate templates or
use macros or inline functions from this file, or you compile
this file and link it with other works to produce a work based
on this file, this file does not by itself cause the resulting
work to be covered by the GNU General Public License. However
the source code for this file must still be made available in
accordance with section (3) of the GNU General Public License.
This exception does not invalidate any other reasons why a work
based on this file might be covered by the GNU General Public
License.
-------------------------------------------
####COPYRIGHTEND####*/
// Keyed Linked List Library
//
// This is an enhanced array of pointers to data.
// The list is sorted, indexed, and keyed.
// The array is much faster than a linked list.
// It stores a pointer to data, which you must
// malloc and free on your own, or just use
// static data
#include <stdlib.h>
#include "keylist.h" // check for valid prototypes
#ifndef FALSE
#define FALSE 0
#endif
#ifndef TRUE
#define TRUE 1
#endif
/////////////////////////////////////////////////////////////////////
// Generic node routines
/////////////////////////////////////////////////////////////////////
// grab memory for a node
static struct Keylist_Node *NodeCreate(void)
{
return calloc(1, sizeof(struct Keylist_Node));
}
// grab memory for a list
static struct Keylist *KeylistCreate(void)
{
return calloc(1, sizeof(struct Keylist));
}
// check to see if the array is big enough for an addition
// or is too big when we are deleting and we can shrink
// returns TRUE if success, FALSE if failed
static int CheckArraySize(OS_Keylist list)
{
int new_size = 0; // set it up so that no size change is the default
const int chunk = 8; // minimum number of nodes to allocate memory for
struct Keylist_Node **new_array; // new array of nodes, if needed
int i; // counter
if (!list)
return FALSE;
// indicates the need for more memory allocation
if (list->count == list->size)
new_size = list->size + chunk;
// allow for shrinking memory
else if ((list->size > chunk) && (list->count < (list->size - chunk)))
new_size = list->size - chunk;
if (new_size > 0) {
// Allocate more room for node pointer array
new_array = calloc((size_t)new_size, sizeof(struct Keylist_Node *));
// See if we got the memory we wanted
if (!new_array)
return FALSE;
// copy the nodes from the old array to the new array
if (list->array) {
for (i = 0; i < list->count; i++) {
new_array[i] = list->array[i];
}
free(list->array);
}
list->array = new_array;
list->size = new_size;
} else if (new_size < 0) {
return FALSE;
}
return TRUE;
}
// find the index of the key that we are looking for
// since it is sorted, we can optimize the search
// returns TRUE if found, and FALSE not found
// returns the found key and the index where it was found in parameters
// If the key is not found, the nearest index from the bottom will be returned,
// allowing the ability to find where an key should go into the list.
static int FindIndex(OS_Keylist list, KEY key, int *pIndex)
{
struct Keylist_Node *node; // holds the new node
int left = 0; // the left branch of tree, beginning of list
int right = 0; // the right branch on the tree, end of list
int index = 0; // our current search place in the array
KEY current_key = 0; // place holder for current node key
int status = FALSE; // return value
if (!list || !list->array || !list->count) {
*pIndex = 0;
return (FALSE);
}
right = list->count - 1;
// assume that the list is sorted
do {
// A binary search
index = (left + right) / 2;
node = list->array[index];
if (!node)
break;
current_key = node->key;
if (key < current_key)
right = index - 1;
else
left = index + 1;
} while ((key != current_key) && (left <= right));
if (key == current_key) {
status = TRUE;
*pIndex = index;
}
else {
// where the index should be
if (key > current_key)
*pIndex = index + 1;
else
*pIndex = index;
}
return (status);
}
/////////////////////////////////////////////////////////////////////
// list data functions
/////////////////////////////////////////////////////////////////////
// inserts a node into its sorted position
int Keylist_Data_Add(OS_Keylist list, KEY key, void *data)
{
struct Keylist_Node *node; // holds the new node
int index = -1; // return value
int i; // counts through the array
if (list && CheckArraySize(list)) {
// figure out where to put the new node
if (list->count) {
(void)FindIndex(list, key, &index);
// Add to the beginning of the list
if (index < 0)
index = 0;
// Add to the end of the list
else if (index > list->count)
index = list->count;
// Move all the items up to make room for the new one
for (i = list->count; i > index; i--) {
list->array[i] = list->array[i - 1];
}
}
else {
index = 0;
}
// create and add the node
node = NodeCreate();
if (node) {
list->count++;
node->key = key;
node->data = data;
list->array[index] = node;
}
}
return index;
}
// deletes a node specified by its index
// returns the data from the node
void *Keylist_Data_Delete_By_Index(OS_Keylist list, int index)
{
struct Keylist_Node *node;
void *data = NULL;
if (list && list->array && list->count && (index >= 0) &&
(index < list->count)) {
node = list->array[index];
if (node)
data = node->data;
// move the nodes to account for the deleted one
if (list->count == 1) {
// There is no node shifting to do
}
// We are the last one
else if (index == (list->count - 1)) {
// There is no node shifting to do
}
// Move all the nodes down one
else {
int i; // counter
int count = list->count - 1;
for (i = index; i < count; i++) {
list->array[i] = list->array[i + 1];
}
}
list->count--;
if (node)
free(node);
// potentially reduce the size of the array
(void)CheckArraySize(list);
}
return (data);
}
// deletes a node specified by its key
// returns the data from the node
void *Keylist_Data_Delete(OS_Keylist list, KEY key)
{
void *data = NULL; // return value
int index; // where the node is in the array
if (list) {
if (FindIndex(list, key, &index))
data = Keylist_Data_Delete_By_Index(list, index);
}
return data;
}
// returns the data from last node, and removes it from the list
void *Keylist_Data_Pop(OS_Keylist list)
{
void *data = NULL; // return value
int index; // position in the array
if (list && list->count) {
index = list->count - 1;
data = Keylist_Data_Delete_By_Index(list, index);
}
return data;
}
// returns the data from the node specified by key
void *Keylist_Data(OS_Keylist list, KEY key)
{
struct Keylist_Node *node = NULL;
int index = 0; // used to look up the index of node
if (list && list->array && list->count) {
if (FindIndex(list, key, &index))
node = list->array[index];
}
return node ? node->data : NULL;
}
// returns the data specified by key
void *Keylist_Data_Index(OS_Keylist list, int index)
{
struct Keylist_Node *node = NULL;
if (list && list->array && list->count && (index >= 0) &&
(index < list->count))
node = list->array[index];
return node ? node->data : NULL;
}
// return the key at the given index
KEY Keylist_Key(OS_Keylist list, int index)
{
KEY key = 0; // return value
struct Keylist_Node *node;
if (list && list->array && list->count && (index >= 0) &&
(index < list->count)) {
node = list->array[index];
if (node)
key = node->key;
}
return key;
}
// returns the next empty key from the list
KEY Keylist_Next_Empty_Key(OS_Keylist list, KEY key)
{
int index;
if (list) {
while (FindIndex(list, key, &index)) {
if (KEY_LAST(key))
break;
key++;
}
}
return key;
}
// return the number of nodes in this list
int Keylist_Count(OS_Keylist list)
{
return list->count;
}
/////////////////////////////////////////////////////////////////////
// Public List functions
/////////////////////////////////////////////////////////////////////
// returns head of the list or NULL on failure.
OS_Keylist Keylist_Create(void)
{
struct Keylist *list;
list = KeylistCreate();
if (list)
CheckArraySize(list);
return list;
}
// delete specified list
void Keylist_Delete(OS_Keylist list) // list number to be deleted
{
if (list) {
// clean out the list
while (list->count) {
(void)Keylist_Data_Delete_By_Index(list, 0);
}
if (list->array)
free(list->array);
free(list);
}
return;
}
#ifdef TEST
#include <assert.h>
#include <string.h>
#include "ctest.h"
// test the encode and decode macros
void testKeySample(Test *pTest)
{
int type, id;
int type_list[] = {
0, 1, KEY_TYPE_MAX / 2, KEY_TYPE_MAX - 2, KEY_TYPE_MAX - 1, -1};
int id_list[] = {0, 1, KEY_ID_MAX / 2, KEY_ID_MAX - 2, KEY_ID_MAX - 1, -1};
int type_index = 0;
int id_index = 0;
int decoded_type, decoded_id;
KEY key;
while (type_list[type_index] != -1) {
while (id_list[id_index] != -1) {
type = type_list[type_index];
id = id_list[id_index];
key = KEY_ENCODE(type, id);
decoded_type = KEY_DECODE_TYPE(key);
decoded_id = KEY_DECODE_ID(key);
ct_test(pTest, decoded_type == type);
ct_test(pTest, decoded_id == id);
id_index++;
}
id_index = 0;
type_index++;
}
return;
}
// test the FIFO
void testKeyListFIFO(Test *pTest)
{
OS_Keylist list;
KEY key;
int index;
char *data1 = "Joshua";
char *data2 = "Anna";
char *data3 = "Mary";
char *data;
list = Keylist_Create();
ct_test(pTest, list != NULL);
key = 0;
index = Keylist_Data_Add(list, key, data1);
ct_test(pTest, index == 0);
index = Keylist_Data_Add(list, key, data2);
ct_test(pTest, index == 0);
index = Keylist_Data_Add(list, key, data3);
ct_test(pTest, index == 0);
ct_test(pTest, Keylist_Count(list) == 3);
data = Keylist_Data_Pop(list);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
data = Keylist_Data_Pop(list);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data2) == 0);
}
data = Keylist_Data_Pop(list);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data3) == 0);
}
data = Keylist_Data_Pop(list);
ct_test(pTest, data == NULL);
data = Keylist_Data_Pop(list);
ct_test(pTest, data == NULL);
Keylist_Delete(list);
return;
}
// test the FILO
void testKeyListFILO(Test *pTest)
{
OS_Keylist list;
KEY key;
int index;
char *data1 = "Joshua";
char *data2 = "Anna";
char *data3 = "Mary";
char *data;
list = Keylist_Create();
ct_test(pTest, list != NULL);
key = 0;
index = Keylist_Data_Add(list, key, data1);
ct_test(pTest, index == 0);
index = Keylist_Data_Add(list, key, data2);
ct_test(pTest, index == 0);
index = Keylist_Data_Add(list, key, data3);
ct_test(pTest, index == 0);
ct_test(pTest, Keylist_Count(list) == 3);
data = Keylist_Data_Delete_By_Index(list, 0);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data3) == 0);
}
data = Keylist_Data_Delete_By_Index(list, 0);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data2) == 0);
}
data = Keylist_Data_Delete_By_Index(list, 0);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
data = Keylist_Data_Delete_By_Index(list, 0);
ct_test(pTest, data == NULL);
data = Keylist_Data_Delete_By_Index(list, 0);
ct_test(pTest, data == NULL);
Keylist_Delete(list);
return;
}
void testKeyListDataKey(Test *pTest)
{
OS_Keylist list;
KEY key;
KEY test_key;
int index;
char *data1 = "Joshua";
char *data2 = "Anna";
char *data3 = "Mary";
char *data;
list = Keylist_Create();
ct_test(pTest, list != NULL);
key = 1;
index = Keylist_Data_Add(list, key, data1);
ct_test(pTest, index == 0);
test_key = Keylist_Key(list, index);
ct_test(pTest, test_key == key);
key = 2;
index = Keylist_Data_Add(list, key, data2);
ct_test(pTest, index == 1);
test_key = Keylist_Key(list, index);
ct_test(pTest, test_key == key);
key = 3;
index = Keylist_Data_Add(list, key, data3);
ct_test(pTest, index == 2);
test_key = Keylist_Key(list, index);
ct_test(pTest, test_key == key);
ct_test(pTest, Keylist_Count(list) == 3);
// look at the data
key = 2;
data = Keylist_Data(list, key);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data2) == 0);
}
key = 1;
data = Keylist_Data(list, key);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
key = 3;
data = Keylist_Data(list, key);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data3) == 0);
}
// work the data
key = 2;
data = Keylist_Data_Delete(list, key);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data2) == 0);
}
data = Keylist_Data_Delete(list, key);
ct_test(pTest, data == NULL);
ct_test(pTest, Keylist_Count(list) == 2);
key = 1;
data = Keylist_Data(list, key);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
key = 3;
data = Keylist_Data(list, key);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data3) == 0);
}
// cleanup
do {
data = Keylist_Data_Pop(list);
} while (data);
Keylist_Delete(list);
return;
}
void testKeyListDataIndex(Test *pTest)
{
OS_Keylist list;
KEY key;
int index;
char *data1 = "Joshua";
char *data2 = "Anna";
char *data3 = "Mary";
char *data;
list = Keylist_Create();
ct_test(pTest, list != NULL);
key = 0;
index = Keylist_Data_Add(list, key, data1);
ct_test(pTest, index == 0);
index = Keylist_Data_Add(list, key, data2);
ct_test(pTest, index == 0);
index = Keylist_Data_Add(list, key, data3);
ct_test(pTest, index == 0);
ct_test(pTest, Keylist_Count(list) == 3);
// look at the data
data = Keylist_Data_Index(list, 0);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data3) == 0);
}
data = Keylist_Data_Index(list, 1);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data2) == 0);
}
data = Keylist_Data_Index(list, 2);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
// work the data
data = Keylist_Data_Delete_By_Index(list, 1);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data2) == 0);
}
ct_test(pTest, Keylist_Count(list) == 2);
data = Keylist_Data_Index(list, 0);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data3) == 0);
}
data = Keylist_Data_Index(list, 1);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
data = Keylist_Data_Delete_By_Index(list, 1);
ct_test(pTest, data != NULL);
if (data != NULL) {
ct_test(pTest, strcmp(data, data1) == 0);
}
data = Keylist_Data_Delete_By_Index(list, 1);
ct_test(pTest, data == NULL);
// cleanup
do {
data = Keylist_Data_Pop(list);
} while (data);
Keylist_Delete(list);
return;
}
// test access of a lot of entries
void testKeyListLarge(Test *pTest)
{
int data1 = 42;
int *data;
OS_Keylist list;
KEY key;
int index;
const unsigned num_keys = 1024 * 16;
list = Keylist_Create();
if (!list)
return;
for (key = 0; key < num_keys; key++) {
index = Keylist_Data_Add(list, key, &data1);
ct_test(pTest, index != -1);
}
for (key = 0; key < num_keys; key++) {
data = Keylist_Data(list, key);
ct_test(pTest, *data == data1);
}
for (index = 0; index < num_keys; index++) {
data = Keylist_Data_Index(list, index);
ct_test(pTest, *data == data1);
}
Keylist_Delete(list);
return;
}
#ifdef TEST_KEYLIST
int main(void)
{
Test *pTest;
bool rc;
pTest = ct_create("keylist", NULL);
/* individual tests */
rc = ct_addTestFunction(pTest, testKeyListFIFO);
assert(rc);
rc = ct_addTestFunction(pTest, testKeyListFILO);
assert(rc);
rc = ct_addTestFunction(pTest, testKeyListDataKey);
assert(rc);
rc = ct_addTestFunction(pTest, testKeySample);
assert(rc);
rc = ct_addTestFunction(pTest, testKeyListDataIndex);
assert(rc);
rc = ct_addTestFunction(pTest, testKeyListLarge);
assert(rc);
ct_setStream(pTest, stdout);
ct_run(pTest);
(void)ct_report(pTest);
ct_destroy(pTest);
return 0;
}
#endif /* TEST_KEYLIST */
#endif /* TEST */