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

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

Itemの戻り値型をTにできた

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 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
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
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(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 Function Operator [](key As Key) As Object
99 Return Item[key]
100 End Function
101
102 Sub Operator []=(key As Key, value As T)
103' Item[key] = vaule
104 End Sub
105*/
106 'Methods
107 Sub Add(key As Key, value As T)
108 Imports System.Collections.Generic.Detail
109
110 If Object.ReferenceEquals(key, Nothing) Then
111 'Throw ArgumentNullError
112 Exit Sub
113 End If
114
115 Dim hash = key.GetHashCode Mod al.Count
116 Dim a = al[hash] As ArrayList
117
118 If Object.ReferenceEquals(a, Nothing) Then
119 a = New ArrayList
120 al[hash] = a
121 Else
122 Dim i As Long
123 For i = 0 To ELM(a.Count)
124 Dim pair = a[i] As Pair
125 If pair.Key.Equals(key) Then
126 'Throw ArgumentError
127 End If
128 Next
129 End If
130
131 a.Add(New Pair(key, value))
132 End Sub
133
134 Sub Clear()
135 Dim i As Long
136 For i = 0 To ELM(al.Count)
137 al[i] = Nothing
138 Next
139 End Sub
140
141' Function ContainsKey(key As Key) As Boolean
142' End Function
143' Function ContainsValue(value As T) As Boolean
144' End Function
145' Function GetEnumerator() As Enumerator
146' Function Remove(key As Key) As Boolean
147' End Function
148' Function TryGetValue(key As Key, ByRef value As T) As Boolean
149' End Function
150
151 'Classses
152' Class KeyCollection
153' Class ValuesCollection
154' Class Enumerator
155
156Private
157
158' comp As System.Collections.Generic.Detail.DefaultEqualityComparer<Key> 'IEqualityComparer<Key>
159
160 Sub initialize(c As Long)
161 al = ArrayList.Repeat(Nothing, c)
162 Dim e = al[0]
163 Dim p = ObjPtr(e) As ULONG_PTR
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.