_ch_array.c
Upload User: gzelex
Upload Date: 2007-01-07
Package Size: 707k
Code Size: 4k
Development Platform:

MultiPlatform

  1. /*******************************************************************************
  2. +
  3. +  LEDA-R  3.2.3
  4. +
  5. +  _ch_array.c
  6. +
  7. +  Copyright (c) 1995  by  Max-Planck-Institut fuer Informatik
  8. +  Im Stadtwald, 66123 Saarbruecken, Germany     
  9. +  All rights reserved.
  10. *******************************************************************************/
  11. #include <LEDA/impl/ch_array.h>
  12. //------------------------------------------------------------------------------
  13. //
  14. //  hashing array with chaining and table doubling
  15. //
  16. //  only integer/pointer keys
  17. //  no delete operation
  18. //
  19. //  S. Naeher (1994)
  20. //
  21. //------------------------------------------------------------------------------
  22. ch_array_elem ch_array::STOP;
  23. #define NIL GenPtr(this)
  24. /* #define HASH(x) (table + (int(x) & table_size_1)) */
  25. #define HASH(x) (table + (hash_fct(x) & table_size_1))
  26. ch_array::ch_array(int n) 
  27. { int ts = 1;
  28.   while (ts < n) ts <<= 1;
  29.   init_table(ts); 
  30. }
  31. GenPtr ch_array::lookup(GenPtr x) const
  32. { ch_array_item p = HASH(x);
  33.   STOP.k = x;
  34.   while (p->k != x) p = p->succ;
  35.   return (p == &STOP) ? nil : p;
  36. }
  37. GenPtr ch_array::access(GenPtr x) const
  38. { ch_array_item p = HASH(x);
  39.   STOP.k = x;
  40.   while (p->k != x) p = p->succ;
  41.   if (p == &STOP) init_inf(x);
  42.   else x = p->i;
  43.   return x;
  44. }
  45. GenPtr& ch_array::access(GenPtr x)
  46.   //if (x == NIL) error_handler(1,"internal error in ch_array");
  47.   ch_array_item p = HASH(x);
  48.   if (p->k == x) return p->i;
  49.   if (p->k == NIL)
  50.   { p->k = x;
  51.     init_inf(p->i);
  52.     return p->i;
  53.    }
  54.   STOP.k = x;
  55.   ch_array_item q = p->succ; 
  56.   while (q->k != x) q = q->succ;
  57.   if (q != &STOP) return q->i;
  58.   // index x not present, insert it
  59.   if (free == table_end)   // table full: rehash
  60.   { rehash();
  61.     p = HASH(x);
  62.    }
  63.   q = free++;
  64.   q->k = x;
  65.   init_inf(q->i);
  66.   q->succ = p->succ;
  67.   p->succ = q;
  68.   return q->i;
  69. }
  70. void ch_array::destroy() 
  71. { for(ch_array_item p = table; p < table_end; p++)
  72.     if (p->k != NIL) clear_inf(p->i);
  73.   delete[] table; 
  74.  }
  75. void ch_array::init_table(int T)
  76.   table_size = T;
  77.   table_size_1 = T-1;
  78.   table = new ch_array_elem[2*T];
  79.   free      = table + T;
  80.   table_end = free  + T;
  81.   ch_array_item p=table;
  82.   while (p<free) 
  83.   { p->k = NIL;
  84.     p->succ = &STOP;
  85.     p++;
  86.    }
  87.   while (p<table_end) 
  88.   { p->k = NIL;
  89.     p++;
  90.    }
  91. }
  92. #define INSERT(x,y)
  93. { ch_array_item q = HASH(x);
  94.   if (q->k == NIL)
  95.     { q->k = x;
  96.       q->i = y;
  97.      }
  98.   else
  99.    { free->k = x;
  100.      free->i = y;
  101.      free->succ = q->succ;
  102.      q->succ = free++;
  103.    }
  104. }
  105. void ch_array::rehash()
  106.   ch_array_item old_table = table;
  107.   ch_array_item old_table_end = table_end;
  108.   
  109.   init_table(2*table_size);
  110.   for(ch_array_item p = old_table; p < old_table_end; p++)
  111.       if (p->k != NIL) INSERT(p->k,p->i);
  112.   delete[] old_table;
  113. }
  114. ch_array_item ch_array::first_item() const
  115. { ch_array_item p = table;
  116.   while (p < table_end && p->k == NIL) p++;
  117.   return (p < table_end) ? p : 0;
  118.  }
  119. ch_array_item ch_array::next_item(ch_array_item p) const
  120. { p++;
  121.   while (p < table_end && p->k == NIL) p++;
  122.   return (p < table_end) ? p : 0;
  123.  }
  124. ch_array::ch_array(const ch_array& D)
  125.   init_table(D.table_size);
  126.   for(ch_array_item p = D.table; p < D.table_end; p++) 
  127.   { if (p->k != GenPtr(&D)) 
  128.     { INSERT(p->k,p->i);
  129.       D.copy_inf(p->i);
  130.      }
  131.    }
  132. }
  133. ch_array& ch_array::operator=(const ch_array& D)
  134.   destroy();
  135.   init_table(D.table_size);
  136.   for(ch_array_item p = D.table; p < D.table_end; p++) 
  137.   { if (p->k != GenPtr(&D)) 
  138.     { INSERT(p->k,p->i);
  139.       copy_inf(p->i);
  140.      }
  141.    }
  142.   return *this;
  143. }
  144. void ch_array::print()
  145. { for (int i=0; i<table_size; i++)
  146.   { ch_array_item p = table + i;
  147.     if (p->k != NIL)
  148.     { int l = 0;
  149.       while(p != &STOP)
  150.       { l++; 
  151.         p = p->succ;
  152.        }
  153.       cout << string("L(%d) = %d",i,l) << endl;
  154.      }
  155.    }
  156. }
  157.