-
Notifications
You must be signed in to change notification settings - Fork 37
/
Copy pathHash.h
58 lines (44 loc) · 1.44 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
// Hash.h
// an unordered_map in C++'s terminology
// This implementation follows
// Weiss, Data Structures and Algorithm Analysis in C++, 3rd edition
// It uses open addressing with linear programming
// possible implementation is
// http://uthash.sourceforge.net/userguide.html
#ifndef HASH_H
#define HASH_H
#include <inttypes.h>
#include "Atom.h"
#define T Hash_T
typedef struct T *T;
struct Hash_T {
Atom_T *keysP;
char **valuesP;
uint32_t tableSize;
uint32_t nElements;
};
// General case: key is Atom_T; value is char *
extern T Hash_new(uint32_t initTableSizeHint);
extern void Hash_free(T *selfP);
// replace any current value
// return mutated self
extern T Hash_insert(T self, Atom_T key, char *value);
// return pointer to value or NULL if key is not present
extern char *Hash_find(T self, Atom_T *key);
// iterating through the elements
#define T_ITERATOR Hash_Iterator_T
typedef struct T_ITERATOR *T_ITERATOR;
struct Hash_Iterator_T {
T hash;
uint32_t nextIndex;
};
extern T_ITERATOR HashIterator_new(T self);
extern void HashIterator_free(T_ITERATOR *selfP);
// return 1 if a next element, in which case set *valueP to point to the value
// return 0 if no next element, in which case set *valueP to NULL
extern unsigned HashIterator_next(T_ITERATOR iterator,
Atom_T **keyP,
char **valueP);
#undef T_ITERATOR
#undef T
#endif