generated from gemstone/gemtem
-
Notifications
You must be signed in to change notification settings - Fork 0
/
FileBackedHashSet.cs
596 lines (515 loc) · 27.2 KB
/
FileBackedHashSet.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
//******************************************************************************************************
// FileBackedHashSet.cs - Gbtc
//
// Copyright © 2014, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 12/02/2014 - Stephen C. Wills
// Generated original version of source code.
//
//******************************************************************************************************
using System;
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using Gemstone.GuidExtensions;
namespace Gemstone.IO.Collections;
/// <summary>
/// Represents a lookup table backed by a file, with very little memory overhead.
/// </summary>
/// <typeparam name="T">The type of the items in the lookup table.</typeparam>
public sealed class FileBackedHashSet<T> : ISet<T>, IDisposable where T : notnull
{
#region [ Members ]
// Fields
private readonly FileBackedLookupTable<T, object> m_lookupTable;
#endregion
#region [ Constructors ]
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
/// <remarks>
/// This constructor uses the default equality comparer for file backed lookup tables,
/// which is not the same as the default equality comparer for <typeparamref name="T"/>
/// objects. This is because the default implementation of <see cref="object.GetHashCode"/>
/// does not provide guarantees about consistency across platforms, or even implementations
/// of the CLR. Instead, the default equality comparer uses a byte-for-byte comparison to
/// determine equality between keys and a CRC-32 for its hash code implementation. This
/// means the performance of the hashing function is dependent on the performance of the
/// serialization function.
/// </remarks>
public FileBackedHashSet()
: this(Path.GetTempFileName(), (IEqualityComparer<T>?)default)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="filePath">The path to the file used to store the lookup table.</param>
/// <exception cref="ArgumentException"><paramref name="filePath"/> is a zero-length string, contains only white space, or contains one or more invalid characters as defined by <see cref="Path.GetInvalidPathChars"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null.</exception>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
/// <remarks>
/// This constructor uses the default equality comparer for file backed lookup tables,
/// which is not the same as the default equality comparer for <typeparamref name="T"/>
/// objects. This is because the default implementation of <see cref="object.GetHashCode"/>
/// does not provide guarantees about consistency across platforms, or even implementations
/// of the CLR. Instead, the default equality comparer uses a byte-for-byte comparison to
/// determine equality between keys and a CRC-32 for its hash code implementation. This
/// means the performance of the hashing function is dependent on the performance of the
/// serialization function.
/// </remarks>
public FileBackedHashSet(string filePath)
: this(filePath, (IEqualityComparer<T>?)default)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="comparer">The equality comparer used to compare items in the hash set.</param>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
public FileBackedHashSet(IEqualityComparer<T>? comparer)
: this(Path.GetTempFileName(), comparer)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="enumerable">The enumerable whose elements are copied to this hash set.</param>
/// <exception cref="ArgumentNullException"><paramref name="enumerable"/> is null.</exception>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
/// <remarks>
/// This constructor uses the default equality comparer for file backed lookup tables,
/// which is not the same as the default equality comparer for <typeparamref name="T"/>
/// objects. This is because the default implementation of <see cref="object.GetHashCode"/>
/// does not provide guarantees about consistency across platforms, or even implementations
/// of the CLR. Instead, the default equality comparer uses a byte-for-byte comparison to
/// determine equality between keys and a CRC-32 for its hash code implementation. This
/// means the performance of the hashing function is dependent on the performance of the
/// serialization function.
/// </remarks>
public FileBackedHashSet(IEnumerable<T> enumerable)
: this(enumerable, default!)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="filePath">The path to the file used to store the lookup table.</param>
/// <param name="comparer">The equality comparer used to compare items in the hash set.</param>
/// <exception cref="ArgumentException"><paramref name="filePath"/> is a zero-length string, contains only white space, or contains one or more invalid characters as defined by <see cref="Path.GetInvalidPathChars"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null.</exception>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
public FileBackedHashSet(string filePath, IEqualityComparer<T>? comparer)
{
m_lookupTable = new FileBackedLookupTable<T, object>(LookupTableType.HashSet, filePath, comparer);
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="filePath">The path to the file used to store the lookup table.</param>
/// <param name="enumerable">The enumerable whose elements are copied to this hash set.</param>
/// <exception cref="ArgumentException"><paramref name="filePath"/> is a zero-length string, contains only white space, or contains one or more invalid characters as defined by <see cref="Path.GetInvalidPathChars"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null or <paramref name="enumerable"/> is null.</exception>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
/// <remarks>
/// This constructor uses the default equality comparer for file backed lookup tables,
/// which is not the same as the default equality comparer for <typeparamref name="T"/>
/// objects. This is because the default implementation of <see cref="object.GetHashCode"/>
/// does not provide guarantees about consistency across platforms, or even implementations
/// of the CLR. Instead, the default equality comparer uses a byte-for-byte comparison to
/// determine equality between keys and a CRC-32 for its hash code implementation. This
/// means the performance of the hashing function is dependent on the performance of the
/// serialization function.
/// </remarks>
public FileBackedHashSet(string filePath, IEnumerable<T> enumerable)
: this(filePath, enumerable, default!)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="enumerable">The enumerable whose elements are copied to this hash set.</param>
/// <param name="comparer">The equality comparer used to compare items in the hash set.</param>
/// <exception cref="ArgumentNullException"><paramref name="enumerable"/> is null.</exception>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
public FileBackedHashSet(IEnumerable<T> enumerable, IEqualityComparer<T>? comparer)
: this(Path.GetTempFileName(), enumerable, comparer)
{
}
/// <summary>
/// Creates a new instance of the <see cref="FileBackedHashSet{T}"/> class.
/// </summary>
/// <param name="filePath">The path to the file used to store the lookup table.</param>
/// <param name="enumerable">The enumerable whose elements are copied to this hash set.</param>
/// <param name="comparer">The equality comparer used to compare items in the hash set.</param>
/// <exception cref="ArgumentException"><paramref name="filePath"/> is a zero-length string, contains only white space, or contains one or more invalid characters as defined by <see cref="Path.GetInvalidPathChars"/>.</exception>
/// <exception cref="ArgumentNullException"><paramref name="filePath"/> is null or <paramref name="enumerable"/> is null.</exception>
/// <exception cref="InvalidOperationException"><typeparamref name="T"/> cannot be serialized.</exception>
public FileBackedHashSet(string filePath, IEnumerable<T> enumerable, IEqualityComparer<T>? comparer)
{
m_lookupTable = new FileBackedLookupTable<T, object>(LookupTableType.HashSet, filePath, comparer);
foreach (T item in enumerable)
Add(item);
}
#endregion
#region [ Properties ]
/// <summary>
/// Gets or sets the path to the file backing this hash set.
/// </summary>
/// <exception cref="ArgumentException">FilePath is set and is a zero-length string, contains only white space, or contains one or more invalid characters as defined by <see cref="Path.GetInvalidPathChars"/>.</exception>
/// <remarks>
/// Changes to this property will cause the file to close if the file is already opened.
/// Data will not be automatically written from the old file to the new file.
/// </remarks>
public string FilePath
{
get => m_lookupTable.FilePath;
set => m_lookupTable.FilePath = value;
}
/// <summary>
/// Gets the number of elements contained in the <see cref="FileBackedHashSet{T}"/>.
/// </summary>
/// <returns>
/// The number of elements contained in the <see cref="FileBackedHashSet{T}"/>.
/// </returns>
public int Count => m_lookupTable.Count;
/// <summary>
/// Gets a value indicating whether the <see cref="FileBackedHashSet{T}"/> is read-only.
/// </summary>
/// <returns>
/// true if the <see cref="FileBackedHashSet{T}"/> is read-only; otherwise, false.
/// </returns>
public bool IsReadOnly => m_lookupTable.IsReadOnly;
/// <summary>
/// Gets the default signature used by the <see cref="FileBackedHashSet{T}"/>
/// if no user-defined signature is supplied.
/// </summary>
public byte[] DefaultSignature => new Guid(FileBackedLookupTable<T, object>.HashSetSignature).ToRfcBytes();
/// <summary>
/// Gets or sets the signature of the file backing the lookup table.
/// </summary>
/// <exception cref="ArgumentNullException">Attempt is made to set Signature to a null value.</exception>
/// <exception cref="ArgumentException">Attempt is made to set Signature to a value larger than the maximum allowed size.</exception>
/// <exception cref="NotSupportedException">Attempt is made to modify Signature of a read-only lookup table.</exception>
public byte[] Signature
{
get => m_lookupTable.Signature;
set => m_lookupTable.Signature = value;
}
/// <summary>
/// Gets or sets the size of the cache used
/// to store data from the file in memory.
/// </summary>
public long CacheSize
{
get => m_lookupTable.CacheSize;
set => m_lookupTable.CacheSize = value;
}
/// <summary>
/// Gets the number of operations that fragment the
/// lookup table that have occurred since the last
/// time the lookup table was compacted.
/// </summary>
/// <remarks>
/// This value is not stored in the file and may therefore
/// be inaccurate if the lookup table has not been compacted
/// since the last time it was opened.
/// </remarks>
public int FragmentationCount => m_lookupTable.FragmentationCount;
#endregion
#region [ Methods ]
/// <summary>
/// Opens the file backing this hash set.
/// </summary>
/// <exception cref="InvalidOperationException">File is already open.</exception>
public void Open() => m_lookupTable.Open();
/// <summary>
/// Opens the file backing this hash set in read-only mode.
/// </summary>
/// <exception cref="InvalidOperationException">File is already open.</exception>
public void OpenRead() => m_lookupTable.OpenRead();
/// <summary>
/// Adds an element to the current set and returns a value to indicate if the element was successfully added.
/// </summary>
/// <param name="item">The element to add to the set.</param>
/// <returns>
/// true if the element is added to the set; false if the element is already in the set.
/// </returns>
public bool Add(T item) => m_lookupTable.TryAdd(item, default!);
/// <summary>
/// Removes the first occurrence of a specific object from the <see cref="FileBackedHashSet{T}"/>.
/// </summary>
/// <param name="item">The object to remove from the <see cref="FileBackedHashSet{T}"/>.</param>
/// <returns>
/// true if <paramref name="item"/> was successfully removed from the <see cref="FileBackedHashSet{T}"/>; otherwise, false. This method also returns false if <paramref name="item"/> is not found in the original <see cref="FileBackedHashSet{T}"/>.
/// </returns>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public bool Remove(T item) => m_lookupTable.Remove(item);
/// <summary>
/// Removes all elements that match the conditions defined by the specified predicate from a <see cref="FileBackedHashSet{T}"/> collection.
/// </summary>
/// <param name="match">The <see cref="Predicate{T}"/> delegate that defines the conditions of the elements to remove.</param>
/// <returns>The number of elements that were removed from the <see cref="FileBackedHashSet{T}"/> collection.</returns>
/// <exception cref="ArgumentNullException">match is null</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public int RemoveWhere(Predicate<T> match)
{
if (match is null)
throw new ArgumentNullException(nameof(match));
int removedCount = 0;
m_lookupTable.UnmarkAll();
foreach (T item in this)
{
if (!match(item))
continue;
m_lookupTable.TryMark(item);
removedCount++;
}
m_lookupTable.RemoveMarked();
return removedCount;
}
/// <summary>
/// Determines whether the <see cref="FileBackedHashSet{T}"/> contains a specific value.
/// </summary>
/// <param name="item">The object to locate in the <see cref="FileBackedHashSet{T}"/>.</param>
/// <returns>
/// true if <paramref name="item"/> is found in the <see cref="FileBackedHashSet{T}"/>; otherwise, false.
/// </returns>
public bool Contains(T item) => m_lookupTable.ContainsKey(item);
/// <summary>
/// Modifies the current set so that it contains all elements that are present in either the current set or the specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public void UnionWith(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
foreach (T item in other)
Add(item);
}
/// <summary>
/// Modifies the current set so that it contains only elements that are also in a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public void IntersectWith(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
m_lookupTable.UnmarkAll();
foreach (T item in other)
m_lookupTable.TryMark(item);
m_lookupTable.RemoveUnmarked();
}
/// <summary>
/// Removes all elements in the specified collection from the current set.
/// </summary>
/// <param name="other">The collection of items to remove from the set.</param>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public void ExceptWith(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
foreach (T item in other)
m_lookupTable.Remove(item);
}
/// <summary>
/// Modifies the current set so that it contains only elements that are present
/// either in the current set or in the specified collection, but not both.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public void SymmetricExceptWith(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
m_lookupTable.UnmarkAll();
List<T> list = other.Where(item => !m_lookupTable.TryMark(item)).ToList();
m_lookupTable.RemoveMarked();
foreach (T item in list)
Add(item);
}
/// <summary>
/// Determines whether a set is a subset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>
/// true if the current set is a subset of <paramref name="other"/>; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public bool IsSubsetOf(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
m_lookupTable.UnmarkAll();
foreach (T item in other)
m_lookupTable.TryMark(item);
return m_lookupTable.AllMarked();
}
/// <summary>
/// Determines whether the current set is a superset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>
/// true if the current set is a superset of <paramref name="other"/>; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
public bool IsSupersetOf(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
return other.All(Contains);
}
/// <summary>
/// Determines whether the current set is a proper (strict) superset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set. </param>
/// <returns>
/// true if the current set is a proper superset of <paramref name="other"/>; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public bool IsProperSupersetOf(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
m_lookupTable.UnmarkAll();
if (other.Any(item => !m_lookupTable.TryMark(item)))
return false;
return !m_lookupTable.AllMarked();
}
/// <summary>
/// Determines whether the current set is a proper (strict) subset of a specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>
/// true if the current set is a proper subset of <paramref name="other"/>; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public bool IsProperSubsetOf(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
bool canBeProperSubset = false;
m_lookupTable.UnmarkAll();
foreach (T item in other)
{
if (!m_lookupTable.TryMark(item))
canBeProperSubset = true;
}
return canBeProperSubset && m_lookupTable.AllMarked();
}
/// <summary>
/// Determines whether the current set overlaps with the specified collection.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>
/// true if the current set and <paramref name="other"/> share at least one common element; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
public bool Overlaps(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
return other.Any(Contains);
}
/// <summary>
/// Determines whether the current set and the specified collection contain the same elements.
/// </summary>
/// <param name="other">The collection to compare to the current set.</param>
/// <returns>
/// true if the current set is equal to <paramref name="other"/>; otherwise, false.
/// </returns>
/// <exception cref="ArgumentNullException"><paramref name="other"/> is null.</exception>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public bool SetEquals(IEnumerable<T> other)
{
if (other is null)
throw new ArgumentNullException(nameof(other));
m_lookupTable.UnmarkAll();
return other.All(item => m_lookupTable.TryMark(item)) && m_lookupTable.AllMarked();
}
/// <summary>
/// Removes all items from the <see cref="FileBackedHashSet{T}"/>.
/// </summary>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
public void Clear() => m_lookupTable.Clear();
/// <summary>
/// Copies the elements of the <see cref="FileBackedHashSet{T}"/> to an <see cref="Array"/>, starting at a particular <see cref="Array"/> index.
/// </summary>
/// <param name="array">The one-dimensional <see cref="Array"/> that is the destination of the elements copied from <see cref="FileBackedHashSet{T}"/>. The <see cref="Array"/> must have zero-based indexing.</param>
/// <param name="arrayIndex">The zero-based index in <paramref name="array"/> at which copying begins.</param>
/// <exception cref="ArgumentNullException"><paramref name="array"/> is null.</exception>
/// <exception cref="ArgumentOutOfRangeException"><paramref name="arrayIndex"/> is less than 0.</exception>
/// <exception cref="ArgumentException">The number of elements in the source <see cref="FileBackedHashSet{T}"/> is greater than the available space from <paramref name="arrayIndex"/> to the end of the destination <paramref name="array"/>.</exception>
public void CopyTo(T[] array, int arrayIndex)
{
if (array is null)
throw new ArgumentNullException(nameof(array));
if (arrayIndex < 0)
throw new ArgumentOutOfRangeException(nameof(arrayIndex));
if (m_lookupTable.Count > array.Length - arrayIndex)
throw new ArgumentException("Not enough available space in array to copy elements from hash set");
foreach (T item in this)
array[arrayIndex++] = item;
}
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="IEnumerator{T}"/> that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>1</filterpriority>
public IEnumerator<T> GetEnumerator() => m_lookupTable.GetKeys().GetEnumerator();
/// <summary>
/// Defragments the item section of the hash set,
/// which gets fragmented after removing items.
/// </summary>
public void Compact() => m_lookupTable.Compact();
/// <summary>
/// Closes the file backing this hash set.
/// </summary>
public void Close() => m_lookupTable.Close();
/// <summary>
/// Releases all the resources used by the <see cref="FileBackedHashSet{T}"/> object.
/// </summary>
public void Dispose() => m_lookupTable.Dispose();
/// <summary>
/// Adds an item to the <see cref="FileBackedHashSet{T}"/>.
/// </summary>
/// <param name="item">The object to add to the <see cref="FileBackedHashSet{T}"/>.</param>
/// <exception cref="NotSupportedException">The <see cref="FileBackedHashSet{T}"/> is read-only.</exception>
// ReSharper disable once AssignNullToNotNullAttribute
void ICollection<T>.Add(T item) => Add(item);
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
/// <filterpriority>2</filterpriority>
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
#endregion
}