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
|
/*
* Copyright (C) 2010 Regents of the University of Michigan
*
* This program 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.
*
* This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "BasicHash.h"
#include "Error.h"
#include <stdio.h>
BasicHash::BasicHash(int startsize)
{
count = 0;
size = startsize;
mask = startsize - 1;
// In this implementation, the size of hash tables must be a power of two
if (startsize & mask)
error("BasicHash: Hash table size must be a power of two.\n");
objects = new void * [size];
keys = new unsigned int [size];
for (unsigned int i = 0; i < size; i++)
{
objects[i] = NULL;
}
};
BasicHash::~BasicHash()
{
delete [] objects;
delete [] keys;
}
void BasicHash::Clear()
{
// printf("Clearing...\n");
count = 0;
if (size > 16)
SetSize(16);
for (unsigned int i = 0; i < size; i++)
objects[i] = NULL;
}
void BasicHash::SetSize(int newsize)
{
int newmask = newsize - 1;
void ** newobjects = new void * [newsize];
unsigned int * newkeys = new unsigned int [newsize];
for (int i = 0; i < newsize; i++)
{
newobjects[i] = NULL;
}
if (count)
for (unsigned int i = 0; i < size; i++)
if (objects[i] != NULL)
{
unsigned int key = keys[i];
unsigned int h = key & newmask;
while (newobjects[h] != NULL && newkeys[h] != h)
h = (h + 1) & newmask;
newkeys[h] = key;
newobjects[h] = objects[i];
}
delete [] objects;
delete [] keys;
objects = newobjects;
keys = newkeys;
size = newsize;
mask = newmask;
}
int BasicHash::Add(int key, void * object)
{
if (count * 2 > size)
Grow();
unsigned int h = Iterate(key);
while ((objects[h] != NULL) && (objects[h] != object))
h = ReIterate(key, h);
if (objects[h] == NULL)
{
// printf("At position %d, inserted %x\n", h, key);
keys[h] = key;
count++;
}
objects[h] = object;
return h;
}
int BasicHash::Find(int key)
{
int h = Iterate(key);
return objects[h] == NULL ? -1 : h;
}
int BasicHash::Rehash(int key, int h)
{
h = ReIterate(key, h);
return objects[h] == NULL ? -1 : h;
}
void BasicHash::Delete(unsigned int index)
{
if (index >= size || objects[index] == NULL)
return;
objects[index] = NULL;
count--;
if (count * 8 < size && size > 32)
Shrink();
else
{
// rehash the next entries until we find empty slot
index = (index + 1) & mask;
while (objects[index] != NULL)
{
if ((keys[index] & mask) != index)
{
unsigned int h = Iterate(keys[index]);
while ((objects[h] != NULL) && (objects[h] != objects[index]))
h = ReIterate(keys[index], h);
if (h != (unsigned int) index)
{
keys[h] = keys[index];
objects[h] = objects[index];
objects[index] = NULL;
}
}
index = (index + 1) & mask;
}
}
}
|