cgma
IntegerHash.cpp
Go to the documentation of this file.
00001 /*
00002  *
00003  *
00004  * Copyright (C) 2004 Sandia Corporation.  Under the terms of Contract DE-AC04-94AL85000
00005  * with Sandia Corporation, the U.S. Government retains certain rights in this software.
00006  *
00007  * This file is part of facetbool--contact via cubit@sandia.gov
00008  *
00009  * This library is free software; you can redistribute it and/or
00010  * modify it under the terms of the GNU Lesser General Public
00011  * License as published by the Free Software Foundation; either
00012  * version 2.1 of the License, or (at your option) any later version.
00013  *
00014  * This library is distributed in the hope that it will be useful,
00015  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00016  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017  * Lesser General Public License for more details.
00018  *
00019  * You should have received a copy of the GNU Lesser General Public
00020  * License along with this library; if not, write to the Free Software
00021  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00022  *
00023  *
00024  *
00025  */
00026 
00027 #include "IntegerHash.hpp"
00028 
00029 IntegerHash::IntegerHash(int numBins, int binSizeIncr)
00030 {
00031 int i;
00032 
00033     numberofBins = numBins;
00034     binSizeIncrement = binSizeIncr;
00035     binSize = new int[numberofBins];
00036     maxBinSize = new int[numberofBins];
00037     binArray = new int *[numberofBins];
00038 
00039     for ( i = 0; i < numberofBins; i++ ) {
00040         maxBinSize[i] = binSizeIncrement;
00041         binSize[i] = 0;
00042         binArray[i] = new int[maxBinSize[i]];
00043     }
00044 }
00045 
00046 IntegerHash::~IntegerHash()
00047 {
00048 int i;
00049 
00050     if ( binSize) delete[] binSize;
00051     if ( maxBinSize) delete[] maxBinSize;
00052     for ( i = 0; i < numberofBins; i++ ) {
00053         if ( binArray[i] ) delete[] binArray[i];
00054     }
00055     if ( binArray ) delete [] binArray; 
00056 }
00057 
00058 void IntegerHash::getNumberofBins(int *numBins) const
00059 {
00060   *numBins = numberofBins;
00061 }
00062 
00063 int *IntegerHash::getHashBin(int hashValue, int *binnSize)
00064 {
00065     hashIndex = hashValue%numberofBins;
00066     *binnSize = binSize[hashIndex];
00067     return binArray[hashIndex];
00068 }
00069 
00070 void IntegerHash::addtoHashList(int hashValue, int value)
00071 {
00072 int i;
00073     hashIndex = hashValue%numberofBins;
00074 // Is it already there?
00075     for ( i = 0; i < binSize[hashIndex]; i++ ) {
00076         if ( binArray[hashIndex][i] == value ) return;
00077     }
00078     if ( binSize[hashIndex] > maxBinSize[hashIndex] - 1 ) {
00079 //  Add more memory to this bin.
00080         allocateMoreHash(hashIndex);
00081     }
00082 
00083     binArray[hashIndex][binSize[hashIndex]] = value;
00084     binSize[hashIndex] += 1;
00085 
00086 }
00087 
00088 void IntegerHash::allocateMoreHash(int index)
00089 {
00090 int *itemp;
00091 
00092     maxBinSize[hashIndex] += binSizeIncrement;
00093     itemp = new int[maxBinSize[hashIndex]];
00094     memcpy(itemp,binArray[index],binSize[hashIndex]*sizeof(int));
00095     delete [] binArray[index];
00096     binArray[index] = itemp;
00097 }
00098 
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines