www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Associative Ranges

reply "Freddy" <Hexagonalstar64 gmail.com> writes:
I just curious, do Associative Ranges exist. If so where can i
find them. I started thinking about them when i asked this
question:
http://forum.dlang.org/thread/vauuognmhvtjrktazyeb forum.dlang.org
Aug 01 2014
next sibling parent reply "Xinok" <xinok live.com> writes:
On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
 I just curious, do Associative Ranges exist. If so where can i
 find them. I started thinking about them when i asked this
 question:
 http://forum.dlang.org/thread/vauuognmhvtjrktazyeb forum.dlang.org
Unfortunately not, but I do wonder if we should generalize an interface for these types of ranges.
Aug 02 2014
parent reply "bearophile" <bearophileHUGS lycos.com> writes:
Xinok:

 I do wonder if we should generalize an interface for these 
 types of ranges.
First of all you need some use cases and usage examples. Bye, bearophile
Aug 02 2014
parent reply "Xinok" <xinok live.com> writes:
On Saturday, 2 August 2014 at 14:49:49 UTC, bearophile wrote:
 Xinok:

 I do wonder if we should generalize an interface for these 
 types of ranges.
First of all you need some use cases and usage examples. Bye, bearophile
The most obvious use case is generic functions that can operate on associative ranges of any type, regardless of implementation. There are various ways to implement and optimize hash tables for specific use cases, so it would be convenient not to be restricted to a single container. As for usage examples, I don't have any because I'm not sure what the primitives should be. I will emphasize that "associative ranges" should be distinguishable from random-access ranges. If they weren't, then we would have to comb through Phobos and add checks everywhere, e.g. (!isAssociativeRange!Range). I don't think forward ranges and bidirectional ranges would be an issue though.
Aug 02 2014
next sibling parent "Freddy" <Hexagonalstar64 gmail.com> writes:
On Saturday, 2 August 2014 at 15:46:31 UTC, Xinok wrote:
 On Saturday, 2 August 2014 at 14:49:49 UTC, bearophile wrote:
 Xinok:

 I do wonder if we should generalize an interface for these 
 types of ranges.
First of all you need some use cases and usage examples. Bye, bearophile
The most obvious use case is generic functions that can operate on associative ranges of any type, regardless of implementation. There are various ways to implement and optimize hash tables for specific use cases, so it would be convenient not to be restricted to a single container. As for usage examples, I don't have any because I'm not sure what the primitives should be. I will emphasize that "associative ranges" should be distinguishable from random-access ranges. If they weren't, then we would have to comb through Phobos and add checks everywhere, e.g. (!isAssociativeRange!Range). I don't think forward ranges and bidirectional ranges would be an issue though.
It might be a ploblem if you have an Associative Range where the key is size_t unless Associative ranges are not Input/Forward Ranges
Aug 02 2014
prev sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Saturday, 2 August 2014 at 15:46:31 UTC, Xinok wrote:
 On Saturday, 2 August 2014 at 14:49:49 UTC, bearophile wrote:
 Xinok:

 I do wonder if we should generalize an interface for these 
 types of ranges.
First of all you need some use cases and usage examples. Bye, bearophile
The most obvious use case is generic functions that can operate on associative ranges of any type, regardless of implementation. There are various ways to implement and optimize hash tables for specific use cases, so it would be convenient not to be restricted to a single container.
+1, I'd like to add that compiler is a good example of were this is useful. You'll have quick/small/throwaway and only growing map - to keep track of unwinding infos, break/continue/labels status, switch processing -, long lived one that only grow (and can grow big) - template instance cache, symbol table - as well as map where element goes in and out all the time - for internal machinery -. Optimal algorithms for these use case are different, and it'd be great if part of the code could be factorized. For info, SDC only uses out of the box hashmaps for now, but could benefit from better implementations - and doing so is waaaayyyyyy down the priority queue.
Aug 04 2014
prev sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
 I just curious, do Associative Ranges exist. If so where can i
 find them. I started thinking about them when i asked this
 question:
 http://forum.dlang.org/thread/vauuognmhvtjrktazyeb forum.dlang.org
I started a phobos fork for this, what do you think so far https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
Aug 02 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 3 August 2014 at 06:19:12 UTC, Freddy wrote:
 On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
 I just curious, do Associative Ranges exist. If so where can i
 find them. I started thinking about them when i asked this
 question:
 http://forum.dlang.org/thread/vauuognmhvtjrktazyeb forum.dlang.org
