473,396 Members | 1,816 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Generic recursiveness

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
Sep 4 '07 #1
4 1103
How about
Dictionary<int[], stringd = 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.
"Michael Bray" <mb*****************@you.figure.it.out.comwrote in message
news:Xn****************************@207.46.248.16. ..
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

Sep 4 '07 #2

"Fred Mellender" <no****************@frontiernet.netwrote in message
news:Ut******************@news01.roc.ny...
How about
Dictionary<int[], stringd = 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" <mb*****************@you.figure.it.out.comwrote in
message news:Xn****************************@207.46.248.16. ..
>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


Sep 5 '07 #3
On 5 , 02:59, "Ben Voigt [C++ MVP]" <r...@nospam.nospamwrote:
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, stringdict = 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

Sep 5 '07 #4
"Fred Mellender" <no****************@frontiernet.netschrieb im Newsbeitrag
news:Ut******************@news01.roc.ny...
How about
Dictionary<int[], stringd = 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
Sep 7 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

17
by: Andreas Huber | last post by:
What follows is a discussion of my experience with .NET generics & the ..NET framework (as implemented in the Visual Studio 2005 Beta 1), which leads to questions as to why certain things are the...
1
by: Arthur Dent | last post by:
Hi all... Heres what im looking to do.... I have a Generic class i wrote. Now, on another class, i want to add a method which can take in an object of my generic class... but the catch is, i want...
3
by: Tigger | last post by:
I have an object which could be compared to a DataTable/List which I am trying to genericify. I've spent about a day so far in refactoring and in the process gone through some hoops and hit some...
0
by: crazyone | last post by:
I've got a gaming framework i'm building and i want to save myself the trouble of reading and writting the complete game data to a custom file and load/save it to an XML file but i'm getting...
4
by: Andrew Ducker | last post by:
I have a collection of classes descending from a single root class (let's call it RootClass). They all currently have a property of Logical, of type Logical. However they actually return a...
9
by: mps | last post by:
I want to define a class that has a generic parameter that is itself a generic class. For example, if I have a generic IQueue<Tinterface, and class A wants to make use of a generic class that...
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
7
by: Dave | last post by:
I've got these declarations: public delegate void FormDisplayResultsDelegate<Type>(Type displayResultsValue); public FormDisplayResultsDelegate<stringdisplayMsgDelegate; instantiation:...
26
by: raylopez99 | last post by:
Here is a good example that shows generic delegate types. Read this through and you'll have an excellent understanding of how to use these types. You might say that the combination of the generic...
2
by: SimonDotException | last post by:
I am trying to use reflection in a property of a base type to inspect the properties of an instance of a type which is derived from that base type, when the properties can themselves be instances of...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.