forked from psi46/psi46test
-
Notifications
You must be signed in to change notification settings - Fork 0
/
htable.h
148 lines (125 loc) · 2.88 KB
/
htable.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
// htable2.h
#pragma once
template <class T>
class CHashTable
{
class CHashTableEntry
{
CHashTableEntry *next;
CHashTableEntry *prev;
char *s;
T value;
public:
friend class CHashTable<T>;
};
unsigned int it;
CHashTableEntry *itEl;
unsigned int nEntries;
unsigned int table_size;
CHashTableEntry **table;
unsigned int Hash(const char *key);
bool FindEntry(const char *key, unsigned int &i, CHashTableEntry* &p);
public:
CHashTable(unsigned int tablesize = 64);
~CHashTable();
unsigned int GetSize() { return nEntries; }
T* Find(const char *key);
bool Add(const char *key, const T data);
bool Delete(const char *key);
T* GetFirst()
{
for (it = 0; it < table_size; it++) if ( (itEl = table[it]) ) return &(itEl->value);
return 0;
}
T* GetNext()
{
if (!itEl) return 0;
if ( (itEl = itEl->next) ) return &(itEl->value);
while (++it < table_size) if ( (itEl = table[it]) ) return &(itEl->value);
return 0;
}
const char* GetKey() { return itEl ? itEl->s : 0; }
};
template <class T>
CHashTable<T>::CHashTable(unsigned int tablesize) : itEl(0), nEntries(0), table_size(tablesize)
{
table = new CHashTableEntry*[table_size];
for (unsigned int i=0; i<table_size; i++) table[i] = 0;
}
template <class T>
CHashTable<T>::~CHashTable()
{
for (unsigned int i=0; i<table_size; i++)
{
while (table[i])
{
CHashTableEntry *p = table[i];
table[i] = p->next;
delete[] p->s;
delete p;
}
}
}
template <class T>
unsigned int CHashTable<T>::Hash(const char *key)
{
unsigned int hash = 5381;
unsigned int i = 0;
while (key[i])
{
hash = 33 * hash + key[i];
i++;
}
return hash % table_size;
}
template <class T>
bool CHashTable<T>::FindEntry(const char *key, unsigned int &i, CHashTableEntry* &p)
{
i = Hash(key);
CHashTableEntry *q = table[i];
while (q)
{
if (strcmp(key, q->s) == 0) { p = q; return true; }
q = q->next;
}
return false;
}
template <class T>
T* CHashTable<T>::Find(const char *key)
{
unsigned int i;
CHashTableEntry* p;
if (FindEntry(key, i, p)) return &(p->value);
return 0;
}
template <class T>
bool CHashTable<T>::Add(const char *key, const T data)
{
unsigned int i;
CHashTableEntry* p;
if (FindEntry(key, i, p)) return false;
unsigned int size = strlen(key);
p = new CHashTableEntry;
p->s = new char[size+1];
strcpy(p->s, key);
p->value = data;
p->prev = 0;
p->next = table[i];
if (p->next) p->next->prev = p;
table[i] = p;
nEntries++;
return true;
}
template <class T>
bool CHashTable<T>::Delete(const char *key)
{
unsigned int i;
CHashTableEntry* p;
if (!FindEntry(key, i, p)) return false;
if (p->prev) p->prev->next = p->next; else table[i] = p->next;
if (p->next) p->next->prev = p->prev;
table[i] = p;
delete p;
nEntries--;
return true;
}