www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Multi-associative array

reply Etienne Cimon <etcimon gmail.com> writes:
Would it eventually be in the making for D to have associative arrays 
like string[string, int, Item]?

If it hasn't been planned, I'd say it would be great to have an API that 
allows it to carry functionality of Boost Multi Index

Thoughts?
Feb 22 2014
next sibling parent Max Klyga <max.klyga gmail.com> writes:
On 2014-02-23 03:10:23 +0000, Etienne Cimon said:

 Would it eventually be in the making for D to have associative arrays 
 like string[string, int, Item]?
 
 If it hasn't been planned, I'd say it would be great to have an API 
 that allows it to carry functionality of Boost Multi Index
 
 Thoughts?

Wanted to recomend using tuples, but it seems TypeInfo.compare is not implemented for tuples. Not sure it its a bug or is it fixed in current compiler versions.
Feb 22 2014
prev sibling next sibling parent reply "H. S. Teoh" <hsteoh quickfur.ath.cx> writes:
On Sat, Feb 22, 2014 at 10:10:23PM -0500, Etienne Cimon wrote:
 Would it eventually be in the making for D to have associative
 arrays like string[string, int, Item]?
 
 If it hasn't been planned, I'd say it would be great to have an API
 that allows it to carry functionality of Boost Multi Index

You can just use a struct or tuple as the AA key to achieve the same thing. struct Key { string x; int y; Item z; } string[Key] aa; aa[Key("a", 1, Item(...))] = "b"; T -- Some days you win; most days you lose.
Feb 22 2014
parent reply Etienne Cimon <etcimon gmail.com> writes:
On 2014-02-23 01:27, H. S. Teoh wrote:
 You can just use a struct or tuple as the AA key to achieve the same
 thing.

I thought of that but it's not immediately obvious how I could achieve sorting or filtering by a certain field e.g. auto sorted = aa.sort!(Key.y => { a < b }) auto filtered = aa.filter! .... foreach(x, y, z ; aa){ .... } I don't see a very simple way of doing this right now. I'm sure someone thought of a library for this?
Feb 22 2014
parent Andrei Alexandrescu <SeeWebsiteForEmail erdani.org> writes:
On 2/22/14, 11:10 PM, Etienne Cimon wrote:
 On 2014-02-23 01:27, H. S. Teoh wrote:
 You can just use a struct or tuple as the AA key to achieve the same
 thing.

I thought of that but it's not immediately obvious how I could achieve sorting or filtering by a certain field e.g. auto sorted = aa.sort!(Key.y => { a < b }) auto filtered = aa.filter! .... foreach(x, y, z ; aa){ .... } I don't see a very simple way of doing this right now. I'm sure someone thought of a library for this?

The way multiple binary tree (or rb-tree) indexes are achieved is by storing multiple pointers with the same value. That way really multiple threes are threaded using the same value nodes. There's no trivial way to do this. It would be very valuable to generalize RBTree to accept multiple predicates and build such multiple indexes. Andrei
Feb 23 2014
prev sibling parent "Vladimir Panteleev" <vladimir thecybershadow.net> writes:
On Sunday, 23 February 2014 at 07:10:32 UTC, Etienne Cimon wrote:
 auto sorted = aa.sort!(Key.y => { a < b })

You can't sort an AA, as AAs are unordered. Did you mean to sort the keys? auto sorted = aa.keys.sort!((a, b) => a.y < b.y)();
 auto filtered = aa.filter! ....

auto filtered = aa.keys.filter!(k => k.y > 2).array;
 foreach(x, y, z ; aa){
 ....
 }

foreach (key, value; aa) with(key) { ... use x, y, z ... }
Feb 23 2014