-
Notifications
You must be signed in to change notification settings - Fork 1
/
FasterDictionary.cs
609 lines (500 loc) · 21.7 KB
/
FasterDictionary.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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
using System;
using System.Collections;
using System.Collections.Generic;
namespace Svelto.DataStructures.Experimental
{
public class FasterDictionary<TKey, TValue> : IDictionary<TKey, TValue> where TKey : IComparable<TKey>
{
public FasterDictionary(int size)
{
_valuesInfo = new Node[size];
_values = new TValue[size];
_buckets = new int[HashHelpers.GetPrime(size)];
}
public FasterDictionary():this(1)
{}
ICollection<TKey> IDictionary<TKey,TValue>.Keys
{
get { throw new NotImplementedException(); }
}
public FasterDictionaryKeys Keys
{
get { throw new NotImplementedException(); }
}
ICollection<TValue> IDictionary<TKey,TValue>.Values
{
get { throw new NotImplementedException(); }
}
public ReadOnlyCollectionStruct<TValue> Values
{
get { return new ReadOnlyCollectionStruct<TValue>(_values, _freeValueCellIndex); }
}
public TValue[] GetValuesArray(out int count)
{
count = _freeValueCellIndex;
return _values;
}
public int Count
{
get { return _freeValueCellIndex; }
}
public bool IsReadOnly
{
get { return false; }
}
public void Add(TKey key, TValue value)
{
Add(key, ref value);
}
public void Add(TKey key, ref TValue value)
{
if (AddValue(key, ref value) == false)
{
throw new FasterDictionaryException("Key already present");
}
}
public void Add(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public void Clear()
{
if (_freeValueCellIndex == 0) return;
_freeValueCellIndex = 0;
Array.Clear(_buckets, 0, _buckets.Length);
Array.Clear(_values, 0, _values.Length);
Array.Clear(_valuesInfo, 0, _valuesInfo.Length);
}
public bool Contains(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
public bool ContainsKey(TKey key)
{
uint findIndex;
if (FindIndex(key, _buckets, _valuesInfo, out findIndex))
{
return true;
}
return false;
}
public void CopyTo(KeyValuePair<TKey, TValue>[] array, int arrayIndex)
{
throw new NotImplementedException();
}
IEnumerator<KeyValuePair<TKey, TValue>> IEnumerable<KeyValuePair<TKey, TValue>>.GetEnumerator()
{
return new FasterDictionaryKeyValueEnumerator(this);
}
public FasterDictionaryKeyValueEnumerator GetEnumerator()
{
return new FasterDictionaryKeyValueEnumerator(this);
}
public bool Remove(KeyValuePair<TKey, TValue> item)
{
throw new NotImplementedException();
}
protected uint GetValueIndex(TKey index)
{
return GetIndex(index, _buckets, _valuesInfo);
}
public bool TryGetValue(TKey key, out TValue result)
{
uint findIndex;
if (FindIndex(key, _buckets, _valuesInfo, out findIndex))
{
result = _values[findIndex];
return true;
}
result = default(TValue);
return false;
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public void AddCapacity(int size)
{
throw new NotImplementedException();
}
public TValue this[TKey key]
{
get
{
return _values[GetIndex(key, _buckets, _valuesInfo)];
}
set { AddValue(key, ref value); }
}
static int Hash(TKey key)
{
return key.GetHashCode() & 0x7FFFFFFF;
}
bool AddValue(TKey key, ref TValue value)
{
int hash = Hash(key);
int bucketIndex = hash % _buckets.Length;
//buckets value -1 means it's empty
var valueIndex = GetValueIndexFromBuckets(_buckets, bucketIndex);
if (valueIndex == -1)
//create the info node at the last position and fill it with the relevant information
_valuesInfo[_freeValueCellIndex] = new Node(ref key, hash);
else //collision or already exists
{
{
int currentValueIndex = valueIndex;
do
{
//must check if the key already exists in the dictionary
//for some reason this is faster than using Comparer<TKey>.default, should investigate
if (_valuesInfo[currentValueIndex].hashcode == hash
&& _valuesInfo[currentValueIndex].key.CompareTo(key) == 0)
{//the key already exists, simply replace the value!
_values[currentValueIndex] = value;
return false;
}
currentValueIndex = _valuesInfo[currentValueIndex].previous;
}
while (currentValueIndex != -1); //-1 means no more values with key with the same hash
}
//oops collision!
_collisions++;
//create a new node which previous index points to node currently pointed in the bucket
_valuesInfo[_freeValueCellIndex] = new Node(ref key, hash, valueIndex);
//update the next of the existing cell to point to the new one
//old one -> new one | old one <- next one
_valuesInfo[valueIndex].next = _freeValueCellIndex;
//Important: the new node is always the one that will be pointed by the bucket cell
//so I can assume that the one pointed by the bucket is always the last value added
//(next = -1)
}
//item with this bucketIndex will point to the last value created
//ToDo: if instead I assume that the original one is the one in the bucket
//I wouldn't need to update the bucket here. Small optimization but important
SetValueIndexInBuckets(_buckets, bucketIndex, _freeValueCellIndex);
_values[_freeValueCellIndex] = value;
_freeValueCellIndex++;
if (_freeValueCellIndex == _values.Length)
{
Array.Resize(ref _values,
HashHelpers.ExpandPrime(_freeValueCellIndex));
Array.Resize(ref _valuesInfo,
HashHelpers.ExpandPrime(_freeValueCellIndex));
}
//too many collisions?
if (_collisions > _buckets.Length)
{
//we need more space and less collisions
//ToDo: need to change from prime to Fibonacci sequence (could be quite faster)
_buckets = new int[HashHelpers.ExpandPrime(_collisions)];
_collisions = 0;
//we need to get all the hash code of all the values stored so far and spread them over the new bucket
//length
for (int newValueIndex = 0; newValueIndex < _freeValueCellIndex; newValueIndex++)
{
//get the original hash code and find the new bucketIndex due to the new length
bucketIndex = _valuesInfo[newValueIndex].hashcode % _buckets.Length;
//bucketsIndex can be -1 or a next value. If it's -1 means no collisions. If there is collision,
//we create a new node which prev points to the old one. Old one next points to the new one.
//the bucket will now points to the new one
//In this way we can rebuild the linkedlist.
//get the current valueIndex, it's -1 if no collision happens
int existingValueIndex = GetValueIndexFromBuckets(_buckets, bucketIndex);
//update the bucket index to the index of the current item that share the bucketIndex
//(last found is always the one in the bucket)
SetValueIndexInBuckets(_buckets, bucketIndex, newValueIndex);
if (existingValueIndex != -1)
{ //oops a value was already being pointed by this cell in the new bucket list,
//it means there is a collision, problem
_collisions++;
//the bucket will point to this value, so
//the previous index will be used as previous for the new value.
_valuesInfo[newValueIndex].previous = existingValueIndex;
_valuesInfo[newValueIndex].next = -1;
//and update the previous next index to the new one
_valuesInfo[existingValueIndex].next = newValueIndex;
}
else
{ //ok nothing was indexed, the bucket was empty. We need to update the previous
//values of next and previous
_valuesInfo[newValueIndex].next = -1;
_valuesInfo[newValueIndex].previous = -1;
}
}
}
return true;
}
public bool Remove(TKey key)
{
int hash = Hash(key);
int bucketIndex = hash % _buckets.Length;
//find the bucket
int indexToValueToRemove = GetValueIndexFromBuckets(_buckets, bucketIndex);
//Part one: look for the actual key in the bucket list if found
//we update the bucket list so that it doesn't point anymore
//to the cell to remove
while (indexToValueToRemove != -1)
{
if (_valuesInfo[indexToValueToRemove].hashcode == hash
&& _valuesInfo[indexToValueToRemove].key.CompareTo(key) == 0)
{
//if the key is found and the bucket points directly to the node to remove
if (GetValueIndexFromBuckets(_buckets, bucketIndex) == indexToValueToRemove)
{
DBC.Common.Check.Require(_valuesInfo[indexToValueToRemove].next == -1,
"if the bucket points to the cell, next MUST NOT exists");
//the bucket will point to the previous cell. if a previous cell exists
//its next pointer must be updated!
//<--- iteration order
// B(ucket points always to the last one)
// ------- ------- -------
// | 1 | | 2 | | 3 | //bucket cannot have next, only previous
// ------- ------- -------
//--> insert order
SetValueIndexInBuckets(_buckets, bucketIndex, _valuesInfo[indexToValueToRemove].previous);
}
else
DBC.Common.Check.Require(_valuesInfo[indexToValueToRemove].next != -1,
"if the bucket points to another cell, next MUST exists");
UpdateLinkedList(indexToValueToRemove, _valuesInfo);
break;
}
indexToValueToRemove = _valuesInfo[indexToValueToRemove].previous;
}
if (indexToValueToRemove == -1)
return false; //not found!
_freeValueCellIndex--; //one less value to iterate
//Part two:
//At this point nodes pointers and buckets are updated, but the _values array
//still has got the value to delete. Remember the goal of this dictionary is to be able
//to iterate over the values like an array, so the values array must always be up to date
//if the cell to remove is the last one in the list, we can perform less operations (no swapping needed)
//otherwise we want to move the last value cell over the value to remove
if (indexToValueToRemove != _freeValueCellIndex)
{ //we can move the last value of both arrays in place of the one to delete.
//in order to do so, we need to be sure that the bucket pointer is updated
//first we find the index in the bucket list of the pointer that points to the cell
//to move
var movingBucketIndex = _valuesInfo[_freeValueCellIndex].hashcode % _buckets.Length;
//if the key is found and the bucket points directly to the node to remove
//it must now point to the cell where it's going to be moved
if (GetValueIndexFromBuckets(_buckets, movingBucketIndex) == _freeValueCellIndex)
SetValueIndexInBuckets(_buckets, movingBucketIndex, indexToValueToRemove);
//otherwise it means that there was more than one key with the same hash (collision), so
//we need to update the linked list and its pointers
int next = _valuesInfo[_freeValueCellIndex].next;
int previous = _valuesInfo[_freeValueCellIndex].previous;
//they now point to the cell where the last value is moved into
if (next != -1)
_valuesInfo[next].previous = indexToValueToRemove;
if (previous != -1)
_valuesInfo[previous].next = indexToValueToRemove;
//finally, actually move the values
_valuesInfo[indexToValueToRemove] = _valuesInfo[_freeValueCellIndex];
_values[indexToValueToRemove] = _values[_freeValueCellIndex];
}
return true;
}
public void Trim()
{
if (HashHelpers.ExpandPrime(_freeValueCellIndex) < _valuesInfo.Length)
{
Array.Resize(ref _values,
HashHelpers.ExpandPrime(_freeValueCellIndex));
Array.Resize(ref _valuesInfo,
HashHelpers.ExpandPrime(_freeValueCellIndex));
}
}
//I store all the index with an offset + 1, so that in the bucket
//list 0 means actually not existing.
static void SetValueIndexInBuckets(int[] buckets, int i, int value)
{
buckets[i] = value + 1;
}
//When read the offset must
//be offset by -1 again to be the real one. In this way
//I avoid to initialize the array to -1
static int GetValueIndexFromBuckets(int[] buckets, int i)
{
return buckets[i] - 1;
}
protected bool FindIndex(TKey key, out uint findIndex)
{
int hash = Hash(key);
int bucketIndex = hash % _buckets.Length;
int valueIndex = GetValueIndexFromBuckets(_buckets, bucketIndex);
//even if we found an existing value we need to be sure it's the one we requested
while (valueIndex != -1)
{
//for some reason this is way faster than using Comparer<TKey>.default, should investigate
if (_valuesInfo[valueIndex].hashcode == hash &&
_valuesInfo[valueIndex].key.CompareTo(key) == 0)
{
//this is the one
findIndex = (uint) valueIndex;
return true;
}
valueIndex = _valuesInfo[valueIndex].previous;
}
findIndex = 0;
return false;
}
static uint GetIndex(TKey key, int[] buckets, Node[] valuesInfo)
{
uint findIndex;
if (FindIndex(key, buckets, valuesInfo, out findIndex)) return findIndex;
throw new FasterDictionaryException("Key not found");
}
static bool FindIndex(TKey key, int[] buckets, Node[] valuesInfo, out uint findIndex)
{
int hash = Hash(key);
int bucketIndex = hash % buckets.Length;
int valueIndex = GetValueIndexFromBuckets(buckets, bucketIndex);
while (valueIndex != -1)
{ //for some reason this is way faster they use Comparer<TKey>.default, should investigate
if (valuesInfo[valueIndex].hashcode == hash && valuesInfo[valueIndex].key.CompareTo(key) == 0)
{
findIndex = (uint) valueIndex;
return true;
}
valueIndex = valuesInfo[valueIndex].previous;
}
findIndex = 0;
return false;
}
static void UpdateLinkedList(int index, Node[] valuesInfo)
{
int next = valuesInfo[index].next;
int previous = valuesInfo[index].previous;
if (next != -1)
valuesInfo[next].previous = previous;
if (previous != -1)
valuesInfo[previous].next = next;
}
public struct FasterDictionaryKeyValueEnumerator : IEnumerator<KeyValuePair<TKey, TValue>>
{
public FasterDictionaryKeyValueEnumerator(FasterDictionary<TKey,TValue> dic):this()
{
_dic = dic;
_index = -1;
_count = dic.Count;
}
public void Dispose()
{}
public bool MoveNext()
{
if (_count != _dic.Count)
throw new FasterDictionaryException("can't modify a dictionary during its iteration");
if (_index < _count - 1)
{
_index++;
return true;
}
return false;
}
public void Reset()
{
throw new NotImplementedException();
}
public KeyValuePair<TKey, TValue> Current { get { return new KeyValuePair<TKey, TValue>(_dic._valuesInfo[_index].key, _dic._values[_index]); } }
object IEnumerator.Current
{
get { throw new NotImplementedException(); }
}
readonly FasterDictionary<TKey, TValue> _dic;
readonly int _count;
int _index;
}
struct Node
{
public readonly TKey key;
public readonly int hashcode;
public int previous;
public int next;
public Node(ref TKey key, int hash, int previousNode)
{
this.key = key;
hashcode = hash;
previous = previousNode;
next = -1;
}
public Node(ref TKey key, int hash)
{
this.key = key;
hashcode = hash;
previous = -1;
next = -1;
}
}
public struct FasterDictionaryKeys : ICollection<TKey>
{
internal FasterDictionaryKeys(FasterDictionary<TKey, TValue> dic):this()
{
}
IEnumerator<TKey> IEnumerable<TKey>.GetEnumerator()
{
throw new NotImplementedException();
}
IEnumerator IEnumerable.GetEnumerator()
{
throw new NotImplementedException();
}
public FasterDictionaryKeyEnumerator GetEnumerator()
{
throw new NotImplementedException();
}
public void Add(TKey item)
{
throw new NotImplementedException();
}
public void Clear()
{
throw new NotImplementedException();
}
public bool Contains(TKey item)
{
throw new NotImplementedException();
}
public void CopyTo(TKey[] array, int arrayIndex)
{
throw new NotImplementedException();
}
public bool Remove(TKey item)
{
throw new NotImplementedException();
}
public int Count { get; }
public bool IsReadOnly { get; }
}
public struct FasterDictionaryKeyEnumerator:IEnumerator<TKey>
{
public bool MoveNext()
{
throw new NotImplementedException();
}
public void Reset()
{
throw new NotImplementedException();
}
public TKey Current { get; }
object IEnumerator.Current
{
get { return Current; }
}
public void Dispose()
{
throw new NotImplementedException();
}
}
protected TValue[] _values;
Node[] _valuesInfo;
int[] _buckets;
int _freeValueCellIndex;
int _collisions;
}
public class FasterDictionaryException : Exception
{
public FasterDictionaryException(string keyAlreadyExisting):base(keyAlreadyExisting)
{}
}
}
//todo check https://github.com/benaadams/Ben.TypeDictionary