Generic recursiveness

  • Thread starter Thread starter Michael Bray
  • Start date Start date
M

Michael Bray

Lets say I want to have a sparse lookup by two int values to a string
value... I can do it by:

Dictionary<int, Dictionary<int, string>> d = new ...
d[4953][1344] = "hello";

This is fine if there is only two levels... but I have a situation where
I want to declare one with more levels, lets just say for the sake of the
discussion that I need 4 levels... Using the above, I would have to do:

D<int, D<int, D<int, D<int, string>>>> d = new ...
d[2938][2375][9284378][123491] = "4 levels";

(I write D<...> instead of Dictionary for formatting.)

Obviously this can get pretty unwieldly. If I only had to declare it once
then it wouldn't be a big deal... But when I want to start writing
functions that pass these structures around, it just feels crazy to do it
this way:

public void MyFunc(
D<int, D<int, D<int, D<int, string>>>> input,
D<int, D<int, D<int, D<int, string>>>> map,
D<int, D<int, D<int, D<int, string>>>> output );

In C++, I could do a typedef to declare it once and then just refer to it
by the typedef name... I don't think anything similar in C# exists, but
I'm hopeful. The #define statement obviously doesn't do what I need it to
either.

Any suggestions?

-mdb
 
How about
Dictionary<int[], string> d = new Dictionary<int[], string>(200);

//post entry at (4,3):
int[] p = new int[] {4,3};
d.add(p, "value for (4,3)");

or words to that effect....
I believe this is at least as fast, probably faster, than nested
dictionaries.
 
Fred Mellender said:
How about
Dictionary<int[], string> d = new Dictionary<int[], string>(200);

//post entry at (4,3):
int[] p = new int[] {4,3};
d.add(p, "value for (4,3)");

or words to that effect....
I believe this is at least as fast, probably faster, than nested
dictionaries.

I don't think the standard array is going to work very well, but a struct
with custom GetHashCode() implementation should work quite well.
Michael Bray said:
Lets say I want to have a sparse lookup by two int values to a string
value... I can do it by:

Dictionary<int, Dictionary<int, string>> d = new ...
d[4953][1344] = "hello";

This is fine if there is only two levels... but I have a situation
where
I want to declare one with more levels, lets just say for the sake of the
discussion that I need 4 levels... Using the above, I would have to do:

D<int, D<int, D<int, D<int, string>>>> d = new ...
d[2938][2375][9284378][123491] = "4 levels";

(I write D<...> instead of Dictionary for formatting.)

Obviously this can get pretty unwieldly. If I only had to declare it
once
then it wouldn't be a big deal... But when I want to start writing
functions that pass these structures around, it just feels crazy to do it
this way:

public void MyFunc(
D<int, D<int, D<int, D<int, string>>>> input,
D<int, D<int, D<int, D<int, string>>>> map,
D<int, D<int, D<int, D<int, string>>>> output );

In C++, I could do a typedef to declare it once and then just refer to it
by the typedef name... I don't think anything similar in C# exists, but
I'm hopeful. The #define statement obviously doesn't do what I need it
to
either.

Any suggestions?

-mdb
 
I don't think the standard array is going to work very well, but a struct
with custom GetHashCode() implementation should work quite well.

I agree with Ben, standard array does not do for it.
If you are going to use a struct as a key you have to override two
methods:
GetHashCode() and Equals(Object obj).

Also you may try next approach, it is more simple, but I am not sure
that it is optimal.

Dictionary<string, string> dict = new Dictionary<string, string>();

string key = string.Format("{0}-{1}-{2}", 2938, 2375, 9284378);
dict.Add(key, "some value");

Regars,
Mykola
http://marss.co.ua
 
Fred Mellender said:
How about
Dictionary<int[], string> d = new Dictionary<int[], string>(200);

//post entry at (4,3):
int[] p = new int[] {4,3};
d.add(p, "value for (4,3)");

or words to that effect....
I believe this is at least as fast, probably faster, than nested
dictionaries.
That wouldn't work because the Arrays are compared by reference not by
value. Try:

new int[]{1,2}.Equals(new int[]{1,2})

It returns false.

Christof
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Back
Top