I started a phobos fork for this, what do you think so far https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
Nice! A few comments after a cursory glance: 1) "aligns" sounds strange, use "corresponds" instead. 2) It would be preferable that associative ranges mimic built-in associative arrays as closely as possible, i.e. the range members should be called `byKey` and `byValue`, and it should allow iteration over the associative range directly, instead of using the `range` member. 3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
Aug 03 2014
next sibling parent "Freddy" <Hexagonalstar64 gmail.com> writes:
On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
 On Sunday, 3 August 2014 at 06:19:12 UTC, Freddy wrote:
 On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
 I just curious, do Associative Ranges exist. If so where can i
 find them. I started thinking about them when i asked this
 question:
 http://forum.dlang.org/thread/vauuognmhvtjrktazyeb forum.dlang.org
I started a phobos fork for this, what do you think so far https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
Nice! A few comments after a cursory glance: 1) "aligns" sounds strange, use "corresponds" instead. 2) It would be preferable that associative ranges mimic built-in associative arrays as closely as possible, i.e. the range members should be called `byKey` and `byValue`, and it should allow iteration over the associative range directly, instead of using the `range` member. 3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
Alright i fixed it, although should the implict range be a forward range, an input range or something else and how do you implement front,popFront, and empty on a built-in associative array.
Aug 03 2014
prev sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
 On Sunday, 3 August 2014 at 06:19:12 UTC, Freddy wrote:
 On Friday, 1 August 2014 at 23:57:37 UTC, Freddy wrote:
 I just curious, do Associative Ranges exist. If so where can i
 find them. I started thinking about them when i asked this
 question:
 http://forum.dlang.org/thread/vauuognmhvtjrktazyeb forum.dlang.org
I started a phobos fork for this, what do you think so far https://github.com/Superstar64/phobos/blob/60d3472b1056b298319976f105aa3b9b3f165e97/std/range.d#L1357-1420
Nice! A few comments after a cursory glance: 1) "aligns" sounds strange, use "corresponds" instead. 2) It would be preferable that associative ranges mimic built-in associative arrays as closely as possible, i.e. the range members should be called `byKey` and `byValue`, and it should allow iteration over the associative range directly, instead of using the `range` member. 3) For the value and key ranges, there should be a guarantee that they can be zipped through, i.e. that the elements in them are in the same order so keys and values correspond to each other. The built-in associative arrays provide `byKey` and `byValue`, which satisfy this condition.
Also won't the implicit range make it hard to implement function like map (should you map to whole range or to the value)
Aug 03 2014
parent reply "Marc =?UTF-8?B?U2Now7x0eiI=?= <schuetzm gmx.net> writes:
On Sunday, 3 August 2014 at 18:38:51 UTC, Freddy wrote:
 On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
 3) For the value and key ranges, there should be a guarantee 
 that they can be zipped through, i.e. that the elements in 
 them are in the same order so keys and values correspond to 
 each other. The built-in associative arrays provide `byKey` 
 and `byValue`, which satisfy this condition.
Also won't the implicit range make it hard to implement function like map (should you map to whole range or to the value)
I guess when you iterate over it, you just get tuples. So, `map` would return a normal range of tuples, not an associative range. There could then be a method to convert such a range to an associative range (although it would need to assume that the first tuple elements = keys are unique). I'm not sure whether this is the right thing to do. In any case I don't think it's good to map the keys, because they mustn't be modified, or otherwise you could get duplicate keys. An alternative would be that `map` takes both a the key and the value, but returns only the new value. The same goes for the other range operations (e.g. reduce). But on the other hand, this doesn't seem to scale, because all range algorithms would then have to be specialized for associative ranges...
Aug 04 2014
next sibling parent reply "Freddy" <Hexagonalstar64 gmail.com> writes:
On Monday, 4 August 2014 at 09:21:40 UTC, Marc Schütz wrote:
 On Sunday, 3 August 2014 at 18:38:51 UTC, Freddy wrote:
 On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
 3) For the value and key ranges, there should be a guarantee 
 that they can be zipped through, i.e. that the elements in 
 them are in the same order so keys and values correspond to 
 each other. The built-in associative arrays provide `byKey` 
 and `byValue`, which satisfy this condition.
