Main Page | Namespace List | Class Hierarchy | Alphabetical List | Class List | Directories | File List | Namespace Members | Class Members | File Members

SearchTreeNode.cc

Go to the documentation of this file.
00001 /*
00002  * SearchTreeNode.cc
00003  *
00004  * Smalltalk like class library for C++
00005  * Node of tree.
00006  *
00007  * Copyright (c) 2004 Milan Cermak, Petr Stepanek
00008  */
00009 /*
00010  * This library is free software; you can redistribute it and/or
00011  * modify it under the terms of the GNU Lesser General Public
00012  * License as published by the Free Software Foundation; either
00013  * version 2.1 of the License, or (at your option) any later version.
00014  *
00015  * This library is distributed in the hope that it will be useful,
00016  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00017  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00018  * Lesser General Public License for more details.
00019  *
00020  * You should have received a copy of the GNU Lesser General Public
00021  * License along with this library; if not, write to the Free Software
00022  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
00023  */
00024 #include <stlib/searchTree/SearchTreeNode.h>
00025 
00026 #include <stlib/Dictionary.h>
00027 #include <stlib/SequenceableCollection.h>
00028 #include <stlib/String.h>
00029 
00030 #include <stlib/KeyNotFoundError.h>
00031 
00032 using namespace Core;
00033 
00034 SearchTreeNode::SearchTreeNode(void)
00035 {
00036     branches = new Dictionary(2);
00037     _value = nil;
00038 }
00039 
00040 /* Class-accessing protocol */
00041 String *SearchTreeNode::className(void) const
00042 {
00043     return new String("SearchTreeNode");
00044 }
00045 
00046 SearchTreeNode *SearchTreeNode::createNewNode(void)
00047 {
00048     return new SearchTreeNode;
00049 }
00050 
00051 /* Accessing protocol */
00052 Object *SearchTreeNode::at(SequenceableCollection *key)
00053 {
00054     return valueAt(key, 0, false);
00055 }
00056 
00057 Object *SearchTreeNode::at(SequenceableCollection *key, Object *ifAbsentValue)
00058 {
00059     Object *obj;
00060 
00061     try {
00062         obj = at(key);
00063     }
00064     catch (KeyNotFoundError *ex) {
00065         obj = ifAbsentValue;
00066     }
00067     return obj;
00068 }
00069 
00070 void SearchTreeNode::put(SequenceableCollection *key, Object *value)
00071 {
00072     putValueAt(key, value, 0);
00073 }
00074 
00075 Object *SearchTreeNode::atLongestPrefixOf(SequenceableCollection *key)
00076 {
00077     return valueAt(key, 0, true);
00078 }
00079 
00080 /* Testing protocol */
00081 bool SearchTreeNode::includesKey(SequenceableCollection *key)
00082 {
00083     return at(key, nil) != nil;
00084 }
00085 
00086 /* Private protocol */
00087 Object *SearchTreeNode::valueAt(SequenceableCollection *key,
00088                                 int depth, bool prefix)
00089 {
00090     if (key->size() > depth) {
00091         if (branches->isEmpty() && prefix)
00092             return _value;
00093         else {
00094             if (_value == nil || !prefix) {
00095                 return privDeeperValueAt(key, depth, prefix);
00096             } else {
00097                 /* I know the value. If there is no better answer, I got one. */
00098                 try {
00099                     return privDeeperValueAt(key, depth, prefix);
00100                 }
00101                 catch (KeyNotFoundError *ex) {
00102                     return _value;
00103                 }
00104             }
00105         }
00106     } else {
00107         if (_value == nil)
00108             (new KeyNotFoundError(__PRETTY_FUNCTION__, key))->raiseFrom(this);
00109         return _value;
00110     }
00111 }
00112 
00113 Object *SearchTreeNode::privDeeperValueAt(SequenceableCollection *key,
00114                                           int depth, bool prefix)
00115 {
00116     SearchTreeNode *next;
00117     next = dynamic_cast<SearchTreeNode *>(branches->at(key->at(depth)));
00118     return next->valueAt(key, depth+1, prefix);
00119 }
00120 
00121 void SearchTreeNode::putValueAt(SequenceableCollection *key,
00122                                 Object *value, int depth)
00123 {
00124     if (key->size() > depth) {
00125         SearchTreeNode *next;
00126         if (branches->includesKey(key->at(depth)))
00127             next = dynamic_cast<SearchTreeNode *>(branches->at(key->at(depth)));
00128         else {
00129             next = createNewNode();
00130             branches->put(key->at(depth), next);
00131         }
00132         next->putValueAt(key, value, depth+1);
00133     } else {
00134         _value = value;
00135     }
00136 }

Generated on Mon Nov 27 09:47:55 2006 for Smalltalk like C++ Class Library by  doxygen 1.4.2