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

Last change on this file since 366 was 366, checked in by dai, 17 years ago

Dictionary.Countメソッドを実装。
String.Equalsメソッドのコードミスを修正。

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