libStatGen Software  1
BasicHash.h
00001 /*
00002  *  Copyright (C) 2010  Regents of the University of Michigan
00003  *
00004  *   This program is free software: you can redistribute it and/or modify
00005  *   it under the terms of the GNU General Public License as published by
00006  *   the Free Software Foundation, either version 3 of the License, or
00007  *   (at your option) any later version.
00008  *
00009  *   This program is distributed in the hope that it will be useful,
00010  *   but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *   GNU General Public License for more details.
00013  *
00014  *   You should have received a copy of the GNU General Public License
00015  *   along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #ifndef __BASICHASH_H__
00019 #define __BASICHASH_H__
00020 
00021 #include <stdlib.h>
00022 
00023 class BasicHash
00024 {
00025 protected:
00026     void          ** objects;
00027     unsigned int      * keys;
00028     unsigned int count, size;
00029     unsigned int        mask;
00030 
00031 public:
00032     BasicHash(int startsize = 32);
00033     virtual ~BasicHash();
00034 
00035     void Grow()
00036     {
00037         SetSize(size * 2);
00038     }
00039     void Shrink()
00040     {
00041         SetSize(size / 2);
00042     }
00043 
00044     void SetSize(int newsize);
00045 
00046     void Clear();
00047 
00048     int  Capacity() const
00049     {
00050         return size;
00051     }
00052     int  Entries() const
00053     {
00054         return count;
00055     }
00056 
00057     void * Object(int i) const
00058     {
00059         return objects[i];
00060     }
00061 
00062     void SetObject(int i, void * object)
00063     {
00064         objects[i] = object;
00065     }
00066 
00067     int Add(int key, void * object = NULL);
00068     int Find(int key);
00069     int Rehash(int key, int h);
00070 
00071     BasicHash & operator = (const BasicHash & rhs);
00072 
00073     void * operator [](int i) const
00074     {
00075         return objects[i];
00076     }
00077 
00078     void Delete(unsigned int index);
00079 
00080     bool SlotInUse(int index)
00081     {
00082         return objects[index] != NULL;
00083     }
00084 
00085 private:
00086     unsigned int Iterate(unsigned int key) const
00087     {
00088         unsigned int h = key & mask;
00089 
00090         while (objects[h] != NULL && keys[h] != key)
00091             h = (h + 1) & mask;
00092 
00093         return h;
00094     }
00095 
00096     unsigned int ReIterate(unsigned int key, unsigned int h) const
00097     {
00098         h = (h + 1) & mask;
00099 
00100         while (objects[h] != NULL && keys[h] != key)
00101             h = (h + 1) & mask;
00102 
00103         return h;
00104     }
00105 };
00106 
00107 #endif
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends