File: sim_hashtable.cpp

package info (click to toggle)
mcrl2 201409.0-1
  • links: PTS, VCS
  • area: main
  • in suites: buster, jessie, jessie-kfreebsd
  • size: 46,348 kB
  • ctags: 29,960
  • sloc: cpp: 213,160; ansic: 16,219; python: 13,238; yacc: 309; lex: 214; xml: 197; makefile: 83; sh: 82; pascal: 17
file content (308 lines) | stat: -rwxr-xr-x 5,913 bytes parent folder | download | duplicates (2)
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
// Author(s): Bas Ploeger
// Copyright: see the accompanying file COPYING or copy at
// https://svn.win.tue.nl/trac/MCRL2/browser/trunk/COPYING
//
// Distributed under the Boost Software License, Version 1.0.
// (See accompanying file LICENSE_1_0.txt or copy at
// http://www.boost.org/LICENSE_1_0.txt)
//
/// \file sim_hashtable.cpp
#include "mcrl2/lts/detail/sim_hashtable.h"

#define NOT_FOUND   (size_t)(-1)
#define END_OF_LIST (size_t)(-1)
#define REMOVED     (size_t)(-2)

/* ---------------- hash_table_iterator ----------------------------- */

hash_table2_iterator::hash_table2_iterator(hash_table2* ht)
  : hash_table(ht)
{
  bucket_it = hash_table->buckets.begin();
  while (bucket_it != hash_table->buckets.end()
         && (bucket_it->next == REMOVED))
  {
    ++bucket_it;
  }
}

void hash_table2_iterator::operator ++()
{
  if (bucket_it != hash_table->buckets.end())
  {
    ++bucket_it;
    while (bucket_it != hash_table->buckets.end()
           && (bucket_it->next == REMOVED))
    {
      ++bucket_it;
    }
  }
}

hash_table3_iterator::hash_table3_iterator(hash_table3* ht)
  : hash_table(ht)
{
  bucket_it = hash_table->buckets.begin();
  end = hash_table->buckets.end();
  while (bucket_it != end && bucket_it->next == REMOVED)
  {
    ++bucket_it;
  }
}

void hash_table3_iterator::operator ++()
{
  if (bucket_it != end)
  {
    ++bucket_it;
    while (bucket_it != end && bucket_it->next == REMOVED)
    {
      ++bucket_it;
    }
  }
}

/* ---------------- hash_table -------------------------------------- */

hash_table2::hash_table2(size_t initsize)
{
  mask = 1;
  while (mask < initsize)
  {
    mask = mask << 1;
  }
  --mask;
  clear();
}

hash_table2::~hash_table2()
{}

void hash_table2::clear()
{
  table.assign(mask+1,END_OF_LIST);
  buckets.clear();
  removed_count = 0;
}

void hash_table2::add(size_t x,size_t y)
{
  size_t h = hash(x,y);
  if (hfind(h,x,y) == NOT_FOUND)
  {
    if (check_table())
    {
      h = hash(x,y);
    }
    bucket2 new_bucket;
    new_bucket.x = x;
    new_bucket.y = y;
    new_bucket.next = table[h];
    table[h] = buckets.size();
    buckets.push_back(new_bucket);
  }
}

bool hash_table2::find(size_t x,size_t y)
{
  return (hfind(hash(x,y),x,y) != NOT_FOUND);
}

void hash_table2::remove(size_t x,size_t y)
{
  bucket2 b;
  size_t i, prev_i;
  size_t h = hash(x,y);
  i = table[h];
  if (i != END_OF_LIST)
  {
    b = buckets[i];
    if (b.x == x && b.y == y)
    {
      table[h] = b.next;
      buckets[i].next = REMOVED;
      ++removed_count;
      return;
    }
    prev_i = i;
    i = b.next;
  }
  while (i != END_OF_LIST)
  {
    b = buckets[i];
    if (b.x == x && b.y == y)
    {
      buckets[prev_i].next = buckets[i].next;
      buckets[i].next = REMOVED;
      ++removed_count;
      return;
    }
    prev_i = i;
    i = b.next;
  }
}

