www.digitalmars.com         C & C++   DMDScript  

digitalmars.D.learn - Create variable for RedBlackTree range

reply Gerald <gerald.b.nunn gmail.com> writes:
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

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.
Apr 28 2018
next sibling parent reply Jonathan M Davis <newsgroup.d jmdavisprog.com> writes:
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
Apr 28 2018
parent Gerald <gerald.b.nunn gmail.com> writes:
On Saturday, 28 April 2018 at 17:20:46 UTC, Jonathan M Davis 
wrote:
 On Saturday, April 28, 2018 16:36:41 Gerald via 
 Digitalmars-d-learn wrote:
 [...]
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;
 [...]
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
Thanks for the quick reply and the pointer in the right direction, I ended up using the ReturnType template to make it work: ReturnType!(promptPosition.lowerBound) range;
Apr 28 2018
prev sibling parent reply ag0aep6g <anonymous example.com> writes:
On 04/28/2018 06:36 PM, Gerald 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;
[...]
 }
 ```
 
 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
 
 Which makes sense since RBRange is a private struct.
RedBlackTree also has public range types: Range, ConstRange, ImmutableRange. And `RedBlackTree!long.Range` is an alias for `RBRange!(RBNode!long*)`. So: RedBlackTree!long promptPosition = redBlackTree!long(); RedBlackTree!long.Range range;
May 02 2018
parent Meta <jared771 gmail.com> writes:
On Wednesday, 2 May 2018 at 10:39:29 UTC, ag0aep6g wrote:
 On 04/28/2018 06:36 PM, Gerald 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;
[...]
 }
 ```
 
 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
 
 Which makes sense since RBRange is a private struct.
RedBlackTree also has public range types: Range, ConstRange, ImmutableRange. And `RedBlackTree!long.Range` is an alias for `RBRange!(RBNode!long*)`. So: RedBlackTree!long promptPosition = redBlackTree!long(); RedBlackTree!long.Range range;
For completeness' sake, and if you don't want to re-specify the template parameters you passed to RedBlackTree, you can write: promptPosition.Range range;
May 02 2018