Efficiently streaming data to associative array
kerdemdemir via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Wed Aug 9 03:00:14 PDT 2017
> I haven't yet dug into formattedRead but thx for letting me
> know : )
> I was mostly speaking about the pattern with the AA. I guess
> the best I can do is a templated function to hide the ugliness.
>
>
> ref Value GetWithDefault(Value)(ref Value[string] map, const
> (char[]) key) {
> auto pValue = key in map;
> if(pValue) return *pValue;
> return map[key.idup] = Value.init;
> }
>
> 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.GetWithDefault(a).GetWithDefault(b) = value;
> }
>
> indexed_map.writeln;
> }
>
>
> Not too bad actually !
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.
More information about the Digitalmars-d-learn
mailing list