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

FXDict.h

Go to the documentation of this file.
00001 /********************************************************************************
00002 *                                                                               *
00003 *                  S t r i n g   D i c t i o n a r y    C l a s s               *
00004 *                                                                               *
00005 *********************************************************************************
00006 * Copyright (C) 1998,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: FXDict.h,v 1.34 2009/01/06 13:07:22 fox Exp $                            *
00022 ********************************************************************************/
00023 #ifndef FXDICT_H
00024 #define FXDICT_H
00025 
00026 #ifndef FXOBJECT_H
00027 #include "FXObject.h"
00028 #endif
00029 
00030 namespace FX {
00031 
00032 
00033 /**
00034 * The dictionary class maintains a fast-access hash table of entities
00035 * indexed by a character string.
00036 * It is typically used to map strings to pointers; however, overloading
00037 * the createData() and deleteData() members allows any type of data to
00038 * be indexed by strings.
00039 */
00040 class FXAPI FXDict : public FXObject {
00041   FXDECLARE(FXDict)
00042 protected:
00043   struct FXDictEntry {
00044     FXchar *key;              // Key string
00045     void   *data;             // Data
00046     FXint   hash;             // Hash value of key
00047     FXbool  mark;             // Entry is marked
00048     };
00049 protected:
00050   FXDictEntry *dict;          // Dictionary
00051   FXint        total;         // Dictionary size
00052   FXint        number;        // Number of entries
00053 protected:
00054   static FXint hash(const FXchar* str);
00055 protected:
00056 
00057   /**
00058   * Overload this function in a derived class to return the
00059   * data pointer given an input pointer; the default implementation
00060   * just returns the input pointer.
00061   */
00062   virtual void *createData(void*);
00063 
00064   /**
00065   * Overload this function in a derived class to delete the pointer
00066   * previously returned by createData(); the default implementation
00067   * does nothing.
00068   */
00069   virtual void deleteData(void*);
00070 public:
00071 
00072   /**
00073   * Construct an empty dictionary.
00074   */
00075   FXDict();
00076 
00077   /// Copy constructor; does bit-copy of void pointer data.
00078   FXDict(const FXDict& orig);
00079 
00080   /// Assignment operator
00081   FXDict& operator=(const FXDict& orig);
00082 
00083   /**
00084   * Resize the table to the given size.
00085   */
00086   void size(FXint m);
00087 
00088   /**
00089   * Return the size of the table, including the empty slots.
00090   */
00091   FXint size() const { return total; }
00092 
00093   /**
00094   * Return the total number of entries in the table.
00095   */
00096   FXint no() const { return number; }
00097 
00098   /**
00099   * Insert a new entry into the table given key and mark.
00100   * If there is already an entry with that key, leave it unchanged,
00101   * otherwise insert the new entry.
00102   */
00103   void* insert(const FXchar* ky,void* ptr,FXbool mrk=false);
00104 
00105   /**
00106   * Replace data at key, if the entry's mark is less than
00107   * or equal to the given mark.  If there was no existing entry,
00108   * a new entry is inserted with the given mark.
00109   */
00110   void* replace(const FXchar* ky,void* ptr,FXbool mrk=false);
00111 
00112   /**
00113   * Remove data given key.
00114   */
00115   void* remove(const FXchar* ky);
00116 
00117   /**
00118   * Find data pointer given key.
00119   */
00120   void* find(const FXchar* ky) const;
00121 
00122   /**
00123   * Return true if slot is empty.
00124   */
00125   FXbool empty(FXint pos) const { return dict[pos].hash<0; }
00126 
00127   /**
00128   * Return key at position pos.
00129   */
00130   const FXchar* key(FXint pos) const { return dict[pos].key; }
00131 
00132   /**
00133   * Return data pointer at position pos.
00134   */
00135   void* data(FXint pos) const { return dict[pos].data; }
00136 
00137   /**
00138   * Return mark flag of entry at position pos.
00139   */
00140   FXbool mark(FXint pos) const { return dict[pos].mark; }
00141 
00142   /**
00143   * Return position of first filled slot, or >= total
00144   */
00145   FXint first() const;
00146 
00147   /**
00148   * Return position of last filled slot or -1
00149   */
00150   FXint last() const;
00151 
00152 
00153   /**
00154   * Return position of next filled slot in hash table
00155   * or a value greater than or equal to total if no filled
00156   * slot was found
00157   */
00158   FXint next(FXint pos) const;
00159 
00160   /**
00161   * Return position of previous filled slot in hash table
00162   * or a -1 if no filled slot was found
00163   */
00164   FXint prev(FXint pos) const;
00165 
00166   /// Clear all entries
00167   void clear();
00168 
00169   /// Destructor
00170   virtual ~FXDict();
00171   };
00172 
00173 }
00174 
00175 #endif

Copyright © 1997-2009 Jeroen van der Zijp