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

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

Dictionaryで、keyのGetHashCodeが負の値を返した場合でも問題なく動作するようにした。

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