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

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

KeyNotFoundExceptionを実装

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