Compare commits

...

4 Commits

Author SHA1 Message Date
Tristan B. Velloza Kildaire 0fd770a452
Merge 515d85eae0 into b6106883da 2024-05-02 07:20:26 +00:00
Tristan B. Velloza Kildaire 515d85eae0 View (unittests)
- Added another test to test the last )untested) branch of the `opSlice(size_t, size_t)` method
2024-05-02 09:20:15 +02:00
Tristan B. Velloza Kildaire 1b1cb8f316 View
- Typo fix
2024-05-02 09:18:17 +02:00
Tristan B. Velloza Kildaire 64ba0baa8f View
- New, more efficient `opSlice(size_t, size_t)` implemented

View (unittests)

- Updated slicing tests
2024-05-02 09:18:01 +02:00
1 changed files with 66 additions and 1 deletions

View File

@ -756,9 +756,63 @@ if(isSector!(SectorType)())
public T[] opSlice(size_t start, size_t end)
{
// Invariant of start < end
if(!(start <= end))
{
// TODO: Check
throw new RangeError("Starting index must be smaller than or equal to ending index");
}
// Within range of "fake" size
else if(!((start < this.length) && (end <= this.length)))
{
throw new RangeError("start index or end index not under range");
// throw new ArrayIndexError(idx, this.length);
}
T[] collected;
size_t thunk;
foreach(SectorType sector; this.sectors)
{
// If the current sector contains
// both the starting AND ending
// indices
if(start-thunk < sector.opDollar() && end-thunk <= sector.opDollar())
{
return sector[start-thunk..end-thunk];
}
// If the current sector's starting
// index (only) is included
else if(start-thunk < sector.opDollar() && !(end-thunk <= sector.opDollar()))
{
collected ~= sector[start-thunk..$];
}
// If the current sector's ending
// index (only) is included
else if(!(start-thunk < sector.opDollar()) && end-thunk <= sector.opDollar())
{
collected ~= sector[0..end-thunk];
}
// If the current sector's entirety
// is to be included
else
{
collected ~= sector[];
}
thunk += sector.opDollar();
}
// FIXME: This is lazy, do a check for up to where
// and actually make THIS the real implementation
return this.opSlice()[start..end];
writeln(collected);
// TODO: Also if the range matches the bounds
// of a given range exactly then extract directly
return collected;
}
private static bool isArrayAppend(P)()
@ -895,6 +949,7 @@ unittest
assert(view[2] == 45);
assert(view[3] == 2);
assert(view[0..2] == [1,3]);
assert(view[0..4] == [1,3,45,2]);
// Update elements
view[0] = 71;
@ -947,4 +1002,14 @@ unittest
view.length = 0;
assert(view.length == 0);
assert(view[] == []);
}
unittest
{
View!(int) view;
view ~= 1;
view ~= [2,3,4];
view ~= 5;
assert(view[0..5] == [1,2,3,4,5]);
}