-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathHashTableVoid.h
More file actions
74 lines (57 loc) · 1.62 KB
/
Copy pathHashTableVoid.h
File metadata and controls
74 lines (57 loc) · 1.62 KB
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
//
// Hash Table
//
#include <assert.h>
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
// Each hash entry stores a key, object pair
struct HashTableVoidEntry {
const char * _key;
void * _data;
HashTableVoidEntry * _next;
};
struct ChatRoom {
const char * _key;
const char * _user;
const char * _message;
ChatRoom * _next;
};
struct UserInfo {
const char * _key;
const char * _room;
UserInfo * _next;
};
// This is a Hash table that maps string keys to objects of type Data
class HashTableVoid {
public:
// Number of buckets
enum { TableSize = 2039};
// Array of the hash buckets.
HashTableVoidEntry **_buckets;
// Array of the chat buckets.
ChatRoom ** _chatBuckets;
// Array of the user buckets.
UserInfo ** _userBuckets;
// Obtain the hash code of a key
int hash(const char * key);
public:
HashTableVoid();
// Add a record to the hash table. Returns true if key already exists.
// Substitute content if key already exists.
bool insertItem( const char * key, void * data);
bool insertChat(const char * key, const char * user, const char * message);
// Find a key in the dictionary and place in "data" the corresponding record
// Returns false if key is does not exist
bool find( const char * key, void ** data);
// Removes an element in the hash table. Return false if key does not exist.
bool removeElement(const char * key);
};
class HashTableVoidIterator {
int _currentBucket;
HashTableVoidEntry *_currentEntry;
HashTableVoid * _hashTable;
public:
HashTableVoidIterator(HashTableVoid * hashTable);
bool next(const char * & key, void * & data);
};