1 /*****************************************************************************\
4 * Author : Chris Koeritz *
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 \*****************************************************************************/
15 #include "symbol_tree.h"
17 #include <basis/functions.h>
18 #include <structures/symbol_table.h>
19 #include <textual/parser_bits.h>
20 #include <textual/string_manipulation.h>
22 //#define DEBUG_SYMBOL_TREE
23 // uncomment for totally noisy version.
25 #include <loggers/program_wide_logger.h>
27 #define LOG(s) CLASS_EMERGENCY_LOG(program_wide_logger::get(), s)
28 using namespace loggers;
30 using namespace basis;
31 using namespace structures;
32 using namespace textual;
36 class symbol_tree_associations : public symbol_table<symbol_tree *>
39 symbol_tree_associations(int estimated_elements)
40 : symbol_table<symbol_tree *>(estimated_elements) {}
41 virtual ~symbol_tree_associations() {
42 // for (int i = 0; i < symbols(); i++) {
50 symbol_tree::symbol_tree(const astring &node_name, int estimated_elements)
52 _associations(new symbol_tree_associations(estimated_elements)),
53 _name(new astring(node_name))
57 symbol_tree::~symbol_tree()
59 FUNCDEF("destructor");
60 LOG("prior to whacks");
66 int symbol_tree::children() const { return _associations->symbols(); }
68 const astring &symbol_tree::name() const { return *_name; }
70 int symbol_tree::estimated_elements() const { return _associations->estimated_elements(); }
72 void symbol_tree::rehash(int estimated_elements) { _associations->rehash(estimated_elements); }
74 void symbol_tree::hash_appropriately(int estimated_elements)
75 { _associations->hash_appropriately(estimated_elements); }
77 bool symbol_tree::add(symbol_tree *to_add)
79 #ifdef DEBUG_SYMBOL_TREE
81 LOG(astring("adding node for ") + to_add->name());
83 attach(to_add); // add to tree.
84 _associations->add(to_add->name(), to_add); // add to associations.
88 outcome symbol_tree::prune(tree *to_zap_in)
90 #ifdef DEBUG_SYMBOL_TREE
93 symbol_tree *to_zap = dynamic_cast<symbol_tree *>(to_zap_in);
95 throw("error: symbol_tree::prune: wrong type of node in prune");
96 #ifdef DEBUG_SYMBOL_TREE
97 LOG(astring("zapping node for ") + to_zap->name());
99 int found = which(to_zap); // find the node in the tree.
100 if (negative(found)) return common::NOT_FOUND; // not found.
101 #ifdef DEBUG_SYMBOL_TREE
102 int kids = _associations->symbols();
104 _associations->whack(to_zap->name()); // remove from associations.
105 #ifdef DEBUG_SYMBOL_TREE
106 if (kids - 1 != _associations->symbols())
107 throw("error: symbol_tree::prune: failed to crop kid in symtab");
109 tree::prune(to_zap); // remove from tree.
113 symbol_tree *symbol_tree::branch(int index) const
114 { return dynamic_cast<symbol_tree *>(tree::branch(index)); }
116 // implementation snagged from basis/shell_sort.
117 void symbol_tree::sort()
122 for (gap = n / 2; gap > 0; gap /= 2) {
123 for (i = gap; i < n; i++) {
124 for (j = i - gap; j >= 0 && branch(j)->name() > branch(j + gap)->name();
126 // swap the elements that are disordered.
127 temp = branch(j + gap);
128 prune_index(j + gap);
130 temp = branch(j + 1);
132 insert(j + gap, temp);
138 symbol_tree *symbol_tree::find(const astring &to_find, find_methods how,
139 string_comparator_function *comp)
141 #ifdef DEBUG_SYMBOL_TREE
144 if (comp == NULL_POINTER) comp = astring_comparator;
145 #ifdef DEBUG_SYMBOL_TREE
146 LOG(astring("finding node called ") + to_find);
148 // ensure that we compare the current node.
149 if (comp(name(), to_find)) return this;
151 // perform the upward recursion first, since it's pretty simple.
152 if (how == recurse_upward) {
153 symbol_tree *our_parent = dynamic_cast<symbol_tree *>(parent());
154 if (!our_parent) return NULL_POINTER; // done recursing.
155 return our_parent->find(to_find, how, comp);
158 // see if our branches match the search term.
159 symbol_tree **found = _associations->find(to_find, comp);
160 #ifdef DEBUG_SYMBOL_TREE
161 if (!found) LOG(to_find + " was not found.")
162 else LOG(to_find + " was found successfully.");
165 if (how == recurse_downward) {
166 // see if we can't find that name in a sub-node.
167 symbol_tree *answer = NULL_POINTER;
168 for (int i = 0; i < branches(); i++) {
169 // we will try each branch in turn and see if it has a child named
171 symbol_tree *curr = dynamic_cast<symbol_tree *>(branch(i));
172 #ifdef DEBUG_SYMBOL_TREE
173 LOG(astring("recursing to ") + curr->name());
176 answer = curr->find(to_find, how, comp);
186 //hmmm: is this useful elsewhere?
187 astring hier_prefix(int depth, int kids)
189 astring indent = string_manipulation::indentation( (depth - 1) * 2);
190 if (!depth) return "";
191 else if (!kids) return indent + "|--";
192 else return indent + "+--";
195 astring symbol_tree::text_form() const
197 #ifdef DEBUG_SYMBOL_TREE
198 FUNCDEF("text_form");
202 tree::iterator ted = start(prefix);
203 // create our iterator to do a prefix traversal.
205 tree *curr = (tree *)ted.next();
207 //hmmm: this cast assumes that the tree only contains trees. for more
208 // safety, we might want a dynamic cast here also.
210 // we have a good directory to show.
211 symbol_tree *curr_cast = dynamic_cast<symbol_tree *>(curr);
213 // something very bad with that...
214 #ifdef DEBUG_SYMBOL_TREE
215 LOG("logic error: unknown type in symbol tree.");
220 astring name_to_log = curr_cast->name();
221 if (!ted.size()) name_to_log = "";
222 #ifdef DEBUG_SYMBOL_TREE
223 LOG(a_sprintf("depth %d kids %d name %s", ted.size(), curr_cast->branches(),
226 to_return += hier_prefix(curr->depth(), curr_cast->branches());
227 to_return += name_to_log;
228 to_return += parser_bits::platform_eol_to_chars();
230 curr = (tree *)ted.next();