-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #76 from pfpack/feature/impl-chunk-method
Implement Chunk method
- Loading branch information
Showing
7 changed files
with
179 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
113 changes: 113 additions & 0 deletions
113
src/flatcollections-array/FlatArray.Tests/Tests.FlatArray.T/Functional/Chunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,113 @@ | ||
using System; | ||
using Xunit; | ||
using static PrimeFuncPack.UnitTest.TestData; | ||
|
||
namespace PrimeFuncPack.Core.Tests; | ||
|
||
partial class FlatArrayTest | ||
{ | ||
[Theory] | ||
[MemberData(nameof(Chunk_InvalidSizeTestData))] | ||
public static void Chunk_SizeIsInvalid_ExpectArgumentOutOfRangeException( | ||
FlatArray<string> source, int size) | ||
{ | ||
var ex = Assert.Throws<ArgumentOutOfRangeException>(Test); | ||
Assert.Equal("size", ex.ParamName); | ||
|
||
void Test() | ||
=> | ||
_ = source.Chunk(size); | ||
} | ||
|
||
[Theory] | ||
[MemberData(nameof(Chunk_ValidTestData))] | ||
public static void Chunk_SizeIsValid_ExpectCorrectChunks( | ||
FlatArray<string> source, int size, string[][]? expectedChunks) | ||
{ | ||
var actual = source.Chunk(size); | ||
actual.VerifyTruncatedState(expectedChunks); | ||
} | ||
|
||
public static TheoryData<FlatArray<string>, int> Chunk_InvalidSizeTestData | ||
=> | ||
new() | ||
{ | ||
{ | ||
default, | ||
0 | ||
}, | ||
{ | ||
default, | ||
-1 | ||
}, | ||
{ | ||
new[] { SomeString, AnotherString }.InitializeFlatArray(), | ||
0 | ||
}, | ||
{ | ||
new[] { SomeString, AnotherString }.InitializeFlatArray(), | ||
-1 | ||
} | ||
}; | ||
|
||
public static TheoryData<FlatArray<string>, int, string[][]?> Chunk_ValidTestData | ||
=> | ||
new() | ||
{ | ||
{ | ||
default, | ||
1, | ||
null | ||
}, | ||
{ | ||
new[] { EmptyString }.InitializeFlatArray(0), | ||
1, | ||
null | ||
}, | ||
{ | ||
default, | ||
3, | ||
null | ||
}, | ||
{ | ||
new[] { SomeString, EmptyString, WhiteSpaceString, AnotherString }.InitializeFlatArray(3), | ||
2, | ||
[ | ||
[SomeString, EmptyString], | ||
[WhiteSpaceString] | ||
] | ||
}, | ||
{ | ||
new[] { SomeString, EmptyString, WhiteSpaceString, AnotherString }.InitializeFlatArray(4), | ||
2, | ||
[ | ||
[SomeString, EmptyString], | ||
[WhiteSpaceString, AnotherString] | ||
] | ||
}, | ||
{ | ||
new[] { SomeString, EmptyString, WhiteSpaceString, AnotherString, TabString }.InitializeFlatArray(5), | ||
2, | ||
[ | ||
[SomeString, EmptyString], | ||
[WhiteSpaceString, AnotherString], | ||
[TabString] | ||
] | ||
}, | ||
{ | ||
new[] { ThreeWhiteSpacesString, AnotherString, SomeString, null! }.InitializeFlatArray(3), | ||
3, | ||
[ | ||
[ThreeWhiteSpacesString, AnotherString, SomeString] | ||
] | ||
}, | ||
{ | ||
new[] { ThreeWhiteSpacesString, AnotherString, SomeString, null! }.InitializeFlatArray(4), | ||
3, | ||
[ | ||
[ThreeWhiteSpacesString, AnotherString, SomeString], | ||
[null!] | ||
] | ||
} | ||
}; | ||
} |
36 changes: 36 additions & 0 deletions
36
src/flatcollections-array/FlatArray/FlatArray.T/Functional/Functional.Chunk.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,36 @@ | ||
namespace System; | ||
|
||
partial struct FlatArray<T> | ||
{ | ||
public FlatArray<FlatArray<T>> Chunk(int size) | ||
{ | ||
if (size < 1) | ||
{ | ||
throw InnerExceptionFactory.ChunkSizeOutsideBounds(nameof(size), size); | ||
} | ||
|
||
if (length == default) | ||
{ | ||
return default; | ||
} | ||
|
||
var chunks = length % size == default ? new FlatArray<T>[length / size] : new FlatArray<T>[length / size + 1]; | ||
var start = 0; | ||
|
||
for (var i = 0; i < chunks.Length; i++) | ||
{ | ||
var effectiveSize = length - start; | ||
if (effectiveSize > size) | ||
{ | ||
effectiveSize = size; | ||
} | ||
|
||
var chunkItems = InnerArrayHelper.CopySegment(items!, start, effectiveSize); | ||
chunks[i] = new(chunkItems, default); | ||
|
||
start += effectiveSize; | ||
} | ||
|
||
return new(chunks, default); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters