00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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
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
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
00081 bool SearchTreeNode::includesKey(SequenceableCollection *key)
00082 {
00083 return at(key, nil) != nil;
00084 }
00085
00086
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
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 }