Click here to Skip to main content
15,895,370 members
Home / Discussions / C#
   

C#

 
QuestionPassing methods to ThreadStart - Possible? Pin
stan282-May-05 8:52
stan282-May-05 8:52 
AnswerRe: Passing methods to ThreadStart - Possible? Pin
Judah Gabriel Himango2-May-05 9:05
sponsorJudah Gabriel Himango2-May-05 9:05 
AnswerRe: Passing methods to ThreadStart - Possible? Pin
Marc Clifton2-May-05 10:52
mvaMarc Clifton2-May-05 10:52 
AnswerRe: Passing methods to ThreadStart - Possible? Pin
leppie2-May-05 14:29
leppie2-May-05 14:29 
GeneralGeneric Hashtable Pin
Anonymous2-May-05 8:48
Anonymous2-May-05 8:48 
GeneralRe: Generic Hashtable Pin
Marc Clifton2-May-05 10:53
mvaMarc Clifton2-May-05 10:53 
GeneralRe: Generic Hashtable Pin
Anonymous3-May-05 5:09
Anonymous3-May-05 5:09 
GeneralRe: Generic Hashtable Pin
leppie2-May-05 14:46
leppie2-May-05 14:46 
Here's my 'simple' take. Some features are missing and some things are slighty different to a traditional hashtable. Bit more than a 100 lines Smile | :)

public class GrowingHashtable
{
  struct bucket
  {
    internal object key;
    internal object val;
  }

  int size;
  bucket[] buckets;

  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 GrowingHashtable()
  {
    size = 0;
    buckets = new bucket[0];
  }

  static uint Hash(object key)
  { 
    return unchecked( (uint) key.GetHashCode());
  }

  uint GetCap()
  {
    uint i = 0, l = 0;
    for (;i < size;i++)
    {
      l += primes[i];
    }
    return l;
  }

  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)
      {
        //MUST assign by ref
        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 (key.Equals(buckets[pos].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 (key.Equals(buckets[pos].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 (key.Equals(buckets[pos].key))
          {
            buckets[pos].val = value;
          }
        }
      }
    }
  }
}




xacc-ide 0.0.15 now with C#, MSIL, C, XML, ASP.NET, Nemerle, MyXaml and HLSL coloring - Screenshots
GeneralOle Server Pin
BigAnyon2-May-05 5:58
BigAnyon2-May-05 5:58 
GeneralVisual style icons / bitmaps Pin
Anonymous2-May-05 5:45
Anonymous2-May-05 5:45 
GeneralRe: Visual style icons / bitmaps Pin
Judah Gabriel Himango2-May-05 7:05
sponsorJudah Gabriel Himango2-May-05 7:05 
GeneralRe: Visual style icons / bitmaps Pin
MoustafaS2-May-05 8:49
MoustafaS2-May-05 8:49 
GeneralEasy file I/O Pin
briggs_w2-May-05 4:58
briggs_w2-May-05 4:58 
GeneralRe: Easy file I/O Pin
Heath Stewart2-May-05 5:11
protectorHeath Stewart2-May-05 5:11 
GeneralRe: Easy file I/O Pin
Marc Clifton2-May-05 5:24
mvaMarc Clifton2-May-05 5:24 
General... also ... Pin
Marc Clifton2-May-05 5:26
mvaMarc Clifton2-May-05 5:26 
GeneralRe: Easy file I/O Pin
briggs_w5-May-05 5:50
briggs_w5-May-05 5:50 
GeneralAccess a flat file at http location Pin
mrinmayeek2-May-05 4:27
mrinmayeek2-May-05 4:27 
GeneralRe: Access a flat file at http location Pin
Heath Stewart2-May-05 5:12
protectorHeath Stewart2-May-05 5:12 
GeneralControl Instantiation Pin
terrier_jack2-May-05 4:25
terrier_jack2-May-05 4:25 
GeneralRe: Control Instantiation Pin
Marc Clifton2-May-05 5:08
mvaMarc Clifton2-May-05 5:08 
GeneralRe: Control Instantiation Pin
MoustafaS2-May-05 8:54
MoustafaS2-May-05 8:54 
GeneralHTML control Pin
Anonymous2-May-05 3:39
Anonymous2-May-05 3:39 
GeneralRe: HTML control Pin
Heath Stewart2-May-05 5:16
protectorHeath Stewart2-May-05 5:16 
QuestionHow to Connect Managed and UnManaged code for Socket Programming Pin
Shashidharreddy2-May-05 3:08
Shashidharreddy2-May-05 3:08 

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.