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,2006 by Jeroen van der Zijp.   All Rights Reserved.        *
00007 *********************************************************************************
00008 * This library is free software; you can redistribute it and/or                 *
00009 * modify it under the terms of the GNU Lesser General Public                    *
00010 * License as published by the Free Software Foundation; either                  *
00011 * version 2.1 of the License, or (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 GNU             *
00016 * Lesser General Public License for more details.                               *
00017 *                                                                               *
00018 * You should have received a copy of the GNU Lesser General Public              *
00019 * License along with this library; if not, write to the Free Software           *
00020 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA.    *
00021 *********************************************************************************
00022 * $Id: FXDict.h,v 1.26 2006/01/22 17:58:00 fox Exp $                            *
00023 ********************************************************************************/
00024 #ifndef FXDICT_H
00025 #define FXDICT_H
00026 
00027 #ifndef FXOBJECT_H
00028 #include "FXObject.h"
00029 #endif
00030 
00031 namespace FX {
00032 
00033 
00034 /**
00035 * The dictionary class maintains a fast-access hash table of entities
00036 * indexed by a character string.
00037 * It is typically used to map strings to pointers; however, overloading
00038 * the createData() and deleteData() members allows any type of data to
00039 * be indexed by strings.
00040 */
00041 class FXAPI FXDict : public FXObject {
00042   FXDECLARE(FXDict)
00043 protected:
00044   struct FXDictEntry {
00045     FXchar *key;              // Key string
00046     void   *data;             // Data
00047     FXint   hash;             // Hash value of key
00048     bool    mark;             // Entry is marked
00049     };
00050 protected:
00051   FXDictEntry *dict;          // Dictionary
00052   FXint        total;         // Dictionary size
00053   FXint        number;        // Number of entries
00054 protected:
00055   static FXint hash(const FXchar* str);
00056 protected:
00057 
00058   /**
00059   * Overload this function in a derived class to return the
00060   * data pointer given an input pointer; the default implementation
00061   * just returns the input pointer.
00062   */
00063   virtual void *createData(const void*);
00064 
00065   /**
00066   * Overload this function in a derived class to delete the pointer
00067   * previously returned by createData(); the default implementation
00068   * does nothing.
00069   */
00070   virtual void deleteData(void*);
00071 public:
00072 
00073   /**
00074   * Construct an empty dictionary.
00075   */
00076   FXDict();
00077 
00078   /// Copy constructor; does bit-copy of void pointer data.
00079   FXDict(const FXDict& orig);
00080 
00081   /// Assignment operator
00082   FXDict& operator=(const FXDict& orig);
00083 
00084   /**
00085   * Resize the table to the given size.
00086   */
00087   void size(FXint m);
00088 
00089   /**
00090   * Return the size of the table, including the empty slots.
00091   */
00092   FXint size() const { return total; }
00093 
00094   /**
00095   * Return the total number of entries in the table.
00096   */
00097   FXint no() const { return number; }
00098 
00099   /**
00100   * Insert a new entry into the table given key and mark.
00101   * If there is already an entry with that key, leave it unchanged,
00102   * otherwise insert the new entry.
00103   */
00104   void* insert(const FXchar* ky,const void* ptr,bool mrk=false);
00105 
00106   /**
00107   * Replace data at key, if the entry's mark is less than
00108   * or equal to the given mark.  If there was no existing entry,
00109   * a new entry is inserted with the given mark.
00110   */
00111   void* replace(const FXchar* ky,const void* ptr,bool mrk=false);
00112 
00113   /**
00114   * Remove data given key.
00115   */
00116   void* remove(const FXchar* ky);
00117 
00118   /**
00119   * Find data pointer given key.
00120   */
00121   void* find(const FXchar* ky) const;
00122 
00123   /**
00124   * Return true if slot is empty.
00125   */
00126   bool empty(FXint pos) const { return dict[pos].hash<0; }
00127 
00128   /**
00129   * Return key at position pos.
00130   */
00131   const FXchar* key(FXuint pos) const { return dict[pos].key; }
00132 
00133   /**
00134   * return data pointer at position pos.
00135   */
00136   void* data(FXuint pos) const { return dict[pos].data; }
00137 
00138   /**
00139   * Return mark flag of entry at position pos.
00140   */
00141   bool mark(FXuint pos) const { return dict[pos].mark; }
00142 
00143   /**
00144   * Return position of first filled slot, or >= total
00145   */
00146   FXint first() const;
00147 
00148   /**
00149   * Return position of last filled slot or -1
00150   */
00151   FXint last() const;
00152 
00153 
00154   /**
00155   * Return position of next filled slot in hash table
00156   * or a value greater than or equal to total if no filled
00157   * slot was found
00158   */
00159   FXint next(FXint pos) const;
00160 
00161   /**
00162   * Return position of previous filled slot in hash table
00163   * or a -1 if no filled slot was found
00164   */
00165   FXint prev(FXint pos) const;
00166 
00167   /// Clear all entries
00168   void clear();
00169 
00170   /// Destructor
00171   virtual ~FXDict();
00172   };
00173 
00174 }
00175 
00176 #endif

Copyright © 1997-2005 Jeroen van der Zijp