Re: Set-like collection in C#/.NET
public virtual object this[object key] { get; set; }Declaring Type:System.Collections.Hashtable
Code:
public virtual object this[object key]
{
get
{
uint num1;
uint num2;
Hashtable.bucket bucket1;
if (key == null)
{
throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
}
Hashtable.bucket[] bucketArray1 = this.buckets;
uint num3 = this.InitHash(key, bucketArray1.Length, out num1, out num2);
int num4 = 0;
do
{
int num5 = (int) (num1 % bucketArray1.Length);
bucket1 = bucketArray1[num5];
if (bucket1.key == null)
{
return null;
}
if (((bucket1.hash_coll & 0x7fffffff) == num3) && this.KeyEquals(key, bucket1.key))
{
return bucket1.val;
}
num1 += num2;
}
while ((bucket1.hash_coll < 0) && (++num4 < bucketArray1.Length));
return null;
}
set
{
this.Insert(key, value, false);
}
}
and it should be O(1)
public virtual bool ContainsKey(object key);Declaring Type:System.Collections.Hashtable
Code:
public virtual bool ContainsKey(object key)
{
uint num1;
uint num2;
Hashtable.bucket bucket1;
if (key == null)
{
throw new ArgumentNullException("key", Environment.GetResourceString("ArgumentNull_Key"));
}
Hashtable.bucket[] bucketArray1 = this.buckets;
uint num3 = this.InitHash(key, bucketArray1.Length, out num1, out num2);
int num4 = 0;
do
{
int num5 = (int) (num1 % bucketArray1.Length);
bucket1 = bucketArray1[num5];
if (bucket1.key == null)
{
return false;
}
if (((bucket1.hash_coll & 0x7fffffff) == num3) && this.KeyEquals(bucket1.key, key))
{
return true;
}
num1 += num2;
}
while ((bucket1.hash_coll < 0) && (++num4 < bucketArray1.Length));
return false;
}
and how similar to the perevious one!!and it should be O(1)
thank you very much :wave:
Re: Set-like collection in C#/.NET
how about a real world test? i use a hashtable to weed out the lines of a text file, that are the same as the previous day's text file.. a bit like sql minus union..
a text file 4 megabytes big, with 100000 lines, is processed in about a second:
read all of file one into hashtable, using the whole line as the key, and null as the value
read file 2 and call ContainsKey repeatedly on the hashtable.. if the HT doesnt containskey the line being read, write it out to a third output file.
i htink my program is IO bound, as it takes just less than a second for files only a few hundred lines long...
so in a word.. yes there is a set/collection that disallows duplicate values: use a hashtable with the values you want to store as the Key, and NULL as the value.
ht.put(myValue, null)
Re: Set-like collection in C#/.NET
Quote:
so in a word.. yes there is a set/collection that disallows duplicate values: use a hashtable with the values you want to store as the Key, and NULL as the value.
ht.put(myValue, null)
or wrap this behaviour in a class called Set which is what the last few posts have been about. :)
Re: Set-like collection in C#/.NET
How about simply implementing the CollectionBase class? it's pretty handy for these kind of stuff...
Re: Set-like collection in C#/.NET
Sorry Andy but I don't think that is a good idea. CollectionBase gives you nothing more than an underlying list.
Anything to do with Set functionality you would have to write yourself, which with a basic implementation gives you something similar to
mehdi62b's solution.
Re: Set-like collection in C#/.NET
Quote:
Originally Posted by
Norfy
Couldn't find one myself so knocked up a simple implementation of Set
Was about to start on implementing exactly this, then decided to have a quick look on the net first and came across your post.
Very nice implementation, cheers!
Re: Set-like collection in C#/.NET
Look at System.Collections.Generic.HashSet<T> generic class in .NET 3.5.
As a generic it is type-safe and avoids the boxing/unboxing operations if the "elements" are value types.