-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathlist.c
1415 lines (1150 loc) · 27.3 KB
/
list.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
/****************************************************************************************/
/* List */
/* */
/* Author: Charles Bloom */
/* Description: List/Link/Node Primitives */
/* */
/* The contents of this file are subject to the Genesis3D Public License */
/* Version 1.01 (the "License"); you may not use this file except in */
/* compliance with the License. You may obtain a copy of the License at */
/* http://www.genesis3d.com */
/* */
/* Software distributed under the License is distributed on an "AS IS" */
/* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See */
/* the License for the specific language governing rights and limitations */
/* under the License. */
/* */
/* The Original Code is Genesis3D, released March 25, 1999. */
/*Genesis3D Version 1.1 released November 15, 1999 */
/* Copyright (C) 1999 WildTangent, Inc. All Rights Reserved */
/* */
/****************************************************************************************/
//#define SAFE_HASH_DEBUG
/*****
List_Ram can still be as high as 30% of the time!
*******/
// #define DO_TIMER
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include "list.h"
#include "mempool.h"
#include "ram.h"
#include "crc32.h"
/**********************************/
// Timer Stuff
#ifdef DO_TIMER
#include "timer.h"
TIMER_VARS(List_Ram);
TIMER_VARS(List_RadixWalk);
TIMER_VARS(List_RadixInit);
#else
#define TIMER_P(x)
#define TIMER_Q(X)
#endif // DO_TIMER
#ifdef _DEBUG
#define Debug(func) func;
#define DebugAlert() do { __asm { int 03h } } while(0)
#else
#define Debug(func)
#define DebugAlert()
#endif
#define MemAlloc(size) geRam_AllocateClear(size)
#define MemFree(mem) geRam_Free(mem)
#ifdef DO_TIMER // {
#undef new
#define new(type) mymalloc(sizeof(type))
#undef destroy
#define destroy(mem) if ( mem ) myfree(mem)
void * mymalloc(int size)
{
void *ret;
TIMER_P(List_Ram);
ret = MemAlloc(size);
TIMER_Q(List_Ram);
return ret;
}
void myfree(void *mem)
{
TIMER_P(List_Ram);
MemFree(mem);
TIMER_Q(List_Ram);
}
#undef MemAlloc
#define MemAlloc mymalloc
#undef MemFree
#define MemFree myfree
#else // }{ DO_TIMER
#undef new
#define new(type) MemAlloc(sizeof(type))
#undef destroy
#define destroy(mem) if ( mem ) MemFree(mem)
#endif // } DO_TIMER
/**********************************/
static MemPool *ListPool_g = NULL, *LinkPool_g = NULL, *HashNodePool_g = NULL;
static int UsageCount = 0;
/**********************************/
int LN_ListLen(LinkNode *pList)
{
LinkNode *pNode;
int Len=0;
if ( ! pList )
return 0;
LN_Walk(pNode,pList) {
Len++;
}
return Len;
}
LinkNode * LISTCALL LN_CutHead(LinkNode *pList)
{
LinkNode * LN;
assert(pList);
LN = pList->Next;
if ( LN == pList ) return NULL;
LN_Cut(LN);
return LN;
}
LinkNode * LISTCALL LN_CutTail(LinkNode *pList)
{
LinkNode * LN;
assert(pList);
LN = pList->Prev;
if ( LN == pList ) return NULL;
LN_Cut(LN);
return LN;
}
/**********************************/
struct List
{
List *Next,*Prev;
void * Data;
};
void LISTCALL List_Destroy(List * pList)
{
List * pNode;
assert(pList);
pNode = pList->Next;
while( pNode != pList )
{
List *Next;
Next = pNode->Next;
MemPool_FreeHunk(ListPool_g,pNode);
pNode = Next;
}
MemPool_FreeHunk(ListPool_g,pList);
}
List * LISTCALL List_Create(void)
{
List * pNew;
pNew = MemPool_GetHunk(ListPool_g);
assert(pNew);
pNew->Data = NULL;
pNew->Next = pNew->Prev = pNew;
return pNew;
}
List * LISTCALL List_AddTail(List *pList,void * Data)
{
List * pNew;
pNew = MemPool_GetHunk(ListPool_g);
assert(pNew);
pNew->Next = pList;
pNew->Prev = pList->Prev;
pNew->Next->Prev = pNew;
pNew->Prev->Next = pNew;
pNew->Data= Data;
return pNew;
}
List * LISTCALL List_AddHead(List *pList,void * Data)
{
List * pNew;
pNew = MemPool_GetHunk(ListPool_g);
assert(pNew);
pNew->Data= Data;
pNew->Prev = pList;
pNew->Next = pList->Next;
pNew->Next->Prev = pNew;
pNew->Prev->Next = pNew;
return pNew;
}
void * LISTCALL List_CutHead(List *pList)
{
List * pNode;
void * pData;
pNode = pList->Next;
if ( pNode == pList ) return NULL;
pData = pNode->Data;
pList->Next = pNode->Next;
pList->Next->Prev = pList;
MemPool_FreeHunk(ListPool_g,pNode);
return pData;
}
void * LISTCALL List_CutTail(List *pList)
{
List * pNode;
void * pData;
pNode = pList->Prev;
if ( pNode == pList ) return NULL;
pData = pNode->Data;
pList->Prev = pNode->Prev;
pList->Prev->Next = pList;
MemPool_FreeHunk(ListPool_g,pNode);
return pData;
}
void * LISTCALL List_PeekHead(List *pList)
{
List * pNode;
pNode = pList->Next;
if ( pNode == pList ) return NULL;
return pNode->Data;
}
void * LISTCALL List_PeekTail(List *pList)
{
List * pNode;
pNode = pList->Prev;
if ( pNode == pList ) return NULL;
return pNode->Data;
}
void LISTCALL List_CutNode(List *pNode)
{
pNode->Prev->Next = pNode->Next;
pNode->Next->Prev = pNode->Prev;
pNode->Next = pNode->Prev = pNode;
}
void LISTCALL List_DeleteNode(List *pNode)
{
List_CutNode(pNode);
List_FreeNode(pNode);
}
void LISTCALL List_FreeNode(List *pNode)
{
assert(pNode);
MemPool_FreeHunk(ListPool_g,pNode);
}
void * LISTCALL List_NodeData(List *pNode)
{
assert(pNode);
return pNode->Data;
}
List * LISTCALL List_Next(List *pNode)
{
return pNode->Next;
}
List * LISTCALL List_Prev(List *pNode)
{
return pNode->Prev;
}
List * List_Find(List *pList,void *Data)
{
List *pNode;
assert(pList);
for(pNode = pList->Next; pNode != pList; pNode = pNode->Next )
{
if ( pNode->Data == Data )
return pNode;
}
return NULL;
}
/***********************************************************/
// Stack by linked list
struct Link
{
Link * Next;
void * Data;
};
void LISTCALL Link_Destroy(Link * pLink)
{
while( pLink )
{
Link *Next;
Next = pLink->Next;
MemPool_FreeHunk(LinkPool_g,pLink);
pLink = Next;
}
}
Link * LISTCALL Link_Create(void)
{
Link * pLink;
pLink = MemPool_GetHunk(LinkPool_g);
assert(pLink);
pLink->Next = NULL;
pLink->Data = NULL;
return pLink;
}
void LISTCALL Link_Push(Link *pLink,void * Data)
{
Link * pNode;
assert(pLink);
pNode = MemPool_GetHunk(LinkPool_g);
assert(pNode);
// DebugWarn(geRam_IsValidPtr(pLink));
// DebugWarn(geRam_IsValidPtr(pNode));
pNode->Data = Data;
pNode->Next = pLink->Next;
pLink->Next = pNode;
}
void * LISTCALL Link_Pop(Link *pLink)
{
void *pData;
Link * pNode;
if ( ! pLink ) return NULL;
// DebugWarn(geRam_IsValidPtr(pLink));
pNode = pLink->Next;
if ( ! pNode ) return NULL;
// DebugWarn(geRam_IsValidPtr(pNode));
pData = pNode->Data;
pLink->Next = pNode->Next;
MemPool_FreeHunk(LinkPool_g,pNode);
return pData;
}
void * LISTCALL Link_Peek(Link *pLink)
{
void *pData;
Link * pNode;
if ( ! pLink ) return NULL;
// DebugWarn(geRam_IsValidPtr(pLink));
pNode = pLink->Next;
if ( ! pNode ) return NULL;
// DebugWarn(geRam_IsValidPtr(pNode));
pData = pNode->Data;
return pData;
}
/*************************************************************/
#ifdef _DEBUG // else it's in list.h
struct Stack
{
void ** Buffer, **End;
void ** Head;
int members;
};
#endif
void LISTCALL Stack_Destroy(Stack * pStack)
{
if ( pStack )
{
destroy(pStack->Buffer);
destroy(pStack);
}
}
Stack * LISTCALL Stack_Create(void)
{
Stack * pStack;
pStack = new(Stack);
assert(pStack);
pStack->members = 4096;
pStack->Buffer = MemAlloc(sizeof(void *)*(pStack->members));
assert(pStack->Buffer);
pStack->End = pStack->Buffer + (pStack->members);
pStack->Head = pStack->Buffer;
return pStack;
}
int LISTCALL Stack_Extend(Stack *pStack)
{
void ** NewBuffer;
int newmembers;
// realloc
newmembers = pStack->members + 4096;
NewBuffer = MemAlloc(sizeof(void *)*newmembers);
assert(NewBuffer);
memcpy(NewBuffer,pStack->Buffer, sizeof(void *)*(pStack->members));
pStack->Head = NewBuffer + pStack->members;
pStack->members = newmembers;
MemFree(pStack->Buffer);
pStack->Buffer = NewBuffer;
pStack->End = pStack->Buffer + (pStack->members);
return newmembers;
}
void LISTCALL Stack_Push_Func(Stack *pStack,void * Data)
{
assert(pStack);
*(pStack->Head)++ = Data;
if ( pStack->Head == pStack->End )
Stack_Extend(pStack);
}
void * LISTCALL Stack_Pop_Func(Stack *pStack)
{
assert(pStack);
if ( pStack->Head == pStack->Buffer )
return NULL;
pStack->Head --;
return *(pStack->Head);
}
/*************************************************************/
struct RadixList
{
int NumLists;
int Min,Max;
List ** Lists;
};
RadixList * RadixList_Create(int RadixListMax)
{
RadixList * pRadixList;
int r;
pRadixList = new(RadixList);
assert(pRadixList);
pRadixList->NumLists = RadixListMax+1;
pRadixList->Lists = MemAlloc(sizeof(List *)*(pRadixList->NumLists));
assert(pRadixList->Lists);
for(r=0;r<(pRadixList->NumLists);r++)
{
pRadixList->Lists[r] = List_Create();
assert(pRadixList->Lists[r]);
}
pRadixList->Min = pRadixList->NumLists;
pRadixList->Max = - 1;
return pRadixList;
}
void RadixList_Destroy(RadixList * pRadixList)
{
int r;
assert(pRadixList);
for(r=0;r<(pRadixList->NumLists);r++)
{
List_Destroy(pRadixList->Lists[r]);
}
MemFree(pRadixList->Lists);
MemFree(pRadixList);
}
List * RadixList_Add(RadixList *pRadixList,void * Data,int Key)
{
List * l;
assert(pRadixList);
assert( Key < pRadixList->NumLists && Key >= 0 );
l = List_AddTail(pRadixList->Lists[Key],Data);
if ( Key < pRadixList->Min ) pRadixList->Min = Key;
if ( Key > pRadixList->Max ) pRadixList->Max = Key;
return l;
}
void * RadixList_CutMax(RadixList *pRadixList,int *pKey)
{
assert(pRadixList);
while ( pRadixList->Max >= 0 )
{
void * Data;
if ( Data = List_CutHead(pRadixList->Lists[pRadixList->Max]) )
{
if ( pKey ) *pKey = pRadixList->Max;
return Data;
}
pRadixList->Max --;
}
return NULL;
}
void * RadixList_CutMin(RadixList *pRadixList,int *pKey)
{
assert(pRadixList);
while ( pRadixList->Min < pRadixList->NumLists )
{
void * Data;
if ( Data = List_CutHead(pRadixList->Lists[pRadixList->Min]) )
{
if ( pKey ) *pKey = pRadixList->Min;
return Data;
}
pRadixList->Min ++;
}
return NULL;
}
void * RadixList_CutKey(RadixList *pRadixList,int Key)
{
assert(pRadixList);
return List_CutHead(pRadixList->Lists[Key]);
}
/*************************************************/
struct RadixLN
{
int NumLNs;
int Min,Max;
LinkNode * LNs;
};
RadixLN * RadixLN_Create(int RadixLNMax)
{
RadixLN * pRadixLN;
LinkNode * LNs;
int r;
pRadixLN = new(RadixLN);
assert(pRadixLN);
pRadixLN->NumLNs = RadixLNMax+1;
pRadixLN->LNs = MemAlloc(sizeof(LinkNode)*(pRadixLN->NumLNs));
assert(pRadixLN->LNs);
TIMER_P(List_RadixInit); // not counting the allocs, tracking in List_Ram
LNs = pRadixLN->LNs;
for(r=(pRadixLN->NumLNs);r--;LNs++)
{
// LN_InitList( LNs );
LNs->Next = LNs->Prev = LNs;
}
pRadixLN->Min = RadixLNMax;
pRadixLN->Max = 0;
TIMER_Q(List_RadixInit);
return pRadixLN;
}
void RadixLN_Destroy(RadixLN * pRadixLN)
{
assert(pRadixLN);
MemFree(pRadixLN->LNs);
MemFree(pRadixLN);
}
void RadixLN_AddHead(RadixLN *pRadixLN,LinkNode *LN,int Key)
{
assert(pRadixLN);
assert( Key < pRadixLN->NumLNs && Key >= 0 );
LN_AddHead(&(pRadixLN->LNs[Key]),LN);
if ( Key < pRadixLN->Min ) pRadixLN->Min = Key;
if ( Key > pRadixLN->Max ) pRadixLN->Max = Key;
}
void RadixLN_AddTail(RadixLN *pRadixLN,LinkNode *LN,int Key)
{
assert(pRadixLN);
assert( Key < pRadixLN->NumLNs && Key >= 0 );
LN_AddTail(&(pRadixLN->LNs[Key]),LN);
if ( Key < pRadixLN->Min ) pRadixLN->Min = Key;
if ( Key > pRadixLN->Max ) pRadixLN->Max = Key;
}
LinkNode * RadixLN_CutMax(RadixLN *pRadixLN,int *pKey)
{
assert(pRadixLN);
TIMER_P(List_RadixWalk);
while ( pRadixLN->Max >= 0 )
{
LinkNode * LN;
if ( LN = LN_CutHead(&(pRadixLN->LNs[pRadixLN->Max])) )
{
if ( pKey ) *pKey = pRadixLN->Max;
TIMER_Q(List_RadixWalk);
return LN;
}
pRadixLN->Max --;
}
TIMER_Q(List_RadixWalk);
return NULL;
}
LinkNode * RadixLN_CutMin(RadixLN *pRadixLN,int *pKey)
{
assert(pRadixLN);
TIMER_P(List_RadixWalk);
while ( pRadixLN->Min < pRadixLN->NumLNs )
{
LinkNode * LN;
if ( LN = LN_CutHead(&(pRadixLN->LNs[pRadixLN->Min])) )
{
if ( pKey ) *pKey = pRadixLN->Max;
TIMER_Q(List_RadixWalk);
return LN;
}
pRadixLN->Min ++;
}
TIMER_Q(List_RadixWalk);
return NULL;
}
LinkNode * RadixLN_CutKey(RadixLN *pRadixLN,int Key)
{
assert(pRadixLN);
return LN_CutHead(&(pRadixLN->LNs[Key]));
}
LinkNode * RadixLN_PeekMax(RadixLN *pRadixLN,int *pKey)
{
LinkNode * LNs;
assert(pRadixLN);
TIMER_P(List_RadixWalk);
LNs = & pRadixLN->LNs[pRadixLN->Max];
while ( pRadixLN->Max >= 0 )
{
LinkNode * LN;
LN = LNs->Next;
if ( LN != LNs )
{
if ( pKey ) *pKey = pRadixLN->Max;
TIMER_Q(List_RadixWalk);
return LN;
}
pRadixLN->Max --;
LNs --;
}
TIMER_Q(List_RadixWalk);
return NULL;
}
LinkNode * RadixLN_PeekMin(RadixLN *pRadixLN,int *pKey)
{
LinkNode * LNlist;
assert(pRadixLN);
TIMER_P(List_RadixWalk);
LNlist = & pRadixLN->LNs[pRadixLN->Min];
while ( pRadixLN->Min < pRadixLN->NumLNs )
{
LinkNode * LN;
LN = LNlist->Next;
if ( LN != LNlist )
{
if ( pKey ) *pKey = pRadixLN->Min;
TIMER_Q(List_RadixWalk);
return LN;
}
pRadixLN->Min ++;
LNlist ++;
}
TIMER_Q(List_RadixWalk);
return NULL;
}
/*************************************************/
struct RadixLink
{
int NumLinks;
int Min,Max;
Link * Links;
};
RadixLink * RadixLink_Create(int RadixLinkMax)
{
RadixLink * pRadixLink;
pRadixLink = new(RadixLink);
assert(pRadixLink);
pRadixLink->NumLinks = RadixLinkMax+1;
pRadixLink->Links = MemAlloc(sizeof(Link)*(pRadixLink->NumLinks));
assert(pRadixLink->Links);
//memset(pRadixLink->Links,0,(sizeof(Link)*(pRadixLink->NumLinks)));
pRadixLink->Min = pRadixLink->NumLinks;
pRadixLink->Max = - 1;
return pRadixLink;
}
void RadixLink_Grow(RadixLink *pRadixLink,int NewMax)
{
Link * OldLinks;
int OldNumLinks;
OldNumLinks = pRadixLink->NumLinks;
OldLinks = pRadixLink->Links;
pRadixLink->NumLinks = NewMax + 1;
pRadixLink->Links = MemAlloc(sizeof(Link)*(pRadixLink->NumLinks));
assert(pRadixLink->Links);
memcpy(pRadixLink->Links, OldLinks, (sizeof(Link)*OldNumLinks));
//memset(pRadixLink->Links + OldNumLinks,0,(sizeof(Link)*(pRadixLink->NumLinks - OldNumLinks)));
MemFree(OldLinks);
}
void RadixLink_Destroy(RadixLink * pRadixLink)
{
int r;
assert(pRadixLink);
for(r=0;r<(pRadixLink->NumLinks);r++)
{
if ( pRadixLink->Links[r].Next )
Link_Destroy(pRadixLink->Links[r].Next);
}
MemFree(pRadixLink->Links);
MemFree(pRadixLink);
}
void RadixLink_Add(RadixLink *pRadixLink,void * Data,int Key)
{
assert(pRadixLink);
assert( Key >= 0 );
if ( Key >= pRadixLink->NumLinks )
RadixLink_Grow(pRadixLink, Key + (Key>>2) );
Link_Push(&(pRadixLink->Links[Key]),Data);
if ( Key < pRadixLink->Min ) pRadixLink->Min = Key;
if ( Key > pRadixLink->Max ) pRadixLink->Max = Key;
}
void * RadixLink_CutMax(RadixLink *pRadixLink,int *pKey)
{
Link * pLink;
pLink = (pRadixLink->Links) + (pRadixLink->Max);
while ( pRadixLink->Max >= 0 )
{
if ( pLink->Next )
{
if ( pKey ) *pKey = pRadixLink->Max;
return Link_Pop(pLink);
}
pRadixLink->Max --;
pLink --;
}
return NULL;
}
void * RadixLink_CutMin(RadixLink *pRadixLink,int *pKey)
{
Link * pLink;
pLink = (pRadixLink->Links) + (pRadixLink->Min);
while ( pRadixLink->Min < pRadixLink->NumLinks )
{
if ( pLink->Next )
{
if ( pKey ) *pKey = pRadixLink->Min;
return Link_Pop(pLink);
}
pRadixLink->Min ++;
pLink ++;
}
return NULL;
}
void * RadixLink_CutKey(RadixLink *pRadixLink,int Key)
{
assert(pRadixLink);
return Link_Pop(&(pRadixLink->Links[Key]));
}
/***** debug only : **********************/
void List_TimerReport(void)
{
#ifdef DO_TIMER
TIMER_REPORT(List_Ram);
TIMER_REPORT(List_RadixWalk);
TIMER_REPORT(List_RadixInit);
#endif
}
/*************************************************/
/**************************************************
Hash has separate Keys & Data
We use a single Circular list of all the nodes. We have
cappers of Hash = 0 and HASH_SIZE at the head and tail.
So a general hash looks like : (with HASH_SIZE == 7)
(Head)-->A--x->C--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
the brackets are hash buckets; little x's mean that hash bucket
has seen no nodes of its own hash, so it points to the next
larger hash's list.
to do a Get :
simply look at your current pointer and walk while hash == my hash
example : Get(Hash == 3)
we get node C : ok, test it
next is node E : not my hash, so I'm done
to do a delete :
simply cut your node, and point yourself to the next node.
example : Delete( Node C )
(Head)-->A--x--x--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
now hash bucket 3 points to node E
to do an add :
first you must take your current pointer and walk backwards until
the preceding node has a lower hash than you (see example later).
then simply add the new node before the current one and make yourself
point to the new one.
Example 1: Add an A:
bucket 1 already is in the right place
add before the old node :
+-A1
| |
(Head)-+ A2-x--x--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
now make the hash point to the new add:
A2-+
| |
(Head)---A1 x--x--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
Example 2 : we need the extra seek in each _Add()
add a node C at hash 3
A--+--+
| |
(Head)---A (E) C--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
the funny notation is to indicate that bucket 2 still
points to node E.
now add a node B at hash 2
we currently point to node (E)
the previous node is (C), which is greater than B, so
we step back; now the current is (C) and the previous is (A) :
A--+
| |
(Head)---A +->C--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
which is a good list, so we do the normal add:
A--+
| |
(Head)---A B->C--x->E--x-->(Tail)
[0] [1][2][3][4][5][6]
**************
the whole goal of this system is that the WalkNext() function is
blazing fast. The only thing that hurts in the _Add function, but
the cost of adding N nodes to hash of size M is only O(N + M*M)
(because the first M adds are O(M) and the later adds are O(1))
*****************************/
#ifdef SAFE_HASH_DEBUG //{
#pragma message("using safe debug hash implementation")
struct Hash
{
HashNode ** NodeArray;
int NodeArrayLen;
int NodeCount;
};
struct HashNode
{
uint32 Key;
uint32 Data;
};
Hash * Hash_Create(void)
{
Hash * H;
H = new(Hash);
assert(H);
//memset(H,0,sizeof(*H));
return H;
}
void Hash_Destroy(Hash *H)
{
assert(H);
if ( H->NodeArray )
{
int n;
for(n=0;n<H->NodeCount;n++)
{
MemPool_FreeHunk(HashNodePool_g,H->NodeArray[n]);
}
destroy(H->NodeArray);
}
destroy(H);
}
HashNode * LISTCALL Hash_Add(Hash *H,uint32 Key,uint32 Data)
{
HashNode * hn;
hn = MemPool_GetHunk(HashNodePool_g);
assert(hn);
hn->Key = Key;
hn->Data = Data;
if ( H->NodeCount == H->NodeArrayLen )
{
H->NodeArrayLen = H->NodeCount + 100;
if ( H->NodeArray )
{
H->NodeArray = geRam_Realloc(H->NodeArray,H->NodeArrayLen*sizeof(HashNode *));
}
else
{
H->NodeArray = geRam_Allocate(H->NodeArrayLen*sizeof(HashNode *));
}
assert(H->NodeArray);
}
H->NodeArray[H->NodeCount] = hn;
H->NodeCount ++;
return hn;
}
void LISTCALL Hash_DeleteNode(Hash *pHash,HashNode *pNode)
{
int n;
n = 0;
while( pHash->NodeArray[n] != pNode )
{
n++;
assert( n < pHash->NodeCount );