Create variable for RedBlackTree range

Jonathan M Davis newsgroup.d at jmdavisprog.com
Sat Apr 28 17:20:46 UTC 2018


On Saturday, April 28, 2018 16:36:41 Gerald via Digitalmars-d-learn wrote:
> What is the appropriate way to create a variable for the range
> returned by RedBlackTree lowerBound and upperBound. For example,
> given this code:
>
> ```
> RedBlackTree!long promptPosition = redBlackTree!long();
>
> long row = to!long(vte.getVadjustment().getValue());
> RBRange!(RBNode!long*) range;
> if (direction < 0) {
>      range = promptPosition.lowerBound(row);
>      if (range.empty) result = lower;
>      else result = range.back;
> } else {
>      range = promptPosition.upperBound(row);
>      if (range.empty) result = upper;
>      else result = range.front();
> }
> if (result >= lower) {
>      vte.getVadjustment.setValue(to!double(result));
> } else {
>      promptPosition.remove(range);
> }
> ```
>
> The second line where I declare the range variable as
> RBRange!(RBNode!long*) the compiler complains with the following
> warning:
>
> Deprecation: std.container.rbtree.RBRange(N) is not visible from
> module terminal

In general, you just use auto, but that's not going to work if you can't
directly initialize the variable. In that case, the solution is typeof. e.g.
something like

typeof(prompPosition[]) range;

> Which makes sense since RBRange is a private struct. However I
> cannot use the normal range interfaces here either (ForwardRange,
> BiDirectionalRange, etc) since it complains about RBRange not
> being able to cast to them.

If you mean the interfaces from std.range.interfaces, I don't think that
anything in Phobos uses them except for that module, and I expect that very
little range-based code in general uses them. Ranges are almost always
structs. There are rare cases where those interfaces make sense, but ranges
in general don't use them. Rather, range-based code is almost always
templated.

- Jonathan M Davis



More information about the Digitalmars-d-learn mailing list