Efficiently streaming data to associative array

Guillaume Chatelet via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Wed Aug 9 04:12:51 PDT 2017


On Wednesday, 9 August 2017 at 10:00:14 UTC, kerdemdemir wrote:
> As a total beginner I am feeling a bit not comfortable with 
> basic operations in AA.
>
> First even I am very happy we have pointers but using pointers 
> in a common operation like this IMHO makes the language a bit 
> not safe.
>
> Second "in" keyword always seemed so specific to me.
>
> I think I will use your solution "ref Value 
> GetWithDefault(Value)" very often since it hides the two things 
> above.

You don't need this most of the time, if you already have the 
correct type it's easy:

size_t[string][string] indexed_map;

string a, b; // a and b are strings not char[]
indexed_map[a][b] = value; // this will create the AA slots if 
needed

In my specific case the data is streamed from stdin and is not 
kept in memory.
byLine returns a view of the stdin buffer which may be replaced 
at the next for-loop iteration so I can't use the index operator 
directly, I need a string that does not change over time.

I could have used this code:

void main() {
   size_t[string][string] indexed_map;
   foreach(char[] line ; stdin.byLine) {
     char[] a;
     char[] b;
     size_t value;
     line.formattedRead!"%s,%s,%d"(a,b,value);
     indexed_map[a.idup][b.idup] = value;
   }
   indexed_map.writeln;
}

It's perfectly ok if data is small. In my case data is huge and 
creating a copy of the strings at each iteration is costly.


More information about the Digitalmars-d-learn mailing list