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