Main Page   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members

FXHash.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                       H a s h   T a b l e   C l a s s                         *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 2003,2010 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or modify          *
00009 * it under the terms of the GNU Lesser General Public License as published by   *
00010 * the Free Software Foundation; either version 3 of the License, or             *
00011 * (at your option) any later version.                                           *
00012 *                                                                               *
00013 * This library is distributed in the hope that it will be useful,               *
00014 * but WITHOUT ANY WARRANTY; without even the implied warranty of                *
00015 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                 *
00016 * GNU Lesser General Public License for more details.                           *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public License      *
00019 * along with this program.  If not, see <http://www.gnu.org/licenses/>          *
00020 ********************************************************************************/
00021 #ifndef FXHASH_H
00022 #define FXHASH_H
00023 
00024 namespace FX {
00025 
00026 
00027 /**
00028 * A hash table for associating pointers to pointers.
00029 */
00030 class FXAPI FXHash {
00031 protected:
00032   struct FXEntry {
00033     void* name;
00034     void* data;
00035     };
00036 protected:
00037   FXEntry *table;       // Hash table
00038   FXuint   total;       // Table size
00039   FXuint   used;        // Number of used entries
00040   FXuint   free;        // Number of free entries
00041 private:
00042   FXHash(const FXHash&);
00043   FXHash &operator=(const FXHash&);
00044 public:
00045 
00046   /**
00047   * Construct empty hash table.
00048   */
00049   FXHash();
00050 
00051   /**
00052   * Resize the table to the given size; the size must be
00053   * a power of two.
00054   */
00055   FXbool size(FXuint m);
00056 
00057   /**
00058   * Return the total number of slots in the table.
00059   */
00060   FXuint size() const { return total; }
00061 
00062   /**
00063   * Return number of non-empty slots in the table.
00064   */
00065   FXuint no() const { return used; }
00066 
00067   /**
00068   * Insert key into table, unless the key already exists.
00069   * Returns the current value of the key.
00070   */
00071   void* insert(void* name,void* data=NULL);
00072 
00073   /**
00074   * Replace key in table, overwriting the old value if the
00075   * given key already exists.  Returns the old value of the key.
00076   */
00077   void* replace(void* name,void* data=NULL);
00078 
00079   /**
00080   * Remove key from the table. Returns the old value of the key.
00081   */
00082   void* remove(void* name);
00083 
00084   /**
00085   * Return value of key, or return NULL.
00086   */
00087   void* find(void* name) const;
00088 
00089   /**
00090   * Return true if slot is not occupied by a key.
00091   */
00092   FXbool empty(FXuint pos) const { return (table[pos].name==NULL)||(table[pos].name==(void*)-1L); }
00093 
00094   /**
00095   * Return key at position pos.
00096   */
00097   void* key(FXuint pos) const { return table[pos].name; }
00098 
00099   /**
00100   * Return data pointer at position pos.
00101   */
00102   void* value(FXuint pos) const { return table[pos].data; }
00103 
00104   /**
00105   * Clear hash table.
00106   */
00107   void clear();
00108 
00109   /// Destructor
00110   virtual ~FXHash();
00111   };
00112 
00113 
00114 }
00115 
00116 #endif

Copyright © 1997-2010 Jeroen van der Zijp