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