1 /******************************************************************************** 2 * * 3 * H a s h T a b l e C l a s s * 4 * * 5 ********************************************************************************* 6 * Copyright (C) 2003,2006 by Jeroen van der Zijp. All Rights Reserved. * 7 ********************************************************************************* 8 * This library is free software; you can redistribute it and/or * 9 * modify it under the terms of the GNU Lesser General Public * 10 * License as published by the Free Software Foundation; either * 11 * version 2.1 of the License, or (at your option) any later version. * 12 * * 13 * This library is distributed in the hope that it will be useful, * 14 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * 16 * Lesser General Public License for more details. * 17 * * 18 * You should have received a copy of the GNU Lesser General Public * 19 * License along with this library; if not, write to the Free Software * 20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. * 21 ********************************************************************************* 22 * $Id: FXHash.h,v 1.12 2006/01/22 17:58:04 fox Exp $ * 23 ********************************************************************************/ 24 #ifndef FXHASH_H 25 #define FXHASH_H 26 27 namespace FX { 28 29 30 /** 31 * A hash table for associating pointers to pointers. 32 */ 33 class FXAPI FXHash { 34 private: 35 struct FXEntry { 36 void* key; 37 void* value; 38 }; 39 private: 40 FXEntry *table; // Hash table 41 FXuint total; // Table size 42 FXuint used; // Number of used entries 43 FXuint free; // Number of free entries 44 private: 45 FXHash(const FXHash&); 46 FXHash &operator=(const FXHash&); 47 public: 48 49 /// Construct empty hash table 50 FXHash(); 51 52 /// Resize the table to the given size. 53 void size(FXuint m); 54 55 /// Return the size of the table size()56 FXint size() const { return total; } 57 58 /// Return number of items in table no()59 FXuint no() const { return used; } 60 61 /// Insert key into the table 62 void* insert(void* key,void* value); 63 64 /// Replace key in table 65 void* replace(void* key,void* value); 66 67 /// Remove key from the table 68 void* remove(void* key); 69 70 /// Return value of key 71 void* find(void* key) const; 72 73 /// Return true if slot is empty empty(FXint pos)74 bool empty(FXint pos) const { return (table[pos].key==NULL)||(table[pos].key==(void*)-1L); } 75 76 /// Return key at position pos key(FXint pos)77 void* key(FXint pos) const { return table[pos].key; } 78 79 /// Return data pointer at position pos value(FXint pos)80 void* value(FXint pos) const { return table[pos].value; } 81 82 /// Clear hash table 83 void clear(); 84 85 /// Destructor 86 virtual ~FXHash(); 87 }; 88 89 90 } 91 92 #endif 93