|
Here u go (just over 100 lines), very simple and fast.
using System;
namespace leppie
{
public class Hashtable
{
struct bucket
{
internal object key;
internal object val;
}
private int size;
private bucket[] buckets;
private static readonly uint[] primes = {
11,17,23,29,37,47,59,71,89,107,131,163,197,239,293,353,431,521,631,761,919,
1103,1327,1597,1931,2333,2801,3371,4049,4861,5839,7013,8419,10103,12143,14591,
17519,21023,25229,30293,36353,43627,52361,62851,75431,90523, 108631, 130363,
156437, 187751, 225307, 270371, 324449, 389357, 467237, 560689, 672827, 807403,
968897, 1162687, 1395263, 1674319, 2009191, 2411033, 2893249, 3471899, 4166287,
4999559, 5999471, 7199369 };
public Hashtable()
{
size = 0;
buckets = new bucket[0];
}
private static uint Hash(object key)
{
return (uint) key.GetHashCode();
}
private static bool compare(object key1, object key2)
{
return key1.Equals(key2);
}
private uint GetCap()
{
uint i = 0, l = 0;
for (;i < size;i++)
l += primes[i];
return l;
}
private int AddBucketSet()
{
bucket[] newbuckets = new bucket[GetCap() + primes[size]];
Array.Copy(buckets, newbuckets, buckets.Length);
buckets = newbuckets;
return ++size;
}
public void Add(object key, object value)
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size;i++)
{
pos += hash % primes[i];
if (buckets[pos].key == null)
{
buckets[pos].key = key;
buckets[pos].val = value;
return;
}
}
AddBucketSet();
Add(key, value);
}
public bool Contains(object key)
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size;i++)
{
pos += hash % primes[i];
if (compare(buckets[pos].key,key))
return true;
}
return false;
}
public object this[object key]
{
get
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size;i++)
{
pos += hash % primes[i];
if (compare(buckets[pos].key, key))
return buckets[pos].val;
}
return null;
}
set
{
if (!Contains(key))
Add(key, value);
else
{
for (uint i = 0, pos = 0, hash = Hash(key); i < size;i++)
{
pos += hash % primes[i];
if (compare(buckets[pos].key,key))
buckets[pos].val = value;
}
}
}
}
}
}
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
Thanks!
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
No problem, I never did need removals, so I did do that, too much complication with rehashing, seeing that I'm preventing it in the first place with a bit of voodoo in the resizing .
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
What's its memory consumption compared to, say, a ListDictionary? A .NET ListDictionary takes about 1/5 the space of a .NET Hashtable - is it the same here?
"Blessed are the peacemakers, for they shall be called sons of God." - Jesus
"You must be the change you wish to see in the world." - Mahatma Gandhi
|
|
|
|
|
Hi, again... I've used your code and translated it into C++, for the purposes of the FLUID project. But, I've been having problems with the code...
Your code seems to try a location hash % prime1 if that slot is occupied it will try hash % prime2 + hash % prime1 and so on, always resizing if at the proposed index there isn't a bucket present allocated. But, I've been having problems with this. My copy of the code will generally speaking make the array to twice as many buckets as it needs for a given amount of data. So for 256 entries 490 buckets are allocated, for 4096 entries 11087 buckets are allocated! At first I thought it might be the hashing function, but I have tried 3. I'm just wondering if you've come across the phenomenon in your usage of the code - or if you have anything to suggest. Any help you can give will be much appreciated.
*¨¨`)
¸¸.·´ ¸.·*¨¨`)
(¸¸.·* ¸ .·*
¸¸.·*
(¸¸.~~> Joel Holdsworth.
|
|
|
|
|
Ussually a hashtable will occupy around 5 to 1 space, this is normal, so 3 to 1 aint that bad
leppie::AllocCPArticle("Zee blog"); Seen on my Campus BBS: Linux is free...coz no-one wants to pay for it.
|
|
|
|
|
I'm trying to add a reference in my project to DirectX 9 components. I have the SDK installed, but no references for DirectX show up when I try to add references. I can't find what files to add, so I'm stuck. I'm running the Academic Version of the VS.NET 2003. Any suggestions?
|
|
|
|
|
I am writing a tiny screensaver and it works fine except for the preview. I know it passes the /p parameter to my program when it wants to preview it but what do i do then?
help is greatly appreciated
|
|
|
|
|
I want some methods of one class to be only visible to my desired classes in the same project. So I need some thing more limited than internal access modifier. Can anybody help me?
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
You could write a proxy class that sits in between your other classes and your "internal" class. This class would use your "internal" class and only expose those methods you want.
-Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
Sounds like what you want is an identity demand using .NET's code access security and strong names. If those pointers don't get you headed in the right direction, I'll make a quick sample later today.
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Maybe, but for now, I need them for myself. I just want to follow design patterns. For example I have a Control class and a TransactionManager class. I just want to be able to work with Control class, While I am yet able to do my work with TransactionManager class.
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Hmm, based on this reply it's clear I completely misunderstood what you were asking for in the first question. Unfortunately I can't quite parse this one at all.
meisi wrote:
I just want to be able to work with Control class, While I am yet able to do my work with TransactionManager class.
What exactly do you mean by 'work with' in this context?
--
-Blake (com/bcdev/blake)
|
|
|
|
|
Suppose that there are 3 methods in TransactionManager class, which are invoked by one method in Control class. So when I call that single method from Control class, 3 methods are called from TransactionManager class.
I can do 2 things,
1- Creating an instance of Control class and calling its single method.
2- Creating an instance of TransactionManager class and calling those 3 methods myself.
I don't want to be able to do the second one. I mean I want the TransactionManager to be visible(instanciatable) only for Control class.
I hope it be clear.
Somehow seems imposible, hum?
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Did you completely ignore my post before?
Why not make TransactionManager a private class inside Control? Then you can just make Control public and expose only the methods you want to expose.
I don't mean to sound harsh.... maybe I am just ignorant of you situation....
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
Thank you Nathan! That is exactly what I shoud do.
And excuse me, I misunderstood your first post
Don't forget, that's Persian Gulf not Arabian gulf!
|
|
|
|
|
Sorry about the harsh post... I was in a bad mood yesterday. Also, my first post was a little more (shall I say) abstract than I wanted it to be.
-Nathan
---------------------------
Hmmm... what's a signature?
|
|
|
|
|
Hi there,
here my problem: I added a Table to my webform and added some datarows and cells manually in it(with VS.net). Then i'm adding some new row's at runtime (table.rows.add(myrow)). If i now jump to another site and back (with server.transfer) only the data i inserted manually is present and the one insert at runtime is gone!
Can anyone help me solving this problem please?!
Thanx
Obi
|
|
|
|
|
Can you change the function that the thread calls after it's completed? In other words, is this right:
Thread thrd = new Thread(new ThreadStart(dummyobject.dummyfunction-1));
thrd.Start();
thrd.Join();
// is the following correct?
// are there any objects not destroyed or garbage collected by this?
thrd = new Thread(new ThreadStart(dummyobject.dummyfunction-2));
thrd.Start();
thrd.Join();
If not, how could you achieve this? how are Thread objects reusable?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
Actually, you're not reusing a Thread object, but merely a Thread variable that references a Thread object. In your example, thrd is only a reference variable. If is given an object reference when you instantiate a new Thread using the new operator.
So, that is fine, but you won't be able to access the first thread you created anymore, since you re-referenced a different thread. All access to thrd now would be accessing the second Thread that you created.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
That's perfect, but now, what if I actually want to destroy the first object before making thrd reference the second one? Aren't I actually supposed to do that?
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
I think you need to either pick up a good book on .NET or read the SDK documentation. You are missing one of the most crucial aspects of .NET's CLR - garbage collection. When the thread exits, the memory for that thread object will eventually be reclaimed. If an object implements IDisposable (usually classes that wrap system objects, like files or mutexes), you are supposed to dispose it but they will be diposed when destroyed when the app exits.
In the case of threads, you don't need to worry about it. thrd is only a reference to an object that will get cleaned up. As long as you assign to it, you're not changing the object at all.
These are some of the most fundamental pieces of the .NET framework (and Java, for that matter) and should be explored.
-----BEGIN GEEK CODE BLOCK-----
Version: 3.21
GCS/G/MU d- s: a- C++++ UL@ P++(+++) L+(--) E--- W+++ N++ o+ K? w++++ O- M(+) V? PS-- PE Y++ PGP++ t++@ 5 X+++ R+@ tv+ b(-)>b++ DI++++ D+ G e++>+++ h---* r+++ y+++
-----END GEEK CODE BLOCK-----
|
|
|
|
|
I have read books, it's just that none of them were advanced enough to mention this in detail. I really guessed the answer, but I can never be sure. Could you please recommend a good enough advanced book about these issues?
Thanks for everything.
Sammy
"A good friend, is like a good book: the inside is better than the cover..."
|
|
|
|
|
hello there
im not a pro c#, u might say im a beginer.
but i best learn from expiriance so i decided to
program a "complex" application.
ofcourse every software need a configuration form
so i made up a class with all members/vars to stream
to the configuration file.
and now, here is my question:
instead of writing members values to file
one by one, can i make a trick to loop trough all
members?
so if tommorow i add 5 more values (font, loc, color..etc)
the saving method wont have to change.
is it possible?
|
|
|
|
|
Sounds like you are reinventing a wheel. The .NET framework has support for automatic serialization. Its pretty much as simple as adding a [Searializable] attribute to your class definition. Have a look at this[^].
Ryan
|
|
|
|
|