digitalmars.D.learn - Create variable for RedBlackTree range
- Gerald (31/31) Apr 28 2018 What is the appropriate way to create a variable for the range
- Jonathan M Davis (12/43) Apr 28 2018 In general, you just use auto, but that's not going to work if you can't
- Gerald (6/21) Apr 28 2018 Thanks for the quick reply and the pointer in the right
- ag0aep6g (8/26) May 02 2018 RedBlackTree also has public range types: Range, ConstRange,
- Meta (4/32) May 02 2018 For completeness' sake, and if you don't want to re-specify the
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
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 terminalIn 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
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: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;[...]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
Apr 28 2018
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
On Wednesday, 2 May 2018 at 10:39:29 UTC, ag0aep6g wrote:On 04/28/2018 06:36 PM, Gerald wrote: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;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