January 15, 2014 Re: extend "in" to all array types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wed, Jan 15, 2014 at 10:57:14AM -0800, Ali Çehreli wrote: > On 01/15/2014 07:38 AM, Adam D. Ruppe wrote: > > > Probably not, since there's a significant speed difference between in associative array and in regular array. For a regular array, it means potentially looping over every item in the array. > > And a specialization for std.range.SortedRange would use binary search: > > http://dlang.org/phobos/std_range.html#.SortedRange [...] Does std.algorithm.find have a specialization for SortedRange? I know that some algorithms do, I just don't remember which. T -- What do you get if you drop a piano down a mineshaft? A flat minor. |
January 16, 2014 Re: extend "in" to all array types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Namespace | On Wednesday, 15 January 2014 at 17:33:27 UTC, Namespace wrote:
> Something like that should really be in std.array.
This is gratuitous operator overloading. We have std.algorithm.find that does this in an equally powerful but more transparent way.
|
January 16, 2014 Re: extend "in" to all array types | ||||
---|---|---|---|---|
| ||||
Posted in reply to H. S. Teoh | On Wednesday, 15 January 2014 at 19:10:27 UTC, H. S. Teoh wrote: > Does std.algorithm.find have a specialization for SortedRange? I know > that some algorithms do, I just don't remember which. > > > T Not yet, SortedRange could need some attention. Judging by a certain old but recently relevant pull request[1], I think Andrei has some work on it. [1] https://github.com/D-Programming-Language/phobos/pull/1184 |
January 16, 2014 Re: extend "in" to all array types | ||||
---|---|---|---|---|
| ||||
Posted in reply to Ali Çehreli | On Wednesday, 15 January 2014 at 18:57:15 UTC, Ali Çehreli wrote:
> On 01/15/2014 07:38 AM, Adam D. Ruppe wrote:
>
> > Probably not, since there's a significant speed difference
> between in
> > associative array and in regular array. For a regular array,
> it means
> > potentially looping over every item in the array.
>
> And a specialization for std.range.SortedRange would use binary search:
>
> http://dlang.org/phobos/std_range.html#.SortedRange
>
> Ali
I'm for it, but it would have to have the explicit caveat that it would only return a pointer to the *first* equal element in the list, as there may be duplicates. If we instead return a range, then suddenly we have `std.algorithm.find`.
Maybe `findSplit` should also have a specialization for `SortedRange` that uses `SortedRange.trisect`.
|
January 16, 2014 Re: extend "in" to all array types | ||||
---|---|---|---|---|
| ||||
Posted in reply to pplantinga | On 15.1.2014. 16:30, pplantinga wrote:
> In python, I really like the ability to check if an element is in an array:
>
> if x in array:
> # do something
>
> D has this, but only for associative arrays. Is there any chance we could extend this to every kind of array?
D array uses this operator for checking against array keys, not values.
|
January 16, 2014 Re: extend "in" to all array types | ||||
---|---|---|---|---|
| ||||
Posted in reply to luka8088 | On Thursday, 16 January 2014 at 07:28:43 UTC, luka8088 wrote:
> On 15.1.2014. 16:30, pplantinga wrote:
>> In python, I really like the ability to check if an element is in an array:
>>
>> if x in array:
>> # do something
>>
>> D has this, but only for associative arrays. Is there any chance we
>> could extend this to every kind of array?
>
> D array uses this operator for checking against array keys, not values.
It makes sense for sorted arrays, as they make good sets.
That said, it makes the most sense to limit opBinaryRight!"in" to SortedRange.
|
Copyright © 1999-2021 by the D Language Foundation