home *** CD-ROM | disk | FTP | other *** search
/ Stars of Shareware: Programmierung / SOURCE.mdf / programm / msdos / pascal / rehack / contain / hash.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1993-06-21  |  2.2 KB  |  120 lines

  1. #include "hash.h"
  2.  
  3. hash_index::hash_index(hash_table & base) : double_index(*base.table[0])
  4.  {
  5.   table = &base;
  6.   hash_number = 0;
  7.  }
  8.  
  9. void hash_index::attach(hash_table & base)
  10.  {
  11.   table = &base;
  12.   rewind();
  13.  }
  14.  
  15. void hash_index::rewind()
  16.  {
  17.   hash_number = 0;
  18.   double_index::attach(*table->table[0]);
  19.  }
  20.  
  21. void hash_index::to_end()
  22.  {
  23.   hash_number = table->table_size - 1;
  24.   double_index::attach(*table->table[hash_number]);
  25.   double_index::to_end();
  26.  }
  27.  
  28. int hash_index::has(hashable *arg, int exact)
  29.  {
  30.   hash_number = arg->hash_value() % table->table_size;
  31.   double_index::attach(*table->table[hash_number]);
  32.   return double_index::has(arg, exact);
  33.  }
  34.  
  35. void hash_index::destroy()
  36.  {
  37.   if (*this == 0)
  38.     return;
  39.   table->object_count--;
  40.   double_index::destroy();
  41.  }
  42.  
  43. hashable *hash_index::remove()
  44.  {
  45.   if (*this == 0)
  46.     return NULL;
  47.   table->object_count--;
  48.   return (hashable *) double_index::remove();
  49.  }
  50.  
  51. void hash_index::operator ++ ( int )
  52.  {
  53.   if (*this != 0)
  54.     double_index::operator ++ ( 1 );  // force postfix
  55.   while (*this == 0 && ++hash_number < table->table_size)
  56.     double_index::attach(*table->table[hash_number]);
  57.  }
  58.  
  59. void hash_index::operator -- ( int )
  60.  {
  61.   if (*this != 0)
  62.     double_index::operator -- ( 1 );
  63.   while (*this == 0 && hash_number-- > 0) {
  64.     double_index::attach(*table->table[hash_number]);
  65.     double_index::to_end();
  66.    }
  67.  }
  68.  
  69. typedef double_list *double_list_ptr;
  70.  
  71. hash_table::hash_table(int t_size)
  72.  {
  73.   int i;
  74.  
  75.   table_size = t_size;
  76.   table = new double_list_ptr[t_size];
  77.   for (i = 0; i < table_size; i++)
  78.     table[i] = new double_list;
  79.  }
  80.  
  81. void hash_table::put(hashable *arg)
  82.  {
  83.   table[arg->hash_value() % table_size]->put(arg);
  84.   object_count++;
  85.  }
  86.  
  87. hashable *hash_table::remove(hashable *arg)
  88.  {
  89.   hash_index i(*this);
  90.  
  91.   if (i.has(arg))
  92.     return i.remove();
  93.    else
  94.     return NULL;
  95.  }
  96.  
  97. int hash_table::has(hashable *arg, int exact)
  98.  {
  99.   hash_index i(*this);
  100.  
  101.   return i.has(arg, exact);
  102.  }
  103.  
  104. void hash_table::empty(char kill)
  105.  {
  106.   int i;
  107.  
  108.   for (i = 0; i < table_size; i++)
  109.     table[i]->empty(kill);
  110.  }
  111.  
  112. hash_table::~hash_table()
  113.  {
  114.   int i;
  115.   for (i = 0; i < table_size; i++)
  116.     delete table[i];
  117.   delete table;
  118.  }
  119.  
  120.