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,2009 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 * $Id: FXHash.h,v 1.21 2009/01/06 13:07:24 fox Exp $                            *
00022 ********************************************************************************/
00023 #ifndef FXHASH_H
00024 #define FXHASH_H
00025 
00026 namespace FX {
00027 
00028 
00029 /**
00030 * A hash table for associating pointers to pointers.
00031 */
00032 class FXAPI FXHash {
00033 protected:
00034   struct FXEntry {
00035     void* name;
00036     void* data;
00037     };
00038 protected:
00039   FXEntry *table;       // Hash table
00040   FXuint   total;       // Table size
00041   FXuint   used;        // Number of used entries
00042   FXuint   free;        // Number of free entries
00043 private:
00044   FXHash(const FXHash&);
00045   FXHash &operator=(const FXHash&);
00046 public:
00047 
00048   /**
00049   * Construct empty hash table.
00050   */
00051   FXHash();
00052 
00053   /**
00054   * Resize the table to the given size; the size must be
00055   * a power of two.
00056   */
00057   void size(FXuint m);
00058 
00059   /**
00060   * Return the total number of slots in the table.
00061   */
00062   FXuint size() const { return total; }
00063 
00064   /**
00065   * Return number of non-empty slots in the table.
00066   */
00067   FXuint no() const { return used; }
00068 
00069   /**
00070   * Insert key into table, unless the key already exists.
00071   * Returns the current value of the key.
00072   */
00073   void* insert(void* name,void* data);
00074 
00075   /**
00076   * Replace key in table, overwriting the old value if the
00077   * given key already exists.  Returns the old value of the key.
00078   */
00079   void* replace(void* name,void* data);
00080 
00081   /**
00082   * Remove key from the table. Returns the old value of the key.
00083   */
00084   void* remove(void* name);
00085 
00086   /**
00087   * Return value of key, or return NULL.
00088   */
00089   void* find(void* name) const;
00090 
00091   /**
00092   * Return true if slot is not occupied by a key.
00093   */
00094   FXbool empty(FXuint pos) const { return (table[pos].name==NULL)||(table[pos].name==(void*)-1L); }
00095 
00096   /**
00097   * Return key at position pos.
00098   */
00099   void* key(FXuint pos) const { return table[pos].name; }
00100 
00101   /**
00102   * Return data pointer at position pos.
00103   */
00104   void* value(FXuint pos) const { return table[pos].data; }
00105 
00106   /**
00107   * Clear hash table.
00108   */
00109   void clear();
00110 
00111   /// Destructor
00112   virtual ~FXHash();
00113   };
00114 
00115 
00116 }
00117 
00118 #endif

Copyright © 1997-2009 Jeroen van der Zijp