473,748 Members | 2,595 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

dictionary (hash table) with two values?

Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Titan

Apr 3 '07 #1
5 10215
ti***********@g mail.com <ti***********@ gmail.comwrote:
Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).
Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Apr 3 '07 #2


"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
ti***********@g mail.com <ti***********@ gmail.comwrote:
>Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too
Along these lines, you could also just store the values as an array...so
each key/value would be a key/array.

HTH,
Mythran
Apr 3 '07 #3
Mythran,

The only catch there is that if the two values are of different types,
then you have to have an array of object, which can have an overhead (if you
are boxing and unboxing value types).

In all cases, the solution that Jon suggested will provide a consistent,
expected behavior.
--
- Nicholas Paldino [.NET/C# MVP]
- mv*@spam.guard. caspershouse.co m

"Mythran" <ki********@hot mail.comwrote in message
news:uO******** ******@TK2MSFTN GP04.phx.gbl...
>

"Jon Skeet [C# MVP]" <sk***@pobox.co mwrote in message
news:MP******** *************** *@msnews.micros oft.com...
>ti***********@g mail.com <ti***********@ gmail.comwrote:
>>Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionarie s could get out of synch (due to a bug or something).

Just create a Pair type which has a pair of values, and store that as
the single value in the dictionary.

--
Jon Skeet - <sk***@pobox.co m>
http://www.pobox.com/~skeet Blog: http://www.msmvps.com/jon.skeet
If replying to the group, please do not mail me too

Along these lines, you could also just store the values as an array...so
each key/value would be a key/array.

HTH,
Mythran


Apr 3 '07 #4
PS

<ti***********@ gmail.comwrote in message
news:11******** *************@y 66g2000hsf.goog legroups.com...
Is there a typical way to create a dictionary (or hash table) with two
values, instead of one?

Currently, my data structure is TWO dictionaries, each with matching
and fully sychronized keys. This allows me to have one key with two
values (one value in each dictionary). This is ugly code as the
dictionaries could get out of synch (due to a bug or something).

Titan
As Jon suggested use an object with two properties. Here is a generic
version that you would use like:

DualValueDictio nary<long, string, intd = new DualValueDictio nary<long,
string, int>();
d.Add(123, "hello", 25);
if(d[123].Value1 == "hello") .....
public class DualValueDictio nary<Tkey, V1, V2: Dictionary<Tkey ,
DualValues<V1, V2>>
{
public void Add(Tkey key, V1 value1, V2 value2)
{
base.Add(key, new DualValues<V1,V 2>(value1, value2));
}
}
public class DualValues<V1, V2>
{
public DualValues(V1 v1, V2 v2)
{
this.value1 = v1;
this.value2 = v2;
}
private V1 value1;
public V1 Value1
{
get
{
return this.value1;
}
}
private V2 value2;
public V2 Value2
{
get
{
return this.value2;
}
}
}

PS
Apr 3 '07 #5
Jon, simple elegant solution. Thanks.

PS, thanks for the code. You just guided me around a few pitfalls I
would have got stuck on (I would not have thought of generic classes,
as I'm new to them).

Thanks all,
Titan

Apr 4 '07 #6

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

Similar topics

3
12370
by: Tony | last post by:
Hello. What is hash table? i could only find small reffrence to hash table .. but what exactly is this and how can i creat them? Is there any website with tutorial on this subject ? Thanks
4
548
by: Simone Battagliero | last post by:
I wrote a program which inserts and finds elements in an hash table. Each element of the table is a dinamic list, which holds all elements having the same hash value (calculated by an int hashFunction(char *key, int elements) ), so the table is an array of dynamic lists. The version of the program I've attached to this message works fine; but it's the result of an accurate debugging. I discovered that my problems consisted in just 4 lines...
2
2526
by: Ravi | last post by:
Hi, I am working on a winform app. I need to use an object which can store some information(key/value pairs) and also can be acessed by multiple threads(read/write). From what I heard Hash table is best suited for it. My question is Why hash table. why can't we use an array. I thought hash table uses more memory than array. Correct me if am wrong.
24
4307
by: kdotsky | last post by:
Hello, I am using some very large dictionaries with keys that are long strings (urls). For a large dictionary these keys start to take up a significant amount of memory. I do not need access to these keys -- I only need to be able to retrieve the value associated with a certain key, so I do not want to have the keys stored in memory. Could I just hash() the url strings first and use the resulting integer as the key? I think what I'm...
21
3219
by: Johan Tibell | last post by:
I would be grateful if someone had a minute or two to review my hash table implementation. It's not yet commented but hopefully it's short and idiomatic enough to be readable. Some of the code (i.e. the get_hash function) is borrowed from various snippets I found on the net. Thee free function could probably need some love. I have been thinking about having a second linked list of all entries so that the cost of freeing is in proportion to...
139
14207
by: ravi | last post by:
Hi can anybody tell me that which ds will be best suited to implement a hash table in C/C++ thanx. in advanced
2
1208
by: gurmeet07 | last post by:
hey guys i m new to .net and facing one problem on click of a button (saving the data to the hash table) like this : there are 7 textboxes and first textbox1.text is the key for hash table n all other textbox.text are values which are stored in an object type array........... object obj = { textBox2.Text, textBox3.Text, textBox4.Text, textBox5.Text, textBox6.Text }; ht.Add(textBox1.Text,obj);
23
5741
by: raylopez99 | last post by:
A quick sanity check, and I think I am correct, but just to make sure: if you have a bunch of objects that are very much like one another you can uniquely track them simply by using an ArrayList or Array, correct? An example: create the object, create an array, the stuff the object into the array. Later on, assume the object is mutable, the object changes, but you can find it, if you have enough state information to uniquely identify...
5
2143
Dheeraj Joshi
by: Dheeraj Joshi | last post by:
Hi.. I wrote following code. iindex = 0; foreach (XmlNode Labelnodes1 in Labelnode) { countlab = Labelnodes1.Attributes.Count; for (int i = 0; i < countlab; i++) {
0
8828
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9537
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
1
9319
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9243
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8241
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
0
6073
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4599
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
3309
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
2780
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.