-
Notifications
You must be signed in to change notification settings - Fork 1
/
Enumerators.cs
218 lines (168 loc) · 7.24 KB
/
Enumerators.cs
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
using System;
using System.Collections;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
namespace SimdDictionary {
public partial class SimdDictionary<K, V> {
public struct KeyCollection : ICollection<K>, ICollection {
public readonly SimdDictionary<K, V> Dictionary;
public struct Enumerator : IEnumerator<K> {
internal SimdDictionary<K, V>.Enumerator Inner;
public K Current => Inner.CurrentKey;
object? IEnumerator.Current => Inner.CurrentKey;
internal Enumerator (SimdDictionary<K, V> dictionary) {
Inner = dictionary.GetEnumerator();
}
public void Dispose () =>
Inner.Dispose();
public bool MoveNext () =>
Inner.MoveNext();
public void Reset () =>
Inner.Reset();
}
internal KeyCollection (SimdDictionary<K, V> dictionary) {
Dictionary = dictionary;
}
public int Count => Dictionary.Count;
bool ICollection<K>.IsReadOnly => true;
void ICollection<K>.Add (K item) =>
throw new InvalidOperationException();
void ICollection<K>.Clear () =>
Dictionary.Clear();
bool ICollection<K>.Contains (K item) =>
Dictionary.ContainsKey(item);
void ICollection<K>.CopyTo (K[] array, int arrayIndex) {
// FIXME: Use EnumerateBuckets
using (var e = GetEnumerator())
while (e.MoveNext())
array[arrayIndex++] = e.Current;
}
public Enumerator GetEnumerator () =>
new Enumerator(Dictionary);
IEnumerator<K> IEnumerable<K>.GetEnumerator () =>
GetEnumerator();
IEnumerator IEnumerable.GetEnumerator () =>
GetEnumerator();
bool ICollection<K>.Remove (K item) =>
Dictionary.Remove(item);
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => Dictionary;
void ICollection.CopyTo(System.Array array, int index) {
throw new NotImplementedException();
}
}
public struct ValueCollection : ICollection<V>, ICollection {
public readonly SimdDictionary<K, V> Dictionary;
public struct Enumerator : IEnumerator<V> {
internal SimdDictionary<K, V>.Enumerator Inner;
public V Current => Inner.CurrentValue;
object? IEnumerator.Current => Inner.CurrentValue;
internal Enumerator (SimdDictionary<K, V> dictionary) {
Inner = dictionary.GetEnumerator();
}
public void Dispose () =>
Inner.Dispose();
public bool MoveNext () =>
Inner.MoveNext();
public void Reset () =>
Inner.Reset();
}
internal ValueCollection (SimdDictionary<K, V> dictionary) {
Dictionary = dictionary;
}
public int Count => Dictionary.Count;
bool ICollection<V>.IsReadOnly => true;
void ICollection<V>.Add (V item) =>
throw new InvalidOperationException();
void ICollection<V>.Clear () =>
Dictionary.Clear();
// FIXME
bool ICollection<V>.Contains (V item) =>
throw new InvalidOperationException();
void ICollection<V>.CopyTo (V[] array, int arrayIndex) {
// FIXME: Use EnumerateBuckets
using (var e = GetEnumerator())
while (e.MoveNext())
array[arrayIndex++] = e.Current;
}
public Enumerator GetEnumerator () =>
new Enumerator(Dictionary);
IEnumerator<V> IEnumerable<V>.GetEnumerator () =>
GetEnumerator();
IEnumerator IEnumerable.GetEnumerator () =>
GetEnumerator();
bool ICollection<V>.Remove (V item) =>
throw new InvalidOperationException();
bool ICollection.IsSynchronized => false;
object ICollection.SyncRoot => Dictionary;
void ICollection.CopyTo(System.Array array, int index) {
throw new NotImplementedException();
}
}
public struct Enumerator : IEnumerator<KeyValuePair<K, V>>, IDictionaryEnumerator {
private int _bucketIndex, _valueIndexLocal;
private Bucket _currentBucket;
private Bucket[] _buckets;
public K CurrentKey {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get {
return _currentBucket.Pairs[_valueIndexLocal].Key;
}
}
public V CurrentValue {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get {
return _currentBucket.Pairs[_valueIndexLocal].Value;
}
}
public KeyValuePair<K, V> Current {
[MethodImpl(MethodImplOptions.AggressiveInlining)]
get {
ref var pair = ref _currentBucket.Pairs[_valueIndexLocal];
return new KeyValuePair<K, V>(pair.Key, pair.Value);
}
}
object IEnumerator.Current => Current;
DictionaryEntry IDictionaryEnumerator.Entry {
get {
ref var pair = ref _currentBucket.Pairs[_valueIndexLocal];
return new DictionaryEntry(pair.Key, pair.Value);
}
}
object IDictionaryEnumerator.Key => CurrentKey;
object? IDictionaryEnumerator.Value => CurrentValue;
public Enumerator (SimdDictionary<K, V> dictionary) {
_bucketIndex = -1;
_valueIndexLocal = BucketSizeI;
_buckets = dictionary._Buckets;
}
public void Dispose () {
}
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public bool MoveNext () {
_valueIndexLocal++;
while (_bucketIndex < _buckets.Length) {
var count = _currentBucket.Count;
if (_valueIndexLocal >= count) {
_valueIndexLocal = 0;
_bucketIndex++;
if (_bucketIndex >= _buckets.Length)
return false;
_currentBucket = _buckets[_bucketIndex];
}
while (_valueIndexLocal < count) {
var suffix = _currentBucket.GetSlot(_valueIndexLocal);
if (suffix != 0)
return true;
_valueIndexLocal++;
}
}
return false;
}
public void Reset () {
_bucketIndex = -1;
_valueIndexLocal = BucketSizeI;
}
}
}
}