Skip to content

Commit

Permalink
Add constructors / refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Aug 24, 2023
1 parent f4d01ed commit b2381d7
Show file tree
Hide file tree
Showing 31 changed files with 258 additions and 235 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ partial class Builder
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T>.Builder Empty<T>()
=>
new();
FlatArray<T>.Builder.Empty();

// TODO: Add the tests and make public
[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
namespace System;
using System.Runtime.CompilerServices;

namespace System;

partial struct FlatArray<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlySpan<T> AsSpan()
=>
InnerAsSpan();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public ReadOnlyMemory<T> AsMemory()
=>
InnerAsMemory();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System.Runtime.CompilerServices;

namespace System;

partial struct FlatArray<T>
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ReadOnlySpan<T>(FlatArray<T> flatArray)
=>
flatArray.InnerAsSpan();

[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static implicit operator ReadOnlyMemory<T>(FlatArray<T> flatArray)
=>
flatArray.InnerAsMemory();
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ partial struct FlatArray<T>
{
public FlatArray<T> Clone()
=>
InnerFactory.FromFlatArray(this);
new(this);
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using System.Diagnostics.CodeAnalysis;
using System.Collections.Immutable;
using System.Diagnostics.CodeAnalysis;
using System.Runtime.CompilerServices;

namespace System;
Expand All @@ -18,6 +19,72 @@ public FlatArray([AllowNull] params T[] source)
items = InnerArrayHelper.Copy(source);
}

// TODO: Add the tests and make public
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal FlatArray(FlatArray<T> source)
{
if (source.length == default)
{
this = default;
return;
}

length = source.length;
items = InnerArrayHelper.Copy(source.items!, source.length);
}

// TODO: Add the tests and make public
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal FlatArray(FlatArray<T>? source)
{
var sourceValue = source.GetValueOrDefault();

if (sourceValue.length == default)
{
this = default;
return;
}

length = sourceValue.length;
items = InnerArrayHelper.Copy(sourceValue.items!, sourceValue.length);
}

// TODO: Add the tests and make public
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal FlatArray(ImmutableArray<T> source)
{
if (source.IsDefaultOrEmpty)
{
this = default;
return;
}

var items = new T[source.Length];
source.CopyTo(items);

length = source.Length;
this.items = items;
}

// TODO: Add the tests and make public
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal FlatArray(ImmutableArray<T>? source)
{
var sourceValue = source.GetValueOrDefault();

if (sourceValue.IsDefaultOrEmpty)
{
this = default;
return;
}

var items = new T[sourceValue.Length];
sourceValue.CopyTo(items);

length = sourceValue.Length;
this.items = items;
}

// TODO: Add the tests and make public
[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal FlatArray(ReadOnlySpan<T> source)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,23 +7,26 @@ partial struct FlatArray<T>
{
public static FlatArray<T> From([AllowNull] params T[] source)
=>
source is null ? default : InnerFactory.FromArray(source);
new(source);

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> InternalFromArrayChecked([AllowNull] T[] source, int start, int length)
{
var sourceLength = source?.Length ?? default;
InnerValidateRange();

if (InnerAllocHelper.IsSegmentWithinLength(start, length, sourceLength) is not true)
return source is null || length == default
? default
: new(InnerArrayHelper.CopySegment(source, start, length), default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void InnerValidateRange()
{
var sourceLength = source?.Length ?? default;
if (InnerAllocHelper.IsSegmentWithinLength(start, length, sourceLength))
{
return;
}
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return source is null ? default : InnerFactory.FromArray(source, start, length);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,30 +6,34 @@ partial struct FlatArray<T>
{
public static FlatArray<T> From(FlatArray<T> source)
=>
InnerFactory.FromFlatArray(source);
new(source);

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

return length == default
? default
: new(InnerArrayHelper.CopySegment(source.items!, start, length), default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void InnerValidateRange()
{
if (InnerAllocHelper.IsSegmentWithinLength(start, length, source.length))
{
return;
}
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, source.length);
}
}

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

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> InternalFromFlatArrayChecked(FlatArray<T> source, int start, int length)
{
if (InnerAllocHelper.IsSegmentWithinLength(start, length, source.length) is not true)
{
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, source.length);
}

return InnerFactory.FromFlatArray(source, start, length);
}
From(source.GetValueOrDefault(), start, length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,31 +12,35 @@ public static FlatArray<T> From([AllowNull] IEnumerable<T> source)
{
null => default,

T[] array
T[] items
=>
InnerFactory.FromArray(array),
items.Length == default ? default : new(InnerArrayHelper.Copy(items), default),

List<T> list
List<T> items
=>
InnerFactory.FromList(list),
InnerFactoryHelper.FromICollectionTrusted(items),

ImmutableArray<T> immutableArray
FlatList items
=>
InnerFactory.FromImmutableArray(immutableArray),
InnerFactoryHelper.FromICollectionTrusted(items),

ICollection<T> coll
ImmutableArray<T> items
=>
InnerFactory.FromICollection(coll),
new(items),

IReadOnlyList<T> list
ICollection<T> items
=>
InnerFactory.FromIReadOnlyList(list),
InnerFactoryHelper.FromICollection(items),

IReadOnlyCollection<T> coll
IReadOnlyList<T> items
=>
InnerFactory.FromIEnumerable(coll, coll.Count),
InnerFactoryHelper.FromIReadOnlyList(items),

IReadOnlyCollection<T> items
=>
InnerFactoryHelper.FromIEnumerable(items, items.Count),

_ =>
InnerFactory.FromIEnumerable(source)
InnerFactoryHelper.FromIEnumerable(source)
};
}
Original file line number Diff line number Diff line change
Expand Up @@ -7,32 +7,41 @@ partial struct FlatArray<T>
{
public static FlatArray<T> From(ImmutableArray<T> source)
=>
InnerFactory.FromImmutableArray(source);
new(source);

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

public static FlatArray<T> From(ImmutableArray<T>? source)
=>
InnerFactory.FromImmutableArray(source.GetValueOrDefault());
if (length == default)
{
return default;
}

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> InternalFromImmutableArrayChecked(ImmutableArray<T> source, int start, int length)
{
var sourceLength = source.IsDefault ? default : source.Length;
return new(array, default);

if (InnerAllocHelper.IsSegmentWithinLength(start, length, sourceLength) is not true)
[MethodImpl(MethodImplOptions.AggressiveInlining)]
void InnerValidateRange()
{
var sourceLength = source.IsDefault ? default : source.Length;
if (InnerAllocHelper.IsSegmentWithinLength(start, length, sourceLength))
{
return;
}
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}

return InnerFactory.FromImmutableArray(source, start, length);
}

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

// TODO: Add the tests and make public
internal static FlatArray<T> From(ImmutableArray<T>? source, int start, int length)
=>
From(source.GetValueOrDefault(), start, length);
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,32 @@ partial struct FlatArray<T>
{
public static FlatArray<T> From([AllowNull] List<T> source)
=>
source is null ? default : InnerFactory.FromList(source);
source is null ? default : InnerFactoryHelper.FromICollectionTrusted(source);

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

[MethodImpl(MethodImplOptions.AggressiveInlining)]
internal static FlatArray<T> InternalFromListChecked([AllowNull] List<T> source, int start, int length)
{
var sourceLength = source?.Count ?? default;
InnerValidateRange();

if (InnerAllocHelper.IsSegmentWithinLength(start, length, sourceLength) is not true)
if (source is null || length == default)
{
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
return default;
}

return source is null ? default : InnerFactory.FromList(source, start, length);
var array = new T[length];
source.CopyTo(start, array, 0, length);

return new(array, default);

[MethodImpl(MethodImplOptions.AggressiveInlining)]
void InnerValidateRange()
{
var sourceLength = source?.Count ?? default;
if (InnerAllocHelper.IsSegmentWithinLength(start, length, sourceLength))
{
return;
}
throw InnerExceptionFactory.SegmentIsNotWithinArray(start, length, sourceLength);
}
}
}
Loading

0 comments on commit b2381d7

Please sign in to comment.