source: trunk/ab5.0/ablib/src/Classes/System/Collections/Generic/Dictionary.ab@ 582

Last change on this file since 582 was 582, checked in by イグトランス (egtra), 16 years ago

非Genericコレクションインタフェースの扱いを大幅に縮小。Queue/Stackの実装インタフェースの修正。

File size: 5.3 KB
RevLine 
[310]1'Classes/System/Collections/Generic/Dictionary.ab
2
3Namespace System
4Namespace Collections
5Namespace Generic
6
7Namespace Detail
[313]8 Class Pair
[310]9 Public
[313]10 Sub Pair(key As Object, value As Object)
[310]11 Key = key
12 Value = value
13 End Sub
14
[313]15 Key As Object
16 Value As Object
[310]17 End Class
18End Namespace 'Detail
19
20Class Dictionary<Key, T>
21Public
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
[312]39 Virtual Function Item(key As Key) As T
[537]40 If ActiveBasic.IsNothing(key) Then
[410]41 Throw New ArgumentNullException("key")
[310]42 End If
43
[393]44 Dim hash = getHash(key)
[310]45 Dim a = al[hash] As ArrayList
46
[542]47 If Not ActiveBasic.IsNothing(a) Then
[310]48 Dim i As Long
49 For i = 0 To ELM(a.Count)
[393]50 Dim pair = a[i] As Detail.Pair
[310]51 If pair.Key.Equals(key) Then
52 Return pair.Value
53 End If
54 Next
55 End If
[403]56
57 Throw New KeyNotFoundException
[310]58 End Function
59
60 Virtual Sub Item(key As Key, value As T)
[537]61 If ActiveBasic.IsNothing(key) Then
62 Throw New ArgumentNullException("key")
[310]63 End If
64
[393]65 Dim hash = getHash(key)
[310]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)
[393]74 Dim pair = a[i] As Detail.Pair
[310]75 If pair.Key.Equals(key) Then
76 pair.Value = value
[542]77 Exit Sub
[310]78 End If
79 Next
80 End If
81
[393]82 a.Add(New Detail.Pair(key, value))
[310]83 End Sub
84
85' Function Keys() As KeyCollection
86' Function Values() As ValuesCollection
87/*
88 'Operators
[313]89' Function Operator [](key As Key) As T
90 Function Operator [](key As Key) As Object
[310]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)
[537]100 If ActiveBasic.IsNothing(key) Then
101 Throw New ArgumentNullException("key")
[310]102 End If
103
[393]104 Dim hash = getHash(key)
[310]105 Dim a = al[hash] As ArrayList
106
[537]107 If ActiveBasic.IsNothing(a) Then
[310]108 a = New ArrayList
109 al[hash] = a
110 Else
111 Dim i As Long
112 For i = 0 To ELM(a.Count)
[393]113 Dim pair = a[i] As Detail.Pair
[310]114 If pair.Key.Equals(key) Then
115 'Throw ArgumentError
116 End If
117 Next
118 End If
119
[393]120 a.Add(New Detail.Pair(key, value))
[366]121
[393]122 count++
[310]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
[366]132 Function Count() As Long
133 Return count
134 End Function
135
[542]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
[310]166' Function ContainsValue(value As T) As Boolean
167' End Function
168' Function GetEnumerator() As Enumerator
[537]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
[542]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
[310]204
[542]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
[310]221 'Classses
222' Class KeyCollection
223' Class ValuesCollection
224' Class Enumerator
225
226Private
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)
[366]232 count = 0
[310]233 End Sub
234
[393]235 Function getHash(key As Object) As Long
236 Return (key.GetHashCode As DWord) Mod al.Count
237 End Function
238
[310]239 al As ArrayList
[366]240 count As Long
[310]241
242End Class
243
244End Namespace 'Generic
245End Namespace 'Collections
246End Namespace 'System
Note: See TracBrowser for help on using the repository browser.