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

FXDict.h

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,2004 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.19 2004/02/08 17:17:33 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     FXbool  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 
00056   /**
00057   * Overload this function in a derived class to return the
00058   * data pointer given an input pointer; the default implementation
00059   * just returns the input pointer.
00060   */
00061   virtual void *createData(const void*);
00062 
00063   /**
00064   * Overload this function in a derived class to delete the pointer
00065   * previously returned by createData(); the default implementation
00066   * does nothing.
00067   */
00068   virtual void deleteData(void*);
00069 private:
00070   FXDict(const FXDict&);
00071   FXDict &operator=(const FXDict&);
00072 public:
00073 
00074   /**
00075   * Construct an empty dictionary.
00076   */
00077   FXDict();
00078 
00079   /**
00080   * Return the size of the table, including the empty slots.
00081   */
00082   FXint size() const { return total; }
00083 
00084   /**
00085   * Resize the table to the given size.
00086   */
00087   void size(FXint m);
00088 
00089   /**
00090   * Return the total number of entries in the table.
00091   */
00092   FXint no() const { return number; }
00093 
00094   /**
00095   * Insert a new entry into the table given key and mark.
00096   * If there is already an entry with that key, leave it unchanged,
00097   * otherwise insert the new entry.
00098   */
00099   void* insert(const FXchar* ky,const void* ptr,FXbool mrk=FALSE);
00100 
00101   /**
00102   * Replace data at key, if the entry's mark is less than
00103   * or equal to the given mark.  If there was no existing entry,
00104   * a new entry is inserted with the given mark.
00105   */
00106   void* replace(const FXchar* ky,const void* ptr,FXbool mrk=FALSE);
00107 
00108   /**
00109   * Remove data given key.
00110   */
00111   void* remove(const FXchar* ky);
00112 
00113   /**
00114   * Find data pointer given key.
00115   */
00116   void* find(const FXchar* ky) const;
00117 
00118   /**
00119   * Return key at position pos.
00120   */
00121   const FXchar* key(FXuint pos) const { return dict[pos].key; }
00122 
00123   /**
00124   * return data pointer at position pos.
00125   */
00126   void* data(FXuint pos) const { return dict[pos].data; }
00127 
00128   /**
00129   * Return mark flag of entry at position pos.
00130   */
00131   FXbool mark(FXuint pos) const { return dict[pos].mark; }
00132 
00133   /**
00134   * Return position of first filled slot, or >= total
00135   */
00136   FXint first() const;
00137 
00138   /**
00139   * Return position of last filled slot or -1
00140   */
00141   FXint last() const;
00142 
00143 
00144   /**
00145   * Return position of next filled slot in hash table
00146   * or a value greater than or equal to total if no filled
00147   * slot was found
00148   */
00149   FXint next(FXint pos) const;
00150 
00151   /**
00152   * Return position of previous filled slot in hash table
00153   * or a -1 if no filled slot was found
00154   */
00155   FXint prev(FXint pos) const;
00156 
00157   /// Clear all entries
00158   void clear();
00159 
00160   /// Destructor
00161   virtual ~FXDict();
00162   };
00163 
00164 }
00165 
00166 #endif

Copyright © 1997-2004 Jeroen van der Zijp