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

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

getter側のItemの戻り値の型をTにした版(Pairをジェネリクス化)。注意:現在のコンパイラではコンパイルできない。

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