source: trunk/Include/Classes/System/Collections/Generic/Dictionary.ab@ 403

Last change on this file since 403 was 403, checked in by dai, 16 years ago

・MemberInfoクラスを追加。
・KeyNotFoundExceptionクラスを追加。
TypeInfo.GetMembersメソッドを追加。
・TypeInfoテストケースを追加。

File size: 3.7 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 Object.ReferenceEquals(key, Nothing) Then
43 'Throw ArgumentNullError
44 debug
45 Exit Function
46 End If
47
48 Dim hash = getHash(key)
49 Dim a = al[hash] As ArrayList
50
51 If Not Object.ReferenceEquals(a, Nothing) Then
52 Dim i As Long
53 For i = 0 To ELM(a.Count)
54 Dim pair = a[i] As Detail.Pair
55 If pair.Key.Equals(key) Then
56 Return pair.Value
57 End If
58 Next
59 End If
60
61 Throw New KeyNotFoundException
62
63 Return Nothing
64 End Function
65
66 Virtual Sub Item(key As Key, value As T)
67 If Object.ReferenceEquals(key, Nothing) Then
68 'Throw ArgumentNullError
69 Exit Sub
70 End If
71
72 Dim hash = getHash(key)
73 Dim a = al[hash] As ArrayList
74
75 If Object.ReferenceEquals(a, Nothing) Then
76 a = New ArrayList
77 al[hash] = a
78 Else
79 Dim i As Long
80 For i = 0 To ELM(a.Count)
81 Dim pair = a[i] As Detail.Pair
82 If pair.Key.Equals(key) Then
83 pair.Value = value
84 End If
85 Next
86 End If
87
88 a.Add(New Detail.Pair(key, value))
89 End Sub
90
91' Function Keys() As KeyCollection
92' Function Values() As ValuesCollection
93/*
94 'Operators
95' Function Operator [](key As Key) As T
96 Function Operator [](key As Key) As Object
97 Return Item[key]
98 End Function
99
100 Sub Operator []=(key As Key, value As T)
101' Item[key] = vaule
102 End Sub
103*/
104 'Methods
105 Sub Add(key As Key, value As T)
106 If Object.ReferenceEquals(key, Nothing) Then
107 'Throw ArgumentNullError
108 Exit Sub
109 End If
110
111 Dim hash = getHash(key)
112 Dim a = al[hash] As ArrayList
113
114 If Object.ReferenceEquals(a, Nothing) Then
115 a = New ArrayList
116 al[hash] = a
117 Else
118 Dim i As Long
119 For i = 0 To ELM(a.Count)
120 Dim pair = a[i] As Detail.Pair
121 If pair.Key.Equals(key) Then
122 'Throw ArgumentError
123 End If
124 Next
125 End If
126
127 a.Add(New Detail.Pair(key, value))
128
129 count++
130 End Sub
131
132 Sub Clear()
133 Dim i As Long
134 For i = 0 To ELM(al.Count)
135 al[i] = Nothing
136 Next
137 End Sub
138
139 Function Count() As Long
140 Return count
141 End Function
142
143' Function ContainsKey(key As Key) As Boolean
144' End Function
145' Function ContainsValue(value As T) As Boolean
146' End Function
147' Function GetEnumerator() As Enumerator
148' Function Remove(key As Key) As Boolean
149' End Function
150' Function TryGetValue(key As Key, ByRef value As T) As Boolean
151' End Function
152
153 'Classses
154' Class KeyCollection
155' Class ValuesCollection
156' Class Enumerator
157
158Private
159
160' comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>
161
162 Sub initialize(c As Long)
163 al = ArrayList.Repeat(Nothing, c)
164 count = 0
165 End Sub
166
167 Function getHash(key As Object) As Long
168 Return (key.GetHashCode As DWord) Mod al.Count
169 End Function
170
171 al As ArrayList
172 count As Long
173
174End Class
175
176End Namespace 'Generic
177End Namespace 'Collections
178End Namespace 'System
Note: See TracBrowser for help on using the repository browser.