728
|
1 // octave-map.cc -*- C++ -*- |
|
2 /* |
|
3 |
|
4 Copyright (C) 1992, 1993, 1994 John W. Eaton |
|
5 |
|
6 This file is part of Octave. |
|
7 |
|
8 Octave is free software; you can redistribute it and/or modify it |
|
9 under the terms of the GNU General Public License as published by the |
|
10 Free Software Foundation; either version 2, or (at your option) any |
|
11 later version. |
|
12 |
|
13 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
16 for more details. |
|
17 |
|
18 You should have received a copy of the GNU General Public License |
|
19 along with Octave; see the file COPYING. If not, write to the Free |
|
20 Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. |
|
21 |
|
22 */ |
|
23 |
745
|
24 /* |
|
25 |
|
26 The classes in this file are derived from the old `genclass' versions |
|
27 of Map and CHMap from libg++, originally: |
|
28 |
|
29 Copyright (C) 1988 Free Software Foundation |
|
30 written by Doug Lea (dl@rocky.oswego.edu) |
|
31 |
|
32 and distributed under the terms of the GNU Library General Public |
|
33 License as published by the Free Software Foundation. |
|
34 |
|
35 */ |
|
36 |
728
|
37 #ifdef HAVE_CONFIG_H |
|
38 #include "config.h" |
|
39 #endif |
|
40 |
|
41 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
|
42 #pragma implementation |
|
43 #endif |
|
44 |
|
45 #include <iostream.h> |
|
46 |
|
47 #include "Map.h" |
|
48 |
|
49 static unsigned int |
|
50 hash (const char *str) |
|
51 { |
|
52 unsigned h = 0; |
|
53 while (*str) |
|
54 h = h * 33 + *str++; |
|
55 return h; |
|
56 } |
|
57 |
|
58 template <class C> |
|
59 Pix |
|
60 Map<C>::seek (const char *item) const |
|
61 { |
|
62 for (Pix i = first (); i != 0 && strcmp (key (i), item) != 0; next (i)) |
|
63 ; // Skip items until match found. |
|
64 |
|
65 return i; |
|
66 } |
|
67 |
|
68 template <class C> |
|
69 int |
|
70 Map<C>::owns (Pix idx) const |
|
71 { |
|
72 if (idx == 0) |
|
73 return 0; |
|
74 |
|
75 for (Pix i = first (); i != 0; next (i)) |
|
76 if (i == idx) |
|
77 return 1; |
|
78 |
|
79 return 0; |
|
80 } |
|
81 |
|
82 template <class C> |
|
83 void |
|
84 Map<C>::clear (void) |
|
85 { |
|
86 Pix i = first (); |
|
87 while (i != 0) |
|
88 { |
|
89 del (key (i)); |
|
90 i = first (); |
|
91 } |
|
92 } |
|
93 |
|
94 template <class C> |
|
95 int |
|
96 Map<C>::contains (const char *item) const |
|
97 { |
|
98 return seek (item) != 0; |
|
99 } |
|
100 |
|
101 template <class C> |
|
102 void |
|
103 Map<C>::error (const char* msg) const |
|
104 { |
|
105 cerr << "Map: " << msg << "\n"; |
|
106 } |
|
107 |
|
108 // CHMap class. |
|
109 |
|
110 // The nodes are linked together serially via a version of a trick |
|
111 // used in some vtables: odd pointers are actually links to the next |
|
112 // table entry. Not terrible, but not wonderful either. |
|
113 |
|
114 template <class C> |
|
115 static inline |
|
116 int |
|
117 goodCHptr (CHNode<C> *t) |
|
118 { |
|
119 return ((((unsigned) t) & 1) == 0); |
|
120 } |
|
121 |
|
122 // This sucks, but avoids g++ 2.6.0 `type unification failed' errors. |
|
123 |
|
124 void * |
|
125 index_to_CHptr (int i) |
|
126 { |
|
127 return (void *) ((i << 1) + 1); |
|
128 } |
|
129 |
|
130 template <class C> |
|
131 static inline |
|
132 int |
|
133 CHptr_to_index (CHNode<C> *t) |
|
134 { |
|
135 return ((unsigned) t) >> 1; |
|
136 } |
|
137 |
|
138 template <class C> |
|
139 CHMap<C>::CHMap (const C& dflt, unsigned int sz) : Map<C> (dflt) |
|
140 { |
|
141 tab = new CHNode<C>* [size = sz]; |
|
142 for (unsigned int i = 0; i < size; ++i) |
|
143 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
144 count = 0; |
|
145 } |
|
146 |
|
147 template <class C> |
|
148 CHMap<C>::CHMap (const CHMap& a) : Map<C> (a.def) |
|
149 { |
|
150 tab = new CHNode<C>* [size = a.size]; |
|
151 for (unsigned int i = 0; i < size; ++i) |
|
152 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
153 count = 0; |
|
154 for (Pix p = a.first (); p; a.next (p)) |
|
155 (*this) [a.key (p)] = a.contents (p); |
|
156 } |
|
157 |
|
158 template <class C> |
|
159 Pix |
|
160 CHMap<C>::seek (const char *key) const |
|
161 { |
|
162 unsigned int h = hash (key) % size; |
|
163 |
|
164 for (CHNode<C> *t = tab[h]; goodCHptr (t); t = t->tl) |
|
165 if (strcmp (key, t->hd) == 0) |
|
166 return Pix (t); |
|
167 |
|
168 return 0; |
|
169 } |
|
170 |
|
171 template <class C> |
|
172 C& |
|
173 CHMap<C>::operator [] (const char *item) |
|
174 { |
|
175 unsigned int h = hash (item) % size; |
|
176 |
|
177 for (CHNode<C> *t = tab[h]; goodCHptr (t); t = t->tl) |
|
178 if (strcmp (item, t->hd) == 0) |
|
179 return t->cont; |
|
180 |
|
181 t = new CHNode<C> (item, def, tab[h]); |
|
182 tab[h] = t; |
|
183 ++count; |
|
184 return t->cont; |
|
185 } |
|
186 |
|
187 template <class C> |
|
188 void |
|
189 CHMap<C>::del (const char *key) |
|
190 { |
|
191 unsigned int h = hash (key) % size; |
|
192 |
|
193 CHNode<C> *t = tab[h]; |
|
194 CHNode<C> *trail = t; |
|
195 while (goodCHptr (t)) |
|
196 { |
|
197 if (strcmp (key, t->hd) == 0) |
|
198 { |
|
199 if (trail == t) |
|
200 tab[h] = t->tl; |
|
201 else |
|
202 trail->tl = t->tl; |
|
203 delete t; |
|
204 --count; |
|
205 return; |
|
206 } |
|
207 trail = t; |
|
208 t = t->tl; |
|
209 } |
|
210 } |
|
211 |
|
212 template <class C> |
|
213 void |
|
214 CHMap<C>::clear (void) |
|
215 { |
|
216 for (unsigned int i = 0; i < size; ++i) |
|
217 { |
|
218 CHNode<C> *p = tab[i]; |
|
219 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
220 while (goodCHptr (p)) |
|
221 { |
|
222 CHNode<C> *nxt = p->tl; |
|
223 delete p; |
|
224 p = nxt; |
|
225 } |
|
226 } |
|
227 count = 0; |
|
228 } |
|
229 |
|
230 template <class C> |
|
231 Pix |
|
232 CHMap<C>::first (void) const |
|
233 { |
|
234 for (unsigned int i = 0; i < size; ++i) |
|
235 if (goodCHptr (tab[i])) |
|
236 return Pix (tab[i]); |
|
237 return 0; |
|
238 } |
|
239 |
|
240 template <class C> |
|
241 void |
|
242 CHMap<C>::next (Pix& p) const |
|
243 { |
|
244 CHNode<C> *t = ((CHNode<C> *) p)->tl; |
|
245 if (goodCHptr (t)) |
|
246 p = Pix (t); |
|
247 else |
|
248 { |
|
249 for (unsigned int i = CHptr_to_index (t); i < size; ++i) |
|
250 { |
|
251 if (goodCHptr (tab[i])) |
|
252 { |
|
253 p = Pix (tab[i]); |
|
254 return; |
|
255 } |
|
256 } |
|
257 p = 0; |
|
258 } |
|
259 } |
|
260 |
|
261 template <class C> |
|
262 int |
|
263 CHMap<C>::OK (void) const |
|
264 { |
|
265 int v = tab != 0; |
|
266 int n = 0; |
|
267 |
|
268 for (unsigned int i = 0; i < size; ++i) |
|
269 { |
|
270 for (CHNode<C> *p = tab[i]; goodCHptr (p); p = p->tl) |
|
271 ++n; |
|
272 |
|
273 v &= CHptr_to_index (p) == i + 1; |
|
274 } |
|
275 |
|
276 v &= count == n; |
|
277 |
|
278 if (! v) |
|
279 error ("invariant failure"); |
|
280 |
|
281 return v; |
|
282 } |
|
283 |
|
284 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
|
285 #if defined (OCTAVE_SOURCE) |
|
286 |
745
|
287 #include "tree-const.h" |
|
288 typedef Map<tree_constant> map_type_tree_constant; |
|
289 typedef CHNode<tree_constant> chnode_type_tree_constant; |
|
290 typedef CHMap<tree_constant> chmap_type_tree_constant; |
728
|
291 |
|
292 #elif defined (USER_TYPEDEFS) |
|
293 |
|
294 // Users can generate their own .o files with their own types, as many |
|
295 // times as they like. USER_TYPEDEFS should be defined to be the name |
|
296 // of an include file that contains typdefs for the desired types. |
|
297 // |
|
298 // For example, if my-types.h contains typedefs for the Map types |
|
299 // you are interested in, you might compile this file with the command |
|
300 // |
|
301 // g++ -fexternal-templates -DUSE_EXTERNAL_TEMPLATES \ |
|
302 // -DUSER_TYPEDEFS=\"my-types.h\" |
|
303 |
|
304 #include USER_TYPEDEFS |
|
305 |
|
306 #endif |
|
307 #endif |