-
Notifications
You must be signed in to change notification settings - Fork 0
/
hash.h
104 lines (94 loc) · 2.97 KB
/
hash.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
// -*- C++ -*-
//
// Hash class interface.
//
// Copyright 1992-2021 Deven T. Corzine <[email protected]>
//
// SPDX-License-Identifier: MIT
//
// Check if previously included.
#ifndef _HASH_H
#define _HASH_H 1
#include "object.h"
#include "string2.h"
class HashEntry: public Object {
friend class Hash;
friend class HashIter;
private:
Pointer<HashEntry> next; // Next entry on hash chain.
String key; // Key for hash entry.
String value; // Value for hash entry.
HashEntry(const char *k, const char *v): key(k), value(v) { }
public:
String Key() { return key; }
String Value() { return value; }
HashEntry &operator =(HashEntry &entry) {
value = entry.value;
return *this;
}
HashEntry &operator =(String &v) {
value = v;
return *this;
}
HashEntry &operator =(const char *v) {
value = v;
return *this;
}
operator String() { return value; }
operator const char *() const { return value; }
operator char *() { return value; }
const char *operator ~() const { return ~value; }
const char *operator ~() { return ~value; }
};
class Hash {
friend class HashIter;
private:
static const int Size = 211;
int count;
Pointer<HashEntry> bucket[Size];
int HashFunction(const char *key);
public:
Hash(): count(0) { }
int Count() { return count; }
void Reset() { for (int i = 0; i < Size; i++) bucket[i] = 0; }
boolean Known (String &key) { return Known(~key); }
boolean Known (const char *key);
void Store (String &key, String &value) { Store(~key, ~value); }
void Store (String &key, const char *value) { Store(~key, value); }
void Store (const char *key, String &value) { Store(key, ~value); }
void Store (const char *key, const char *value);
void Delete (String &key) { Delete(~key); }
void Delete (const char *key);
String Fetch (String &key) { return Fetch(~key); }
String Fetch (const char *key);
HashEntry &operator [](const char *key);
HashEntry &operator [](String &key) { return (*this)[~key]; }
};
class HashIter {
private:
Hash *array;
Pointer<HashEntry> entry;
int bucket;
public:
HashIter(): bucket(0) { }
HashIter(Hash &a): array(&a), bucket(0) { }
HashIter(Hash *a): array(a), bucket(0) { }
HashIter &operator =(Hash &a) {
array = &a;
entry = 0;
bucket = 0;
return *this;
}
HashIter &operator =(Hash *a) {
array = a;
entry = 0;
bucket = 0;
return *this;
}
HashEntry *operator ++();
HashEntry *operator ++(int) { return ++(*this); }
operator HashEntry *() { return entry; }
operator HashEntry &() { return *entry; }
const char *operator ~() { return ~(entry->value); }
};
#endif // hash.h