-
Notifications
You must be signed in to change notification settings - Fork 72
/
Copy pathclass_LinkedListAndHashTable.ahk
621 lines (562 loc) · 14 KB
/
class_LinkedListAndHashTable.ahk
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
;Simple Dictionary/Map/Hashtable and Linked List implementation by icuurd12b42, free to use, have fun
; Thanks to Brian Kernighan's description on computerphile on yt for the cheap implementation of the idea
; https://www.youtube.com/watch?v=qTZJLJ3Gm6Q
; V1.2
; added linked list replacing the bucket array, you can use the linked list system on it's own
; tweaked the stringed key hash to give power to the character order to prevent collision between small string keys with same characters so that "KEY" != "YEK"
; added growth/re-hashing of the main hash table
;See bottom of file for example use and api tests
;LinkedListItem
;A Linked List Item has data, a next and a Prev item. Linked list items point to the next item in a chain of data
; It's easy to manage insertion and removal and connecting chains of data by setting the next and prev values of the item
class LinkedListItem
{
m_Next :=
m_Prev :=
m_Data :=
;New
__New(data)
{
this.LLItemInit(data) ;call the init func
}
__Delete() ;you must delete your data held by m_Data yourself
{
this.Destroy()
}
;init
LLItemInit(data)
{
this.m_Next :=
this.m_Prev :=
this.m_Data := data
}
Destroy()
{
this.Unlink()
this.m_Next :=
this.m_Prev :=
this.m_Data :=
}
;get/set next prev
GetNext()
{
return this.m_Next
}
GetPrev()
{
return this.m_Prev
}
SetNext(Nxt)
{
this.m_Next := Nxt
}
SetPrev(Prev)
{
this.m_Prev := Prev
}
;get set data
GetData()
{
return this.m_Data
}
SetData(Data)
{
this.m_Data := Data
}
;Unlink this from next and prev and preserve re-attaches the chain with this item gone from it
UnLink()
{
prv := this.GetPrev()
nxt := this.GetNext()
if(prv) ;Fix broken chain...
{
prv.SetNext(nxt)
this.SetPrev(0)
}
if(nxt) ;Fix broken chain...
{
nxt.SetPrev(prv)
this.SetNext(0)
}
}
;Chain/De-chain functions
UnlinkFromPrev() ;breaks the chain in 2, terminating it at prev (making prev the end of a chain) and this becoming the start of the chain
{
prv := this.GetPrev()
if(prv) ;Fix broken chain...
{
prv.SetNext(0)
this.SetPrev(0)
}
}
UnlinkFromNext() ;breaks the chain in 2, terminating it at next (making nxt the start of a chain) and this becoming the end of the chain
{
nxt := this.GetNext()
if(nxt) ;Fix broken chain...
{
nxt.SetPrev(0)
this.SetNext(0)
}
}
LinkAfter(Prev) ; reconnects the chain, it is assumed that Prev is the end of a chain or a link and this is a start of a chain or a link
{
this.SetPrev(Prev)
Prev.SetNext(this)
}
LinkBefore(Nxt) ; reconnects the chain, it is assumed that Nxt is the start of a chain or a link and this is a end of a chain or a link
{
this.SetNext(Nxt)
Nxt.SetPrev(this)
}
;
InsertAfter(Prev) ; Inserts itself in a chain between Prev and it's next link. It is assumed that this item is a single link, no part of a chain
{
old_next := Prev.GetNext()
if(old_next)
{
old_next.SetPrev(this)
this.SetNext(old_next)
}
this.SetPrev(Prev)
Prev.SetNext(this)
}
InsertBefore(Nxt) ; Inserts itself in a chain between Nxt and it's prev link. It is assumed that this item is a single link, no part of a chain
{
old_prev := Nxt.GetPrev()
if(old_prev)
{
old_prev.SetNext(this)
this.SetPrev(old_prev)
}
this.SetNext(Nxt)
Nxt.SetPrev(this)
}
}
;Dictionary item container, will eventually hold the data stored to the dictionary
;Has a Key to compare with, the data to store is provided by the based linked list class.
;Internal Use Only
Class DicItem extends LinkedListItem
{
m_Key :=
__New(Key,Data)
{
this.DicItemInit()
}
__Delete()
{
this.DicItemDelete()
}
DicItemInit()
{
this.LLItemInit()
this.m_Key :=
}
DicItemDelete()
{
this.LLItemDelete()
}
GetKey()
{
return this.m_Key
}
SetKey(Key)
{
this.m_Key := Key
}
}
;DicEntry (bucket) is a linked list to holds DicItems at a key location
; since a key hash may resolve to the same entry in the dictionary
; it is necessary to have each dictionary spot be capable of holding multiple
; entries. if data is well distributed, this array should only have 1 to a few entries
; when set.
; The class has an Linked List (Tail) and a count for the number of items in the linked list
;Internal Use Only
Class DicEntry
{
m_Tail :=
__New()
{
this.m_Tail :=
}
;add new item
AddItem(key, data)
{
item := new DicItem()
item.SetKey(key)
item.SetData(data)
if(this.m_Tail)
{
item.LinkAfter(this.m_Tail)
}
this.m_Tail := item
}
;Find item, used internally
FindItem(Key)
{
item := this.m_Tail
while(item)
{
if(item.GetKey() = Key)
{
return item
}
item := item.GetPrev()
}
return 0
}
;Set Item, used to add or set item to the array, if a deleted entry is found, re-use it
SetItemData(Key,Data)
{
item := this.FindItem(Key)
if(item)
{
item.SetData(Data)
return 0 ;tells the caller an item was changed
}
else
{
this.AddItem(key, data)
return 1 ;tells the caller an item was added
}
}
;get the item data
GetItemData(Key)
{
item := this.FindItem(Key)
if(item)
{
return item.GetData()
}
else
{
return 0
}
}
;delete the item, you have to destroy the data held in the key yourself
DeleteItem(Key)
{
ret := 0
item := this.FindItem(Key)
if(item)
{
if(item = this.m_Tail)
{
this.m_Tail := item.GetPrev()
}
item.Destroy()
ret := 1
}
return ret ;tell caller item was deleted
}
}
;Dictionary
; The main dictionary is basically an array of fixed size
; The simple dictionary is a Key Based Entry system
; the key is converted to a number (if it was a string)
; and ranged, with modulo so that it fits in the index range of the allocated array
; Then the data is added to that array location. A location may hold multiple data if
; there was collision.
; For example, creating a dictionary with an array of 1000 spots... therefore an average of 1 chance to 1000 of collision
; Dic := New Dictionary(1000)
; Dic.SetItem(1,"Hello1") ;1 modulo 1000 is 1, this is store at pos 1 in array
; Dic.SetItem(2,"Hello2") ; spot 2
; Dic.SetItem(1002,"Hello3") ; Spot 2 again, collision, spot 2 hold 2 keys
; Dic.SetItem("Greeting","Hello4") ; "Greeting" is converted to a number prior
;
; Dont be ridiculous and create a super large dictionary; 10000 items should fit in a 1000 size dictionary
; which would, given an even distribution, fill up the array with 10 entries per slot
; a 1 lookup for the slot and 10 lookups for the exact keyed item is way efficient enough
; compared to a single loop through 10000 items
; the system will grow and re-hash the table if the number of items get too crowded creating too large buckets per slot
; Each growth is twice the previous size
Class Dictionary
{
m_Array := ;the array of size specified
m_Size := ;the size of the array, may grow over time
m_Count := ;the number of items in the dictionary
m_GrowthFactor :=
__New(Size:=1024,GrowthFactor:=2)
{
this.m_Size := Size
if(this.m_Size < 1024)
{
this.m_Size := 1024
}
this.m_GrowthFactor := GrowthFactor
if(this.m_GrowthFactor < 1.25)
{
this.m_GrowthFactor := 1.25
}
this.m_Count := 0
sz := this.m_Size
}
;convert the key to a valid number modulo size + 1 to give 1 to array size range
ConvertKey(Key)
{
if Key is number
{
;number are floored, and made positive... then modulo-ed
return mod(abs(floor(Key)),this.m_Size) + 1
}
else
{
;strings are set to use the compound ASCII values of each character
val := 0
loop parse, Key
{
val += Asc(A_LoopField) * A_Index
}
return Mod(val,this.m_Size) + 1
}
}
; count and size. mainly to monitor and growing the array and for debugging
GetCount()
{
return this.m_Count
}
GetSize()
{
return this.m_Size
}
GetArray()
{
return this.m_Array
}
GetGrowthFactor()
{
return this.m_GrowthFactor
}
;Redo the dictionary a new size... or grow (if size not specified),
rehash(newSize:=0)
{
sz := newSize == 0 ? this.m_Size*this.m_GrowthFactor : newSize
tempDic := New Dictionary(sz)
Size := this.m_Size
Loop %Size%
{
bucket := this.m_Array[A_Index]
while(bucket.m_Tail)
{
item := bucket.m_Tail
tempDic.SetItem(item.GetKey(),item.GetData())
bucket.m_Tail := item.GetPrev()
item.Destroy()
}
}
this.m_Size := tempDic.GetSize()
this.m_Count := tempDic.GetCount()
this.m_Array := tempDic.GetArray()
tempDic :=
}
;set or adds an item to the dictionary
SetItem(Key,Data)
{
index := this.ConvertKey(Key)
entry := this.m_Array[index]
if !entry
{
entry := new DicEntry()
this.m_Array[index] := entry
}
this.m_Count := this.m_Count + entry.SetItemData(Key,Data)
if(this.m_Count > this.m_Size * 2)
{
this.rehash()
}
}
;gets an item from the dictionary
GetItem(Key)
{
index := this.ConvertKey(Key)
entry := this.m_Array[index]
return entry.GetItemData(Key)
}
;delete an item from the dictionary
DeleteItem(Key)
{
index := this.ConvertKey(Key)
entry := this.m_Array[index]
this.m_Count := this.m_Count - entry.DeleteItem(Key)
}
;check if an item exists
KeyExists(Key)
{
index := this.ConvertKey(Key)
entry := this.m_Array[index]
item := entry.FindItem(Key)
if(item)
{
return true
}
return false
}
}
; Feature tests and examples
/*
Debug(Text)
{
MsgBox, %Text%
}
*/
; Linked list test
/*
Start := New LinkedListItem("Item 1")
item2 := New LinkedListItem("Item 2")
item2.LinkAfter(Start)
item3 := New LinkedListItem("Item 3")
item3.LinkAfter(item2)
item4 := New LinkedListItem("Item 4")
item4.LinkAfter(item3)
End := Item4
Debug("should show 1,2,3,4")
DebugForward(Start)
Debug("should show 4,3,2,1")
DebugBackwards(End)
item3.Unlink()
Debug("should show 1,2,4")
DebugForward(Start)
Debug("should show 4,2,1")
DebugBackwards(End)
item3.LinkAfter(item2)
item3.LinkBefore(item4)
Debug("should show 1,2,3,4")
DebugForward(Start)
Debug("should show 4,3,2,1")
DebugBackwards(End)
item3.UnlinkFromPrev()
Debug("should show 1,2")
DebugForward(Start)
Debug("should show 4,3")
DebugBackwards(End)
item3.LinkAfter(item2)
Debug("should show 1,2,3,4")
DebugForward(Start)
Debug("should show 4,3,2,1")
DebugBackwards(End)
braket1 := New LinkedListItem("[")
braket2 := New LinkedListItem("]")
braket1.InsertAfter(item2)
braket2.InsertBefore(item4)
Debug("should show 1,2,[,3,],4")
DebugForward(Start)
Debug("should show 4,],3,[,2,1")
DebugBackwards(End)
;example iterations
DebugForward(Start)
{
item := Start
while(item)
{
Debug(item.GetData())
item := item.GetNext()
}
}
DebugBackwards(End)
{
item := End
while(item)
{
Debug(item.GetData())
item := item.GetPrev()
}
}
*/
;Hash table test, set/get
/*
Dic := New Dictionary(1000)
Debug("Pass 1 of Set Get Test")
Dic.SetItem(1,"Item 1") ;1 modulo 1000 is 1, this is store at pos 1 in array
Dic.SetItem(2,"Item 2") ; spot 2
Dic.SetItem(1002,"Item 3") ; Spot 2 again, collision, spot 2 hold 2 keys
Dic.SetItem("Greeting","Item 4") ; "Greeting" is converted to a number prior
Debug("Test 1, Item 1 == " . Dic.GetItem(1))
Debug("Test 2, Item 2 == " . Dic.GetItem(2))
Debug("Test 3, Item 3 == " . Dic.GetItem(1002))
Debug("Test 4, Item 4 == " . Dic.GetItem("Greeting"))
Debug("Test 5, 1 == " . Dic.KeyExists(1))
Debug("Test 6, 1 == " . Dic.KeyExists(2))
Debug("Test 7, 1 == " . Dic.KeyExists(1002))
Debug("Test 8, 1 == " . Dic.KeyExists("Greeting"))
Dic.DeleteItem(1)
Dic.DeleteItem(1002)
Dic.DeleteItem("Greeting")
Debug("Test 9, 0 == " . Dic.KeyExists(1))
Debug("Test 10, 1 == " . Dic.KeyExists(2))
Debug("Test 11, 0 == " . Dic.KeyExists(1002))
Debug("Test 12, 0 == " . Dic.KeyExists("Greeting"))
Dic.SetItem(1,"New Item 1")
Dic.SetItem(1002,"New Item 3")
Debug("Test 13, 1 == " . Dic.KeyExists(1))
Debug("Test 14, 1 == " . Dic.KeyExists(2))
Debug("Test 15, 1 == " . Dic.KeyExists(1002))
Debug("Test 16, 0 == " . Dic.KeyExists("Greeting"))
Debug("Test 17, New Item 1 == " . Dic.GetItem(1))
Debug("Test 18, Item 2 == " . Dic.GetItem(2))
Debug("Test 19, New Item 3 == " . Dic.GetItem(1002))
Dic.DeleteItem(1)
Dic.DeleteItem(2)
Dic.DeleteItem(1002)
Dic.DeleteItem("Greeting")
Debug("Test 21, 0 == " . Dic.KeyExists(1))
Debug("Test 22, 0 == " . Dic.KeyExists(2))
Debug("Test 23, 0 == " . Dic.KeyExists(1002))
Debug("Test 24, 0 == " . Dic.KeyExists("Greeting"))
;Reshash test
Dic.SetItem(1,"Item 1") ;1 modulo 1000 is 1, this is store at pos 1 in array
Dic.SetItem(2,"Item 2") ; spot 2
Dic.SetItem(1002,"Item 3") ; Spot 2 again, collision, spot 2 hold 2 keys
Dic.SetItem("Greeting","Item 4") ; "Greeting" is converted to a number prior
size := Dic.GetSize()
Debug("Rehash Test Size: " . size)
testsize :=50000
loop %testsize%
{
Dic.SetItem(A_Index,A_Index)
if(Dic.GetSize() != size)
{
size := Dic.GetSize()
Debug("New Size: " . size)
}
}
loop %testsize%
{
if(Dic.GetItem(A_Index) <> A_Index)
{
Debug("Failed the rehash check test")
break
}
}
Debug("Set Get Test after re-hash")
Debug("Test 1, 1 == " . Dic.GetItem(1))
Debug("Test 2, 2 == " . Dic.GetItem(2))
Debug("Test 3, 3 == " . Dic.GetItem(1002))
Debug("Test 4, Item 4 == " . Dic.GetItem("Greeting"))
Debug("Test 5, 1 == " . Dic.KeyExists(1))
Debug("Test 6, 1 == " . Dic.KeyExists(2))
Debug("Test 7, 1 == " . Dic.KeyExists(1002))
Debug("Test 8, 1 == " . Dic.KeyExists("Greeting"))
Dic.DeleteItem(1)
Dic.DeleteItem(1002)
Dic.DeleteItem("Greeting")
Debug("Test 9, 0 == " . Dic.KeyExists(1))
Debug("Test 10, 1 == " . Dic.KeyExists(2))
Debug("Test 11, 0 == " . Dic.KeyExists(1002))
Debug("Test 12, 0 == " . Dic.KeyExists("Greeting"))
Dic.SetItem(1,"New Item 1")
Dic.SetItem(1002,"New Item 3")
Debug("Test 13, 1 == " . Dic.KeyExists(1))
Debug("Test 14, 1 == " . Dic.KeyExists(2))
Debug("Test 15, 1 == " . Dic.KeyExists(1002))
Debug("Test 16, 0 == " . Dic.KeyExists("Greeting"))
Debug("Test 17, New Item 1 == " . Dic.GetItem(1))
Debug("Test 18, Item 2 == " . Dic.GetItem(2))
Debug("Test 19, New Item 3 == " . Dic.GetItem(1002))
Dic.DeleteItem(1)
Dic.DeleteItem(2)
Dic.DeleteItem(1002)
Dic.DeleteItem("Greeting")
Debug("Test 21, 0 == " . Dic.KeyExists(1))
Debug("Test 22, 0 == " . Dic.KeyExists(2))
Debug("Test 23, 0 == " . Dic.KeyExists(1002))
Debug("Test 24, 0 == " . Dic.KeyExists("Greeting"))
*/