tracking down issues in sym tree destructor still
[feisty_meow.git] / nucleus / library / nodes / symbol_tree.cpp
1 /*****************************************************************************\
2 *                                                                             *
3 *  Name   : symbol_tree                                                       *
4 *  Author : Chris Koeritz                                                     *
5 *                                                                             *
6 *******************************************************************************
7 * Copyright (c) 1992-$now By Author.  This program is free software; you can  *
8 * redistribute it and/or modify it under the terms of the GNU General Public  *
9 * License as published by the Free Software Foundation; either version 2 of   *
10 * the License or (at your option) any later version.  This is online at:      *
11 *     http://www.fsf.org/copyleft/gpl.html                                    *
12 * Please send any updates to: fred@gruntose.com                               *
13 \*****************************************************************************/
14
15 #include "symbol_tree.h"
16
17 #include <basis/functions.h>
18 #include <structures/symbol_table.h>
19 #include <textual/parser_bits.h>
20 #include <textual/string_manipulation.h>
21
22 //#define DEBUG_SYMBOL_TREE
23   // uncomment for totally noisy version.
24
25 #include <loggers/program_wide_logger.h>
26 #undef LOG
27 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
28 using namespace loggers;
29
30 using namespace basis;
31 using namespace structures;
32 using namespace textual;
33
34 namespace nodes {
35
36 //hmmm: used for... explain please.
37 class symbol_tree_associations : public symbol_table<symbol_tree *>
38 {
39 public:
40   symbol_tree_associations(int estimated_elements)
41       :  symbol_table<symbol_tree *>(estimated_elements) {}
42   virtual ~symbol_tree_associations() {
43 //hmmm: why was this here?  was it ever needed?
44 //    for (int i = 0; i < symbols(); i++) {
45 //      WHACK(use(i));
46 //    }
47   }
48 };
49
50 //////////////
51
52 symbol_tree::symbol_tree(const astring &node_name, int estimated_elements)
53 : tree(),
54   _associations(new symbol_tree_associations(estimated_elements)),
55   _name(new astring(node_name))
56 {
57 }
58
59 symbol_tree::~symbol_tree()
60 {
61   FUNCDEF("destructor");
62 LOG("prior to whacks");
63   WHACK(_associations);
64   WHACK(_name);
65 LOG("after whacks");
66 }
67
68 int symbol_tree::children() const { return _associations->symbols(); }
69
70 const astring &symbol_tree::name() const { return *_name; }
71
72 int symbol_tree::estimated_elements() const { return _associations->estimated_elements(); }
73
74 void symbol_tree::rehash(int estimated_elements) { _associations->rehash(estimated_elements); }
75
76 void symbol_tree::hash_appropriately(int estimated_elements)
77 { _associations->hash_appropriately(estimated_elements); }
78
79 bool symbol_tree::add(symbol_tree *to_add)
80 {
81 #ifdef DEBUG_SYMBOL_TREE
82   FUNCDEF("add");
83   LOG(astring("adding node for ") + to_add->name());
84 #endif
85   attach(to_add);  // add to tree.
86   _associations->add(to_add->name(), to_add);  // add to associations.
87   return true;
88 }
89
90 outcome symbol_tree::prune(tree *to_zap_in)
91 {
92 #ifdef DEBUG_SYMBOL_TREE
93   FUNCDEF("prune");
94 #endif
95   symbol_tree *to_zap = dynamic_cast<symbol_tree *>(to_zap_in);
96   if (!to_zap) {
97 #ifdef DEBUG_SYMBOL_TREE
98     LOG("about to barf due to null symtree after dynamic cast.");
99 #endif
100     throw("error: symbol_tree::prune: wrong type of node in prune");
101   }
102 #ifdef DEBUG_SYMBOL_TREE
103   LOG(astring("zapping node for ") + to_zap->name());
104 #endif
105   int found = which(to_zap);  // find the node in the tree.
106   if (negative(found)) return common::NOT_FOUND;  // not found.
107 #ifdef DEBUG_SYMBOL_TREE
108   int kids = _associations->symbols();
109 #endif
110   _associations->whack(to_zap->name());  // remove from associations.
111 #ifdef DEBUG_SYMBOL_TREE
112   if (kids - 1 != _associations->symbols())
113     throw("error: symbol_tree::prune: failed to crop kid in symtab");
114 #endif
115   tree::prune(to_zap);  // remove from tree.
116   return common::OKAY;
117 }
118
119 symbol_tree *symbol_tree::branch(int index) const
120 { return dynamic_cast<symbol_tree *>(tree::branch(index)); }
121
122 // implementation snagged from basis/shell_sort.
123 void symbol_tree::sort()
124 {
125   int n = branches();
126   symbol_tree *temp;
127   int gap, i, j;
128   for (gap = n / 2; gap > 0; gap /= 2) {
129     for (i = gap; i < n; i++) {
130       for (j = i - gap; j >= 0 && branch(j)->name() > branch(j + gap)->name();
131           j = j - gap) {
132         // swap the elements that are disordered.
133         temp = branch(j + gap);
134         prune_index(j + gap);
135         insert(j, temp);
136         temp = branch(j + 1);
137         prune_index(j + 1);
138         insert(j + gap, temp);
139       }
140     }
141   }
142 }
143
144 symbol_tree *symbol_tree::find(const astring &to_find, find_methods how,
145     string_comparator_function *comp)
146 {
147 #ifdef DEBUG_SYMBOL_TREE
148   FUNCDEF("find");
149 #endif
150   if (comp == NULL_POINTER) comp = astring_comparator;
151 #ifdef DEBUG_SYMBOL_TREE
152   LOG(astring("finding node called ") + to_find);
153 #endif
154   // ensure that we compare the current node.
155   if (comp(name(), to_find)) return this;
156
157   // perform the upward recursion first, since it's pretty simple.
158   if (how == recurse_upward) {
159     symbol_tree *our_parent = dynamic_cast<symbol_tree *>(parent());
160     if (!our_parent) return NULL_POINTER;  // done recursing.
161     return our_parent->find(to_find, how, comp);
162   }
163
164   // see if our branches match the search term.
165   symbol_tree **found = _associations->find(to_find, comp);
166 #ifdef DEBUG_SYMBOL_TREE
167   if (!found) LOG(to_find + " was not found.")
168   else LOG(to_find + " was found successfully.");
169 #endif
170   if (!found) {
171     if (how == recurse_downward) {
172       // see if we can't find that name in a sub-node.
173       symbol_tree *answer = NULL_POINTER;
174       for (int i = 0; i < branches(); i++) {
175         // we will try each branch in turn and see if it has a child named
176         // appropriately.
177         symbol_tree *curr = dynamic_cast<symbol_tree *>(branch(i));
178 #ifdef DEBUG_SYMBOL_TREE
179         LOG(astring("recursing to ") + curr->name());
180 #endif
181         if (curr)
182           answer = curr->find(to_find, how, comp);
183         if (answer)
184           return answer;
185       }
186     }
187     return NULL_POINTER;
188   }
189   return *found;
190 }
191
192 //hmmm: is this useful elsewhere?
193 astring hier_prefix(int depth, int kids)
194 {
195   astring indent = string_manipulation::indentation( (depth - 1) * 2);
196   if (!depth) return "";
197   else if (!kids) return indent + "|--";
198   else return indent + "+--";
199 }
200
201 astring symbol_tree::text_form() const
202 {
203 #ifdef DEBUG_SYMBOL_TREE
204   FUNCDEF("text_form");
205 #endif
206   astring to_return;
207
208   tree::iterator ted = start(prefix);
209     // create our iterator to do a prefix traversal.
210
211   tree *curr = (tree *)ted.next();
212
213 //hmmm: this cast assumes that the tree only contains trees.  for more
214 //      safety, we might want a dynamic cast here also.
215   while (curr) {
216     // we have a good directory to show.
217     symbol_tree *curr_cast = dynamic_cast<symbol_tree *>(curr);
218     if (!curr_cast) {
219       // something very bad with that...
220 #ifdef DEBUG_SYMBOL_TREE
221       LOG("logic error: unknown type in symbol tree.");
222 #endif
223       ted.next();
224       continue;
225     }
226     astring name_to_log = curr_cast->name();
227     if (!ted.size()) name_to_log = "";
228 #ifdef DEBUG_SYMBOL_TREE
229     LOG(a_sprintf("depth %d kids %d name %s", ted.size(), curr_cast->branches(),
230         name_to_log.s()));
231 #endif
232     to_return += hier_prefix(curr->depth(), curr_cast->branches());
233     to_return += name_to_log;
234     to_return += parser_bits::platform_eol_to_chars();
235
236     curr = (tree *)ted.next();
237   }
238
239   return to_return;
240 }
241
242 } // namespace.
243