Monday, 15 August 2011

c# collection type, to keep index order -



c# collection type, to keep index order -

i have file rows keys values

-----+--------------------------- 1 1 2 0.39785 0.39785 0.2043 36 1 1 3 0.409604 0.409604 0.180792 24 1 1 4 0.407281 0.407281 0.185438 24 1 1 5 0.404958 0.404958 0.190084 24 1 1 6 0.403399 0.403399 0.193203 24 ... 23 34 36 0.414457 0.354921 0.230622 576 ..

-the first 3 numbers keys , represent matchup, unique, , ascending -the float values linked keys. eg: first row's 4th element (0.39785) belongs key 1, 6th element (0.2043) 2.

i read line line , split " " (space). how should store (which collection/structure).

lets want lookup "2 1 1". wrote keys ascending, there won't entry "2 1 1", "1 1 2", first have sort it, want values in lookup's order (0.2043 0.39785 0.39785).

the data-structure below should meet requirements:

dictionary<hashset<int>, dictionary<int, double>>

it should easy create instance of above construction linq original data.

access should easy:

from 2, 1, 1 create hashset (2, 1) lookup (2, 1) in dictionary -> ((1, 0.39785), (2, 0.2043)) with partial key lookup double 2 -> 0.2043

caveat solution work long identical int-values on 1 line double-values identical well. (which seems hold true provided sample-data).

edit code create yourlookup:

list<list<int>> intlist = new list<list<int>>() { new list<int> () {1, 1, 2}, new list<int> () {1, 1, 3}, ... }; list<list<double>> doublelist = new list<list<double>> { new list<double>() {0.39785, 0.39785, 0.2043}, new list<double>() {0.409604, 0.409604, 0.180792}, .... }; var dictionaries = intlist.zip(doublelist, (is, ds) => { homecoming is.zip(ds, (i, d) => new keyvaluepair<int, double>(i, d)).distinct() .todictionary(kv => kv.key, kv => kv.value); }); var yourlookup = dictionaries.select( dictionary => new { hashset = new hashset<int>(dictionary.keys), dictionary }) .todictionary(x => x.hashset, x => x.dictionary);

c# collections

No comments:

Post a Comment