Skip to content
This repository was archived by the owner on Jan 23, 2023. It is now read-only.

Commit 61a60ad

Browse files
author
James Ko
committed
Use Array.Copy in ImmutableArray.Builder
1 parent 51f757c commit 61a60ad

File tree

2 files changed

+8
-25
lines changed

2 files changed

+8
-25
lines changed

src/System.Collections.Immutable/src/System/Collections/Immutable/ImmutableArray`1+Builder.cs

Lines changed: 7 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -269,11 +269,7 @@ public void AddRange(params T[] items)
269269
var offset = this.Count;
270270
this.Count += items.Length;
271271

272-
var nodes = _elements;
273-
for (int i = 0; i < items.Length; i++)
274-
{
275-
nodes[offset + i] = items[i];
276-
}
272+
Array.Copy(items, 0, _elements, offset, items.Length);
277273
}
278274

279275
/// <summary>
@@ -287,11 +283,7 @@ public void AddRange<TDerived>(TDerived[] items) where TDerived : T
287283
var offset = this.Count;
288284
this.Count += items.Length;
289285

290-
var nodes = _elements;
291-
for (int i = 0; i < items.Length; i++)
292-
{
293-
nodes[offset + i] = items[i];
294-
}
286+
Array.Copy(items, 0, _elements, offset, items.Length);
295287
}
296288

297289
/// <summary>
@@ -302,16 +294,12 @@ public void AddRange<TDerived>(TDerived[] items) where TDerived : T
302294
public void AddRange(T[] items, int length)
303295
{
304296
Requires.NotNull(items, "items");
305-
Requires.Range(length >= 0, "length");
297+
Requires.Range(length >= 0 && length <= items.Length, "length");
306298

307299
var offset = this.Count;
308300
this.Count += length;
309301

310-
var nodes = _elements;
311-
for (int i = 0; i < length; i++)
312-
{
313-
nodes[offset + i] = items[i];
314-
}
302+
Array.Copy(items, 0, _elements, offset, length);
315303
}
316304

317305
/// <summary>
@@ -420,14 +408,9 @@ public bool Contains(T item)
420408
/// </summary>
421409
public T[] ToArray()
422410
{
423-
var tmp = new T[this.Count];
424-
var elements = _elements;
425-
for (int i = 0; i < tmp.Length; i++)
426-
{
427-
tmp[i] = elements[i];
428-
}
429-
430-
return tmp;
411+
T[] result = new T[this.Count];
412+
Array.Copy(_elements, 0, result, 0, this.Count);
413+
return result;
431414
}
432415

433416
/// <summary>

src/System.Collections.Immutable/tests/ImmutableArrayBuilderTest.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -133,7 +133,7 @@ public void AddRangeImmutableArray()
133133
Assert.Throws<ArgumentNullException>(() => builder1.AddRange((int[])null));
134134
Assert.Throws<ArgumentNullException>(() => builder1.AddRange(null, 42));
135135
Assert.Throws<ArgumentOutOfRangeException>(() => builder1.AddRange(new int[0], -1));
136-
Assert.Throws<IndexOutOfRangeException>(() => builder1.AddRange(new int[0], 42));
136+
Assert.Throws<ArgumentOutOfRangeException>(() => builder1.AddRange(new int[0], 42));
137137

138138
Assert.Throws<ArgumentNullException>(() => builder1.AddRange((ImmutableArray<int>.Builder)null));
139139
Assert.Throws<ArgumentNullException>(() => builder1.AddRange((IEnumerable<int>)null));

0 commit comments

Comments
 (0)