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

FXDict.h
Go to the documentation of this file.
1 /********************************************************************************
2 * *
3 * S t r i n g D i c t i o n a r y C l a s s *
4 * *
5 *********************************************************************************
6 * Copyright (C) 1998,2006 by Jeroen van der Zijp. All Rights Reserved. *
7 *********************************************************************************
8 * This library is free software; you can redistribute it and/or *
9 * modify it under the terms of the GNU Lesser General Public *
10 * License as published by the Free Software Foundation; either *
11 * version 2.1 of the License, or (at your option) any later version. *
12 * *
13 * This library is distributed in the hope that it will be useful, *
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU *
16 * Lesser General Public License for more details. *
17 * *
18 * You should have received a copy of the GNU Lesser General Public *
19 * License along with this library; if not, write to the Free Software *
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA. *
21 *********************************************************************************
22 * $Id: FXDict.h,v 1.26 2006/01/22 17:58:00 fox Exp $ *
23 ********************************************************************************/
24 #ifndef FXDICT_H
25 #define FXDICT_H
26 
27 #ifndef FXOBJECT_H
28 #include "FXObject.h"
29 #endif
30 
31 namespace FX {
32 
33 
34 /**
35 * The dictionary class maintains a fast-access hash table of entities
36 * indexed by a character string.
37 * It is typically used to map strings to pointers; however, overloading
38 * the createData() and deleteData() members allows any type of data to
39 * be indexed by strings.
40 */
41 class FXAPI FXDict : public FXObject {
43 protected:
44  struct FXDictEntry {
45  FXchar *key; // Key string
46  void *data; // Data
47  FXint hash; // Hash value of key
48  bool mark; // Entry is marked
49  };
50 protected:
51  FXDictEntry *dict; // Dictionary
52  FXint total; // Dictionary size
53  FXint number; // Number of entries
54 protected:
55  static FXint hash(const FXchar* str);
56 protected:
57 
58  /**
59  * Overload this function in a derived class to return the
60  * data pointer given an input pointer; the default implementation
61  * just returns the input pointer.
62  */
63  virtual void *createData(const void*);
64 
65  /**
66  * Overload this function in a derived class to delete the pointer
67  * previously returned by createData(); the default implementation
68  * does nothing.
69  */
70  virtual void deleteData(void*);
71 public:
72 
73  /**
74  * Construct an empty dictionary.
75  */
76  FXDict();
77 
78  /// Copy constructor; does bit-copy of void pointer data.
79  FXDict(const FXDict& orig);
80 
81  /// Assignment operator
82  FXDict& operator=(const FXDict& orig);
83 
84  /**
85  * Resize the table to the given size.
86  */
87  void size(FXint m);
88 
89  /**
90  * Return the size of the table, including the empty slots.
91  */
92  FXint size() const { return total; }
93 
94  /**
95  * Return the total number of entries in the table.
96  */
97  FXint no() const { return number; }
98 
99  /**
100  * Insert a new entry into the table given key and mark.
101  * If there is already an entry with that key, leave it unchanged,
102  * otherwise insert the new entry.
103  */
104  void* insert(const FXchar* ky,const void* ptr,bool mrk=false);
105 
106  /**
107  * Replace data at key, if the entry's mark is less than
108  * or equal to the given mark. If there was no existing entry,
109  * a new entry is inserted with the given mark.
110  */
111  void* replace(const FXchar* ky,const void* ptr,bool mrk=false);
112 
113  /**
114  * Remove data given key.
115  */
116  void* remove(const FXchar* ky);
117 
118  /**
119  * Find data pointer given key.
120  */
121  void* find(const FXchar* ky) const;
122 
123  /**
124  * Return true if slot is empty.
125  */
126  bool empty(FXint pos) const { return dict[pos].hash<0; }
127 
128  /**
129  * Return key at position pos.
130  */
131  const FXchar* key(FXuint pos) const { return dict[pos].key; }
132 
133  /**
134  * return data pointer at position pos.
135  */
136  void* data(FXuint pos) const { return dict[pos].data; }
137 
138  /**
139  * Return mark flag of entry at position pos.
140  */
141  bool mark(FXuint pos) const { return dict[pos].mark; }
142 
143  /**
144  * Return position of first filled slot, or >= total
145  */
146  FXint first() const;
147 
148  /**
149  * Return position of last filled slot or -1
150  */
151  FXint last() const;
152 
153 
154  /**
155  * Return position of next filled slot in hash table
156  * or a value greater than or equal to total if no filled
157  * slot was found
158  */
159  FXint next(FXint pos) const;
160 
161  /**
162  * Return position of previous filled slot in hash table
163  * or a -1 if no filled slot was found
164  */
165  FXint prev(FXint pos) const;
166 
167  /// Clear all entries
168  void clear();
169 
170  /// Destructor
171  virtual ~FXDict();
172  };
173 
174 }
175 
176 #endif
char FXchar
Definition: fxdefs.h:380
unsigned int FXuint
Definition: fxdefs.h:389
#define FXAPI
Definition: fxdefs.h:122
Definition: FX4Splitter.h:31
int FXint
Definition: fxdefs.h:390
The dictionary class maintains a fast-access hash table of entities indexed by a character string...
Definition: FXDict.h:41
Object is the base class for all objects in FOX; in order to receive messages from the user interface...
Definition: FXObject.h:166
#define FXDECLARE(classname)
Macro to set up class declaration.
Definition: FXObject.h:92

Copyright © 1997-2005 Jeroen van der Zijp