Skip to content

Commit

Permalink
Improve dynamic building
Browse files Browse the repository at this point in the history
  • Loading branch information
andreise committed Aug 13, 2023
1 parent 11f5f0a commit 56a5e55
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Xunit;

namespace PrimeFuncPack.Core.Tests;
Expand All @@ -11,6 +13,22 @@ internal static void VerifyInnerState<T>(this FlatArray<T> actual, T[]? expected
Assert.StrictEqual(expectedLength, actualLength);

var actualItems = actual.GetFieldValue<T[]?>("items");
Assert.Equal(expectedItems, actualItems);
var effectiveItems = actualItems is null
? null
: new ReadOnlySpan<T>(actualItems, 0, actualLength).ToArray();

Assert.Equal(expectedItems, effectiveItems);

if (actualItems is not null)
{
var effectiveRest = new ReadOnlySpan<T>(
actualItems,
actualLength,
actualItems.Length - effectiveItems!.Length)
.ToArray();

var actualRestIsDefault = effectiveRest.All(item => EqualityComparer<T>.Default.Equals(item, default));
Assert.True(actualRestIsDefault);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,11 +30,11 @@ public void MoveToFlatArray_SourceIsNotDefault_ExpectArrayItemsAreBuilderItems()
{
const int length = 3;

var sourceItems = new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, ZeroIdRefType };
var sourceItems = new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, null };
var source = sourceItems.InitializeFlatArrayBuilder(length);

var actual = source.MoveToFlatArray();
var expectedItems = new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType, ZeroIdRefType };
var expectedItems = new[] { PlusFifteenIdRefType, null, MinusFifteenIdRefType };

actual.VerifyInnerState(expectedItems, 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 @@ -14,7 +12,7 @@ internal void TrimExcess()
var length = this.length;
var items = this.items;

if (length == items.Length)
if (items.Length == length)
{
return;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,12 +42,7 @@ public override FlatArray<T> Read(ref Utf8JsonReader reader, Type typeToConvert,
array[actualCount++] = InnerReadItem(ref reader, options);
}

if (actualCount < array.Length)
{
InnerArrayHelper.TruncateUnchecked(ref array, actualCount);
}

return new(array, default);
return new(actualCount, array);
}

[MethodImpl(MethodImplOptions.AggressiveInlining)]
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
namespace System;

partial struct FlatArray<T>
{
// TODO: Add the tests and make public
internal FlatArray<T> TrimExcess()
=>
items is null || items.Length == length ? this : new(InnerArrayHelper.Copy(items, length), default);
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ internal static FlatArray<T> FromIEnumerable(IEnumerable<T> source, int estimate
array[actualCount++] = enumerator.Current;
}

if (actualCount < array.Length)
{
InnerArrayHelper.TruncateUnchecked(ref array, actualCount);
}

return new(array, default);
return new(actualCount, array);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,7 @@ internal static FlatArray<T> FromIReadOnlyList(IReadOnlyList<T> source)
actualCount++;
}

if (actualCount < array.Length)
{
InnerArrayHelper.TruncateUnchecked(ref array, actualCount);
}

return new(array, default);
return new(actualCount, array);
}
}
}

0 comments on commit 56a5e55

Please sign in to comment.