Separate Chaining Hashtable

S

Shlomy Shivek

I couldn't find implementation of Separate Chaining Hashtable in
System.Collections while the regular Hastable didn't let me insert more


than one value to a bucket.


So, I inherited the regular Hashtable with the following:


public class SeparateChainingHashTable:Hashtable
{
public SeparateChainingHashTable()
{
}


public override void Add(object key, object value)
{
if(!this.ContainsKey(key))
base.Add(key,new ArrayList());
(this[key] as ArrayList).Add(value);
}


public override bool ContainsValue(object value)
{
ArrayList alChain=this[value.GetHashCode()] as
ArrayList;
if(alChain!=null)
return
alChain.IndexOf(value)!=-1?true:false;
return false;
}
}


Does anyone think this is not good enough ?
 
F

Frans Bouma [C# MVP]

Shlomy said:
I couldn't find implementation of Separate Chaining Hashtable in
System.Collections while the regular Hastable didn't let me insert
more


than one value to a bucket.


So, I inherited the regular Hashtable with the following:


public class SeparateChainingHashTable:Hashtable
{
public SeparateChainingHashTable()
{
}


public override void Add(object key, object value)
{
if(!this.ContainsKey(key))
base.Add(key,new ArrayList());
(this[key] as ArrayList).Add(value);
}


public override bool ContainsValue(object value)
{
ArrayList alChain=this[value.GetHashCode()] as
ArrayList;
if(alChain!=null)
return
alChain.IndexOf(value)!=-1?true:false;
return false;
}
}


Does anyone think this is not good enough ?

I always wonder why people ask that kind of questions in a newsgroup.
If it works for YOU, why do you need public confirmation from a random
group of people that it works or not? :)

The only issue I see with your code is that if I add multiple times
the same value with the same key I end up with that same value multiple
times in the hashtable, I'm not sure if you want that.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 
S

Shlomy Shivek

The idea to ask such question a "random group of people" is to get
comments, suggestions, ideas for something you are not sure of.

Thanks for your comment !
Shlomy said:
I couldn't find implementation of Separate Chaining Hashtable in
System.Collections while the regular Hastable didn't let me insert
more


than one value to a bucket.


So, I inherited the regular Hashtable with the following:


public class SeparateChainingHashTable:Hashtable
{
public SeparateChainingHashTable()
{
}


public override void Add(object key, object value)
{
if(!this.ContainsKey(key))
base.Add(key,new ArrayList());
(this[key] as ArrayList).Add(value);
}


public override bool ContainsValue(object value)
{
ArrayList alChain=this[value.GetHashCode()] as
ArrayList;
if(alChain!=null)
return
alChain.IndexOf(value)!=-1?true:false;
return false;
}
}


Does anyone think this is not good enough ?

I always wonder why people ask that kind of questions in a newsgroup.
If it works for YOU, why do you need public confirmation from a random
group of people that it works or not? :)

The only issue I see with your code is that if I add multiple times
the same value with the same key I end up with that same value multiple
times in the hashtable, I'm not sure if you want that.

FB

--
------------------------------------------------------------------------
Lead developer of LLBLGen Pro, the productive O/R mapper for .NET
LLBLGen Pro website: http://www.llblgen.com
My .NET blog: http://weblogs.asp.net/fbouma
Microsoft MVP (C#)
------------------------------------------------------------------------
 

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

Top