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

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

Dictionary.Removeとテストを追加。#promptでコンパイルできない問題を修正。

File size: 4.2 KB
Line 
1'Classes/System/Collections/Generic/Dictionary.ab
2
3'#require <Classes/System/Collections/Generic/EqualityComparer.ab>
4
5Namespace System
6Namespace Collections
7Namespace Generic
8
9Namespace 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
20End Namespace 'Detail
21
22Class Dictionary<Key, T>
23Public
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 Object.ReferenceEquals(a, Nothing) 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 End If
80 Next
81 End If
82
83 a.Add(New Detail.Pair(key, value))
84 End Sub
85
86' Function Keys() As KeyCollection
87' Function Values() As ValuesCollection
88/*
89 'Operators
90' Function Operator [](key As Key) As T
91 Function Operator [](key As Key) As Object
92 Return Item[key]
93 End Function
94
95 Sub Operator []=(key As Key, value As T)
96' Item[key] = vaule
97 End Sub
98*/
99 'Methods
100 Sub Add(key As Key, value As T)
101 If ActiveBasic.IsNothing(key) Then
102 Throw New ArgumentNullException("key")
103 End If
104
105 Dim hash = getHash(key)
106 Dim a = al[hash] As ArrayList
107
108 If ActiveBasic.IsNothing(a) Then
109 a = New ArrayList
110 al[hash] = a
111 Else
112 Dim i As Long
113 For i = 0 To ELM(a.Count)
114 Dim pair = a[i] As Detail.Pair
115 If pair.Key.Equals(key) Then
116 'Throw ArgumentError
117 End If
118 Next
119 End If
120
121 a.Add(New Detail.Pair(key, value))
122
123 count++
124 End Sub
125
126 Sub Clear()
127 Dim i As Long
128 For i = 0 To ELM(al.Count)
129 al[i] = Nothing
130 Next
131 End Sub
132
133 Function Count() As Long
134 Return count
135 End Function
136
137' Function ContainsKey(key As Key) As Boolean
138' End Function
139' Function ContainsValue(value As T) As Boolean
140' End Function
141' Function GetEnumerator() As Enumerator
142
143 /*!
144 @biref 指定されたキーの要素を削除する。
145 @date 2008/07/11
146 @param[in] key 削除対象のキー
147 @retval True 対象要素が見つかって削除されたとき
148 @retval False 見つからず削除されなかったとき
149 @author Egtra
150 */
151 Function Remove(key As Key) As Boolean
152 Remove = False
153 If Not ActiveBasic.IsNothing(key) Then
154 Dim hash = getHash(key)
155 Dim a = al[hash] As ArrayList
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 a.RemoveAt(i)
162 Remove = True
163 count--
164 Exit Function
165 End If
166 Next
167 End If
168 End If
169 End Function
170
171' Function TryGetValue(key As Key, ByRef value As T) As Boolean
172' End Function
173
174 'Classses
175' Class KeyCollection
176' Class ValuesCollection
177' Class Enumerator
178
179Private
180
181' comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>
182
183 Sub initialize(c As Long)
184 al = ArrayList.Repeat(Nothing, c)
185 count = 0
186 End Sub
187
188 Function getHash(key As Object) As Long
189 Return (key.GetHashCode As DWord) Mod al.Count
190 End Function
191
192 al As ArrayList
193 count As Long
194
195End Class
196
197End Namespace 'Generic
198End Namespace 'Collections
199End Namespace 'System
Note: See TracBrowser for help on using the repository browser.