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 |
|
24 #ifdef HAVE_CONFIG_H |
|
25 #include "config.h" |
|
26 #endif |
|
27 |
|
28 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
|
29 #pragma implementation |
|
30 #endif |
|
31 |
|
32 #include <iostream.h> |
|
33 |
|
34 #include "Map.h" |
|
35 |
|
36 static unsigned int |
|
37 hash (const char *str) |
|
38 { |
|
39 unsigned h = 0; |
|
40 while (*str) |
|
41 h = h * 33 + *str++; |
|
42 return h; |
|
43 } |
|
44 |
|
45 template <class C> |
|
46 Pix |
|
47 Map<C>::seek (const char *item) const |
|
48 { |
|
49 for (Pix i = first (); i != 0 && strcmp (key (i), item) != 0; next (i)) |
|
50 ; // Skip items until match found. |
|
51 |
|
52 return i; |
|
53 } |
|
54 |
|
55 template <class C> |
|
56 int |
|
57 Map<C>::owns (Pix idx) const |
|
58 { |
|
59 if (idx == 0) |
|
60 return 0; |
|
61 |
|
62 for (Pix i = first (); i != 0; next (i)) |
|
63 if (i == idx) |
|
64 return 1; |
|
65 |
|
66 return 0; |
|
67 } |
|
68 |
|
69 template <class C> |
|
70 void |
|
71 Map<C>::clear (void) |
|
72 { |
|
73 Pix i = first (); |
|
74 while (i != 0) |
|
75 { |
|
76 del (key (i)); |
|
77 i = first (); |
|
78 } |
|
79 } |
|
80 |
|
81 template <class C> |
|
82 int |
|
83 Map<C>::contains (const char *item) const |
|
84 { |
|
85 return seek (item) != 0; |
|
86 } |
|
87 |
|
88 template <class C> |
|
89 void |
|
90 Map<C>::error (const char* msg) const |
|
91 { |
|
92 cerr << "Map: " << msg << "\n"; |
|
93 } |
|
94 |
|
95 // CHMap class. |
|
96 |
|
97 // The nodes are linked together serially via a version of a trick |
|
98 // used in some vtables: odd pointers are actually links to the next |
|
99 // table entry. Not terrible, but not wonderful either. |
|
100 |
|
101 template <class C> |
|
102 static inline |
|
103 int |
|
104 goodCHptr (CHNode<C> *t) |
|
105 { |
|
106 return ((((unsigned) t) & 1) == 0); |
|
107 } |
|
108 |
|
109 // This sucks, but avoids g++ 2.6.0 `type unification failed' errors. |
|
110 |
|
111 void * |
|
112 index_to_CHptr (int i) |
|
113 { |
|
114 return (void *) ((i << 1) + 1); |
|
115 } |
|
116 |
|
117 template <class C> |
|
118 static inline |
|
119 int |
|
120 CHptr_to_index (CHNode<C> *t) |
|
121 { |
|
122 return ((unsigned) t) >> 1; |
|
123 } |
|
124 |
|
125 template <class C> |
|
126 CHMap<C>::CHMap (const C& dflt, unsigned int sz) : Map<C> (dflt) |
|
127 { |
|
128 tab = new CHNode<C>* [size = sz]; |
|
129 for (unsigned int i = 0; i < size; ++i) |
|
130 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
131 count = 0; |
|
132 } |
|
133 |
|
134 template <class C> |
|
135 CHMap<C>::CHMap (const CHMap& a) : Map<C> (a.def) |
|
136 { |
|
137 tab = new CHNode<C>* [size = a.size]; |
|
138 for (unsigned int i = 0; i < size; ++i) |
|
139 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
140 count = 0; |
|
141 for (Pix p = a.first (); p; a.next (p)) |
|
142 (*this) [a.key (p)] = a.contents (p); |
|
143 } |
|
144 |
|
145 template <class C> |
|
146 Pix |
|
147 CHMap<C>::seek (const char *key) const |
|
148 { |
|
149 unsigned int h = hash (key) % size; |
|
150 |
|
151 for (CHNode<C> *t = tab[h]; goodCHptr (t); t = t->tl) |
|
152 if (strcmp (key, t->hd) == 0) |
|
153 return Pix (t); |
|
154 |
|
155 return 0; |
|
156 } |
|
157 |
|
158 template <class C> |
|
159 C& |
|
160 CHMap<C>::operator [] (const char *item) |
|
161 { |
|
162 unsigned int h = hash (item) % size; |
|
163 |
|
164 for (CHNode<C> *t = tab[h]; goodCHptr (t); t = t->tl) |
|
165 if (strcmp (item, t->hd) == 0) |
|
166 return t->cont; |
|
167 |
|
168 t = new CHNode<C> (item, def, tab[h]); |
|
169 tab[h] = t; |
|
170 ++count; |
|
171 return t->cont; |
|
172 } |
|
173 |
|
174 template <class C> |
|
175 void |
|
176 CHMap<C>::del (const char *key) |
|
177 { |
|
178 unsigned int h = hash (key) % size; |
|
179 |
|
180 CHNode<C> *t = tab[h]; |
|
181 CHNode<C> *trail = t; |
|
182 while (goodCHptr (t)) |
|
183 { |
|
184 if (strcmp (key, t->hd) == 0) |
|
185 { |
|
186 if (trail == t) |
|
187 tab[h] = t->tl; |
|
188 else |
|
189 trail->tl = t->tl; |
|
190 delete t; |
|
191 --count; |
|
192 return; |
|
193 } |
|
194 trail = t; |
|
195 t = t->tl; |
|
196 } |
|
197 } |
|
198 |
|
199 template <class C> |
|
200 void |
|
201 CHMap<C>::clear (void) |
|
202 { |
|
203 for (unsigned int i = 0; i < size; ++i) |
|
204 { |
|
205 CHNode<C> *p = tab[i]; |
|
206 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
207 while (goodCHptr (p)) |
|
208 { |
|
209 CHNode<C> *nxt = p->tl; |
|
210 delete p; |
|
211 p = nxt; |
|
212 } |
|
213 } |
|
214 count = 0; |
|
215 } |
|
216 |
|
217 template <class C> |
|
218 Pix |
|
219 CHMap<C>::first (void) const |
|
220 { |
|
221 for (unsigned int i = 0; i < size; ++i) |
|
222 if (goodCHptr (tab[i])) |
|
223 return Pix (tab[i]); |
|
224 return 0; |
|
225 } |
|
226 |
|
227 template <class C> |
|
228 void |
|
229 CHMap<C>::next (Pix& p) const |
|
230 { |
|
231 CHNode<C> *t = ((CHNode<C> *) p)->tl; |
|
232 if (goodCHptr (t)) |
|
233 p = Pix (t); |
|
234 else |
|
235 { |
|
236 for (unsigned int i = CHptr_to_index (t); i < size; ++i) |
|
237 { |
|
238 if (goodCHptr (tab[i])) |
|
239 { |
|
240 p = Pix (tab[i]); |
|
241 return; |
|
242 } |
|
243 } |
|
244 p = 0; |
|
245 } |
|
246 } |
|
247 |
|
248 template <class C> |
|
249 int |
|
250 CHMap<C>::OK (void) const |
|
251 { |
|
252 int v = tab != 0; |
|
253 int n = 0; |
|
254 |
|
255 for (unsigned int i = 0; i < size; ++i) |
|
256 { |
|
257 for (CHNode<C> *p = tab[i]; goodCHptr (p); p = p->tl) |
|
258 ++n; |
|
259 |
|
260 v &= CHptr_to_index (p) == i + 1; |
|
261 } |
|
262 |
|
263 v &= count == n; |
|
264 |
|
265 if (! v) |
|
266 error ("invariant failure"); |
|
267 |
|
268 return v; |
|
269 } |
|
270 |
|
271 #if defined (__GNUG__) && defined (USE_EXTERNAL_TEMPLATES) |
|
272 #if defined (OCTAVE_SOURCE) |
|
273 |
|
274 //typedef Map<double> map_type_double; |
|
275 //typedef CHNode<double> chnode_type_double; |
|
276 typedef CHMap<double> chmap_type_double; |
|
277 |
|
278 #elif defined (USER_TYPEDEFS) |
|
279 |
|
280 // Users can generate their own .o files with their own types, as many |
|
281 // times as they like. USER_TYPEDEFS should be defined to be the name |
|
282 // of an include file that contains typdefs for the desired types. |
|
283 // |
|
284 // For example, if my-types.h contains typedefs for the Map types |
|
285 // you are interested in, you might compile this file with the command |
|
286 // |
|
287 // g++ -fexternal-templates -DUSE_EXTERNAL_TEMPLATES \ |
|
288 // -DUSER_TYPEDEFS=\"my-types.h\" |
|
289 |
|
290 #include USER_TYPEDEFS |
|
291 |
|
292 #endif |
|
293 #endif |