-
Notifications
You must be signed in to change notification settings - Fork 72
/
class_OrderedAssociateArray.ahk
78 lines (64 loc) · 1.24 KB
/
class_OrderedAssociateArray.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
; Link: https://www.autohotkey.com/boards/viewtopic.php?f=6&t=37083
; Author: Capn Odin
; Date:
; for: AHK_L
/*
*/
Class OrderedAssociativeArray {
__New() {
ObjRawSet(this, "__Data", {})
ObjRawSet(this, "__Order", [])
}
__Get(args*) {
return this.__Data[args*]
}
__Set(args*) {
key := args[1]
val := args.Pop()
if(args.Length() < 2 && this.__Data.HasKey(key)) {
this.Delete(key)
}
if(!this.__Data.HasKey(key)) {
this.__Order.Push(key)
}
this.__Data[args*] := val
return val
}
InsertAt(pos, key, val) {
this.__Order.InsertAt(pos, key)
this.__Data[key] := val
}
RemoveAt(pos) {
val := this.__Data[this.__Order[pos]]
this.__Data.Delete(this.__Order[pos])
this.__Order.RemoveAt(pos)
return val
}
Delete(key) {
for i, v in this.__Order {
if(key == v) {
return this.RemoveAt(i)
}
}
}
Length() {
return this.__Order.Length()
}
HasKey(key) {
return this.__Data.HasKey(key)
}
_NewEnum() {
return new OrderedAssociativeArray.Enum(this.__Data, this.__Order)
}
Class Enum {
__New(Data, Order) {
this.Data := Data
this.oEnum := Order._NewEnum()
}
Next(ByRef key, ByRef val := "") {
res := this.oEnum.next(, key)
val := this.Data[key]
return res
}
}
}