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
/*
 *
 *
 * Copyright (C) 2004 Sandia Corporation.  Under the terms of Contract DE-AC04-94AL85000
 * with Sandia Corporation, the U.S. Government retains certain rights in this software.
 *
 * This file is part of facetbool--contact via [email protected]
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library 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
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 *
 *
 *
 */

#include "IntegerHash.hpp"

IntegerHash::IntegerHash(int numBins, int binSizeIncr)
{
int i;

	numberofBins = numBins;
	binSizeIncrement = binSizeIncr;
	binSize = new int[numberofBins];
	maxBinSize = new int[numberofBins];
	binArray = new int *[numberofBins];

	for ( i = 0; i < numberofBins; i++ ) {
		maxBinSize[i] = binSizeIncrement;
		binSize[i] = 0;
		binArray[i] = new int[maxBinSize[i]];
	}
}

IntegerHash::~IntegerHash()
{
int i;

	if ( binSize) delete[] binSize;
	if ( maxBinSize) delete[] maxBinSize;
	for ( i = 0; i < numberofBins; i++ ) {
		if ( binArray[i] ) delete[] binArray[i];
	}
	if ( binArray ) delete [] binArray;	
}

void IntegerHash::getNumberofBins(int *numBins) const<--- The function 'getNumberofBins' is never used.
{
  *numBins = numberofBins;
}

int *IntegerHash::getHashBin(int hashValue, int *binnSize)
{
	hashIndex = hashValue%numberofBins;
	*binnSize = binSize[hashIndex];
	return binArray[hashIndex];
}

void IntegerHash::addtoHashList(int hashValue, int value)
{
int i;
	hashIndex = hashValue%numberofBins;
// Is it already there?
	for ( i = 0; i < binSize[hashIndex]; i++ ) {
		if ( binArray[hashIndex][i] == value ) return;
	}
	if ( binSize[hashIndex] > maxBinSize[hashIndex] - 1 ) {
//  Add more memory to this bin.
		allocateMoreHash(hashIndex);
	}

	binArray[hashIndex][binSize[hashIndex]] = value;
	binSize[hashIndex] += 1;

}

void IntegerHash::allocateMoreHash(int index)
{
int *itemp;

	maxBinSize[hashIndex] += binSizeIncrement;
	itemp = new int[maxBinSize[hashIndex]];
	memcpy(itemp,binArray[index],binSize[hashIndex]*sizeof(int));
	delete [] binArray[index];
	binArray[index] = itemp;
}