libStatGen Software
1
|
00001 /* 00002 * Copyright (C) 2000-2007 Goncalo Abecasis 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 ////////////////////////////////////////////////////////////////////// 00019 // libsrc/IntHash.h 00020 // (c) 2000-2007 Goncalo Abecasis 00021 // 00022 // This file is distributed as part of the MaCH source code package 00023 // and may not be redistributed in any form, without prior written 00024 // permission from the author. Permission is granted for you to 00025 // modify this file for your own personal use, but modified versions 00026 // must retain this copyright notice and must not be distributed. 00027 // 00028 // Permission is granted for you to use this file to compile MaCH. 00029 // 00030 // All computer programs have bugs. Use this file at your own risk. 00031 // 00032 // Monday October 29, 2007 00033 // 00034 00035 #ifndef __INTHASH_H__ 00036 #define __INTHASH_H__ 00037 00038 #include <stdlib.h> 00039 00040 class IntHash 00041 { 00042 protected: 00043 bool * objects; 00044 unsigned int * keys; 00045 unsigned int count, size; 00046 unsigned int mask; 00047 00048 public: 00049 IntHash(int startsize = 32); 00050 virtual ~IntHash(); 00051 00052 void Grow() 00053 { 00054 SetSize(size * 2); 00055 } 00056 void Shrink() 00057 { 00058 SetSize(size / 2); 00059 } 00060 00061 void SetSize(int newsize); 00062 00063 void Clear(); 00064 00065 int Capacity() const 00066 { 00067 return size; 00068 } 00069 int Entries() const 00070 { 00071 return count; 00072 } 00073 00074 bool Object(int i) const 00075 { 00076 return objects[i]; 00077 } 00078 00079 void SetObject(int i, bool object) 00080 { 00081 objects[i] = object; 00082 } 00083 00084 int Add(int key, bool object = true); 00085 int Find(int key); 00086 int Rehash(int key, int h); 00087 00088 IntHash & operator = (const IntHash & rhs); 00089 00090 bool operator [](int i) const 00091 { 00092 return objects[i]; 00093 } 00094 00095 void Delete(unsigned int index); 00096 00097 bool SlotInUse(int index) 00098 { 00099 return objects[index] != false; 00100 } 00101 00102 private: 00103 unsigned int Iterate(unsigned int key) const 00104 { 00105 unsigned int h = key & mask; 00106 00107 while (objects[h] != false && keys[h] != key) 00108 h = (h + 1) & mask; 00109 00110 return h; 00111 } 00112 00113 unsigned int ReIterate(unsigned int key, unsigned int h) const 00114 { 00115 h = (h + 1) & mask; 00116 00117 while (objects[h] != false && keys[h] != key) 00118 h = (h + 1) & mask; 00119 00120 return h; 00121 } 00122 }; 00123 00124 #endif 00125