1 | 'Classes/System/Collections/Generic/Dictionary.ab
|
---|
2 |
|
---|
3 | '#require <Classes/System/Collections/Generic/EqualityComparer.ab>
|
---|
4 |
|
---|
5 | Namespace System
|
---|
6 | Namespace Collections
|
---|
7 | Namespace Generic
|
---|
8 |
|
---|
9 | Namespace Detail
|
---|
10 | Class Pair
|
---|
11 | Public
|
---|
12 | Sub Pair(key As Object, value As Object)
|
---|
13 | Key = key
|
---|
14 | Value = value
|
---|
15 | End Sub
|
---|
16 |
|
---|
17 | Key As Object
|
---|
18 | Value As Object
|
---|
19 | End Class
|
---|
20 | End Namespace 'Detail
|
---|
21 |
|
---|
22 | Class Dictionary<Key, T>
|
---|
23 | Public
|
---|
24 | 'Constructor
|
---|
25 | Sub Dictionary()
|
---|
26 | initialize(23)
|
---|
27 | End Sub
|
---|
28 | ' Sub Dictionary(d As IDictionary<Key, T>)
|
---|
29 | ' Sub Dictionary(comparer As IEqualityComparer<Key>)
|
---|
30 | Sub Dictionary(capacity As Long)
|
---|
31 | initialize(capacity)
|
---|
32 | End Sub
|
---|
33 | ' Sub Dictionary(d As IDictionary<Key, T>, comparer As IEqualityComparer<Key>)
|
---|
34 | ' Sub Dictionary(capacity As Long, comparer As IEqualityComparer<Key>)
|
---|
35 |
|
---|
36 | 'Properties
|
---|
37 | ' Function Comparer() As IEqualityComparer<Key>
|
---|
38 | ' Virtual Function Count() As Long
|
---|
39 | ' End Function
|
---|
40 |
|
---|
41 | Virtual Function Item(key As Key) As T
|
---|
42 | If ActiveBasic.IsNothing(key) Then
|
---|
43 | Throw New ArgumentNullException("key")
|
---|
44 | End If
|
---|
45 |
|
---|
46 | Dim hash = getHash(key)
|
---|
47 | Dim a = al[hash] As ArrayList
|
---|
48 |
|
---|
49 | If Not ActiveBasic.IsNothing(a) Then
|
---|
50 | Dim i As Long
|
---|
51 | For i = 0 To ELM(a.Count)
|
---|
52 | Dim pair = a[i] As Detail.Pair
|
---|
53 | If pair.Key.Equals(key) Then
|
---|
54 | Return pair.Value
|
---|
55 | End If
|
---|
56 | Next
|
---|
57 | End If
|
---|
58 |
|
---|
59 | Throw New KeyNotFoundException
|
---|
60 | End Function
|
---|
61 |
|
---|
62 | Virtual Sub Item(key As Key, value As T)
|
---|
63 | If ActiveBasic.IsNothing(key) Then
|
---|
64 | Throw New ArgumentNullException("key")
|
---|
65 | End If
|
---|
66 |
|
---|
67 | Dim hash = getHash(key)
|
---|
68 | Dim a = al[hash] As ArrayList
|
---|
69 |
|
---|
70 | If Object.ReferenceEquals(a, Nothing) Then
|
---|
71 | a = New ArrayList
|
---|
72 | al[hash] = a
|
---|
73 | Else
|
---|
74 | Dim i As Long
|
---|
75 | For i = 0 To ELM(a.Count)
|
---|
76 | Dim pair = a[i] As Detail.Pair
|
---|
77 | If pair.Key.Equals(key) Then
|
---|
78 | pair.Value = value
|
---|
79 | Exit Sub
|
---|
80 | End If
|
---|
81 | Next
|
---|
82 | End If
|
---|
83 |
|
---|
84 | a.Add(New Detail.Pair(key, value))
|
---|
85 | End Sub
|
---|
86 |
|
---|
87 | ' Function Keys() As KeyCollection
|
---|
88 | ' Function Values() As ValuesCollection
|
---|
89 | /*
|
---|
90 | 'Operators
|
---|
91 | ' Function Operator [](key As Key) As T
|
---|
92 | Function Operator [](key As Key) As Object
|
---|
93 | Return Item[key]
|
---|
94 | End Function
|
---|
95 |
|
---|
96 | Sub Operator []=(key As Key, value As T)
|
---|
97 | ' Item[key] = vaule
|
---|
98 | End Sub
|
---|
99 | */
|
---|
100 | 'Methods
|
---|
101 | Sub Add(key As Key, value As T)
|
---|
102 | If ActiveBasic.IsNothing(key) Then
|
---|
103 | Throw New ArgumentNullException("key")
|
---|
104 | End If
|
---|
105 |
|
---|
106 | Dim hash = getHash(key)
|
---|
107 | Dim a = al[hash] As ArrayList
|
---|
108 |
|
---|
109 | If ActiveBasic.IsNothing(a) Then
|
---|
110 | a = New ArrayList
|
---|
111 | al[hash] = a
|
---|
112 | Else
|
---|
113 | Dim i As Long
|
---|
114 | For i = 0 To ELM(a.Count)
|
---|
115 | Dim pair = a[i] As Detail.Pair
|
---|
116 | If pair.Key.Equals(key) Then
|
---|
117 | 'Throw ArgumentError
|
---|
118 | End If
|
---|
119 | Next
|
---|
120 | End If
|
---|
121 |
|
---|
122 | a.Add(New Detail.Pair(key, value))
|
---|
123 |
|
---|
124 | count++
|
---|
125 | End Sub
|
---|
126 |
|
---|
127 | Sub Clear()
|
---|
128 | Dim i As Long
|
---|
129 | For i = 0 To ELM(al.Count)
|
---|
130 | al[i] = Nothing
|
---|
131 | Next
|
---|
132 | End Sub
|
---|
133 |
|
---|
134 | Function Count() As Long
|
---|
135 | Return count
|
---|
136 | End Function
|
---|
137 |
|
---|
138 | /*!
|
---|
139 | @biref 指定されたキーが格納されているか調べる。
|
---|
140 | @date 2008/07/11
|
---|
141 | @param[in] key 検索対象のキー
|
---|
142 | @retval True 格納されていたとき
|
---|
143 | @retval False 格納されていなかったとき
|
---|
144 | @throw ArgumentNullException keyがNothingだったとき
|
---|
145 | @author Egtra
|
---|
146 | */
|
---|
147 | Function ContainsKey(key As Key) As Boolean
|
---|
148 | If ActiveBasic.IsNothing(key) Then
|
---|
149 | Throw New ArgumentNullException("key")
|
---|
150 | End If
|
---|
151 | ContainsKey = False
|
---|
152 |
|
---|
153 | Dim hash = getHash(key)
|
---|
154 | Dim a = al[hash] As ArrayList
|
---|
155 |
|
---|
156 | If Not ActiveBasic.IsNothing(a) Then
|
---|
157 | Dim i As Long
|
---|
158 | For i = 0 To ELM(a.Count)
|
---|
159 | Dim pair = a[i] As Detail.Pair
|
---|
160 | If pair.Key.Equals(key) Then
|
---|
161 | ContainsKey = True
|
---|
162 | Exit Function
|
---|
163 | End If
|
---|
164 | Next
|
---|
165 | End If
|
---|
166 | End Function
|
---|
167 |
|
---|
168 | ' Function ContainsValue(value As T) As Boolean
|
---|
169 | ' End Function
|
---|
170 | ' Function GetEnumerator() As Enumerator
|
---|
171 |
|
---|
172 | /*!
|
---|
173 | @biref 指定されたキーの要素を削除する。
|
---|
174 | @date 2008/07/11
|
---|
175 | @param[in] key 削除対象のキー
|
---|
176 | @retval True 対象要素が見つかって削除されたとき
|
---|
177 | @retval False 見つからず削除されなかったとき
|
---|
178 | @author Egtra
|
---|
179 | */
|
---|
180 | Function Remove(key As Key) As Boolean
|
---|
181 | Remove = False
|
---|
182 | If Not ActiveBasic.IsNothing(key) Then
|
---|
183 | Dim hash = getHash(key)
|
---|
184 | Dim a = al[hash] As ArrayList
|
---|
185 | If Not ActiveBasic.IsNothing(a) Then
|
---|
186 | Dim i As Long
|
---|
187 | For i = 0 To ELM(a.Count)
|
---|
188 | Dim pair = a[i] As Detail.Pair
|
---|
189 | If pair.Key.Equals(key) Then
|
---|
190 | a.RemoveAt(i)
|
---|
191 | Remove = True
|
---|
192 | count--
|
---|
193 | Exit Function
|
---|
194 | End If
|
---|
195 | Next
|
---|
196 | End If
|
---|
197 | End If
|
---|
198 | End Function
|
---|
199 |
|
---|
200 | Function TryGetValue(key As Key, ByRef value As T) As Boolean
|
---|
201 | If ActiveBasic.IsNothing(key) Then
|
---|
202 | Throw New ArgumentNullException("key")
|
---|
203 | End If
|
---|
204 | TryGetValue = False
|
---|
205 | value = Nothing
|
---|
206 |
|
---|
207 | Dim hash = getHash(key)
|
---|
208 | Dim a = al[hash] As ArrayList
|
---|
209 |
|
---|
210 | If Not ActiveBasic.IsNothing(a) Then
|
---|
211 | Dim i As Long
|
---|
212 | For i = 0 To ELM(a.Count)
|
---|
213 | Dim pair = a[i] As Detail.Pair
|
---|
214 | If pair.Key.Equals(key) Then
|
---|
215 | TryGetValue = True
|
---|
216 | value = pair.Value
|
---|
217 | Exit Function
|
---|
218 | End If
|
---|
219 | Next
|
---|
220 | End If
|
---|
221 | End Function
|
---|
222 |
|
---|
223 | 'Classses
|
---|
224 | ' Class KeyCollection
|
---|
225 | ' Class ValuesCollection
|
---|
226 | ' Class Enumerator
|
---|
227 |
|
---|
228 | Private
|
---|
229 |
|
---|
230 | ' comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>
|
---|
231 |
|
---|
232 | Sub initialize(c As Long)
|
---|
233 | al = ArrayList.Repeat(Nothing, c)
|
---|
234 | count = 0
|
---|
235 | End Sub
|
---|
236 |
|
---|
237 | Function getHash(key As Object) As Long
|
---|
238 | Return (key.GetHashCode As DWord) Mod al.Count
|
---|
239 | End Function
|
---|
240 |
|
---|
241 | al As ArrayList
|
---|
242 | count As Long
|
---|
243 |
|
---|
244 | End Class
|
---|
245 |
|
---|
246 | End Namespace 'Generic
|
---|
247 | End Namespace 'Collections
|
---|
248 | End Namespace 'System
|
---|