839
|
1 // Map.cc -*- C++ -*- |
728
|
2 /* |
|
3 |
1884
|
4 Copyright (C) 1996 John W. Eaton |
728
|
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 |
1315
|
20 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
728
|
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 |
1192
|
38 #include <config.h> |
728
|
39 #endif |
|
40 |
|
41 #include <iostream.h> |
|
42 |
|
43 #include "Map.h" |
|
44 |
|
45 static unsigned int |
1755
|
46 hash (const string& str) |
728
|
47 { |
|
48 unsigned h = 0; |
1755
|
49 for (unsigned i = 0; i < str.length (); i++) |
|
50 h = h * 33 + str[i]; |
728
|
51 return h; |
|
52 } |
|
53 |
|
54 template <class C> |
|
55 Pix |
1755
|
56 Map<C>::seek (const string& item) const |
728
|
57 { |
1321
|
58 Pix i = 0; |
|
59 |
1755
|
60 for (i = first (); i != 0 && key (i) != item; next (i)) |
728
|
61 ; // Skip items until match found. |
|
62 |
|
63 return i; |
|
64 } |
|
65 |
|
66 template <class C> |
|
67 int |
|
68 Map<C>::owns (Pix idx) const |
|
69 { |
|
70 if (idx == 0) |
|
71 return 0; |
|
72 |
|
73 for (Pix i = first (); i != 0; next (i)) |
|
74 if (i == idx) |
|
75 return 1; |
|
76 |
|
77 return 0; |
|
78 } |
|
79 |
|
80 template <class C> |
|
81 void |
|
82 Map<C>::clear (void) |
|
83 { |
|
84 Pix i = first (); |
|
85 while (i != 0) |
|
86 { |
|
87 del (key (i)); |
|
88 i = first (); |
|
89 } |
|
90 } |
|
91 |
|
92 template <class C> |
|
93 int |
1755
|
94 Map<C>::contains (const string& item) const |
728
|
95 { |
|
96 return seek (item) != 0; |
|
97 } |
|
98 |
|
99 template <class C> |
|
100 void |
1755
|
101 Map<C>::error (const string& msg) const |
728
|
102 { |
|
103 cerr << "Map: " << msg << "\n"; |
|
104 } |
|
105 |
|
106 // CHMap class. |
|
107 |
|
108 // The nodes are linked together serially via a version of a trick |
|
109 // used in some vtables: odd pointers are actually links to the next |
|
110 // table entry. Not terrible, but not wonderful either. |
|
111 |
|
112 template <class C> |
755
|
113 static int |
728
|
114 goodCHptr (CHNode<C> *t) |
|
115 { |
|
116 return ((((unsigned) t) & 1) == 0); |
|
117 } |
|
118 |
|
119 // This sucks, but avoids g++ 2.6.0 `type unification failed' errors. |
|
120 |
755
|
121 static void * |
728
|
122 index_to_CHptr (int i) |
|
123 { |
|
124 return (void *) ((i << 1) + 1); |
|
125 } |
|
126 |
|
127 template <class C> |
1364
|
128 static unsigned int |
728
|
129 CHptr_to_index (CHNode<C> *t) |
|
130 { |
|
131 return ((unsigned) t) >> 1; |
|
132 } |
|
133 |
|
134 template <class C> |
|
135 CHMap<C>::CHMap (const C& dflt, unsigned int sz) : Map<C> (dflt) |
|
136 { |
|
137 tab = new CHNode<C>* [size = sz]; |
|
138 for (unsigned int i = 0; i < size; ++i) |
|
139 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
140 count = 0; |
|
141 } |
|
142 |
|
143 template <class C> |
|
144 CHMap<C>::CHMap (const CHMap& a) : Map<C> (a.def) |
|
145 { |
|
146 tab = new CHNode<C>* [size = a.size]; |
|
147 for (unsigned int i = 0; i < size; ++i) |
|
148 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
149 count = 0; |
|
150 for (Pix p = a.first (); p; a.next (p)) |
|
151 (*this) [a.key (p)] = a.contents (p); |
|
152 } |
|
153 |
|
154 template <class C> |
|
155 Pix |
1755
|
156 CHMap<C>::seek (const string& key) const |
728
|
157 { |
|
158 unsigned int h = hash (key) % size; |
|
159 |
|
160 for (CHNode<C> *t = tab[h]; goodCHptr (t); t = t->tl) |
1755
|
161 if (key == t->hd) |
728
|
162 return Pix (t); |
|
163 |
|
164 return 0; |
|
165 } |
|
166 |
|
167 template <class C> |
|
168 C& |
1755
|
169 CHMap<C>::operator [] (const string& item) |
728
|
170 { |
|
171 unsigned int h = hash (item) % size; |
|
172 |
1321
|
173 CHNode<C> *t = 0; |
|
174 for (t = tab[h]; goodCHptr (t); t = t->tl) |
1755
|
175 if (item == t->hd) |
728
|
176 return t->cont; |
|
177 |
|
178 t = new CHNode<C> (item, def, tab[h]); |
|
179 tab[h] = t; |
|
180 ++count; |
|
181 return t->cont; |
|
182 } |
|
183 |
|
184 template <class C> |
|
185 void |
1755
|
186 CHMap<C>::del (const string& key) |
728
|
187 { |
|
188 unsigned int h = hash (key) % size; |
|
189 |
|
190 CHNode<C> *t = tab[h]; |
|
191 CHNode<C> *trail = t; |
|
192 while (goodCHptr (t)) |
|
193 { |
1755
|
194 if (key == t->hd) |
728
|
195 { |
|
196 if (trail == t) |
|
197 tab[h] = t->tl; |
|
198 else |
|
199 trail->tl = t->tl; |
|
200 delete t; |
|
201 --count; |
|
202 return; |
|
203 } |
|
204 trail = t; |
|
205 t = t->tl; |
|
206 } |
|
207 } |
|
208 |
|
209 template <class C> |
|
210 void |
|
211 CHMap<C>::clear (void) |
|
212 { |
|
213 for (unsigned int i = 0; i < size; ++i) |
|
214 { |
|
215 CHNode<C> *p = tab[i]; |
|
216 tab[i] = (CHNode<C> *) index_to_CHptr (i+1); |
|
217 while (goodCHptr (p)) |
|
218 { |
|
219 CHNode<C> *nxt = p->tl; |
|
220 delete p; |
|
221 p = nxt; |
|
222 } |
|
223 } |
|
224 count = 0; |
|
225 } |
|
226 |
|
227 template <class C> |
|
228 Pix |
|
229 CHMap<C>::first (void) const |
|
230 { |
|
231 for (unsigned int i = 0; i < size; ++i) |
|
232 if (goodCHptr (tab[i])) |
|
233 return Pix (tab[i]); |
|
234 return 0; |
|
235 } |
|
236 |
|
237 template <class C> |
|
238 void |
|
239 CHMap<C>::next (Pix& p) const |
|
240 { |
|
241 CHNode<C> *t = ((CHNode<C> *) p)->tl; |
|
242 if (goodCHptr (t)) |
|
243 p = Pix (t); |
|
244 else |
|
245 { |
|
246 for (unsigned int i = CHptr_to_index (t); i < size; ++i) |
|
247 { |
|
248 if (goodCHptr (tab[i])) |
|
249 { |
|
250 p = Pix (tab[i]); |
|
251 return; |
|
252 } |
|
253 } |
|
254 p = 0; |
|
255 } |
|
256 } |
|
257 |
|
258 template <class C> |
|
259 int |
|
260 CHMap<C>::OK (void) const |
|
261 { |
|
262 int v = tab != 0; |
|
263 int n = 0; |
|
264 |
|
265 for (unsigned int i = 0; i < size; ++i) |
|
266 { |
1321
|
267 CHNode<C> *p = 0; |
|
268 |
|
269 for (p = tab[i]; goodCHptr (p); p = p->tl) |
728
|
270 ++n; |
|
271 |
|
272 v &= CHptr_to_index (p) == i + 1; |
|
273 } |
|
274 |
|
275 v &= count == n; |
|
276 |
|
277 if (! v) |
|
278 error ("invariant failure"); |
|
279 |
|
280 return v; |
|
281 } |
|
282 |
755
|
283 /* |
|
284 ;;; Local Variables: *** |
|
285 ;;; mode: C++ *** |
|
286 ;;; page-delimiter: "^/\\*" *** |
|
287 ;;; End: *** |
|
288 */ |