Skip to content

Commit

Permalink
feat: Range improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
alexyakunin committed Oct 23, 2023
1 parent c228d79 commit 9ff4489
Show file tree
Hide file tree
Showing 3 changed files with 16 additions and 0 deletions.
2 changes: 2 additions & 0 deletions src/Stl/Mathematics/Range.cs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ namespace Stl.Mathematics;
public bool IsEmpty => EqualityComparer<T>.Default.Equals(Start, End);
[JsonIgnore, Newtonsoft.Json.JsonIgnore, IgnoreDataMember, MemoryPackIgnore]
public bool IsNegative => Comparer<T>.Default.Compare(Start, End) > 0;
[JsonIgnore, Newtonsoft.Json.JsonIgnore, IgnoreDataMember, MemoryPackIgnore]
public bool IsEmptyOrNegative => Comparer<T>.Default.Compare(Start, End) >= 0;

/// <summary>
/// Creates a new range.
Expand Down
5 changes: 5 additions & 0 deletions src/Stl/Mathematics/TileLayer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,9 @@ public void AssertIsTile(Range<T> range)

public Tile<T>[] GetCoveringTiles(Range<T> range)
{
if (range.IsEmptyOrNegative)
return Array.Empty<Tile<T>>();

var tiles = ArrayBuffer<Tile<T>>.Lease(true);
try {
GetCoveringTiles(range, ref tiles);
Expand Down Expand Up @@ -104,6 +107,8 @@ private void GetOptimalCoveringTiles(Range<T> range, ref ArrayBuffer<Tile<T>> ap
GetCoveringTiles(range, ref appendTo);
return;
}
if (range.IsEmptyOrNegative)
return;

var tiles = ArrayBuffer<Tile<T>>.Lease(true);
try {
Expand Down
9 changes: 9 additions & 0 deletions tests/Stl.Tests/Mathematics/TileStackTest.cs
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,11 @@ public void LongTileStackTest()
s.TryGetSmallestCoveringTile((-17, 0), out tile).Should().BeTrue();
tile.Should().Be(new Tile<long>(-64, 0, layer1));

layer0.GetCoveringTiles((0, 0)).Should().BeEmpty();
layer0.GetCoveringTiles((0, -1)).Should().BeEmpty();
layer0.GetCoveringTiles((-16, -16)).Should().BeEmpty();
layer0.GetCoveringTiles((-16, -17)).Should().BeEmpty();

layer0.GetCoveringTiles((-1, 1)).Select(t => t.Range)
.Should()
.BeEquivalentTo(new Range<long>[] {
Expand All @@ -77,6 +82,10 @@ public void LongTileStackTest()
(0, 64),
});

s.GetOptimalCoveringTiles((0, 0)).Should().BeEmpty();
s.GetOptimalCoveringTiles((0, -1)).Should().BeEmpty();
s.GetOptimalCoveringTiles((16, -1000)).Should().BeEmpty();

s.GetOptimalCoveringTiles((-17, 257)).Select(t => t.Range)
.Should()
.BeEquivalentTo(new Range<long>[] {
Expand Down

0 comments on commit 9ff4489

Please sign in to comment.