size_t hash_table2::hfind(size_t h,size_t x,size_t y)
{
  size_t i = table[h];
  bucket2 b;
  while (i != END_OF_LIST)
  {
    b = buckets[i];
    if (b.x == x && b.y == y)
    {
      return i;
    }
    i = b.next;
  }
  return NOT_FOUND;
}

bool hash_table2::check_table()
{
  if (4*(buckets.size() - removed_count) >= 3*table.size())
  {
    /* table is filled for at least 75%, so let's rehash! */
    mask = (2*mask) + 1;
    table.assign(mask+1,END_OF_LIST);
    size_t h;
    for (size_t i = 0; i < buckets.size(); ++i)
    {
      if (buckets[i].next != REMOVED)
      {
        h = hash(buckets[i].x,buckets[i].y);
        buckets[i].next = table[h];
        table[h] = i;
      }
    }
    return true;
  }
  return false;
}

size_t hash_table2::hash(size_t x, size_t y)
{
  return ((159403*x + 389651*y) & mask);
}

hash_table3::hash_table3(size_t initsize)
{
  mask = 1;
  while (mask < initsize)
  {
    mask = mask << 1;
  }
  --mask;
  clear();
}

hash_table3::~hash_table3()
{}

void hash_table3::clear()
{
  table.assign(mask+1,END_OF_LIST);
  buckets.clear();
  removed_count = 0;
}

void hash_table3::add(size_t x,size_t y,size_t z)
{
  size_t h = hash(x,y,z);
  if (hfind(h,x,y,z) == NOT_FOUND)
  {
    if (check_table())
    {
      h = hash(x,y,z);
    }
    bucket3 new_bucket;
    new_bucket.x = x;
    new_bucket.y = y;
    new_bucket.z = z;
    new_bucket.next = table[h];
    table[h] = buckets.size();
    buckets.push_back(new_bucket);
  }
}

bool hash_table3::find(size_t x,size_t y,size_t z)
{
  return (hfind(hash(x,y,z),x,y,z) != NOT_FOUND);
}

void hash_table3::remove(size_t x,size_t y,size_t z)
{
  bucket3 b;
  size_t i, prev_i;
  size_t h = hash(x,y,z);
  i = table[h];
  if (i != END_OF_LIST)
  {
    b = buckets[i];
    if (b.x == x && b.y == y && b.z == z)
    {
      table[h] = b.next;
      buckets[i].next = REMOVED;
      ++removed_count;
      return;
    }
    prev_i = i;
    i = b.next;
  }
  while (i != END_OF_LIST)
  {
    b = buckets[i];
    if (b.x == x && b.y == y && b.z == z)
    {
      buckets[prev_i].next = buckets[i].next;
      buckets[i].next = REMOVED;
      ++removed_count;
      return;
    }
    prev_i = i;
    i = b.next;
  }
}

size_t hash_table3::hfind(size_t h,size_t x,size_t y,size_t z)
{
  size_t i = table[h];
  bucket3 b;
  while (i != END_OF_LIST)
  {
    b = buckets[i];
    if (b.x == x && b.y == y && b.z == z)
    {
      return i;
    }
    i = b.next;
  }
  return NOT_FOUND;
}

bool hash_table3::check_table()
{
  if (4*(buckets.size() - removed_count) >= 3*table.size())
  {
    /* table is filled for at least 75%, so let's rehash! */
    mask = (2*mask) + 1;
    table.assign(mask+1,END_OF_LIST);
    size_t h;
    for (size_t i = 0; i < buckets.size(); ++i)
    {
      if (buckets[i].next != REMOVED)
      {
        h = hash(buckets[i].x,buckets[i].y,buckets[i].z);
        buckets[i].next = table[h];
        table[h] = i;
      }
    }
    return true;
  }
  return false;
}

size_t hash_table3::hash(size_t x, size_t y, size_t z)
{
  return ((159403*x + 389651*y + 521503*z) & mask);
}