mirror of
https://github.com/dlang/phobos.git
synced 2025-04-29 06:30:28 +03:00
Add back lowerBound, upperBound, canFindSorted, equalRange to std.algorithm, but with "scheduled for deprecaton" pragmas on instantiation.
This commit is contained in:
parent
f252d5e611
commit
72d6bf1217
2 changed files with 60 additions and 11 deletions
|
@ -6147,6 +6147,57 @@ unittest
|
|||
assert(canFind!"a == 2"(a));
|
||||
}
|
||||
|
||||
// Scheduled for deprecation. Use std.range.SortedRange.canFind.
|
||||
bool canFindSorted(alias pred = "a < b", Range, V)(Range range, V value) {
|
||||
pragma(msg, "std.algorithm.canFindSorted is scheduled for " ~
|
||||
"deprecation. Use std.range.SortedRange.canFind instead.");
|
||||
return assumeSorted!pred(range).canFind!V(value);
|
||||
}
|
||||
|
||||
unittest {
|
||||
assert(canFindSorted([1,2,3,4,5], 4));
|
||||
assert(!canFindSorted([1,2,3,4,5], 8));
|
||||
}
|
||||
|
||||
// Scheduled for deprecation. Use std.range.SortedRange.lowerBound.
|
||||
Range lowerBound(alias pred = "a < b", Range, V)(Range range, V value) {
|
||||
pragma(msg, "std.algorithm.lowerBound is scheduled for " ~
|
||||
"deprecation. Use std.range.SortedRange.lowerBound instead.");
|
||||
return assumeSorted!pred(range).lowerBound!V(value).release;
|
||||
}
|
||||
|
||||
unittest {
|
||||
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
|
||||
auto p = lowerBound!("a < b")(a, 4);
|
||||
assert(equal(p, [0, 1, 2, 3]));
|
||||
}
|
||||
|
||||
// Scheduled for deprecation. Use std.range.SortedRange.upperBound.
|
||||
Range upperBound(alias pred = "a < b", Range, V)(Range range, V value) {
|
||||
pragma(msg, "std.algorithm.upperBound is scheduled for " ~
|
||||
"deprecation. Use std.range.SortedRange.upperBound instead.");
|
||||
return assumeSorted!pred(range).upperBound!V(value).release;
|
||||
}
|
||||
|
||||
unittest {
|
||||
int[] a = [ 1, 2, 3, 3, 3, 4, 4, 5, 6 ];
|
||||
auto p = upperBound(a, 3);
|
||||
assert(equal(p, [4, 4, 5, 6 ]));
|
||||
}
|
||||
|
||||
// Scheduled for deprecation. Use std.range.SortedRange.equalRange.
|
||||
Range equalRange(alias pred = "a < b", Range, V)(Range range, V value) {
|
||||
pragma(msg, "std.algorithm.equalRange is scheduled for " ~
|
||||
"deprecation. Use std.range.SortedRange.equalRange instead.");
|
||||
return assumeSorted!pred(range).equalRange!V(value).release;
|
||||
}
|
||||
|
||||
unittest {
|
||||
auto a = [ 1, 2, 3, 3, 3, 4, 4, 5, 6 ];
|
||||
auto r = equalRange(a, 3);
|
||||
assert(r == [ 3, 3, 3 ]);
|
||||
}
|
||||
|
||||
/**
|
||||
Copies the top $(D n) elements of the input range $(D source) into the
|
||||
random-access range $(D target), where $(D n =
|
||||
|
|
20
std/range.d
20
std/range.d
|
@ -5137,14 +5137,12 @@ Releases the controlled range and returns it.
|
|||
|
||||
Example:
|
||||
----
|
||||
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
|
||||
auto p = lowerBound!("a < b")(a, 4);
|
||||
assert(p == [ 0, 1, 2, 3 ]);
|
||||
p = lowerBound(a, 4); // uses "a < b" by default
|
||||
assert(p == [ 0, 1, 2, 3 ]);
|
||||
auto a = assumeSorted([ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ]);
|
||||
auto p = a.lowerBound(4);
|
||||
assert(p.release == [ 0, 1, 2, 3 ]);
|
||||
----
|
||||
*/
|
||||
typeof(this) lowerBound(alias pred = "a < b", V)(V value)
|
||||
typeof(this) lowerBound(V)(V value)
|
||||
{
|
||||
auto first = 0, count = this._input.length;
|
||||
while (count > 0)
|
||||
|
@ -5185,9 +5183,9 @@ Releases the controlled range and returns it.
|
|||
|
||||
Example:
|
||||
----
|
||||
auto a = [ 1, 2, 3, 3, 3, 4, 4, 5, 6 ];
|
||||
auto p = upperBound(a, 3);
|
||||
assert(p == begin(a) + 5);
|
||||
auto a = assumeSorted([ 1, 2, 3, 3, 3, 4, 4, 5, 6 ]);
|
||||
auto p = a.upperBound(3);
|
||||
assert(p == [4, 4, 5, 6]);
|
||||
----
|
||||
*/
|
||||
typeof(this) upperBound(V)(V value)
|
||||
|
@ -5297,11 +5295,11 @@ unittest
|
|||
static assert(isRandomAccessRange!(SortedRange!(int[])));
|
||||
// scope(success) writeln("unittest @", __FILE__, ":", __LINE__, " done.");
|
||||
int[] a = [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ];
|
||||
auto p = assumeSorted(a).lowerBound!("a < b")(4);
|
||||
auto p = assumeSorted(a).lowerBound(4);
|
||||
assert(equal(p, [0, 1, 2, 3]));
|
||||
p = assumeSorted(a).lowerBound(5);
|
||||
assert(equal(p, [0, 1, 2, 3, 4]));
|
||||
p = assumeSorted(a).lowerBound!(q{a < b})(6);
|
||||
p = assumeSorted(a).lowerBound(6);
|
||||
assert(equal(p, [ 0, 1, 2, 3, 4, 5]));
|
||||
}
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue