-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathFastHashSet.cs
More file actions
2073 lines (1857 loc) · 81.6 KB
/
FastHashSet.cs
File metadata and controls
2073 lines (1857 loc) · 81.6 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
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
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
// See the LICENSE file in the project root for more information.
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
namespace System.Collections.Generic
{
/// <summary>
/// Implementation notes:
/// This uses an array-based implementation similar to <see cref="Dictionary{TKey, TValue}"/>, using a buckets array
/// to map hash values to the Slots array. Items in the Slots array that hash to the same value
/// are chained together through the "next" indices.
///
/// The capacity is always prime; so during resizing, the capacity is chosen as the next prime
/// greater than double the last capacity.
///
/// The underlying data structures are lazily initialized. Because of the observation that,
/// in practice, hashtables tend to contain only a few elements, the initial capacity is
/// set very small (3 elements) unless the ctor with a collection is used.
///
/// The +/- 1 modifications in methods that add, check for containment, etc allow us to
/// distinguish a hash code of 0 from an uninitialized bucket. This saves us from having to
/// reset each bucket to -1 when resizing. See Contains, for example.
///
/// Set methods such as UnionWith, IntersectWith, ExceptWith, and SymmetricExceptWith modify
/// this set.
///
/// Some operations can perform faster if we can assume "other" contains unique elements
/// according to this equality comparer. The only times this is efficient to check is if
/// other is a hashset. Note that checking that it's a hashset alone doesn't suffice; we
/// also have to check that the hashset is using the same equality comparer. If other
/// has a different equality comparer, it will have unique elements according to its own
/// equality comparer, but not necessarily according to ours. Therefore, to go these
/// optimized routes we check that other is a hashset using the same equality comparer.
///
/// A FastHashSet with no elements has the properties of the empty set. (See IsSubset, etc. for
/// special empty set checks.)
///
/// A couple of methods have a special case if other is this (e.g. SymmetricExceptWith).
/// If we didn't have these checks, we could be iterating over the set and modifying at
/// the same time.
/// </summary>
/// <typeparam name="T"></typeparam>
[DebuggerTypeProxy(typeof(ICollectionDebugView<>))]
[DebuggerDisplay("Count = {Count}")]
[SuppressMessage("Microsoft.Naming", "CA1710:IdentifiersShouldHaveCorrectSuffix", Justification = "By design")]
[Serializable]
[System.Runtime.CompilerServices.TypeForwardedFrom("System.Core, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
public class FastHashSet<T> : ICollection<T>, ISet<T>, IReadOnlyCollection<T>, ISerializable, IDeserializationCallback
{
// store lower 31 bits of hash code
private const int Lower31BitMask = 0x7FFFFFFF;
// cutoff point, above which we won't do stackallocs. This corresponds to 100 integers.
private const int StackAllocThreshold = 100;
// when constructing a hashset from an existing collection, it may contain duplicates,
// so this is used as the max acceptable excess ratio of capacity to count. Note that
// this is only used on the ctor and not to automatically shrink if the hashset has, e.g,
// a lot of adds followed by removes. Users must explicitly shrink by calling TrimExcess.
// This is set to 3 because capacity is acceptable as 2x rounded up to nearest prime.
private const int ShrinkThreshold = 3;
// constants for serialization
private const string CapacityName = "Capacity"; // Do not rename (binary serialization)
private const string ElementsName = "Elements"; // Do not rename (binary serialization)
private const string ComparerName = "Comparer"; // Do not rename (binary serialization)
private const string VersionName = "Version"; // Do not rename (binary serialization)
private int[]? _buckets;
private Slot[] _slots = default!; // TODO-NULLABLE: This should be Slot[]?, but the resulting annotations causes GenPartialFacadeSource to blow up: error : Unable to cast object of type 'Microsoft.CodeAnalysis.CSharp.Syntax.CompilationUnitSyntax' to type 'Microsoft.CodeAnalysis.CSharp.Syntax.BaseTypeDeclarationSyntax'
private int _count;
private int _lastIndex;
private int _freeList;
private IEqualityComparer<T>? _comparer;
private int _version;
private SerializationInfo? _siInfo; // temporary variable needed during deserialization
#region Constructors
public FastHashSet()
: this((IEqualityComparer<T>?)null)
{ }
public FastHashSet(IEqualityComparer<T>? comparer)
{
if (comparer == EqualityComparer<T>.Default)
{
comparer = null;
}
_comparer = comparer;
_lastIndex = 0;
_count = 0;
_freeList = -1;
_version = 0;
}
public FastHashSet(int capacity)
: this(capacity, null)
{ }
public FastHashSet(IEnumerable<T> collection)
: this(collection, null)
{ }
/// <summary>
/// Implementation Notes:
/// Since resizes are relatively expensive (require rehashing), this attempts to minimize
/// the need to resize by setting the initial capacity based on size of collection.
/// </summary>
/// <param name="collection"></param>
/// <param name="comparer"></param>
public FastHashSet(IEnumerable<T> collection, IEqualityComparer<T>? comparer)
: this(comparer)
{
if (collection == null)
{
throw new ArgumentNullException(nameof(collection));
}
var otherAsHashSet = collection as FastHashSet<T>;
if (otherAsHashSet != null && AreEqualityComparersEqual(this, otherAsHashSet))
{
CopyFrom(otherAsHashSet);
}
else
{
// to avoid excess resizes, first set size based on collection's count. Collection
// may contain duplicates, so call TrimExcess if resulting hashset is larger than
// threshold
ICollection<T>? coll = collection as ICollection<T>;
int suggestedCapacity = coll == null ? 0 : coll.Count;
Initialize(suggestedCapacity);
UnionWith(collection);
if (_count > 0 && _slots.Length / _count > ShrinkThreshold)
{
TrimExcess();
}
}
}
protected FastHashSet(SerializationInfo info, StreamingContext context)
{
// We can't do anything with the keys and values until the entire graph has been
// deserialized and we have a reasonable estimate that GetHashCode is not going to
// fail. For the time being, we'll just cache this. The graph is not valid until
// OnDeserialization has been called.
_siInfo = info;
}
// Initializes the FastHashSet from another FastHashSet with the same element type and
// equality comparer.
private void CopyFrom(FastHashSet<T> source)
{
int count = source._count;
if (count == 0)
{
// As well as short-circuiting on the rest of the work done,
// this avoids errors from trying to access otherAsHashSet._buckets
// or otherAsHashSet._slots when they aren't initialized.
return;
}
int capacity = source._buckets!.Length;
int threshold = HashHelpers.ExpandPrime(count + 1);
if (threshold >= capacity)
{
_buckets = (int[])source._buckets.Clone();
_slots = (Slot[])source._slots.Clone();
_lastIndex = source._lastIndex;
_freeList = source._freeList;
}
else
{
int lastIndex = source._lastIndex;
Slot[] slots = source._slots;
Initialize(count);
int index = 0;
for (int i = 0; i < lastIndex; ++i)
{
int hashCode = slots[i].hashCode;
if (hashCode >= 0)
{
AddValue(index, hashCode, slots[i].value);
++index;
}
}
Debug.Assert(index == count);
_lastIndex = index;
}
_count = count;
}
public FastHashSet(int capacity, IEqualityComparer<T>? comparer)
: this(comparer)
{
if (capacity < 0)
{
throw new ArgumentOutOfRangeException(nameof(capacity));
}
if (capacity > 0)
{
Initialize(capacity);
}
}
#endregion
#region ICollection<T> methods
/// <summary>
/// Add item to this hashset. This is the explicit implementation of the <see cref="ICollection{T}"/>
/// interface. The other Add method returns bool indicating whether item was added.
/// </summary>
/// <param name="item">item to add</param>
void ICollection<T>.Add(T item)
{
AddIfNotPresent(item);
}
/// <summary>
/// Remove all items from this set. This clears the elements but not the underlying
/// buckets and slots array. Follow this call by TrimExcess to release these.
/// </summary>
public void Clear()
{
if (_lastIndex > 0)
{
Debug.Assert(_buckets != null, "_buckets was null but _lastIndex > 0");
// clear the elements so that the gc can reclaim the references.
// clear only up to _lastIndex for _slots
Array.Clear(_slots, 0, _lastIndex);
Array.Clear(_buckets, 0, _buckets.Length);
_lastIndex = 0;
_count = 0;
_freeList = -1;
}
_version++;
}
/// <summary>
/// Checks if this hashset contains the item
/// </summary>
/// <param name="item">item to check for containment</param>
/// <returns>true if item contained; false if not</returns>
public bool Contains(T item)
{
int[]? buckets = _buckets;
if (buckets != null)
{
int collisionCount = 0;
Slot[] slots = _slots;
IEqualityComparer<T>? comparer = _comparer;
if (comparer == null)
{
int hashCode = item == null ? 0 : InternalGetHashCode(item.GetHashCode());
if (default(T)! != null) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
{
// see note at "FastHashSet" level describing why "- 1" appears in for loop
for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next)
{
if (slots[i].hashCode == hashCode && EqualityComparer<T>.Default.Equals(slots[i].value, item))
{
return true;
}
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
}
}
else
{
// Object type: Shared Generic, EqualityComparer<TValue>.Default won't devirtualize
// https://github.com/dotnet/coreclr/issues/17273
// So cache in a local rather than get EqualityComparer per loop iteration
EqualityComparer<T> defaultComparer = EqualityComparer<T>.Default;
// see note at "FastHashSet" level describing why "- 1" appears in for loop
for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next)
{
if (slots[i].hashCode == hashCode && defaultComparer.Equals(slots[i].value, item))
{
return true;
}
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
}
}
}
else
{
int hashCode = item == null ? 0 : InternalGetHashCode(comparer.GetHashCode(item));
// see note at "FastHashSet" level describing why "- 1" appears in for loop
for (int i = buckets[hashCode % buckets.Length] - 1; i >= 0; i = slots[i].next)
{
if (slots[i].hashCode == hashCode && comparer.Equals(slots[i].value, item))
{
return true;
}
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException(SR.InvalidOperation_ConcurrentOperationsNotSupported);
}
collisionCount++;
}
}
}
// either _buckets is null or wasn't found
return false;
}
/// <summary>
/// Copy items in this hashset to array, starting at arrayIndex
/// </summary>
/// <param name="array">array to add items to</param>
/// <param name="arrayIndex">index to start at</param>
public void CopyTo(T[] array, int arrayIndex)
{
CopyTo(array, arrayIndex, _count);
}
/// <summary>
/// Remove item from this hashset
/// </summary>
/// <param name="item">item to remove</param>
/// <returns>true if removed; false if not (i.e. if the item wasn't in the FastHashSet)</returns>
public bool Remove(T item)
{
int hashCode;
int bucket;
int last = -1;
int collisionCount = 0;
int i;
Slot[] slots;
IEqualityComparer<T>? comparer = _comparer;
if (_buckets != null)
{
slots = _slots;
if (comparer == null)
{
hashCode = item == null ? 0 : InternalGetHashCode(item.GetHashCode());
bucket = hashCode % _buckets!.Length;
if (default(T)! != null) // TODO-NULLABLE: default(T) == null warning (https://github.com/dotnet/roslyn/issues/34757)
{
for (i = _buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
if (slots[i].hashCode == hashCode && EqualityComparer<T>.Default.Equals(slots[i].value, item))
{
goto ReturnFound;
}
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException("SR.InvalidOperation_ConcurrentOperationsNotSupported");
}
collisionCount++;
}
}
else
{
// Object type: Shared Generic, EqualityComparer<TValue>.Default won't devirtualize
// https://github.com/dotnet/coreclr/issues/17273
// So cache in a local rather than get EqualityComparer per loop iteration
EqualityComparer<T> defaultComparer = EqualityComparer<T>.Default;
for (i = _buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
if (slots[i].hashCode == hashCode && defaultComparer.Equals(slots[i].value, item))
{
goto ReturnFound;
}
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException("SR.InvalidOperation_ConcurrentOperationsNotSupported");
}
collisionCount++;
}
}
}
else
{
hashCode = item == null ? 0 : InternalGetHashCode(comparer.GetHashCode(item));
bucket = hashCode % _buckets!.Length;
for (i = _buckets[bucket] - 1; i >= 0; last = i, i = slots[i].next)
{
if (slots[i].hashCode == hashCode && EqualityComparer<T>.Default.Equals(slots[i].value, item))
{
goto ReturnFound;
}
if (collisionCount >= slots.Length)
{
// The chain of entries forms a loop, which means a concurrent update has happened.
throw new InvalidOperationException("SR.InvalidOperation_ConcurrentOperationsNotSupported");
}
collisionCount++;
}
}
}
// either _buckets is null or wasn't found
return false;
ReturnFound:
if (last < 0)
{
// first iteration; update buckets
_buckets[bucket] = slots[i].next + 1;
}
else
{
// subsequent iterations; update 'next' pointers
slots[last].next = slots[i].next;
}
slots[i].hashCode = -1;
if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
{
slots[i].value = default!;
}
slots[i].next = _freeList;
_count--;
_version++;
if (_count == 0)
{
_lastIndex = 0;
_freeList = -1;
}
else
{
_freeList = i;
}
return true;
}
/// <summary>
/// Number of elements in this hashset
/// </summary>
public int Count
{
get { return _count; }
}
/// <summary>
/// Whether this is readonly
/// </summary>
bool ICollection<T>.IsReadOnly
{
get { return false; }
}
#endregion
#region IEnumerable methods
public Enumerator GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return new Enumerator(this);
}
IEnumerator IEnumerable.GetEnumerator()
{
return new Enumerator(this);
}
#endregion
#region ISerializable methods
public virtual void GetObjectData(SerializationInfo info, StreamingContext context)
{
if (info == null)
{
throw new ArgumentNullException(nameof(info));
}
info.AddValue(VersionName, _version); // need to serialize version to avoid problems with serializing while enumerating
info.AddValue(ComparerName, _comparer ?? EqualityComparer<T>.Default, typeof(IEqualityComparer<T>));
info.AddValue(CapacityName, _buckets == null ? 0 : _buckets.Length);
if (_buckets != null)
{
T[] array = new T[_count];
CopyTo(array);
info.AddValue(ElementsName, array, typeof(T[]));
}
}
#endregion
#region IDeserializationCallback methods
public virtual void OnDeserialization(object? sender)
{
if (_siInfo == null)
{
// It might be necessary to call OnDeserialization from a container if the
// container object also implements OnDeserialization. We can return immediately
// if this function is called twice. Note we set _siInfo to null at the end of this method.
return;
}
int capacity = _siInfo.GetInt32(CapacityName);
_comparer = (IEqualityComparer<T>)_siInfo.GetValue(ComparerName, typeof(IEqualityComparer<T>))!;
_freeList = -1;
if (capacity != 0)
{
_buckets = new int[capacity];
_slots = new Slot[capacity];
T[]? array = (T[]?)_siInfo.GetValue(ElementsName, typeof(T[]));
if (array == null)
{
throw new SerializationException(SR.Serialization_MissingKeys);
}
// there are no resizes here because we already set capacity above
for (int i = 0; i < array.Length; i++)
{
AddIfNotPresent(array[i]);
}
}
else
{
_buckets = null;
}
_version = _siInfo.GetInt32(VersionName);
_siInfo = null;
}
#endregion
#region FastHashSet methods
/// <summary>
/// Add item to this FastHashSet. Returns bool indicating whether item was added (won't be
/// added if already present)
/// </summary>
/// <param name="item"></param>
/// <returns>true if added, false if already present</returns>
public bool Add(T item)
{
return AddIfNotPresent(item);
}
/// <summary>
/// Searches the set for a given value and returns the equal value it finds, if any.
/// </summary>
/// <param name="equalValue">The value to search for.</param>
/// <param name="actualValue">The value from the set that the search found, or the default value of <typeparamref name="T"/> when the search yielded no match.</param>
/// <returns>A value indicating whether the search was successful.</returns>
/// <remarks>
/// This can be useful when you want to reuse a previously stored reference instead of
/// a newly constructed one (so that more sharing of references can occur) or to look up
/// a value that has more complete data than the value you currently have, although their
/// comparer functions indicate they are equal.
/// </remarks>
public bool TryGetValue(T equalValue, [MaybeNullWhen(false)] out T actualValue)
{
if (_buckets != null)
{
int i = InternalIndexOf(equalValue);
if (i >= 0)
{
actualValue = _slots[i].value;
return true;
}
}
actualValue = default!;
return false;
}
/// <summary>
/// Take the union of this FastHashSet with other. Modifies this set.
///
/// Implementation note: GetSuggestedCapacity (to increase capacity in advance avoiding
/// multiple resizes ended up not being useful in practice; quickly gets to the
/// point where it's a wasteful check.
/// </summary>
/// <param name="other">enumerable with items to add</param>
public void UnionWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
foreach (T item in other)
{
AddIfNotPresent(item);
}
}
/// <summary>
/// Takes the intersection of this set with other. Modifies this set.
///
/// Implementation Notes:
/// We get better perf if other is a hashset using same equality comparer, because we
/// get constant contains check in other. Resulting cost is O(n1) to iterate over this.
///
/// If we can't go above route, iterate over the other and mark intersection by checking
/// contains in this. Then loop over and delete any unmarked elements. Total cost is n2+n1.
///
/// Attempts to return early based on counts alone, using the property that the
/// intersection of anything with the empty set is the empty set.
/// </summary>
/// <param name="other">enumerable with items to add </param>
public void IntersectWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// intersection of anything with empty set is empty set, so return if count is 0
if (_count == 0)
{
return;
}
// set intersecting with itself is the same set
if (other == this)
{
return;
}
// if other is empty, intersection is empty set; remove all elements and we're done
// can only figure this out if implements ICollection<T>. (IEnumerable<T> has no count)
ICollection<T>? otherAsCollection = other as ICollection<T>;
if (otherAsCollection != null)
{
if (otherAsCollection.Count == 0)
{
Clear();
return;
}
FastHashSet<T>? otherAsSet = other as FastHashSet<T>;
// faster if other is a hashset using same equality comparer; so check
// that other is a hashset using the same equality comparer.
if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
{
IntersectWithHashSetWithSameEC(otherAsSet);
return;
}
}
IntersectWithEnumerable(other);
}
/// <summary>
/// Remove items in other from this set. Modifies this set.
/// </summary>
/// <param name="other">enumerable with items to remove</param>
public void ExceptWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// this is already the empty set; return
if (_count == 0)
{
return;
}
// special case if other is this; a set minus itself is the empty set
if (other == this)
{
Clear();
return;
}
// remove every element in other from this
foreach (T element in other)
{
Remove(element);
}
}
/// <summary>
/// Takes symmetric difference (XOR) with other and this set. Modifies this set.
/// </summary>
/// <param name="other">enumerable with items to XOR</param>
public void SymmetricExceptWith(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// if set is empty, then symmetric difference is other
if (_count == 0)
{
UnionWith(other);
return;
}
// special case this; the symmetric difference of a set with itself is the empty set
if (other == this)
{
Clear();
return;
}
FastHashSet<T>? otherAsSet = other as FastHashSet<T>;
// If other is a FastHashSet, it has unique elements according to its equality comparer,
// but if they're using different equality comparers, then assumption of uniqueness
// will fail. So first check if other is a hashset using the same equality comparer;
// symmetric except is a lot faster and avoids bit array allocations if we can assume
// uniqueness
if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
{
SymmetricExceptWithUniqueHashSet(otherAsSet);
}
else
{
SymmetricExceptWithEnumerable(other);
}
}
/// <summary>
/// Checks if this is a subset of other.
///
/// Implementation Notes:
/// The following properties are used up-front to avoid element-wise checks:
/// 1. If this is the empty set, then it's a subset of anything, including the empty set
/// 2. If other has unique elements according to this equality comparer, and this has more
/// elements than other, then it can't be a subset.
///
/// Furthermore, if other is a hashset using the same equality comparer, we can use a
/// faster element-wise check.
/// </summary>
/// <param name="other"></param>
/// <returns>true if this is a subset of other; false if not</returns>
public bool IsSubsetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// The empty set is a subset of any set
if (_count == 0)
{
return true;
}
// Set is always a subset of itself
if (other == this)
{
return true;
}
FastHashSet<T>? otherAsSet = other as FastHashSet<T>;
// faster if other has unique elements according to this equality comparer; so check
// that other is a hashset using the same equality comparer.
if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
{
// if this has more elements then it can't be a subset
if (_count > otherAsSet.Count)
{
return false;
}
// already checked that we're using same equality comparer. simply check that
// each element in this is contained in other.
return IsSubsetOfHashSetWithSameEC(otherAsSet);
}
else
{
ElementCount result = CheckUniqueAndUnfoundElements(other, false);
return (result.uniqueCount == _count && result.unfoundCount >= 0);
}
}
/// <summary>
/// Checks if this is a proper subset of other (i.e. strictly contained in)
///
/// Implementation Notes:
/// The following properties are used up-front to avoid element-wise checks:
/// 1. If this is the empty set, then it's a proper subset of a set that contains at least
/// one element, but it's not a proper subset of the empty set.
/// 2. If other has unique elements according to this equality comparer, and this has >=
/// the number of elements in other, then this can't be a proper subset.
///
/// Furthermore, if other is a hashset using the same equality comparer, we can use a
/// faster element-wise check.
/// </summary>
/// <param name="other"></param>
/// <returns>true if this is a proper subset of other; false if not</returns>
public bool IsProperSubsetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// no set is a proper subset of itself.
if (other == this)
{
return false;
}
ICollection<T>? otherAsCollection = other as ICollection<T>;
if (otherAsCollection != null)
{
// no set is a proper subset of an empty set
if (otherAsCollection.Count == 0)
{
return false;
}
// the empty set is a proper subset of anything but the empty set
if (_count == 0)
{
return otherAsCollection.Count > 0;
}
FastHashSet<T>? otherAsSet = other as FastHashSet<T>;
// faster if other is a hashset (and we're using same equality comparer)
if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
{
if (_count >= otherAsSet.Count)
{
return false;
}
// this has strictly less than number of items in other, so the following
// check suffices for proper subset.
return IsSubsetOfHashSetWithSameEC(otherAsSet);
}
}
ElementCount result = CheckUniqueAndUnfoundElements(other, false);
return (result.uniqueCount == _count && result.unfoundCount > 0);
}
/// <summary>
/// Checks if this is a superset of other
///
/// Implementation Notes:
/// The following properties are used up-front to avoid element-wise checks:
/// 1. If other has no elements (it's the empty set), then this is a superset, even if this
/// is also the empty set.
/// 2. If other has unique elements according to this equality comparer, and this has less
/// than the number of elements in other, then this can't be a superset
///
/// </summary>
/// <param name="other"></param>
/// <returns>true if this is a superset of other; false if not</returns>
public bool IsSupersetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// a set is always a superset of itself
if (other == this)
{
return true;
}
// try to fall out early based on counts
ICollection<T>? otherAsCollection = other as ICollection<T>;
if (otherAsCollection != null)
{
// if other is the empty set then this is a superset
if (otherAsCollection.Count == 0)
{
return true;
}
FastHashSet<T>? otherAsSet = other as FastHashSet<T>;
// try to compare based on counts alone if other is a hashset with
// same equality comparer
if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
{
if (otherAsSet.Count > _count)
{
return false;
}
}
}
return ContainsAllElements(other);
}
/// <summary>
/// Checks if this is a proper superset of other (i.e. other strictly contained in this)
///
/// Implementation Notes:
/// This is slightly more complicated than above because we have to keep track if there
/// was at least one element not contained in other.
///
/// The following properties are used up-front to avoid element-wise checks:
/// 1. If this is the empty set, then it can't be a proper superset of any set, even if
/// other is the empty set.
/// 2. If other is an empty set and this contains at least 1 element, then this is a proper
/// superset.
/// 3. If other has unique elements according to this equality comparer, and other's count
/// is greater than or equal to this count, then this can't be a proper superset
///
/// Furthermore, if other has unique elements according to this equality comparer, we can
/// use a faster element-wise check.
/// </summary>
/// <param name="other"></param>
/// <returns>true if this is a proper superset of other; false if not</returns>
public bool IsProperSupersetOf(IEnumerable<T> other)
{
if (other == null)
{
throw new ArgumentNullException(nameof(other));
}
// the empty set isn't a proper superset of any set.
if (_count == 0)
{
return false;
}
// a set is never a strict superset of itself
if (other == this)
{
return false;
}
ICollection<T>? otherAsCollection = other as ICollection<T>;
if (otherAsCollection != null)
{
// if other is the empty set then this is a superset
if (otherAsCollection.Count == 0)
{
// note that this has at least one element, based on above check
return true;
}
FastHashSet<T>? otherAsSet = other as FastHashSet<T>;
// faster if other is a hashset with the same equality comparer
if (otherAsSet != null && AreEqualityComparersEqual(this, otherAsSet))
{
if (otherAsSet.Count >= _count)
{
return false;
}
// now perform element check
return ContainsAllElements(otherAsSet);
}
}
// couldn't fall out in the above cases; do it the long way
ElementCount result = CheckUniqueAndUnfoundElements(other, true);
return (result.uniqueCount < _count && result.unfoundCount == 0);
}
/// <summary>
/// Checks if this set overlaps other (i.e. they share at least one item)
/// </summary>
/// <param name="other"></param>
/// <returns>true if these have at least one common element; false if disjoint</returns>
public bool Overlaps(IEnumerable<T> other)
{
if (other == null)