Skip to content

Commit

Permalink
Fix From segment factories
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Aug 21, 2023
1 parent adaa521 commit edd1771
Show file tree
Hide file tree
Showing 10 changed files with 29 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ private static Builder InnerFromArray(T[] source)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
private static Builder InnerFromFlatArray(FlatArray<T> source)
=>
// Work with the inner state of FlatArray here
source.length == default ? new() : new(InnerArrayHelper.Copy(source.items!, source.length), default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,6 @@ internal static FlatArray<T> InternalFromArrayChecked([AllowNull] T[] source, in
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return source is null ? default : InnerFactory.FromArray(source, length);
return source is null ? default : InnerFactory.FromArray(source, start, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,6 @@ internal static FlatArray<T> InternalFromFlatArrayChecked(FlatArray<T> source, i
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, source.length);
}

return InnerFactory.FromFlatArray(source, length);
return InnerFactory.FromFlatArray(source, start, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,6 @@ internal static FlatArray<T> InternalFromImmutableArrayChecked(ImmutableArray<T>
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return InnerFactory.FromImmutableArray(source, length);
return InnerFactory.FromImmutableArray(source, start, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,6 @@ internal static FlatArray<T> InternalFromListChecked([AllowNull] List<T> source,
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return source is null ? default : InnerFactory.FromList(source, length);
return source is null ? default : InnerFactory.FromList(source, start, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ internal static T[] Copy(T[] array)
=>
new ReadOnlySpan<T>(array).ToArray();

// The caller MUST ensure the length is within the source length
// The caller MUST ensure the length is within the array length
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T[] Copy(T[] array, int length)
{
Expand All @@ -24,6 +24,18 @@ internal static T[] Copy(T[] array, int length)
return sourceSpan.ToArray();
}

// The caller MUST ensure the segment is within the array
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T[] CopySegment(T[] array, int start, int length)
{
Debug.Assert(InnerAllocHelper.IsSegmentWithin(start, length, array.Length));

var sourceSpan = start == default && length == array.Length
? new ReadOnlySpan<T>(array)
: new ReadOnlySpan<T>(array, start, length);
return sourceSpan.ToArray();
}

// The caller MUST ensure the lengths are within the arrays actual lengths
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static T[] Concat(T[] array1, int length1, T[] array2, int length2)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal static FlatArray<T> FromArray(T[] source)
source.Length == default ? default : new(InnerArrayHelper.Copy(source), default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> FromArray(T[] source, int length)
internal static FlatArray<T> FromArray(T[] source, int start, int length)
{
Debug.Assert(length >= 0 && length <= source.Length);
Debug.Assert(InnerAllocHelper.IsSegmentWithin(start, length, source.Length));

return length == default ? default : new(InnerArrayHelper.Copy(source, length), default);
return length == default ? default : new(InnerArrayHelper.CopySegment(source, start, length), default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,11 @@ internal static FlatArray<T> FromFlatArray(FlatArray<T> source)
source.length == default ? default : new(InnerArrayHelper.Copy(source.items!, source.length), default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> FromFlatArray(FlatArray<T> source, int length)
internal static FlatArray<T> FromFlatArray(FlatArray<T> source, int start, int length)
{
Debug.Assert(length >= 0 && length <= source.length);
Debug.Assert(InnerAllocHelper.IsSegmentWithin(start, length, source.length));

return length == default ? default : new(InnerArrayHelper.Copy(source.items!, length), default);
return length == default ? default : new(InnerArrayHelper.CopySegment(source.items!, start, length), default);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ internal static FlatArray<T> FromImmutableArray(ImmutableArray<T> source)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> FromImmutableArray(ImmutableArray<T> source, int length)
internal static FlatArray<T> FromImmutableArray(ImmutableArray<T> source, int start, int length)
{
//Debug.Assert(length >= 0 && length <= source.Length);
Debug.Assert(InnerAllocHelper.IsSegmentWithin(start, length, source.IsDefault ? default : source.Length));

if (source.IsDefaultOrEmpty)
{
return default;
}

var array = new T[length];
source.CopyTo(0, array, 0, length);
source.CopyTo(start, array, 0, length);

return new(array, default);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,17 +24,17 @@ internal static FlatArray<T> FromList(List<T> source)
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> FromList(List<T> source, int length)
internal static FlatArray<T> FromList(List<T> source, int start, int length)
{
Debug.Assert(length >= 0 && length <= source.Count);
Debug.Assert(InnerAllocHelper.IsSegmentWithin(start, length, source.Count));

if (length == default)
{
return default;
}

var array = new T[length];
source.CopyTo(0, array, 0, length);
source.CopyTo(start, array, 0, length);

return new(array, default);
}
Expand Down

0 comments on commit edd1771

Please sign in to comment.