-
Notifications
You must be signed in to change notification settings - Fork 3
/
hashtab.cxx
100 lines (89 loc) · 2.37 KB
/
hashtab.cxx
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
//-< HASHTAB.H >-----------------------------------------------------*--------*
// POST++ Version 1.0 (c) 1998 GARRET * ? *
// (Persistent Object Storage) * /\| *
// * / \ *
// Created: 7-Feb-98 K.A. Knizhnik * / [] \ *
// Last update: 7-Feb-98 K.A. Knizhnik * GARRET *
//-------------------------------------------------------------------*--------*
// Hash table: example of POST++ class definition
//-------------------------------------------------------------------*--------*
#define INSIDE_POST
#include "hashtab.h"
BEGIN_POST_NAMESPACE
inline unsigned string_hash_function(const char* name)
{
unsigned h = 0, g;
while(*name) {
h = (h << 4) + *name++;
if ((g = h & 0xF0000000) != 0) {
h ^= g >> 24;
}
h &= ~g;
}
return h;
}
void hash_table::put(const char* name, object* obj)
{
unsigned h = string_hash_function(name) % size;
table[h] = new (hash_item::self_class, *get_storage(), strlen(name))
hash_item(name, obj, table[h]);
}
object* hash_table::get(const char* name) const
{
for (hash_item* ip = table[string_hash_function(name) % size];
ip != NULL;
ip = ip->next)
{
if (strcmp(ip->name, name) == 0) {
return ip->obj;
}
}
return NULL;
}
bool hash_table::del(const char* name)
{
hash_item *ip, **ipp;
ipp = &table[string_hash_function(name) % size];
while ((ip = *ipp) != NULL && strcmp(ip->name, name) != 0) {
ipp = &ip->next;
}
if (ip != NULL) {
*ipp = ip->next;
delete ip;
return true;
}
return false;
}
bool hash_table::del(const char* name, object* obj)
{
hash_item *ip, **ipp;
ipp = &table[string_hash_function(name) % size];
while ((ip=*ipp) != NULL && (ip->obj != obj || strcmp(ip->name,name) != 0))
{
ipp = &ip->next;
}
if (ip != NULL) {
*ipp = ip->next;
delete ip;
return true;
}
return false;
}
void hash_table::purge(bool delete_objects)
{
for (int i = size; --i >= 0;) {
hash_item* ip = table[i];
while (ip != NULL) {
hash_item* next = ip->next;
if (delete_objects) {
delete ip->obj;
}
delete ip;
ip = next;
}
table[i] = NULL;
}
}
REGISTER(hash_item);
REGISTER(hash_table);
END_POST_NAMESPACE