-
Notifications
You must be signed in to change notification settings - Fork 5
/
TimedHashMap.h
200 lines (158 loc) · 4.8 KB
/
TimedHashMap.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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*
* This file is part of esynth.
*
* esynth is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* esynth is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with esynth. If not, see <http://www.gnu.org/licenses/>.
*/
#ifndef _TIMED_HASH_MAP_GUARD
#define _TIMED_HASH_MAP_GUARD 1
#include <utility>
#include <map>
#include <iostream>
#include <sstream>
#include "TimedLikeValueContainer.h"
class TimedHashMap
{
private:
TimedLikeValueContainer** like_containers;
static unsigned SIZE;
static unsigned rel_UPPERBOUND;
static unsigned ABS_UPPERBOUND;
unsigned int sz;
unsigned int currentTime;
//
// Trusted hash function for strings (found online)
//
unsigned int hash(const char* str) const
{
unsigned long hash = 5381;
int c;
while (c = *str++)
{
hash = ((hash << 5) + hash) + c; /* hash * 33 + c */
}
return hash % SIZE;
}
//
// Remove all the old values from the container
//
void PurgeAndRenumber()
{
std::cerr << "Purging" << std::endl;
// Purge all the hashing containers
for (int index = 0; index < SIZE; index++)
{
if (like_containers[index] != 0)
{
like_containers[index]->PurgeAndRenumber();
// If this list is now empty, kill it.
if (like_containers[index]->empty())
{
delete like_containers[index];
like_containers[index] = 0;
}
}
}
// Update the size of the number of elements in this container to the relative max.
sz = rel_UPPERBOUND;
// Reset time to the sliding window upper-bound. We then subtract down to 0.
currentTime = ABS_UPPERBOUND - rel_UPPERBOUND;
}
//
// Handle collisions with linear probing
//
bool privateAdd(const std::string& that)
{
//
// Do we need to purge before addition?
// (this updates sz)
if (sz == ABS_UPPERBOUND) PurgeAndRenumber();
//
// Addition can be performed normally
//
unsigned int hashVal = hash(that.c_str()) % SIZE;
// Do we need to create this list first?
if (like_containers[hashVal] == 0)
{
like_containers[hashVal] = new TimedLikeValueContainer();
}
like_containers[hashVal]->add(that, currentTime);
// Count down to 0.
currentTime--;
return true;
}
public:
static void SetThresholds(unsigned relUB, unsigned absUB)
{
rel_UPPERBOUND = relUB;
ABS_UPPERBOUND = absUB;
SIZE = absUB;
TimedLikeValueContainer::SetThresholds(relUB, absUB);
}
// Init to the absolute upper bound so we can populate the container completely.
TimedHashMap() : sz(0), currentTime(ABS_UPPERBOUND)
{
like_containers = new TimedLikeValueContainer*[SIZE];
for (int i = 0; i < SIZE; i++)
{
like_containers[i] = 0;
}
}
//
// Delete all entries
//
~TimedHashMap()
{
for (int i = 0; i < SIZE; i++)
{
if (like_containers[i]) delete like_containers[i];
}
delete[] like_containers;
}
unsigned int size() const { return sz; }
bool add(const std::string& that)
{
bool added = privateAdd(that);
if (added) sz++;
return added;
}
//
// Should only be called once
//
bool contains(const std::string& that) const
{
unsigned int hashVal = hash(that.c_str()) % SIZE;
if (like_containers[hashVal] == 0) return false;
// The list exists, inquire if the key matches our key.
return like_containers[hashVal]->contains(that);
}
std::string toString() const
{
std::ostringstream oss;
for (int index = 0; index < SIZE; index++)
{
if (like_containers[index] != 0)
{
oss << index << ": ";
oss << *like_containers[index] << std::endl;
}
}
return oss.str();
}
friend std::ostream& operator<<(std::ostream& os, const TimedHashMap& map)
{
os << map.toString();
return os;
}
};
#endif