1
|
1 /* |
|
2 |
2847
|
3 Copyright (C) 1996, 1997 John W. Eaton |
1
|
4 |
|
5 This file is part of Octave. |
|
6 |
|
7 Octave is free software; you can redistribute it and/or modify it |
|
8 under the terms of the GNU General Public License as published by the |
|
9 Free Software Foundation; either version 2, or (at your option) any |
|
10 later version. |
|
11 |
|
12 Octave is distributed in the hope that it will be useful, but WITHOUT |
|
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
|
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
|
15 for more details. |
|
16 |
|
17 You should have received a copy of the GNU General Public License |
|
18 along with Octave; see the file COPYING. If not, write to the Free |
1315
|
19 Software Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
1
|
20 |
|
21 */ |
|
22 |
1297
|
23 #if defined (__GNUG__) |
|
24 #pragma implementation |
|
25 #endif |
|
26 |
240
|
27 #ifdef HAVE_CONFIG_H |
1192
|
28 #include <config.h> |
1
|
29 #endif |
|
30 |
3013
|
31 #include <cassert> |
1962
|
32 #include <cctype> |
3013
|
33 #include <climits> |
|
34 |
|
35 #include <iomanip.h> |
1962
|
36 |
2926
|
37 #include "glob-match.h" |
1755
|
38 #include "str-vec.h" |
|
39 |
1352
|
40 #include "error.h" |
2979
|
41 #include "oct-lvalue.h" |
2975
|
42 #include "ov.h" |
3013
|
43 #include "pager.h" |
1755
|
44 #include "symtab.h" |
1
|
45 #include "utils.h" |
1352
|
46 #include "variables.h" |
1
|
47 |
3013
|
48 octave_allocator |
|
49 symbol_record::symbol_def::allocator (sizeof (symbol_record::symbol_def)); |
195
|
50 |
3239
|
51 void |
|
52 symbol_record::symbol_def::dump_symbol_info (void) |
|
53 { |
|
54 octave_stdout << "symbol_def::count: " << count << "\n"; |
|
55 octave_stdout << "def.type_name(): " << definition.type_name () << "\n"; |
3240
|
56 octave_stdout << "def.count(): " << definition.get_count () << "\n"; |
3239
|
57 } |
|
58 |
767
|
59 // Individual records in a symbol table. |
|
60 |
3013
|
61 // XXX FIXME XXX -- there are lots of places below where we should |
|
62 // probably be temporarily ignoring interrupts. |
1
|
63 |
572
|
64 void |
1755
|
65 symbol_record::rename (const string& new_name) |
572
|
66 { |
2791
|
67 if (! read_only_error ("rename")) |
|
68 nm = new_name; |
572
|
69 } |
|
70 |
3013
|
71 void |
|
72 symbol_record::define (const octave_value& v, unsigned int sym_type) |
195
|
73 { |
3013
|
74 if (! (is_variable () && read_only_error ("redefine"))) |
|
75 { |
3258
|
76 if (is_function () || is_constant ()) |
3013
|
77 push_def (new symbol_def ()); |
195
|
78 |
3013
|
79 if (definition->type () == symbol_record::BUILTIN_VARIABLE) |
|
80 sym_type = symbol_record::BUILTIN_VARIABLE; |
1
|
81 |
3013
|
82 definition->define (v, sym_type); |
195
|
83 } |
|
84 } |
|
85 |
|
86 void |
3013
|
87 symbol_record::define_builtin_var (const octave_value& v) |
195
|
88 { |
3013
|
89 define (v, symbol_record::BUILTIN_VARIABLE); |
195
|
90 |
3013
|
91 if (chg_fcn) |
|
92 chg_fcn (); |
1
|
93 } |
|
94 |
3013
|
95 bool |
3258
|
96 symbol_record::define_builtin_const (const octave_value& v) |
1
|
97 { |
3013
|
98 bool retval = false; |
1
|
99 |
3258
|
100 if (! read_only_error ("redefine")) |
195
|
101 { |
3258
|
102 replace_all_defs (new symbol_def (v, symbol_record::BUILTIN_CONSTANT)); |
2893
|
103 |
3013
|
104 retval = true; |
1
|
105 } |
|
106 |
2893
|
107 return retval; |
2390
|
108 } |
|
109 |
3013
|
110 bool |
|
111 symbol_record::define (octave_function *f, unsigned int sym_type) |
1
|
112 { |
3013
|
113 bool retval = false; |
2949
|
114 |
3013
|
115 if (! read_only_error ("redefine")) |
|
116 { |
|
117 octave_value tmp (f); |
|
118 |
|
119 replace_all_defs (new symbol_def (tmp, sym_type)); |
|
120 |
|
121 retval = true; |
|
122 } |
2949
|
123 |
|
124 return retval; |
1
|
125 } |
|
126 |
|
127 void |
195
|
128 symbol_record::clear (void) |
1
|
129 { |
195
|
130 if (linked_to_global) |
1
|
131 { |
3013
|
132 if (--definition->count <= 0) |
|
133 delete definition; |
|
134 |
|
135 definition = new symbol_def (); |
|
136 |
195
|
137 linked_to_global = 0; |
1
|
138 } |
2846
|
139 else if (! tagged_static) |
195
|
140 { |
3013
|
141 remove_top_def (); |
|
142 |
|
143 if (! definition) |
|
144 definition = new symbol_def (); |
195
|
145 } |
1
|
146 } |
|
147 |
|
148 void |
3013
|
149 symbol_record::alias (symbol_record *s, bool /* force */) |
1
|
150 { |
3013
|
151 chg_fcn = s->chg_fcn; |
195
|
152 |
3013
|
153 replace_all_defs (s->definition); |
|
154 |
|
155 definition->count++; |
1
|
156 } |
|
157 |
|
158 void |
|
159 symbol_record::mark_as_formal_parameter (void) |
|
160 { |
3013
|
161 if (is_linked_to_global ()) |
|
162 error ("can't mark global variable `%s' as function parameter", |
|
163 nm.c_str ()); |
|
164 else if (is_static ()) |
|
165 error ("can't mark static variable `%s' as function paraemter", |
|
166 nm.c_str ()); |
|
167 else |
|
168 formal_param = 1; |
1
|
169 } |
|
170 |
|
171 void |
195
|
172 symbol_record::mark_as_linked_to_global (void) |
122
|
173 { |
2975
|
174 if (is_formal_parameter ()) |
|
175 error ("can't make function parameter `%s' global", nm.c_str ()); |
|
176 else if (is_static ()) |
|
177 error ("can't make static variable `%s' global", nm.c_str ()); |
|
178 else |
|
179 linked_to_global = 1; |
122
|
180 } |
|
181 |
2846
|
182 void |
|
183 symbol_record::mark_as_static (void) |
|
184 { |
|
185 if (is_linked_to_global ()) |
2975
|
186 error ("can't make global variable `%s' static", nm.c_str ()); |
2846
|
187 else if (is_formal_parameter ()) |
2975
|
188 error ("can't make formal parameter `%s' static", nm.c_str ()); |
2846
|
189 else |
|
190 tagged_static = 1; |
|
191 } |
|
192 |
2856
|
193 bool |
3013
|
194 symbol_record::hides_fcn (void) const |
2846
|
195 { |
3013
|
196 bool retval = false; |
|
197 |
|
198 if (is_variable () && is_defined ()) |
|
199 { |
|
200 symbol_def *hidden_def = definition->next_elem; |
|
201 |
|
202 if (hidden_def && hidden_def->is_builtin_function ()) |
|
203 retval = true; |
|
204 } |
|
205 |
|
206 return retval; |
|
207 } |
|
208 |
|
209 bool |
|
210 symbol_record::hides_builtin (void) const |
|
211 { |
|
212 bool retval = false; |
|
213 |
|
214 if (is_variable () && is_defined ()) |
|
215 { |
|
216 symbol_def *hidden_def = definition->next_elem; |
|
217 |
|
218 if (hidden_def && hidden_def->is_user_function ()) |
|
219 retval = true; |
|
220 } |
|
221 |
|
222 return retval; |
2846
|
223 } |
|
224 |
2975
|
225 octave_value& |
|
226 symbol_record::variable_value (void) |
2390
|
227 { |
2975
|
228 static octave_value foo; |
2390
|
229 |
2975
|
230 return is_variable () ? def () : foo; |
2390
|
231 } |
|
232 |
3258
|
233 inline void |
|
234 symbol_record::link_to_builtin_variable (void) |
|
235 { |
|
236 symbol_record *tmp_sym = global_sym_tab->lookup (name ()); |
|
237 |
|
238 if (tmp_sym && tmp_sym->is_builtin_variable ()) |
|
239 alias (tmp_sym); |
|
240 } |
|
241 |
|
242 |
2979
|
243 octave_lvalue |
2390
|
244 symbol_record::variable_reference (void) |
|
245 { |
3258
|
246 if (is_function () || is_constant ()) |
2390
|
247 clear (); |
|
248 |
|
249 if (! is_defined ()) |
|
250 { |
|
251 if (! (is_formal_parameter () || is_linked_to_global ())) |
3258
|
252 link_to_builtin_variable (); |
2390
|
253 |
|
254 if (! is_defined ()) |
2975
|
255 { |
|
256 octave_value tmp; |
|
257 define (tmp); |
|
258 } |
2390
|
259 } |
|
260 |
3013
|
261 return octave_lvalue (&(def ()), chg_fcn); |
195
|
262 } |
|
263 |
220
|
264 void |
|
265 symbol_record::push_context (void) |
|
266 { |
2846
|
267 if (! is_static ()) |
|
268 { |
|
269 context.push (definition); |
3013
|
270 |
|
271 definition = new symbol_def (); |
395
|
272 |
2893
|
273 global_link_context.push (static_cast<unsigned int> (linked_to_global)); |
3013
|
274 |
2846
|
275 linked_to_global = 0; |
|
276 } |
220
|
277 } |
|
278 |
|
279 void |
|
280 symbol_record::pop_context (void) |
|
281 { |
1653
|
282 // It is possible for context to be empty if new symbols have been |
|
283 // inserted in the symbol table during recursive calls. This can |
|
284 // happen as a result of calls to eval() and feval(). |
220
|
285 |
1653
|
286 if (! context.empty ()) |
220
|
287 { |
3013
|
288 replace_all_defs (context.pop ()); |
1653
|
289 |
|
290 linked_to_global = global_link_context.pop (); |
220
|
291 } |
|
292 } |
|
293 |
3013
|
294 void |
|
295 symbol_record::print_symbol_info_line (ostream& os) |
|
296 { |
|
297 os << (is_read_only () ? " r-" : " rw") |
|
298 << (is_eternal () ? "-" : "d") |
|
299 #if 0 |
|
300 << (hides_fcn () ? "f" : (hides_builtin () ? "F" : "-")) |
|
301 #endif |
|
302 << " " |
|
303 << setiosflags (ios::left) << setw (24) << type_name () . c_str (); |
|
304 |
|
305 os << resetiosflags (ios::left); |
|
306 |
|
307 int nr = rows (); |
|
308 int nc = columns (); |
|
309 |
|
310 if (nr < 0) |
|
311 os << " -"; |
|
312 else |
|
313 os << setiosflags (ios::right) << setw (7) << nr; |
|
314 |
|
315 if (nc < 0) |
|
316 os << " -"; |
|
317 else |
|
318 os << setiosflags (ios::right) << setw (7) << nc; |
|
319 |
|
320 os << resetiosflags (ios::right); |
|
321 |
|
322 os << " " << name () << "\n"; |
|
323 } |
|
324 |
3239
|
325 void |
|
326 symbol_record::dump_symbol_info (void) |
|
327 { |
|
328 if (definition) |
|
329 definition->dump_symbol_info (); |
|
330 else |
|
331 octave_stdout << "symbol " << name () << " is undefined\n"; |
|
332 } |
|
333 |
3013
|
334 bool |
2791
|
335 symbol_record::read_only_error (const char *action) |
195
|
336 { |
|
337 if (is_read_only ()) |
|
338 { |
3258
|
339 if (is_variable () || is_constant ()) |
3013
|
340 ::error ("can't %s read-only constant `%s'", action, nm.c_str ()); |
195
|
341 else if (is_function ()) |
3013
|
342 ::error ("can't %s read-only function `%s'", action, nm.c_str ()); |
1045
|
343 else |
3013
|
344 ::error ("can't %s read-only symbol `%s'", action, nm.c_str ()); |
195
|
345 |
3013
|
346 return true; |
195
|
347 } |
|
348 else |
3013
|
349 return false; |
195
|
350 } |
|
351 |
|
352 void |
|
353 symbol_record::push_def (symbol_def *sd) |
|
354 { |
530
|
355 if (! sd) |
195
|
356 return; |
|
357 |
3013
|
358 assert (definition == 0 || definition->next_elem == 0); |
|
359 |
195
|
360 sd->next_elem = definition; |
3013
|
361 |
195
|
362 definition = sd; |
|
363 } |
|
364 |
3013
|
365 void |
|
366 symbol_record::remove_top_def (void) |
195
|
367 { |
|
368 symbol_def *top = definition; |
|
369 |
3013
|
370 definition = definition->next_elem; |
2404
|
371 |
3013
|
372 if (--top->count <= 0) |
|
373 delete top; |
195
|
374 } |
|
375 |
3013
|
376 void |
|
377 symbol_record::replace_all_defs (symbol_def *sd) |
195
|
378 { |
3013
|
379 while (definition) |
|
380 remove_top_def (); |
2404
|
381 |
3013
|
382 push_def (sd); |
195
|
383 } |
|
384 |
767
|
385 // A symbol table. |
1
|
386 |
|
387 symbol_record * |
2856
|
388 symbol_table::lookup (const string& nm, bool insert, bool warn) |
1
|
389 { |
3013
|
390 unsigned int index = hash (nm); |
1
|
391 |
|
392 symbol_record *ptr = table[index].next (); |
|
393 |
530
|
394 while (ptr) |
1
|
395 { |
1755
|
396 if (ptr->name () == nm) |
1
|
397 return ptr; |
3013
|
398 |
1
|
399 ptr = ptr->next (); |
|
400 } |
|
401 |
|
402 if (insert) |
|
403 { |
3013
|
404 symbol_record *sr = new symbol_record (nm, table[index].next ()); |
|
405 |
|
406 table[index].chain (sr); |
|
407 |
|
408 return sr; |
1
|
409 } |
|
410 else if (warn) |
1755
|
411 warning ("lookup: symbol`%s' not found", nm.c_str ()); |
1
|
412 |
530
|
413 return 0; |
1
|
414 } |
|
415 |
|
416 void |
1755
|
417 symbol_table::rename (const string& old_name, const string& new_name) |
572
|
418 { |
3013
|
419 unsigned int index = hash (old_name); |
572
|
420 |
|
421 symbol_record *prev = &table[index]; |
|
422 symbol_record *ptr = prev->next (); |
|
423 |
|
424 while (ptr) |
|
425 { |
1755
|
426 if (ptr->name () == old_name) |
572
|
427 { |
|
428 ptr->rename (new_name); |
|
429 |
2791
|
430 if (! error_state) |
|
431 { |
|
432 prev->chain (ptr->next ()); |
|
433 |
3013
|
434 index = hash (new_name); |
3125
|
435 ptr->chain (table[index].next ()); |
2791
|
436 table[index].chain (ptr); |
|
437 |
|
438 return; |
|
439 } |
|
440 |
|
441 break; |
572
|
442 } |
|
443 |
|
444 prev = ptr; |
|
445 ptr = ptr->next (); |
|
446 } |
|
447 |
1755
|
448 error ("unable to rename `%s' to `%s'", old_name.c_str (), |
|
449 new_name.c_str ()); |
572
|
450 } |
|
451 |
|
452 void |
2856
|
453 symbol_table::clear (bool clear_user_functions) |
1
|
454 { |
3013
|
455 for (unsigned int i = 0; i < table_size; i++) |
1
|
456 { |
169
|
457 symbol_record *ptr = table[i].next (); |
1
|
458 |
530
|
459 while (ptr) |
169
|
460 { |
195
|
461 if (ptr->is_user_variable () |
|
462 || (clear_user_functions && ptr->is_user_function ())) |
|
463 { |
|
464 ptr->clear (); |
|
465 } |
|
466 |
169
|
467 ptr = ptr->next (); |
1
|
468 } |
|
469 } |
|
470 } |
|
471 |
3013
|
472 bool |
2856
|
473 symbol_table::clear (const string& nm, bool clear_user_functions) |
1
|
474 { |
3013
|
475 unsigned int index = hash (nm); |
1
|
476 |
195
|
477 symbol_record *ptr = table[index].next (); |
1
|
478 |
530
|
479 while (ptr) |
1
|
480 { |
1755
|
481 if (ptr->name () == nm |
195
|
482 && (ptr->is_user_variable () |
|
483 || (clear_user_functions && ptr->is_user_function ()))) |
1
|
484 { |
195
|
485 ptr->clear (); |
3013
|
486 return true; |
1
|
487 } |
195
|
488 ptr = ptr->next (); |
1
|
489 } |
|
490 |
3013
|
491 return false; |
1
|
492 } |
|
493 |
|
494 int |
164
|
495 symbol_table::size (void) const |
1
|
496 { |
|
497 int count = 0; |
3013
|
498 |
|
499 for (unsigned int i = 0; i < table_size; i++) |
1
|
500 { |
|
501 symbol_record *ptr = table[i].next (); |
3013
|
502 |
530
|
503 while (ptr) |
1
|
504 { |
|
505 count++; |
|
506 ptr = ptr->next (); |
|
507 } |
|
508 } |
3013
|
509 |
1
|
510 return count; |
|
511 } |
|
512 |
3013
|
513 static bool |
|
514 matches_patterns (const string& name, const string_vector& pats) |
1
|
515 { |
3013
|
516 int npats = pats.length (); |
1
|
517 |
3013
|
518 if (npats == 0) |
|
519 return true; |
|
520 |
|
521 glob_match pattern (pats); |
|
522 |
|
523 return pattern.match (name); |
1
|
524 } |
|
525 |
3013
|
526 Array<symbol_record *> |
|
527 symbol_table::symbol_list (int& count, const string_vector& pats, |
|
528 unsigned int type, unsigned int scope) const |
1
|
529 { |
115
|
530 count = 0; |
3013
|
531 |
1
|
532 int n = size (); |
3013
|
533 |
1
|
534 if (n == 0) |
530
|
535 return 0; |
1
|
536 |
3013
|
537 Array<symbol_record *> symbols (n); |
|
538 |
|
539 for (unsigned int i = 0; i < table_size; i++) |
1
|
540 { |
|
541 symbol_record *ptr = table[i].next (); |
3013
|
542 |
530
|
543 while (ptr) |
1
|
544 { |
|
545 assert (count < n); |
867
|
546 |
2893
|
547 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
867
|
548 |
2893
|
549 unsigned int my_type = ptr->type (); |
867
|
550 |
1755
|
551 string my_name = ptr->name (); |
867
|
552 |
|
553 if ((type & my_type) && (scope & my_scope) |
3013
|
554 && matches_patterns (my_name, pats)) |
|
555 symbols(count++) = ptr; |
605
|
556 |
1
|
557 ptr = ptr->next (); |
|
558 } |
|
559 } |
1755
|
560 |
|
561 symbols.resize (count); |
1
|
562 |
3013
|
563 return symbols; |
|
564 } |
|
565 |
|
566 string_vector |
|
567 symbol_table::name_list (int& count, const string_vector& pats, bool sort, |
|
568 unsigned int type, unsigned int scope) const |
|
569 { |
|
570 Array<symbol_record *> symbols |
|
571 = symbol_list (count, pats, type, scope); |
|
572 |
|
573 string_vector names; |
|
574 |
|
575 int n = symbols.length (); |
|
576 |
|
577 if (n > 0) |
|
578 { |
|
579 names.resize (n); |
|
580 |
|
581 for (int i = 0; i < n; i++) |
|
582 names[i] = symbols(i)->name (); |
|
583 } |
|
584 |
|
585 if (sort) |
|
586 names.qsort (); |
|
587 |
|
588 return names; |
|
589 } |
|
590 |
|
591 static int |
3145
|
592 maybe_list_cmp_fcn (const void *a_arg, const void *b_arg) |
3013
|
593 { |
3145
|
594 const symbol_record *a = *(X_CAST (const symbol_record **, a_arg)); |
|
595 const symbol_record *b = *(X_CAST (const symbol_record **, b_arg)); |
3013
|
596 |
3145
|
597 string a_nm = a->name (); |
|
598 string b_nm = b->name (); |
|
599 |
|
600 return a_nm.compare (b_nm); |
3013
|
601 } |
1
|
602 |
3013
|
603 int |
|
604 symbol_table::maybe_list (const char *header, const string_vector& argv, |
|
605 ostream& os, bool show_verbose, |
|
606 unsigned type, unsigned scope) |
|
607 { |
|
608 int count; |
|
609 |
|
610 int status = 0; |
|
611 |
|
612 if (show_verbose) |
|
613 { |
|
614 Array<symbol_record *> symbols = symbol_list (count, argv, type, scope); |
|
615 |
|
616 int len = symbols.length (); |
|
617 |
|
618 if (len > 0 && count > 0) |
|
619 { |
|
620 os << "\n" << header << "\n\n" |
|
621 << "prot type rows cols name\n" |
|
622 << "==== ==== ==== ==== ====\n"; |
|
623 |
|
624 symbols.qsort (maybe_list_cmp_fcn); |
|
625 |
|
626 for (int i = 0; i < len; i++) |
|
627 symbols(i)->print_symbol_info_line (os); |
|
628 |
|
629 status = 1; |
|
630 } |
|
631 } |
|
632 else |
|
633 { |
|
634 string_vector symbols = name_list (count, argv, 1, type, scope); |
|
635 |
|
636 if (symbols.length () > 0 && count > 0) |
|
637 { |
|
638 os << "\n" << header << "\n\n"; |
|
639 |
|
640 symbols.list_in_columns (os); |
|
641 |
|
642 status = 1; |
|
643 } |
|
644 } |
|
645 |
|
646 return status; |
1
|
647 } |
|
648 |
605
|
649 symbol_record ** |
2893
|
650 symbol_table::glob (int& count, const string& pat, unsigned int type, |
|
651 unsigned int scope) const |
605
|
652 { |
|
653 count = 0; |
|
654 int n = size (); |
|
655 if (n == 0) |
|
656 return 0; |
|
657 |
|
658 symbol_record **symbols = new symbol_record * [n+1]; |
3013
|
659 |
|
660 for (unsigned int i = 0; i < table_size; i++) |
605
|
661 { |
|
662 symbol_record *ptr = table[i].next (); |
3013
|
663 |
605
|
664 while (ptr) |
|
665 { |
|
666 assert (count < n); |
|
667 |
2893
|
668 unsigned int my_scope = ptr->is_linked_to_global () + 1; // Tricky... |
605
|
669 |
2893
|
670 unsigned int my_type = ptr->type (); |
605
|
671 |
1792
|
672 glob_match pattern (pat); |
1755
|
673 |
605
|
674 if ((type & my_type) && (scope & my_scope) |
1792
|
675 && pattern.match (ptr->name ())) |
605
|
676 { |
|
677 symbols[count++] = ptr; |
|
678 } |
|
679 |
|
680 ptr = ptr->next (); |
|
681 } |
|
682 } |
|
683 symbols[count] = 0; |
|
684 |
|
685 return symbols; |
|
686 } |
|
687 |
220
|
688 void |
|
689 symbol_table::push_context (void) |
|
690 { |
3013
|
691 for (unsigned int i = 0; i < table_size; i++) |
220
|
692 { |
|
693 symbol_record *ptr = table[i].next (); |
|
694 |
530
|
695 while (ptr) |
220
|
696 { |
|
697 ptr->push_context (); |
|
698 ptr = ptr->next (); |
|
699 } |
|
700 } |
|
701 } |
|
702 |
|
703 void |
|
704 symbol_table::pop_context (void) |
|
705 { |
3013
|
706 for (unsigned int i = 0; i < table_size; i++) |
220
|
707 { |
|
708 symbol_record *ptr = table[i].next (); |
|
709 |
530
|
710 while (ptr) |
220
|
711 { |
|
712 ptr->pop_context (); |
|
713 ptr = ptr->next (); |
|
714 } |
|
715 } |
|
716 } |
|
717 |
3013
|
718 void |
|
719 symbol_table::print_stats (void) |
|
720 { |
|
721 int count = 0; |
|
722 int empty_chains = 0; |
|
723 int max_chain_length = 0; |
|
724 int min_chain_length = INT_MAX; |
|
725 |
|
726 for (unsigned int i = 0; i < table_size; i++) |
|
727 { |
|
728 int num_this_chain = 0; |
|
729 |
|
730 symbol_record *ptr = table[i].next (); |
|
731 |
|
732 if (ptr) |
|
733 octave_stdout << "chain number " << i << ":\n"; |
|
734 else |
|
735 { |
|
736 empty_chains++; |
|
737 min_chain_length = 0; |
|
738 } |
|
739 |
|
740 while (ptr) |
|
741 { |
|
742 num_this_chain++; |
|
743 |
|
744 octave_stdout << " " << ptr->name () << "\n"; |
|
745 |
|
746 ptr = ptr->next (); |
|
747 } |
|
748 |
|
749 count += num_this_chain; |
|
750 |
|
751 if (num_this_chain > max_chain_length) |
|
752 max_chain_length = num_this_chain; |
|
753 |
|
754 if (num_this_chain < min_chain_length) |
|
755 min_chain_length = num_this_chain; |
|
756 |
|
757 if (num_this_chain > 0) |
|
758 octave_stdout << "\n"; |
|
759 } |
|
760 |
|
761 octave_stdout << "max chain length: " << max_chain_length << "\n"; |
|
762 octave_stdout << "min chain length: " << min_chain_length << "\n"; |
|
763 octave_stdout << "empty chains: " << empty_chains << "\n"; |
|
764 octave_stdout << "total chains: " << table_size << "\n"; |
|
765 octave_stdout << "total symbols: " << count << "\n"; |
|
766 } |
|
767 |
1
|
768 // Chris Torek's fave hash function. |
|
769 |
|
770 unsigned int |
1755
|
771 symbol_table::hash (const string& str) |
1
|
772 { |
2893
|
773 unsigned int h = 0; |
3013
|
774 |
2893
|
775 for (unsigned int i = 0; i < str.length (); i++) |
1755
|
776 h = h * 33 + str[i]; |
1
|
777 |
3013
|
778 return h & (table_size - 1); |
2790
|
779 } |
|
780 |
1
|
781 /* |
|
782 ;;; Local Variables: *** |
|
783 ;;; mode: C++ *** |
|
784 ;;; End: *** |
|
785 */ |