Skip to content

Commit 6caa5fa

Browse files
authored
std.container.rbtree: Add trisect (#10756)
Useful in case we need >= or <= (instead of > or < or = as provided by lowerBound/upperBound/equalRange). Same convention as SortedRange.trisect, except just return a static array instead of a tuple (as the type is the same).
1 parent 0dbb785 commit 6caa5fa

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

std/container/rbtree.d

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1737,6 +1737,26 @@ assert(equal(rbt[], [5]));
17371737
}
17381738
}
17391739

1740+
/**
1741+
* Returns a static array of 3 ranges `r` such that `r[0]` is the
1742+
* same as the result of `lowerBound(value)`, `r[1]` is the same
1743+
* as the result of $(D equalRange(value)), and `r[2]` is the same
1744+
* as the result of $(D upperBound(value)).
1745+
*
1746+
* Complexity: $(BIGOH log(n))
1747+
*/
1748+
auto trisect(this This)(Elem e)
1749+
{
1750+
auto equalRange = this.equalRange(e);
1751+
alias RangeType = typeof(equalRange);
1752+
RangeType[3] result = [
1753+
RangeType(_begin, equalRange._begin),
1754+
equalRange,
1755+
RangeType(equalRange._end, _end)
1756+
];
1757+
return result;
1758+
}
1759+
17401760
static if (doUnittest) @safe pure unittest
17411761
{
17421762
import std.algorithm.comparison : equal;
@@ -2186,6 +2206,11 @@ if ( is(typeof(binaryFun!less((ElementType!Stuff).init, (ElementType!Stuff).init
21862206
assert(rt1.lowerBound(3).equal([1, 2]));
21872207
assert(rt1.equalRange(3).equal([3]));
21882208
assert(rt1[].equal([1, 2, 3, 4, 5]));
2209+
2210+
auto t = rt1.trisect(3);
2211+
assert(t[0].equal(rt1.lowerBound(3)));
2212+
assert(t[1].equal(rt1.equalRange(3)));
2213+
assert(t[2].equal(rt1.upperBound(3)));
21892214
}
21902215

21912216
//immutable checks

0 commit comments

Comments
 (0)