home *** CD-ROM | disk | FTP | other *** search
- #include "hash.h"
-
- hash_index::hash_index(hash_table & base) : double_index(*base.table[0])
- {
- table = &base;
- hash_number = 0;
- }
-
- void hash_index::attach(hash_table & base)
- {
- table = &base;
- rewind();
- }
-
- void hash_index::rewind()
- {
- hash_number = 0;
- double_index::attach(*table->table[0]);
- }
-
- void hash_index::to_end()
- {
- hash_number = table->table_size - 1;
- double_index::attach(*table->table[hash_number]);
- double_index::to_end();
- }
-
- int hash_index::has(hashable *arg, int exact)
- {
- hash_number = arg->hash_value() % table->table_size;
- double_index::attach(*table->table[hash_number]);
- return double_index::has(arg, exact);
- }
-
- void hash_index::destroy()
- {
- if (*this == 0)
- return;
- table->object_count--;
- double_index::destroy();
- }
-
- hashable *hash_index::remove()
- {
- if (*this == 0)
- return NULL;
- table->object_count--;
- return (hashable *) double_index::remove();
- }
-
- void hash_index::operator ++ ( int )
- {
- if (*this != 0)
- double_index::operator ++ ( 1 ); // force postfix
- while (*this == 0 && ++hash_number < table->table_size)
- double_index::attach(*table->table[hash_number]);
- }
-
- void hash_index::operator -- ( int )
- {
- if (*this != 0)
- double_index::operator -- ( 1 );
- while (*this == 0 && hash_number-- > 0) {
- double_index::attach(*table->table[hash_number]);
- double_index::to_end();
- }
- }
-
- typedef double_list *double_list_ptr;
-
- hash_table::hash_table(int t_size)
- {
- int i;
-
- table_size = t_size;
- table = new double_list_ptr[t_size];
- for (i = 0; i < table_size; i++)
- table[i] = new double_list;
- }
-
- void hash_table::put(hashable *arg)
- {
- table[arg->hash_value() % table_size]->put(arg);
- object_count++;
- }
-
- hashable *hash_table::remove(hashable *arg)
- {
- hash_index i(*this);
-
- if (i.has(arg))
- return i.remove();
- else
- return NULL;
- }
-
- int hash_table::has(hashable *arg, int exact)
- {
- hash_index i(*this);
-
- return i.has(arg, exact);
- }
-
- void hash_table::empty(char kill)
- {
- int i;
-
- for (i = 0; i < table_size; i++)
- table[i]->empty(kill);
- }
-
- hash_table::~hash_table()
- {
- int i;
- for (i = 0; i < table_size; i++)
- delete table[i];
- delete table;
- }
-
-