mirror of
https://github.com/beefytech/Beef.git
synced 2025-06-10 04:22:20 +02:00
Reversed ranges (ie: for (int i in (0 ..< count).Reversed)
)
This commit is contained in:
parent
465050b81d
commit
3fd6b5a111
2 changed files with 125 additions and 2 deletions
|
@ -79,10 +79,29 @@ namespace System
|
|||
return mEnd == mStart;
|
||||
}
|
||||
}
|
||||
|
||||
public ReverseEnumerator Reversed
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return ReverseEnumerator(mEnd - 1, mStart);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(int idx)
|
||||
{
|
||||
return (idx >= mStart) && (idx < mStart);
|
||||
return (idx >= mStart) && (idx < mEnd);
|
||||
}
|
||||
|
||||
public bool Contains(Range val)
|
||||
{
|
||||
return (val.[Friend]mStart >= mStart) && (val.[Friend]mEnd <= mEnd);
|
||||
}
|
||||
|
||||
public bool Contains(ClosedRange val)
|
||||
{
|
||||
return (val.[Friend]mStart >= mStart) && (val.[Friend]mEnd <= mEnd - 1);
|
||||
}
|
||||
|
||||
public void Clear() mut
|
||||
|
@ -135,7 +154,41 @@ namespace System
|
|||
return .Err;
|
||||
return ++mIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public struct ReverseEnumerator : IEnumerator<int>
|
||||
{
|
||||
private int mEnd;
|
||||
private int mIndex;
|
||||
|
||||
[Inline]
|
||||
public this(int start, int end)
|
||||
{
|
||||
mIndex = start + 1;
|
||||
mEnd = end;
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
}
|
||||
|
||||
public ref int Index
|
||||
{
|
||||
get mut
|
||||
{
|
||||
return ref mIndex;
|
||||
}
|
||||
}
|
||||
|
||||
public int End => mEnd;
|
||||
|
||||
[Inline]
|
||||
public Result<int> GetNext() mut
|
||||
{
|
||||
if (mIndex <= mEnd)
|
||||
return .Err;
|
||||
return --mIndex;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -210,10 +263,29 @@ namespace System
|
|||
return mEnd == mStart;
|
||||
}
|
||||
}
|
||||
|
||||
public Range.ReverseEnumerator Reversed
|
||||
{
|
||||
[Inline]
|
||||
get
|
||||
{
|
||||
return Range.ReverseEnumerator(mEnd, mStart);
|
||||
}
|
||||
}
|
||||
|
||||
public bool Contains(int idx)
|
||||
{
|
||||
return (idx >= mStart) && (idx < mStart);
|
||||
return (idx >= mStart) && (idx <= mEnd);
|
||||
}
|
||||
|
||||
public bool Contains(Range val)
|
||||
{
|
||||
return (val.[Friend]mStart >= mStart) && (val.[Friend]mEnd - 1 <= mEnd);
|
||||
}
|
||||
|
||||
public bool Contains(ClosedRange val)
|
||||
{
|
||||
return (val.[Friend]mStart >= mStart) && (val.[Friend]mEnd <= mEnd);
|
||||
}
|
||||
|
||||
public void Clear() mut
|
||||
|
|
|
@ -2,6 +2,7 @@
|
|||
|
||||
using System;
|
||||
using System.Collections;
|
||||
using System.Diagnostics;
|
||||
|
||||
namespace Tests
|
||||
{
|
||||
|
@ -75,13 +76,63 @@ namespace Tests
|
|||
|
||||
int total = 0;
|
||||
for (int i in 1..<10)
|
||||
{
|
||||
if (i == 5)
|
||||
Test.Assert(total == 1+2+3+4);
|
||||
total += i;
|
||||
}
|
||||
Test.Assert(total == 1+2+3+4+5+6+7+8+9);
|
||||
|
||||
total = 0;
|
||||
for (int i in 1...10)
|
||||
{
|
||||
if (i == 5)
|
||||
Test.Assert(total == 1+2+3+4);
|
||||
total += i;
|
||||
}
|
||||
Test.Assert(total == 1+2+3+4+5+6+7+8+9+10);
|
||||
|
||||
total = 0;
|
||||
for (int i in (1..<10).Reversed)
|
||||
{
|
||||
if (i == 5)
|
||||
Test.Assert(total == 9+8+7+6);
|
||||
total += i;
|
||||
|
||||
}
|
||||
Test.Assert(total == 9+8+7+6+5+4+3+2+1);
|
||||
|
||||
total = 0;
|
||||
for (int i in (1...10).Reversed)
|
||||
{
|
||||
if (i == 5)
|
||||
Test.Assert(total == 10+9+8+7+6);
|
||||
total += i;
|
||||
|
||||
}
|
||||
Test.Assert(total == 10+9+8+7+6+5+4+3+2+1);
|
||||
|
||||
Test.Assert(!(1...3).Contains(0));
|
||||
Test.Assert((1...3).Contains(1));
|
||||
Test.Assert((1...3).Contains(2));
|
||||
Test.Assert((1...3).Contains(3));
|
||||
Test.Assert(!(1...3).Contains(4));
|
||||
|
||||
Test.Assert(!(1..<3).Contains(0));
|
||||
Test.Assert((1..<3).Contains(1));
|
||||
Test.Assert((1..<3).Contains(2));
|
||||
Test.Assert(!(1..<3).Contains(3));
|
||||
|
||||
Test.Assert((1...3).Contains(1...3));
|
||||
Test.Assert((1...3).Contains(1...2));
|
||||
Test.Assert(!(1...3).Contains(1...4));
|
||||
Test.Assert((1...3).Contains(2..<3));
|
||||
Test.Assert((1...3).Contains(2..<4));
|
||||
Test.Assert(!(1...3).Contains(2..<5));
|
||||
Test.Assert(!(1..<3).Contains(1...3));
|
||||
Test.Assert((1..<3).Contains(1..<3));
|
||||
Test.Assert(!(1..<3).Contains(1..<4));
|
||||
|
||||
}
|
||||
|
||||
public static void TestEnumerator1(EnumeratorTest e)
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue