@@ -306,46 +306,45 @@ public void TrimCapacity()
306
306
}
307
307
308
308
/// <summary>
309
- /// Appends the first <paramref name="length"/> elements of <paramref name="src"/> to the end.
309
+ /// Appends the elements of <paramref name="src"/> to the end.
310
310
/// This method is thread safe related to calls to <see cref="M:CopyTo"/> (assuming those copy operations
311
311
/// are happening over ranges already added), but concurrent calls to
312
312
/// <see cref="M:AddRange"/> should not be attempted. Intended usage is that
313
313
/// one thread will call this method, while multiple threads may access
314
314
/// previously added ranges from <see cref="M:CopyTo"/>, concurrently with
315
315
/// this method or themselves.
316
316
/// </summary>
317
- public void AddRange ( T [ ] src , int length )
317
+ public void AddRange ( ReadOnlySpan < T > src )
318
318
{
319
- Contracts . Assert ( 0 <= length && length <= Utils . Size ( src ) ) ;
320
- if ( length == 0 )
319
+ if ( src . IsEmpty )
321
320
return ;
322
- Contracts . AssertValue ( src ) ;
323
321
324
322
int maMin ;
325
323
int miMin ;
326
324
int maMax ;
327
325
int miLim ;
328
326
LongMinToMajorMinorMin ( _length , out maMin , out miMin ) ;
329
- LongLimToMajorMaxMinorLim ( _length + length , out maMax , out miLim ) ;
327
+ LongLimToMajorMaxMinorLim ( _length + src . Length , out maMax , out miLim ) ;
330
328
331
329
Contracts . Assert ( maMin <= maMax ) ; // Could be violated if length == 0, but we already took care of this.
332
330
Utils . EnsureSize ( ref _entries , maMax + 1 , BlockSize ) ;
333
331
switch ( maMax - maMin )
334
332
{
335
333
case 0 :
336
334
// Spans only one subarray, most common case and simplest implementation.
337
- Contracts . Assert ( miLim - miMin == length ) ;
335
+ Contracts . Assert ( miLim - miMin == src . Length ) ;
338
336
Utils . EnsureSize ( ref _entries [ maMax ] , maMax >= FullAllocationBeyond ? BlockSize : miLim , BlockSize ) ;
339
- Array . Copy ( src , 0 , _entries [ maMax ] , miMin , length ) ;
337
+ src . CopyTo ( _entries [ maMax ] . AsSpan ( miMin ) ) ;
340
338
break ;
341
339
case 1 :
342
340
// Spans two subarrays.
343
- Contracts . Assert ( ( BlockSize - miMin ) + miLim == length ) ;
341
+ Contracts . Assert ( ( BlockSize - miMin ) + miLim == src . Length ) ;
344
342
Utils . EnsureSize ( ref _entries [ maMin ] , BlockSize , BlockSize ) ;
345
- Array . Copy ( src , 0 , _entries [ maMin ] , miMin , BlockSize - miMin ) ;
343
+ int firstSubArrayCapacity = BlockSize - miMin ;
344
+ src . Slice ( 0 , firstSubArrayCapacity ) . CopyTo ( _entries [ maMin ] . AsSpan ( miMin ) ) ;
346
345
Contracts . Assert ( _entries [ maMax ] == null ) ;
347
346
Utils . EnsureSize ( ref _entries [ maMax ] , maMax >= FullAllocationBeyond ? BlockSize : miLim , BlockSize ) ;
348
- Array . Copy ( src , BlockSize - miMin , _entries [ maMax ] , 0 , miLim ) ;
347
+ src . Slice ( firstSubArrayCapacity , miLim ) . CopyTo ( _entries [ maMax ] ) ;
349
348
break ;
350
349
default :
351
350
// Spans three or more subarrays. Very rare.
@@ -354,24 +353,24 @@ public void AddRange(T[] src, int length)
354
353
// Copy the first segment.
355
354
Utils . EnsureSize ( ref _entries [ maMin ] , BlockSize , BlockSize ) ;
356
355
int srcSoFar = BlockSize - miMin ;
357
- Array . Copy ( src , 0 , _entries [ maMin ] , miMin , srcSoFar ) ;
356
+ src . Slice ( 0 , srcSoFar ) . CopyTo ( _entries [ maMin ] . AsSpan ( miMin ) ) ;
358
357
// Copy the internal segments.
359
358
for ( int major = maMin + 1 ; major < maMax ; ++ major )
360
359
{
361
360
Contracts . Assert ( _entries [ major ] == null ) ;
362
361
_entries [ major ] = new T [ BlockSize ] ;
363
- Array . Copy ( src , srcSoFar , _entries [ major ] , 0 , BlockSize ) ;
362
+ src . Slice ( srcSoFar , BlockSize ) . CopyTo ( _entries [ major ] ) ;
364
363
srcSoFar += BlockSize ;
365
- Contracts . Assert ( srcSoFar < length ) ;
364
+ Contracts . Assert ( srcSoFar < src . Length ) ;
366
365
}
367
366
// Copy the last segment.
368
- Contracts . Assert ( length - srcSoFar == miLim ) ;
367
+ Contracts . Assert ( src . Length - srcSoFar == miLim ) ;
369
368
Contracts . Assert ( _entries [ maMax ] == null ) ;
370
369
Utils . EnsureSize ( ref _entries [ maMax ] , maMax >= FullAllocationBeyond ? BlockSize : miLim , BlockSize ) ;
371
- Array . Copy ( src , srcSoFar , _entries [ maMax ] , 0 , miLim ) ;
370
+ src . Slice ( srcSoFar , miLim ) . CopyTo ( _entries [ maMax ] ) ;
372
371
break ;
373
372
}
374
- _length += length ;
373
+ _length += src . Length ;
375
374
}
376
375
377
376
/// <summary>
0 commit comments