Skip to content

Commit 77e8f89

Browse files
committed
update: added bulk inserts for set and queue
1 parent e162dc1 commit 77e8f89

File tree

3 files changed

+43
-14
lines changed

3 files changed

+43
-14
lines changed

src/SwiftCollections/Collections/SwiftHashSet.cs

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -172,6 +172,26 @@ public bool Add(T item)
172172

173173
void ICollection<T>.Add(T item) => Add(item);
174174

175+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
176+
public void AddRange(IEnumerable<T> items)
177+
{
178+
if (items == null) ThrowHelper.ThrowArgumentNullException(nameof(items));
179+
180+
if (items is ICollection<T> collection)
181+
{
182+
EnsureCapacity(collection.Count);
183+
184+
foreach (T item in collection)
185+
if (item != null) InsertIfNotExists(item);
186+
187+
return;
188+
}
189+
190+
// Fallback for non-ICollection, adding each item individually
191+
foreach (T item in items)
192+
if (item != null) Add(item);
193+
}
194+
175195
/// <summary>
176196
/// Adds the specified element to the set if it's not already present.
177197
/// </summary>
@@ -410,7 +430,7 @@ private void CalculateAdaptiveResizeFactors(int newSize)
410430
[MethodImpl(MethodImplOptions.AggressiveInlining)]
411431
private void Initialize(int capacity)
412432
{
413-
int size = capacity < DefaultCapacity ? DefaultCapacity: HashTools.NextPowerOfTwo(capacity);
433+
int size = capacity < DefaultCapacity ? DefaultCapacity : HashTools.NextPowerOfTwo(capacity);
414434
_entries = new Entry[size];
415435
_entryMask = size - 1;
416436

@@ -546,7 +566,7 @@ private int FindEntry(T item)
546566
return -1;
547567
if (entry.IsUsed && entry.HashCode == hashCode && _comparer.Equals(entry.Value, item))
548568
return entryIndex; // Match found
549-
569+
550570
// Perform quadratic probing to see if maybe the entry was shifted.
551571
step++;
552572
entryIndex = (entryIndex + step * step) & _entryMask;

src/SwiftCollections/Collections/SwiftList.cs

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ public SwiftList(int capacity)
7676
{
7777
capacity = HashTools.NextPowerOfTwo(capacity <= DefaultCapacity ? DefaultCapacity : capacity);
7878
_innerArray = new T[capacity];
79-
}
79+
}
8080
}
8181

8282
/// <summary>
@@ -213,12 +213,13 @@ public void AddRange(IEnumerable<T> items)
213213
collection.CopyTo(_innerArray, _count);
214214
_count += collection.Count;
215215
_version++;
216+
217+
return;
216218
}
217-
else
218-
{
219-
foreach (var item in items)
220-
Add(item); // Fallback for non-ICollection, adding each item individually
221-
}
219+
220+
// Fallback for non-ICollection, adding each item individually
221+
foreach (T item in items)
222+
Add(item);
222223
}
223224

224225
/// <summary>
@@ -485,7 +486,7 @@ bool IList.Contains(object value)
485486
/// </summary>
486487
public void CopyTo(SwiftList<T> target)
487488
{
488-
if(_count + 1 > target._innerArray.Length)
489+
if (_count + 1 > target._innerArray.Length)
489490
target.Resize(target._innerArray.Length * 2);
490491
Array.Copy(_innerArray, 0, target._innerArray, 0, _count);
491492
target._count = _count;

src/SwiftCollections/Collections/SwiftQueue.cs

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,10 +13,6 @@ namespace SwiftCollections
1313
/// Aggressive inlining and optimized exception handling further reduce overhead, making SwiftQueue outperform traditional queues,
1414
/// especially in scenarios with high-frequency additions and removals.
1515
/// </para>
16-
/// <para>
17-
/// This implementation is optimized for performance and does not perform versioning checks.
18-
/// Modifying the queue during enumeration may result in undefined behavior.
19-
/// </para>
2016
/// </summary>
2117
/// <typeparam name="T">Specifies the type of elements in the queue.</typeparam>
2218
[Serializable]
@@ -158,7 +154,7 @@ public SwiftQueue(IEnumerable<T> items)
158154
#region Collection Management
159155

160156
/// <inheritdoc/>
161-
public void Add(T item) => Enqueue(item);
157+
void ICollection<T>.Add(T item) => Enqueue(item);
162158

163159
/// <summary>
164160
/// Adds an item to the end of the queue. Automatically resizes the queue if the capacity is exceeded.
@@ -175,6 +171,18 @@ public void Enqueue(T item)
175171
_version++;
176172
}
177173

174+
[MethodImpl(MethodImplOptions.AggressiveInlining)]
175+
public void EnqueueRange(IEnumerable<T> items)
176+
{
177+
if (items == null) ThrowHelper.ThrowArgumentNullException(nameof(items));
178+
179+
if (items is ICollection<T> collection)
180+
EnsureCapacity(collection.Count);
181+
182+
foreach (T item in items)
183+
if (item != null) Enqueue(item);
184+
}
185+
178186
/// <summary>
179187
/// Removes and returns the item at the front of the queue.
180188
/// Throws an InvalidOperationException if the queue is empty.

0 commit comments

Comments
 (0)