Skip to content

Commit

Permalink
Implement From factories start/length segment based
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Aug 21, 2023
1 parent fdf370a commit 66d5fa0
Show file tree
Hide file tree
Showing 15 changed files with 65 additions and 77 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,11 @@ internal Builder AddRange([AllowNull] params T[] items)
// TODO: Add the tests and make public
internal Builder AddRange([AllowNull] T[] items, int length)
{
var actualLength = items?.Length ?? default;
var itemsLength = items?.Length ?? default;

if (InnerAllocHelper.IsWithin(length, actualLength) is not true)
if (InnerAllocHelper.IsWithin(length, itemsLength) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(nameof(length), length, actualLength);
throw InnerExceptionFactory.StartSegmentIsNotWithinArray(nameof(length), length, itemsLength);
}

if (items is null || items.Length == default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,19 +21,19 @@ internal Builder AddRange(FlatArray<T> items)
// TODO: Add the tests and make public
internal Builder AddRange(FlatArray<T> items, int length)
=>
InternalAddRangeChecked(items, length);
InnerAddRangeChecked(items, length);

// TODO: Add the tests and make public
internal Builder AddRange(FlatArray<T>? items, int length)
=>
InternalAddRangeChecked(items.GetValueOrDefault(), length);
InnerAddRangeChecked(items.GetValueOrDefault(), length);

internal Builder InternalAddRangeChecked(
private Builder InnerAddRangeChecked(
FlatArray<T> items, int length, [CallerArgumentExpression(nameof(length))] string lengthParamName = "")
{
if (InnerAllocHelper.IsWithin(length, items.length) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(lengthParamName, length, items.length);
throw InnerExceptionFactory.StartSegmentIsNotWithinArray(lengthParamName, length, items.length);
}

if (items.length == default)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,21 +23,21 @@ internal Builder AddRange(ImmutableArray<T> items)
// TODO: Add the tests and make public
internal Builder AddRange(ImmutableArray<T> items, int length)
=>
InternalAddRangeChecked(items, length);
InnerAddRangeChecked(items, length);

// TODO: Add the tests and make public
internal Builder AddRange(ImmutableArray<T>? items, int length)
=>
InternalAddRangeChecked(items.GetValueOrDefault(), length);
InnerAddRangeChecked(items.GetValueOrDefault(), length);

internal Builder InternalAddRangeChecked(
private Builder InnerAddRangeChecked(
ImmutableArray<T> items, int length, [CallerArgumentExpression(nameof(length))] string lengthParamName = "")
{
var actualLength = items.IsDefault ? default : items.Length;
var itemsLength = items.IsDefault ? default : items.Length;

if (InnerAllocHelper.IsWithin(length, actualLength) is not true)
if (InnerAllocHelper.IsWithin(length, itemsLength) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(lengthParamName, length, actualLength);
throw InnerExceptionFactory.StartSegmentIsNotWithinArray(lengthParamName, length, itemsLength);
}

if (items.IsDefaultOrEmpty)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal static ArgumentOutOfRangeException CapacityOutOfRange_MustBeGreaterThan

internal static ArgumentOutOfRangeException CapacityOutOfRange_MustBeGreaterThanOrEqualToLength(int actualValue, int length)
=>
new(null, Invariant($"Capacity must be greater than or equal to the array length. Actual value was {actualValue}. Length was {length}."));
new(null, Invariant($"Capacity must be greater than or equal to the array length. Actual value was {actualValue}. Array length was {length}."));
}
}
}
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
using System.Diagnostics.CodeAnalysis;

namespace System;
namespace System;

partial struct FlatArray<T>
{
// TODO: Add the tests and make public
internal FlatArray<TOther>? TryCast<TOther>() where TOther : class?
internal FlatArray<TOther>? TryCast<TOther>() where TOther : class? // 'As' cast
{
if (InnerItems() is not TOther[] otherItems)
{
Expand All @@ -16,11 +14,4 @@ partial struct FlatArray<T>

return new(length, otherItemsNormalized, default);
}

// TODO: Add the tests and make public
[Obsolete("This method is not intended for use. Call TryCast instead.", error: true)]
[DoesNotReturn]
internal FlatArray<TOther>? As<TOther>() where TOther : class?
=>
throw new NotImplementedException();
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System;

Expand All @@ -10,18 +9,17 @@ public static FlatArray<T> From([AllowNull] params T[] source)
source is null ? default : InnerFactory.FromArray(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From([AllowNull] T[] source, int length)
internal static FlatArray<T> From([AllowNull] T[] source, int start, int length)
=>
InternalFromArrayChecked(source, length);
InternalFromArrayChecked(source, start, length);

internal static FlatArray<T> InternalFromArrayChecked(
[AllowNull] T[] source, int length, [CallerArgumentExpression(nameof(length))] string lengthParamName = "")
internal static FlatArray<T> InternalFromArrayChecked([AllowNull] T[] source, int start, int length)
{
var actualLength = source?.Length ?? default;
var sourceLength = source?.Length ?? default;

if (InnerAllocHelper.IsWithin(length, actualLength) is not true)
if (InnerAllocHelper.IsSegmentWithin(start, length, sourceLength) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(lengthParamName, length, actualLength);
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return source is null ? default : InnerFactory.FromArray(source, length);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,4 @@
using System.Runtime.CompilerServices;

namespace System;
namespace System;

partial struct FlatArray<T>
{
Expand All @@ -9,25 +7,24 @@ public static FlatArray<T> From(FlatArray<T> source)
InnerFactory.FromFlatArray(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From(FlatArray<T> source, int length)
internal static FlatArray<T> From(FlatArray<T> source, int start, int length)
=>
InternalFromFlatArrayChecked(source, length);
InternalFromFlatArrayChecked(source, start, length);

public static FlatArray<T> From(FlatArray<T>? source)
=>
InnerFactory.FromFlatArray(source.GetValueOrDefault());

// TODO: Add the tests and make public
internal static FlatArray<T> From(FlatArray<T>? source, int length)
internal static FlatArray<T> From(FlatArray<T>? source, int start, int length)
=>
InternalFromFlatArrayChecked(source.GetValueOrDefault(), length);
InternalFromFlatArrayChecked(source.GetValueOrDefault(), start, length);

internal static FlatArray<T> InternalFromFlatArrayChecked(
FlatArray<T> source, int length, [CallerArgumentExpression(nameof(length))] string lengthParamName = "")
internal static FlatArray<T> InternalFromFlatArrayChecked(FlatArray<T> source, int start, int length)
{
if (InnerAllocHelper.IsWithin(length, source.length) is not true)
if (InnerAllocHelper.IsSegmentWithin(start, length, source.length) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(lengthParamName, length, source.length);
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, source.length);
}

return InnerFactory.FromFlatArray(source, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,26 @@ public static FlatArray<T> From(ImmutableArray<T> source)
InnerFactory.FromImmutableArray(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From(ImmutableArray<T> source, int length)
internal static FlatArray<T> From(ImmutableArray<T> source, int start, int length)
=>
InternalFromImmutableArrayChecked(source, length);
InternalFromImmutableArrayChecked(source, start, length);

public static FlatArray<T> From(ImmutableArray<T>? source)
=>
InnerFactory.FromImmutableArray(source.GetValueOrDefault());

// TODO: Add the tests and make public
internal static FlatArray<T> From(ImmutableArray<T>? source, int length)
internal static FlatArray<T> From(ImmutableArray<T>? source, int start, int length)
=>
InternalFromImmutableArrayChecked(source.GetValueOrDefault(), length);
InternalFromImmutableArrayChecked(source.GetValueOrDefault(), start, length);

internal static FlatArray<T> InternalFromImmutableArrayChecked(ImmutableArray<T> source, int length)
internal static FlatArray<T> InternalFromImmutableArrayChecked(ImmutableArray<T> source, int start, int length)
{
var actualLength = source.IsDefault ? default : source.Length;
var sourceLength = source.IsDefault ? default : source.Length;

if (InnerAllocHelper.IsWithin(length, actualLength) is not true)
if (InnerAllocHelper.IsSegmentWithin(start, length, sourceLength) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(nameof(length), length, actualLength);
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return InnerFactory.FromImmutableArray(source, length);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System;

Expand All @@ -11,18 +10,17 @@ public static FlatArray<T> From([AllowNull] List<T> source)
source is null ? default : InnerFactory.FromList(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From([AllowNull] List<T> source, int length)
internal static FlatArray<T> From([AllowNull] List<T> source, int start, int length)
=>
InternalFromListChecked(source, length);
InternalFromListChecked(source, start, length);

internal static FlatArray<T> InternalFromListChecked(
[AllowNull] List<T> source, int length, [CallerArgumentExpression(nameof(length))] string lengthParamName = "")
internal static FlatArray<T> InternalFromListChecked([AllowNull] List<T> source, int start, int length)
{
var actualLength = source?.Count ?? default;
var sourceLength = source?.Count ?? default;

if (InnerAllocHelper.IsWithin(length, actualLength) is not true)
if (InnerAllocHelper.IsSegmentWithin(start, length, sourceLength) is not true)
{
throw InnerExceptionFactory.StartSegmentLengthOutOfArrayLength(lengthParamName, length, actualLength);
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return source is null ? default : InnerFactory.FromList(source, length);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ internal static bool IsWithin(int value, int threshold)
unchecked((uint)value) <= unchecked((uint)threshold);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static bool IsWithin(int start, int length, int threshold)
internal static bool IsSegmentWithin(int start, int length, int threshold)
=>
(ulong)unchecked((uint)start) + unchecked((uint)length) <= unchecked((uint)threshold);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,17 @@ partial struct FlatArray<T>
{
private static class InnerExceptionFactory
{
internal static ArgumentOutOfRangeException StartSegmentLengthOutOfArrayLength(string paramName, int actualValue, int length)
internal static ArgumentOutOfRangeException StartSegmentIsNotWithinArray(string paramName, int segmentLength, int arrayLength)
=>
new(paramName, Invariant($"Start segment length must be within the array length. Actual value was {actualValue}. Array length was {length}."));
new(paramName, Invariant($"Start segment length must be within the array length. Start segment length was {segmentLength}. Array length was {arrayLength}."));

internal static IndexOutOfRangeException IndexOutOfRange(int actualValue, int length)
internal static ArgumentOutOfRangeException SegmentIsNotWithinArray(int segmentStart, int segmentLength, int arrayLength)
=>
new(Invariant($"Index must be greater than or equal to zero and less than the array length. Actual value was {actualValue}. Length was {length}."));
new(null, Invariant($"Segment must be within array. Segment start was {segmentStart}. Segment length was {segmentLength}. Array length was {arrayLength}."));

internal static IndexOutOfRangeException IndexOutOfRange(int index, int length)
=>
new(Invariant($"Index must be greater than or equal to zero and less than the array length. Index was {index}. Array length was {length}."));

internal static InvalidOperationException EnumerationEitherNotStartedOrFinished()
=>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ public static FlatArray<T> From<T>([AllowNull] params T[] source)
FlatArray<T>.From(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From<T>([AllowNull] T[] source, int length)
internal static FlatArray<T> From<T>([AllowNull] T[] source, int start, int length)
=>
FlatArray<T>.InternalFromArrayChecked(source, length);
FlatArray<T>.InternalFromArrayChecked(source, start, length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,16 @@ public static FlatArray<T> From<T>(FlatArray<T> source)
FlatArray<T>.From(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From<T>(FlatArray<T> source, int length)
internal static FlatArray<T> From<T>(FlatArray<T> source, int start, int length)
=>
FlatArray<T>.InternalFromFlatArrayChecked(source, length);
FlatArray<T>.InternalFromFlatArrayChecked(source, start, length);

public static FlatArray<T> From<T>(FlatArray<T>? source)
=>
FlatArray<T>.From(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From<T>(FlatArray<T>? source, int length)
internal static FlatArray<T> From<T>(FlatArray<T>? source, int start, int length)
=>
FlatArray<T>.InternalFromFlatArrayChecked(source.GetValueOrDefault(), length);
FlatArray<T>.InternalFromFlatArrayChecked(source.GetValueOrDefault(), start, length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -9,16 +9,16 @@ public static FlatArray<T> From<T>(ImmutableArray<T> source)
FlatArray<T>.From(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From<T>(ImmutableArray<T> source, int length)
internal static FlatArray<T> From<T>(ImmutableArray<T> source, int start, int length)
=>
FlatArray<T>.InternalFromImmutableArrayChecked(source, length);
FlatArray<T>.InternalFromImmutableArrayChecked(source, start, length);

public static FlatArray<T> From<T>(ImmutableArray<T>? source)
=>
FlatArray<T>.From(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From<T>(ImmutableArray<T>? source, int length)
internal static FlatArray<T> From<T>(ImmutableArray<T>? source, int start, int length)
=>
FlatArray<T>.InternalFromImmutableArrayChecked(source.GetValueOrDefault(), length);
FlatArray<T>.InternalFromImmutableArrayChecked(source.GetValueOrDefault(), start, length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ public static FlatArray<T> From<T>([AllowNull] List<T> source)
FlatArray<T>.From(source);

// TODO: Add the tests and make public
internal static FlatArray<T> From<T>([AllowNull] List<T> source, int length)
internal static FlatArray<T> From<T>([AllowNull] List<T> source, int start, int length)
=>
FlatArray<T>.InternalFromListChecked(source, length);
FlatArray<T>.InternalFromListChecked(source, start, length);
}

0 comments on commit 66d5fa0

Please sign in to comment.