LinkedDictionary.cs
2.76 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections;
namespace Org.BouncyCastle.Utilities.Collections
{
public class LinkedDictionary
: IDictionary
{
internal readonly IDictionary hash = Platform.CreateHashtable();
internal readonly IList keys = Platform.CreateArrayList();
public LinkedDictionary()
{
}
public virtual void Add(object k, object v)
{
hash.Add(k, v);
keys.Add(k);
}
public virtual void Clear()
{
hash.Clear();
keys.Clear();
}
public virtual bool Contains(object k)
{
return hash.Contains(k);
}
public virtual void CopyTo(Array array, int index)
{
foreach (object k in keys)
{
array.SetValue(hash[k], index++);
}
}
public virtual int Count
{
get { return hash.Count; }
}
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
public virtual IDictionaryEnumerator GetEnumerator()
{
return new LinkedDictionaryEnumerator(this);
}
public virtual void Remove(object k)
{
hash.Remove(k);
keys.Remove(k);
}
public virtual bool IsFixedSize
{
get { return false; }
}
public virtual bool IsReadOnly
{
get { return false; }
}
public virtual bool IsSynchronized
{
get { return false; }
}
public virtual object SyncRoot
{
get { return false; }
}
public virtual ICollection Keys
{
get { return Platform.CreateArrayList(keys); }
}
public virtual ICollection Values
{
// NB: Order has to be the same as for Keys property
get
{
IList values = Platform.CreateArrayList(keys.Count);
foreach (object k in keys)
{
values.Add(hash[k]);
}
return values;
}
}
public virtual object this[object k]
{
get
{
return hash[k];
}
set
{
if (!hash.Contains(k))
keys.Add(k);
hash[k] = value;
}
}
}
internal class LinkedDictionaryEnumerator : IDictionaryEnumerator
{
private readonly LinkedDictionary parent;
private int pos = -1;
internal LinkedDictionaryEnumerator(LinkedDictionary parent)
{
this.parent = parent;
}
public virtual object Current
{
get { return Entry; }
}
public virtual DictionaryEntry Entry
{
get
{
object k = CurrentKey;
return new DictionaryEntry(k, parent.hash[k]);
}
}
public virtual object Key
{
get
{
return CurrentKey;
}
}
public virtual bool MoveNext()
{
if (pos >= parent.keys.Count)
return false;
return ++pos < parent.keys.Count;
}
public virtual void Reset()
{
this.pos = -1;
}
public virtual object Value
{
get
{
return parent.hash[CurrentKey];
}
}
private object CurrentKey
{
get
{
if (pos < 0 || pos >= parent.keys.Count)
throw new InvalidOperationException();
return parent.keys[pos];
}
}
}
}