Bug 9119 – [AA] Forward range addition to associative arrays.
Status
RESOLVED
Resolution
FIXED
Severity
enhancement
Priority
P2
Component
druntime
Product
D
Version
D2
Platform
All
OS
All
Creation time
2012-12-06T14:15:00Z
Last change time
2015-02-18T03:41:11Z
Assigned to
nobody
Creator
maidenphil
Comments
Comment #0 by maidenphil — 2012-12-06T14:15:25Z
Providing a forward range to associative arrays would allow user to use functionalities like "filter" and make their own (that return intelligent ranges instead of eagerly constructing a result, then returning said result or a range on it). It could be provided as a "range" property and "front" could return an entry struct where entry.key is the key and entry.value is the value.
Example usage:
float[ Item ] itemsCost;
auto cheapItems = itemsCost.range.filter!"a.value < 5"();
foreach( cheap; cheapItems ) {
sendGiftToEnemy( cheap.key );
}
Comment #1 by bearophile_hugs — 2012-12-06T17:49:48Z
Comment #7 by bearophile_hugs — 2015-01-03T20:02:51Z
The printing of such key-value pairs is not very good. I don't know if you want to improve this, or leave the better printing to byPair:
void main() {
import std.stdio, std.algorithm;
auto aa = [1:2, 3:4];
aa.byKeyValue.writeln;
}
[Pair(1513FE8, 1513FEC), Pair(1513FC8, 1513FCC)]
-----------------
There are also problems with a constant key-value pair:
void main() {
auto aa = [1:2, 3:4];
foreach (const t; aa.byKeyValue) {
auto k = t.key;
auto v = t.value;
}
}
test.d(4,18): Error: mutable method object.byKeyValue!(int[int], int, int).byKeyValue.Result.front.Pair.key is not callable using a const object
test.d(5,18): Error: mutable method object.byKeyValue!(int[int], int, int).byKeyValue.Result.front.Pair.value is not callable using a const object
-----------------
Comment #8 by github-bugzilla — 2015-01-04T23:06:18Z
Now that all the PR's have merged, here's the current situation:
- druntime now provides a byKeyValue method that returns a forward range of opaque structs with .key and .value members.
- std.array in Phobos now provides a "nice" method byPair that wraps around byKeyValue and returns a forward range of Tuple's of keys and values.
So this both satisfies the OP's original request (for .key and .value) and later requests for Tuple compatibility.
Comment #13 by hsteoh — 2015-01-08T23:16:52Z
Furthermore, printing of the range returned by byPair() produces output of the form:
-----
immutable(Tuple!(string, int))("a", 123)
immutable(Tuple!(string, int))("b", 234)
immutable(Tuple!(string, int))("c", 345)
-----
So the printing issue is also taken care of.
Comment #14 by bearophile_hugs — 2015-01-08T23:37:46Z
(In reply to hsteoh from comment #12)
> So this both satisfies the OP's original request (for .key and .value) and
> later requests for Tuple compatibility.
Yes, of all the possible solutions, I think the current is one of the few acceptable ones.
One point worth studying is the run-time efficiency of using byPair using the dmd compiler. I have seen a slowdown compared to using byKeyValue (but I have not done formal benchamarks).
Comment #15 by github-bugzilla — 2015-02-18T03:38:41Z