Click here to Skip to main content
15,891,833 members
Home / Discussions / C#
   

C#

 
GeneralRe: Transparent Background for PocketPC Pin
Rowdy12330-Sep-03 14:22
Rowdy12330-Sep-03 14:22 
GeneralRe: Transparent Background for PocketPC Pin
J. Dunlap30-Sep-03 14:28
J. Dunlap30-Sep-03 14:28 
QuestionFile/Folder Name Problems - How to get around? Pin
Khang Nguyen30-Sep-03 12:44
Khang Nguyen30-Sep-03 12:44 
AnswerRe: File/Folder Name Problems - How to get around? Pin
Meysam Mahfouzi30-Sep-03 18:03
Meysam Mahfouzi30-Sep-03 18:03 
GeneralProblem with winsock programming with character encoding. Pin
Andrlage30-Sep-03 11:14
Andrlage30-Sep-03 11:14 
GeneralSpecialized Collection/List Pin
J. Dunlap30-Sep-03 10:44
J. Dunlap30-Sep-03 10:44 
GeneralRe: Specialized Collection/List Pin
leppie30-Sep-03 12:01
leppie30-Sep-03 12:01 
GeneralRe: Specialized Collection/List Pin
leppie30-Sep-03 12:22
leppie30-Sep-03 12:22 
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 = { // i wonder where they came from :)
      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;
        }
      }
      /* too small */
      AddBucketSet();

      /* just do it again */
      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.

GeneralRe: Specialized Collection/List Pin
J. Dunlap30-Sep-03 12:25
J. Dunlap30-Sep-03 12:25 
GeneralRe: Specialized Collection/List Pin
leppie30-Sep-03 12:30
leppie30-Sep-03 12:30 
GeneralRe: Specialized Collection/List Pin
J. Dunlap30-Sep-03 13:37
J. Dunlap30-Sep-03 13:37 
GeneralRe: Specialized Collection/List Pin
Joel Holdsworth14-Oct-03 12:09
Joel Holdsworth14-Oct-03 12:09 
GeneralRe: Specialized Collection/List Pin
leppie14-Oct-03 17:10
leppie14-Oct-03 17:10 
GeneralDirectX 9 Pin
diltsman30-Sep-03 8:30
diltsman30-Sep-03 8:30 
GeneralScreensaver Pin
obelisk2930-Sep-03 8:25
obelisk2930-Sep-03 8:25 
QuestionMore limited than internal? Pin
Meysam Mahfouzi30-Sep-03 5:59
Meysam Mahfouzi30-Sep-03 5:59 
AnswerRe: More limited than internal? Pin
Nathan Blomquist30-Sep-03 10:48
Nathan Blomquist30-Sep-03 10:48 
AnswerRe: More limited than internal? Pin
Blake Coverett30-Sep-03 11:39
Blake Coverett30-Sep-03 11:39 
GeneralRe: More limited than internal? Pin
Meysam Mahfouzi30-Sep-03 18:09
Meysam Mahfouzi30-Sep-03 18:09 
GeneralRe: More limited than internal? Pin
Blake Coverett30-Sep-03 19:26
Blake Coverett30-Sep-03 19:26 
GeneralRe: More limited than internal? Pin
Meysam Mahfouzi1-Oct-03 3:36
Meysam Mahfouzi1-Oct-03 3:36 
GeneralRe: More limited than internal? Pin
Nathan Blomquist2-Oct-03 10:44
Nathan Blomquist2-Oct-03 10:44 
GeneralRe: More limited than internal? Pin
Meysam Mahfouzi2-Oct-03 20:10
Meysam Mahfouzi2-Oct-03 20:10 
GeneralRe: More limited than internal? Pin
Nathan Blomquist3-Oct-03 4:36
Nathan Blomquist3-Oct-03 4:36 
GeneralTable won't store data Pin
Obi730-Sep-03 1:37
Obi730-Sep-03 1:37 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.