-
Notifications
You must be signed in to change notification settings - Fork 2
/
TDict.bmx
148 lines (130 loc) · 3.4 KB
/
TDict.bmx
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
Strict
Import BRL.LinkedList
Type TDict
Field tdictitems:Tlist
'create a new TDict
Method New()
Local tdictitems=New tlist
End Method
'Set the mapping from key to item
Method Set(key$,value:Object)
Local i:TDictItem=getTDictItem(key)
If i
i.value=value
Else
tdictitems.addlast TDictItem.create(key,value)
EndIf
End Method
'Get the object corresponding to key
Method Get:Object(key$)
Local i:TDictItem
For i:TDictItem=EachIn tdictitems
If i.key=key Then Return i.value
Next
End Method
'Get the TDictItem corresponding to key
Method GetTDictItem:TDictItem(key$)
Local i:TDictItem
For i:TDictItem=EachIn tdictitems
If i.key=key Then Return i
Next
End Method
'Get the list of keys in this dictionary
Method Keys:TList()
Local list:TList=New TList
Local i:TDictItem
For i:TDictItem=EachIn tdictitems
list.addlast i.key
Next
Return list
End Method
'Get the list of items matched to each key in this dictionary
'(will include duplicates if one object is mapped to by several keys)
Method Values:Tlist()
Local list:TList=New TList
Local i:TDictItem
For i:TDictItem=EachIn tdictitems
list.addlast i.value
Next
Return list
End Method
'Remove the mapping from key from the dictionary
Method Remove(key$)
Local i:TDictItem=getTDictItem(key)
If Not i
Throw "Key not in dictionary"
EndIf
tdictitems.remove i
End Method
'The number of keys in the dictionary
Method Count()
Return tdictitems.Count()
End Method
'Clear all the keys from the dictionary
Method Clear()
tdictitems=New tlist
End Method
'Make a copy of this dictionary
Method Copy:TDict()
Local d:TDict=New TDict
Local i:TDictItem
For i:TDictItem=EachIn tdictitems
d.set(i.key,i.value)
Next
Return d
End Method
'Update this dictionary with all the mappings from b
'leaving untouched keys that aren't in b
Method Update(b:TDict)
Local i:TDictItem
For i:TDictItem=EachIn b.tdictitems
set(i.key,i.value)
Next
End Method
'Create a new dictionary from the given list of keys, with an optional list of items
'to map them to. Called like this: TDict.fromkeys(keys,[items])
Function FromKeys:TDict(keys:Tlist,values:Tlist=Null)
Local d:TDict=New tdict
Local keylink:TLink=keys.firstlink()
Local valuelink:TLink
Local value:Object
If values<>Null
If values.count()<>keys.count()
Throw "Items list is not the same length as keys list"
EndIf
valuelink=values.firstlink()
Else
valuelink=Null
EndIf
While keylink
If values=Null Then value:Object=Null Else value:Object=valuelink.value()
d.set(String(keylink.value()),value)
keylink=keylink.nextlink()
If valuelink valuelink=valuelink.nextlink()
Wend
Return d
End Function
'Returns value relating to key if key is in dictionary
'else returns passed value AND sets key to value.
Method SetDefault:Object(key$,value:Object=Null)
Local i:TDictItem=getTDictItem(key)
If i
Return i.value
Else
set(key,value)
Return value
EndIf
End Method
Method ObjectEnumerator:TListEnum()
Return values().ObjectEnumerator()
End Method
End Type
Type TDictItem
Field key$,value:Object
Function Create:TDictItem(key$,value:Object)
Local i:TDictItem=New TDictItem
i.key=key
i.value=value
Return i
End Function
End Type