72 lines
2.5 KiB
C
72 lines
2.5 KiB
C
|
#ifndef __HASH_H
|
||
|
#define __HASH_H
|
||
|
|
||
|
#define STR_LEN 100
|
||
|
#include <string.h>
|
||
|
#include "arrays.h"
|
||
|
#include "tll.h"
|
||
|
|
||
|
//#define DBG
|
||
|
|
||
|
class HashItem {
|
||
|
public:
|
||
|
HashItem(void) {strcpy(str, ""); value = 0;}
|
||
|
HashItem(char *s, int v);
|
||
|
void Set(char *s, int v);
|
||
|
int operator == (const HashItem &h_item) {return !strcmp(str, h_item.str);}
|
||
|
friend ostream &operator<<(ostream &s, HashItem &h_item) {
|
||
|
s<<h_item.str<<":"<<h_item.value; return s;}
|
||
|
int value;
|
||
|
char str[STR_LEN];
|
||
|
};
|
||
|
|
||
|
class HashTable {
|
||
|
public:
|
||
|
HashTable(int size) {data.Allocate(size);}
|
||
|
void Insert(HashItem &h_item); // we insert a complete item, i.e. the
|
||
|
// str and its assoc.
|
||
|
int Retrieve(HashItem &h_item); // returns 0 if item is not found
|
||
|
// we retrieve a complete item, the value
|
||
|
// of assoc is not specified beforehand,
|
||
|
// and is returned in h_item
|
||
|
friend ostream &operator<<(ostream &s, HashTable &ht);
|
||
|
private:
|
||
|
Array<LinkedList<HashItem> > data;
|
||
|
unsigned HashFunc(char *str);
|
||
|
};
|
||
|
|
||
|
// these store ints, not strings
|
||
|
class HashItemInt {
|
||
|
public:
|
||
|
HashItemInt(void) {key = 0; value = 0;}
|
||
|
HashItemInt(int k, int v) {key = k; value = v;}
|
||
|
// the copy constructor
|
||
|
HashItemInt(const HashItemInt &h_item) {key = h_item.key; value = h_item.value;}
|
||
|
void Set(int k, int v) {key = k; value = v;}
|
||
|
int operator == (const HashItemInt &h_item)
|
||
|
{return ((key == h_item.key) ? 1 : 0);}
|
||
|
HashItemInt &operator = (const HashItemInt &h_item);
|
||
|
friend ostream &operator<<(ostream &s, HashItemInt &h_item) {
|
||
|
s<<h_item.key<<":"<<h_item.value; return s;}
|
||
|
int value, key;
|
||
|
};
|
||
|
|
||
|
class HashTableInt {
|
||
|
public:
|
||
|
HashTableInt(int size) {data.Allocate(size);}
|
||
|
void Insert(HashItemInt &h_item); // we insert a complete item, i.e. the
|
||
|
// str and its assoc.
|
||
|
int Retrieve(HashItemInt &h_item); // returns 0 if item is not found
|
||
|
// we retrieve a complete item, the value
|
||
|
// of assoc is not specified beforehand,
|
||
|
// and is returned in h_item
|
||
|
friend ostream &operator<<(ostream &s, HashTableInt &ht);
|
||
|
void PutInArray(Array<HashItemInt> &h_array, int &num_items); // puts it into a linear array
|
||
|
int ExtToInt(int key, int next_value);
|
||
|
private:
|
||
|
Array<LinkedList<HashItemInt> > data;
|
||
|
unsigned HashFunc(int key);
|
||
|
};
|
||
|
|
||
|
#endif
|