Branch data Line data Source code
1 : : /*
2 : : *
3 : : *
4 : : * Copyright (C) 2004 Sandia Corporation. Under the terms of Contract DE-AC04-94AL85000
5 : : * with Sandia Corporation, the U.S. Government retains certain rights in this software.
6 : : *
7 : : * This file is part of facetbool--contact via [email protected]
8 : : *
9 : : * This library is free software; you can redistribute it and/or
10 : : * modify it under the terms of the GNU Lesser General Public
11 : : * License as published by the Free Software Foundation; either
12 : : * version 2.1 of the License, or (at your option) any later version.
13 : : *
14 : : * This library is distributed in the hope that it will be useful,
15 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 : : * Lesser General Public License for more details.
18 : : *
19 : : * You should have received a copy of the GNU Lesser General Public
20 : : * License along with this library; if not, write to the Free Software
21 : : * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 : : *
23 : : *
24 : : *
25 : : */
26 : :
27 : : #include "IntegerHash.hpp"
28 : :
29 : 0 : IntegerHash::IntegerHash(int numBins, int binSizeIncr)
30 : : {
31 : : int i;
32 : :
33 : 0 : numberofBins = numBins;
34 : 0 : binSizeIncrement = binSizeIncr;
35 [ # # ]: 0 : binSize = new int[numberofBins];
36 [ # # ]: 0 : maxBinSize = new int[numberofBins];
37 [ # # ]: 0 : binArray = new int *[numberofBins];
38 : :
39 [ # # ]: 0 : for ( i = 0; i < numberofBins; i++ ) {
40 : 0 : maxBinSize[i] = binSizeIncrement;
41 : 0 : binSize[i] = 0;
42 [ # # ]: 0 : binArray[i] = new int[maxBinSize[i]];
43 : : }
44 : 0 : }
45 : :
46 : 0 : IntegerHash::~IntegerHash()
47 : : {
48 : : int i;
49 : :
50 [ # # ][ # # ]: 0 : if ( binSize) delete[] binSize;
51 [ # # ][ # # ]: 0 : if ( maxBinSize) delete[] maxBinSize;
52 [ # # ]: 0 : for ( i = 0; i < numberofBins; i++ ) {
53 [ # # ][ # # ]: 0 : if ( binArray[i] ) delete[] binArray[i];
54 : : }
55 [ # # ][ # # ]: 0 : if ( binArray ) delete [] binArray;
56 : 0 : }
57 : :
58 : 0 : void IntegerHash::getNumberofBins(int *numBins) const
59 : : {
60 : 0 : *numBins = numberofBins;
61 : 0 : }
62 : :
63 : 0 : int *IntegerHash::getHashBin(int hashValue, int *binnSize)
64 : : {
65 : 0 : hashIndex = hashValue%numberofBins;
66 : 0 : *binnSize = binSize[hashIndex];
67 : 0 : return binArray[hashIndex];
68 : : }
69 : :
70 : 0 : void IntegerHash::addtoHashList(int hashValue, int value)
71 : : {
72 : : int i;
73 : 0 : hashIndex = hashValue%numberofBins;
74 : : // Is it already there?
75 [ # # ]: 0 : for ( i = 0; i < binSize[hashIndex]; i++ ) {
76 [ # # ]: 0 : if ( binArray[hashIndex][i] == value ) return;
77 : : }
78 [ # # ]: 0 : if ( binSize[hashIndex] > maxBinSize[hashIndex] - 1 ) {
79 : : // Add more memory to this bin.
80 : 0 : allocateMoreHash(hashIndex);
81 : : }
82 : :
83 : 0 : binArray[hashIndex][binSize[hashIndex]] = value;
84 : 0 : binSize[hashIndex] += 1;
85 : :
86 : : }
87 : :
88 : 0 : void IntegerHash::allocateMoreHash(int index)
89 : : {
90 : : int *itemp;
91 : :
92 : 0 : maxBinSize[hashIndex] += binSizeIncrement;
93 [ # # ]: 0 : itemp = new int[maxBinSize[hashIndex]];
94 : 0 : memcpy(itemp,binArray[index],binSize[hashIndex]*sizeof(int));
95 [ # # ]: 0 : delete [] binArray[index];
96 : 0 : binArray[index] = itemp;
97 : 0 : }
98 : :
|