Grafalgo
Library of useful data structures and algorithms
/Users/jst/src/grafalgo/cpp/dataStructures/searchTrees/TreeMap.cpp
Go to the documentation of this file.
00001 
00009 #include "TreeMap.h"
00010 
00011 namespace grafalgo {
00012 
00016 TreeMap::TreeMap(int size) : Adt(size) {
00017         makeSpace(size);
00018 }
00019 
00021 TreeMap::~TreeMap() { freeSpace(); }
00022 
00026 void TreeMap::makeSpace(int size) {
00027         try {
00028                 st = new BalBstSet(size);
00029                 values = new uint32_t[size+1];
00030                 nodes = new SetPair(size);
00031         } catch (std::bad_alloc e) {
00032                 stringstream ss;
00033                 ss << "makeSpace:: insufficient space for "
00034                    << size << "index values";
00035                 string s = ss.str();
00036                 throw OutOfSpaceException(s);
00037         }
00038         root = 0;
00039         nn = size; clear();
00040 }
00041 
00043 void TreeMap::freeSpace() {
00044         delete st; delete [] values; delete nodes;
00045 }
00046 
00048 void TreeMap::clear() {
00049         while (root != 0) { remove(st->key(root)); }
00050 }
00051 
00055 void TreeMap::resize(int size) {
00056         freeSpace();
00057         try { makeSpace(size); } catch(OutOfSpaceException e) {
00058                 string s; s = "TreeMap::resize::" + e.toString(s);
00059                 throw OutOfSpaceException(s);
00060         }
00061 }
00062 
00067 void TreeMap::expand(int size) {
00068         if (size <= n()) return;
00069         TreeMap old(this->n()); old.copyFrom(*this);
00070         resize(size); this->copyFrom(old);
00071 }
00075 void TreeMap::copyFrom(const TreeMap& source) {
00076         if (&source == this) return;
00077         if (source.n() > n()) resize(source.n());
00078         else clear();
00079         
00080         for (index x = source.nodes->firstIn(); x != 0;
00081                    x = source.nodes->nextIn(x)) {
00082                 put(source.st->key(x),source.values[x]); 
00083         }
00084 }
00085 
00090 int TreeMap::get(keytyp key) {
00091         if (root == 0) return UNDEF_VAL;
00092         index x = st->access(key,root);
00093         if (x == 0) return UNDEF_VAL;
00094         return values[x];
00095 }
00096 
00104 bool TreeMap::put(uint64_t key, uint32_t val) {
00105         index x;
00106         if (root == 0 || (x = st->access(key,root)) == 0) {
00107                 x = nodes->firstOut();
00108                 if (x == 0) return false;
00109                 nodes->swap(x);
00110                 st->setkey(x,key);
00111                 if (root == 0) root = x;
00112                 else st->insert(x,root);
00113         }
00114         values[x] = val;
00115         return true;
00116 }
00117 
00121 void TreeMap::remove(uint64_t key) {
00122         index x;
00123         if (root != 0 && (x = st->access(key,root)) != 0) {
00124                 st->remove(x,root); nodes->swap(x);
00125         }
00126         return;
00127 }
00128 
00129 // Construct string listing the key,value pairs in the map
00130 string& TreeMap::toString(string& s) const {
00131         stringstream ss;
00132         for (index u = nodes->firstIn(); u != 0; u = nodes->nextIn(u)) {
00133                 ss << " " << st->key(u) << "," << values[u];
00134         }
00135         s = ss.str();
00136         return s;
00137 }
00138 
00139 } // ends namespace
 All Classes Files Functions Variables Typedefs Friends