User Manual, Developers Guide and API Documentation

RangeMap.hpp

Go to the documentation of this file.
00001 /*******************************************************************************
00002  * This file is part of openWNS (open Wireless Network Simulator)
00003  * _____________________________________________________________________________
00004  *
00005  * Copyright (C) 2004-2007
00006  * Chair of Communication Networks (ComNets)
00007  * Kopernikusstr. 16, D-52074 Aachen, Germany
00008  * phone: ++49-241-80-27910,
00009  * fax: ++49-241-80-22242
00010  * email: info@openwns.org
00011  * www: http://www.openwns.org
00012  * _____________________________________________________________________________
00013  *
00014  * openWNS is free software; you can redistribute it and/or modify it under the
00015  * terms of the GNU Lesser General Public License version 2 as published by the
00016  * Free Software Foundation;
00017  *
00018  * openWNS is distributed in the hope that it will be useful, but WITHOUT ANY
00019  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
00020  * A PARTICULAR PURPOSE.  See the GNU Lesser General Public License for more
00021  * details.
00022  *
00023  * You should have received a copy of the GNU Lesser General Public License
00024  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00025  *
00026  ******************************************************************************/
00027 
00028 #ifndef WNS_CONTAINER_RANGEMAP_HPP
00029 #define WNS_CONTAINER_RANGEMAP_HPP
00030 
00031 #include <WNS/Interval.hpp>
00032 #include <WNS/Ttos.hpp>
00033 #include <WNS/container/BinaryTree.hpp>
00034 
00035 #include <utility>
00036 
00037 namespace wns { namespace container {
00038 
00053     template<typename RangeType, typename ValueType, typename CleanupStrategy = tree::NoneOnErase>
00054     class RangeMap
00055     {
00056     public:
00057 
00060     class Exception : public wns::Exception
00061     {
00062     public:
00063         Exception(const std::string& s)
00064         : wns::Exception(s)
00065         {}
00066         ~Exception() throw() {}
00067     };
00068 
00069 
00072     typedef RangeType Index;
00073 
00076     typedef Interval<Index> IntervalType;
00077 
00080     typedef ValueType Value;
00081 
00084     RangeMap()
00085         : root(NULL)
00086     {}
00087 
00090     virtual ~RangeMap()
00091     {
00092         delete root;
00093     }
00094 
00106     RangeMap& insert(const IntervalType& interval, const ValueType& value)
00107     {
00108         IntervalValuePair intervalValue = IntervalValuePair(interval, value);
00109         assure(!interval.isEmpty(), "RangeMap: Can not insert empty intervals!");
00110         if (root == NULL) {
00111         root = new RangeTree(intervalValue);
00112         }
00113         else insert(intervalValue, root);
00114         return *this;
00115     }
00116 
00125     const Value& get(const Index& index) const
00126     {
00127         RangeTree* node = root;
00128         while (node != NULL) {
00129         if (node->getValue().first.contains(index)) return node->getValue().second;
00130         else {
00131             if (node->getValue().first.isAbove(index))
00132             node = node->getSubTree(0);
00133             else
00134             node = node->getSubTree(1);
00135         }
00136         }
00137         throw Exception("RangeMap: No value for index " + Ttos(index) + "!");
00138     }
00139 
00145     bool has(const Index& index) const
00146     {
00147         try {
00148         get(index);
00149         }
00150         catch (Exception) {
00151         return false;
00152         }
00153         return true;
00154     }
00155 
00156     private:
00157 
00158     typedef std::pair<IntervalType, Value> IntervalValuePair;
00160     typedef BinaryTree<IntervalValuePair, CleanupStrategy > RangeTree;
00161 
00162     void insert(const IntervalValuePair& intervalValue, RangeTree* node)
00163     {
00164         assure(node != NULL, "RangeMap: Attempt to insert into an empty tree");
00165         assure(!intervalValue.first.overlaps(node->getValue().first), "RangeMap: Can only insert disjoint intervals!");
00166 
00167         const int treeNo = (intervalValue.first.isBelow(node->getValue().first))?0:1;
00168 
00169         if (node->hasSubTree(treeNo)) insert(intervalValue, node->getSubTree(treeNo));
00170         else node->createSubTree(treeNo, intervalValue);
00171     }
00172 
00173     RangeTree* root;
00174     };
00175 
00176 } // container
00177 } // wns
00178 
00179 #endif // NOT defined WNS_CONTAINER_RANGEMAP_HPP

Generated on Fri May 25 03:31:36 2012 for openWNS by  doxygen 1.5.5