digitalmars.D.learn - More flexible sorted ranges?
- bearophile (14/14) Nov 02 2014 I have often arrays that are sorted, and sometimes I'd like to
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (3/9) Nov 02 2014 Shouldn't sorted range maintain the invariant automatically in
- bearophile (4/6) Nov 02 2014 Yes, of course.
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (6/10) Nov 02 2014 If SortedRange is fixed, please also switch the names of
- Xinok (6/18) Nov 02 2014 D got it right. C++ returns an iterator which can be a bit
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (12/15) Nov 02 2014 No, according to docs D has defined the lower bound to be the
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (5/5) Nov 02 2014 And while you're at it, consider fixing the name to be accurate.
- Xinok (7/13) Nov 02 2014 Sorry, you're right, it's not an "upper bound". I read that
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (6/11) Nov 02 2014 Hm… I think an "upper set" requires that element is present it in
- Xinok (5/19) Nov 02 2014 My concern is that SortedRange only accepts a range which is
- bearophile (12/16) Nov 02 2014 I understand, that's why I am asking this here...
- Xinok (12/29) Nov 02 2014 I take back my original argument. As of 2.066, the requirements
- "Ola Fosheim =?UTF-8?B?R3LDuHN0YWQi?= (4/8) Nov 02 2014 You might be interested in the this proposal:
- =?UTF-8?B?Ik5vcmRsw7Z3Ig==?= (2/5) Dec 03 2014 Have anybody implemented SortedRange? I can't find any refs.
- Tobias Pankrath (8/22) Dec 04 2014 To make it compatible with std.container, you should call it
I have often arrays that are sorted, and sometimes I'd like to append items to them. So I'd like to write something like: SortedRange!(Foo[], q{ a.x < b.x }) data; data ~= Foo(5); immutable n = data.upperBound(Foo(2)).length; This means having an array of Foos as sorted range, and appending an item to it keeping the sorting invariant (so in non-release mode the append verifies the array is empty or the last two items satisfy the sorting invariant), and this allows me to call functions like upperBound any time I want on 'data' without using any unsafe and unclean assumeSorted. Is this possible and a good idea to do? Bye, bearophile
Nov 02 2014
On Sunday, 2 November 2014 at 15:13:37 UTC, bearophile wrote:This means having an array of Foos as sorted range, and appending an item to it keeping the sorting invariant (so in non-release mode the append verifies the array is empty or the last two items satisfy the sorting invariant), and this allows me to call functions like upperBound any time I want on 'data' without using any unsafe and unclean assumeSorted.Shouldn't sorted range maintain the invariant automatically in order to remain typesafe?
Nov 02 2014
Ola Fosheim Grøstad:Shouldn't sorted range maintain the invariant automatically in order to remain typesafe?Yes, of course. Bye, bearophile
Nov 02 2014
On Sunday, 2 November 2014 at 16:59:30 UTC, bearophile wrote:Ola Fosheim Grøstad:If SortedRange is fixed, please also switch the names of upperBound and lowerBound… They are currently wrong. An upperBound on something should return the values lower than it and a lowerBound should return values larger… (C++ got it right).Shouldn't sorted range maintain the invariant automatically in order to remain typesafe?Yes, of course.
Nov 02 2014
On Sunday, 2 November 2014 at 17:21:04 UTC, Ola Fosheim Grøstad wrote:On Sunday, 2 November 2014 at 16:59:30 UTC, bearophile wrote:D got it right. C++ returns an iterator which can be a bit confusing. D returns a slice so it's meaning is much clearer. https://en.wikipedia.org/wiki/Upper_and_lower_bounds http://www.cplusplus.com/reference/algorithm/upper_bound/Ola Fosheim Grøstad:If SortedRange is fixed, please also switch the names of upperBound and lowerBound… They are currently wrong. An upperBound on something should return the values lower than it and a lowerBound should return values larger… (C++ got it right).Shouldn't sorted range maintain the invariant automatically in order to remain typesafe?Yes, of course.
Nov 02 2014
On Sunday, 2 November 2014 at 19:54:38 UTC, Xinok wrote:D got it right. C++ returns an iterator which can be a bit confusing. D returns a slice so it's meaning is much clearer.No, according to docs D has defined the lower bound to be the negation of the lower bound…https://en.wikipedia.org/wiki/Upper_and_lower_boundsQuoting your link: «5 is lower bound for the set { 5, 10, 34, 13934 }» Hence if you lowerBound(4) the sequence [ 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 ] it should return [ 4, 5, 6, 7, 8, 9 ] not [ 0, 1, 2, 3 ]
Nov 02 2014
And while you're at it, consider fixing the name to be accurate. Call it: lowerBoundedBy(value) or something similar. A lower bound is a single element, not a set.
Nov 02 2014
On Sunday, 2 November 2014 at 20:06:51 UTC, Ola Fosheim Grøstad wrote:On Sunday, 2 November 2014 at 19:54:38 UTC, Xinok wrote:Sorry, you're right, it's not an "upper bound". I read that definition a few times over and still got it wrong. O_o In terms of what to call it, perhaps what you're looking for is "upper set"? https://en.wikipedia.org/wiki/Upper_setD got it right. C++ returns an iterator which can be a bit confusing. D returns a slice so it's meaning is much clearer.No, according to docs D has defined the lower bound to be the negation of the lower bound…
Nov 02 2014
On Sunday, 2 November 2014 at 21:35:00 UTC, Xinok wrote:Sorry, you're right, it's not an "upper bound". I read that definition a few times over and still got it wrong. O_oIt is always easier to look for the examples :-).In terms of what to call it, perhaps what you're looking for is "upper set"? https://en.wikipedia.org/wiki/Upper_setHm… I think an "upper set" requires that element is present it in the set. I assume you want to be able to do something like: a.lowerBounded(2).upperBounded(10) and then get "for all x in a where 2 <= x <= 10".
Nov 02 2014
On Sunday, 2 November 2014 at 15:13:37 UTC, bearophile wrote:I have often arrays that are sorted, and sometimes I'd like to append items to them. So I'd like to write something like: SortedRange!(Foo[], q{ a.x < b.x }) data; data ~= Foo(5); immutable n = data.upperBound(Foo(2)).length; This means having an array of Foos as sorted range, and appending an item to it keeping the sorting invariant (so in non-release mode the append verifies the array is empty or the last two items satisfy the sorting invariant), and this allows me to call functions like upperBound any time I want on 'data' without using any unsafe and unclean assumeSorted. Is this possible and a good idea to do? Bye, bearophileMy concern is that SortedRange only accepts a range which is random-access and limits its functionality to those primitives. Concatenation is not required for random-access ranges, so should we expect SortedRange to overload this operator?
Nov 02 2014
Xinok:My concern is that SortedRange only accepts a range which is random-access and limits its functionality to those primitives. Concatenation is not required for random-access ranges, so should we expect SortedRange to overload this operator?I understand, that's why I am asking this here... I think the desire to keep a sorted range around and grow it keeping its invariant is a common enough need for my code. Currently I keep an array then I use assumeSorted + upperBound, but this is not safe nor nice. Perhaps sorted ranges should become more transparent in Phobos. There are other invariants beside sortness that can be useful to carry around, like set-ness (every item is unique inside this collection) and few more. Bye, bearophile
Nov 02 2014
On Sunday, 2 November 2014 at 20:19:12 UTC, bearophile wrote:Xinok:I take back my original argument. As of 2.066, the requirements for SortedRange have been relaxed so it now accepts input ranges. The documentation needs to be updated to reflect this change. Still, I'm not comfortable to adding concatenation to SortedRange. I would prefer named functions which append / prepend elements with the guarantee that it preserves the invariant. In general, I don't feel that SortedRange is an ideal solution anyways. Wrapping ranges in a struct adds too much overhead and we can't extend the functionality of it. Would it be possible to replace SortedRange with a sorted attribute or something?My concern is that SortedRange only accepts a range which is random-access and limits its functionality to those primitives. Concatenation is not required for random-access ranges, so should we expect SortedRange to overload this operator?I understand, that's why I am asking this here... I think the desire to keep a sorted range around and grow it keeping its invariant is a common enough need for my code. Currently I keep an array then I use assumeSorted + upperBound, but this is not safe nor nice. Perhaps sorted ranges should become more transparent in Phobos. There are other invariants beside sortness that can be useful to carry around, like set-ness (every item is unique inside this collection) and few more. Bye, bearophile
Nov 02 2014
On Sunday, 2 November 2014 at 22:14:33 UTC, Xinok wrote:In general, I don't feel that SortedRange is an ideal solution anyways. Wrapping ranges in a struct adds too much overhead and we can't extend the functionality of it. Would it be possible to replace SortedRange with a sorted attribute or something?You might be interested in the this proposal: http://forum.dlang.org/thread/tpctqjlvnrrasiktehaq forum.dlang.org I think it is doable… ;)
Nov 02 2014
On Sunday, 2 November 2014 at 15:13:37 UTC, bearophile wrote:SortedRange!(Foo[], q{ a.x < b.x }) data; data ~= Foo(5); immutable n = data.upperBound(Foo(2)).length;Have anybody implemented SortedRange? I can't find any refs.
Dec 03 2014
On Sunday, 2 November 2014 at 15:13:37 UTC, bearophile wrote:I have often arrays that are sorted, and sometimes I'd like to append items to them. So I'd like to write something like: SortedRange!(Foo[], q{ a.x < b.x }) data; data ~= Foo(5); immutable n = data.upperBound(Foo(2)).length; This means having an array of Foos as sorted range, and appending an item to it keeping the sorting invariant (so in non-release mode the append verifies the array is empty or the last two items satisfy the sorting invariant), and this allows me to call functions like upperBound any time I want on 'data' without using any unsafe and unclean assumeSorted. Is this possible and a good idea to do? Bye, bearophileTo make it compatible with std.container, you should call it Sorted(T, pred) and make it a accept any std.container, whose opSlice returns a RandomAccesRange. A Sorted(Array!U, pred) wouldn't itself be a RandomAcessRange but a container and it's opSlice should return a SortedRange. The algorithms that work on arrays and currently return a SortedRange could retur n a Sorted!(U[], pred) instead.
Dec 04 2014