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
See Issue 5075
Comment #2 by hsteoh — 2013-08-15T12:23:05Z
Comment #3 by hsteoh — 2013-08-15T12:33:54Z
Using the code in the pull request, this code works: import std.algorithm; import std.conv; import std.stdio: writeln; void main () { int[string] aa; aa["a"] = 1; aa["b"] = 2; aa["c"] = 3; writeln(aa.byPair .map!((a) => "key=" ~ a.key ~ " value=" ~ to!string(a.value)) .joiner("\n") ); } The output is: key=a value=1 key=b value=2 key=c value=3
Comment #4 by github-bugzilla — 2015-01-02T21:50:21Z
Comment #5 by github-bugzilla — 2015-01-02T23:58:36Z
Commit pushed to master at https://github.com/D-Programming-Language/dlang.org https://github.com/D-Programming-Language/dlang.org/commit/74e226ccb7e7f6e8967a8e9c00735d01a9676be5 Merge pull request #743 from quickfur/issue9119 Document AA .byKeyValue().
Comment #6 by github-bugzilla — 2015-01-03T18:22:03Z
Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/b3a8032e3960480a1588b3d1a4491808b4502d67 Merge pull request #1077 from quickfur/issue9119_fixup Fix wrong copy-pasta from object_.d.
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
Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/9ec46a649e5fcd112dec7af102b4cab9189b48f3 Merge pull request #1079 from quickfur/issue9119_const byKeyValue should also work with const loop variables.
Comment #9 by github-bugzilla — 2015-01-04T23:42:15Z
Commit pushed to master at https://github.com/D-Programming-Language/druntime https://github.com/D-Programming-Language/druntime/commit/841c9a68cc209aa2abcab95593dce5f9e777b4c3 Merge pull request #1080 from quickfur/issue9119_fwdrange .save needs to be @property otherwise isForwardRange fails.
Comment #10 by github-bugzilla — 2015-01-05T01:48:00Z
Commit pushed to master at https://github.com/D-Programming-Language/dlang.org https://github.com/D-Programming-Language/dlang.org/commit/a61963ea2dbb96298137936c04551b38bd64feab Merge pull request #745 from quickfur/issue9119_changelog Document byKeyValue in changelog.
Comment #11 by github-bugzilla — 2015-01-08T20:37:22Z
Commit pushed to master at https://github.com/D-Programming-Language/phobos https://github.com/D-Programming-Language/phobos/commit/88722a83e2a12db9e796d049ac9787518577cc50 Merge pull request #2842 from quickfur/issue9119_byPair Implement byPair.
Comment #12 by hsteoh — 2015-01-08T23:15:46Z
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
Comment #16 by github-bugzilla — 2015-02-18T03:41:11Z