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

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

System.Collections.Generic.Dictionary(連想配列)を実装

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