Skip to content

Commit

Permalink
Code improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
pmosk committed Sep 20, 2024
1 parent c571b70 commit 831ef61
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 17 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,35 +6,31 @@ public FlatArray<FlatArray<T>> Chunk(int size)
{
if (size < 1)
{
throw InnerExceptionFactory.SizeOutsideBounds(nameof(size), size);
throw InnerExceptionFactory.ChunkSizeOutsideBounds(nameof(size), size);
}

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

var chunksLength = length / size;
if (length % size != default)
{
chunksLength++;
}
var chunks = length % size == default ? new FlatArray<T>[length / size] : new FlatArray<T>[length / size + 1];
var start = 0;

var chunks = new FlatArray<T>[chunksLength];
for (var i = 0; i < chunks.Length; i++)
{
var start = i * size;

var chunkLength = length - start;
if (chunkLength > size)
var effectiveSize = length - start;
if (effectiveSize > size)
{
chunkLength = size;
effectiveSize = size;
}

var chunkItems = InnerArrayHelper.CopySegment(items!, start, chunkLength);
chunks[i] = new(chunkLength, chunkItems);
var chunkItems = InnerArrayHelper.CopySegment(items!, start, effectiveSize);
chunks[i] = new(chunkItems, default);

start += effectiveSize;
}

return new(chunksLength, chunks);
return new(chunks, default);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,9 @@ internal static ArgumentOutOfRangeException SegmentOutsideBounds(int segmentStar
=>
new(null, Invariant($"Segment must be within the array bounds. Segment start was {segmentStart}. Segment length was {segmentLength}. Array length was {arrayLength}."));

internal static ArgumentOutOfRangeException SizeOutsideBounds(string paramName, int size)
internal static ArgumentOutOfRangeException ChunkSizeOutsideBounds(string paramName, int size)
=>
new(paramName, Invariant($"Size must be greater than 0 but was {size}."));
new(paramName, Invariant($"Chunk size must be greater than 0 but was {size}."));

internal static IndexOutOfRangeException IndexOutOfRange(int index, int length)
=>
Expand Down

0 comments on commit 831ef61

Please sign in to comment.