Also won't the implicit range make it hard to implement function like map (should you map to whole range or to the value)
I guess when you iterate over it, you just get tuples. So, `map` would return a normal range of tuples, not an associative range. There could then be a method to convert such a range to an associative range (although it would need to assume that the first tuple elements = keys are unique). I'm not sure whether this is the right thing to do. In any case I don't think it's good to map the keys, because they mustn't be modified, or otherwise you could get duplicate keys. An alternative would be that `map` takes both a the key and the value, but returns only the new value. The same goes for the other range operations (e.g. reduce). But on the other hand, this doesn't seem to scale, because all range algorithms would then have to be specialized for associative ranges...
The way I have it now is that type doesn't matter when you iterate over a assciative range, all that matters is that that type has a .key and .value. This allow custom opAssign and other bonuses. Should I switch to use type tuples. A lot of the functions that take Input/Forward ranges don't need specialization because you could use byValue and zip with byKey(however then we might need to add a default .key and .value to type tuples if we go with my original idea).
Aug 04 2014
parent "Freddy" <Hexagonalstar64 gmail.com> writes:
On Tuesday, 5 August 2014 at 00:11:21 UTC, Freddy wrote:
 On Monday, 4 August 2014 at 09:21:40 UTC, Marc Schütz wrote:
 On Sunday, 3 August 2014 at 18:38:51 UTC, Freddy wrote:
 On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
 3) For the value and key ranges, there should be a guarantee 
 that they can be zipped through, i.e. that the elements in 
 them are in the same order so keys and values correspond to 
 each other. The built-in associative arrays provide `byKey` 
 and `byValue`, which satisfy this condition.
Also won't the implicit range make it hard to implement function like map (should you map to whole range or to the value)
I guess when you iterate over it, you just get tuples. So, `map` would return a normal range of tuples, not an associative range. There could then be a method to convert such a range to an associative range (although it would need to assume that the first tuple elements = keys are unique). I'm not sure whether this is the right thing to do. In any case I don't think it's good to map the keys, because they mustn't be modified, or otherwise you could get duplicate keys. An alternative would be that `map` takes both a the key and the value, but returns only the new value. The same goes for the other range operations (e.g. reduce). But on the other hand, this doesn't seem to scale, because all range algorithms would then have to be specialized for associative ranges...
The way I have it now is that type doesn't matter when you iterate over a assciative range, all that matters is that that type has a .key and .value. This allow custom opAssign and other bonuses. Should I switch to use type tuples. A lot of the functions that take Input/Forward ranges don't need specialization because you could use byValue and zip with byKey(however then we might need to add a default .key and .value to type tuples if we go with my original idea).
Wait, the build-in associative arrays already a opApply with a single element, this makes them unable to be an associate range (depending on priority we would break backwards compatabily or be unable to use them as a range) unless we change the associtive range interface to be an input/forward range of the value type and ask users to zip byKeys and the range
Aug 04 2014
prev sibling parent "Freddy" <Hexagonalstar64 gmail.com> writes:
On Monday, 4 August 2014 at 09:21:40 UTC, Marc Schütz wrote:
 On Sunday, 3 August 2014 at 18:38:51 UTC, Freddy wrote:
 On Sunday, 3 August 2014 at 08:50:47 UTC, Marc Schütz wrote:
 3) For the value and key ranges, there should be a guarantee 
 that they can be zipped through, i.e. that the elements in 
 them are in the same order so keys and values correspond to 
 each other. The built-in associative arrays provide `byKey` 
 and `byValue`, which satisfy this condition.
Also won't the implicit range make it hard to implement function like map (should you map to whole range or to the value)
I guess when you iterate over it, you just get tuples. So, `map` would return a normal range of tuples, not an associative range. There could then be a method to convert such a range to an associative range (although it would need to assume that the first tuple elements = keys are unique). I'm not sure whether this is the right thing to do. In any case I don't think it's good to map the keys, because they mustn't be modified, or otherwise you could get duplicate keys. An alternative would be that `map` takes both a the key and the value, but returns only the new value. The same goes for the other range operations (e.g. reduce). But on the other hand, this doesn't seem to scale, because all range algorithms would then have to be specialized for associative ranges...
I have come up with a new interface: ---- R r=void; auto v=r.front;//r is an input range auto k=r.byKey.front;//byKey is an input range v=r[k];//opIndex of k v=*(k in r);//opBinearyRight!"in" of k ---- The allows function like map to not alter key(similar to how map can't alter the postioning of random access ranges), and it shouldn't conflit with the build-in associative array's opApply
Aug 04 2014