Re: More flexible sorted ranges?

2014-12-04 Thread Tobias Pankrath via Digitalmars-d-learn
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

Re: More flexible sorted ranges?

2014-12-03 Thread Nordlöw
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.

Re: More flexible sorted ranges?

2014-11-02 Thread via Digitalmars-d-learn
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 o

Re: More flexible sorted ranges?

2014-11-02 Thread via Digitalmars-d-learn
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_o It is always easier to look for the examples :-). In terms of what to call it, perhaps what you're looking for is "upper

Re: More flexible sorted ranges?

2014-11-02 Thread Xinok via Digitalmars-d-learn
On Sunday, 2 November 2014 at 20:19:12 UTC, bearophile wrote: 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

Re: More flexible sorted ranges?

2014-11-02 Thread Xinok via Digitalmars-d-learn
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: 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 low

Re: More flexible sorted ranges?

2014-11-02 Thread via Digitalmars-d-learn
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.

Re: More flexible sorted ranges?

2014-11-02 Thread bearophile via Digitalmars-d-learn
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 her

Re: More flexible sorted ranges?

2014-11-02 Thread via Digitalmars-d-learn
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

Re: More flexible sorted ranges?

2014-11-02 Thread Xinok via Digitalmars-d-learn
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

Re: More flexible sorted ranges?

2014-11-02 Thread Xinok via Digitalmars-d-learn
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: Ola Fosheim Grøstad: Shouldn't sorted range maintain the invariant automatically in order to remain typesafe? Yes, of course. If SortedRange is fixed, please

Re: More flexible sorted ranges?

2014-11-02 Thread via Digitalmars-d-learn
On Sunday, 2 November 2014 at 16:59:30 UTC, bearophile wrote: Ola Fosheim Grøstad: Shouldn't sorted range maintain the invariant automatically in order to remain typesafe? Yes, of course. If SortedRange is fixed, please also switch the names of upperBound and lowerBound… They are currently

Re: More flexible sorted ranges?

2014-11-02 Thread bearophile via Digitalmars-d-learn
Ola Fosheim Grøstad: Shouldn't sorted range maintain the invariant automatically in order to remain typesafe? Yes, of course. Bye, bearophile

Re: More flexible sorted ranges?

2014-11-02 Thread via Digitalmars-d-learn
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 th

More flexible sorted ranges?

2014-11-02 Thread bearophile via Digitalmars-d-learn
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