IntervalTree.h

Go to the documentation of this file.
00001 /*
00002  * See the dyninst/COPYRIGHT file for copyright information.
00003  * 
00004  * We provide the Paradyn Tools (below described as "Paradyn")
00005  * on an AS IS basis, and do not warrant its validity or performance.
00006  * We reserve the right to update, modify, or discontinue this
00007  * software at any time.  We shall have no obligation to supply such
00008  * updates or modifications or any other form of support to you.
00009  * 
00010  * By your use of Paradyn, you understand and agree that we (or any
00011  * other person or entity with proprietary rights in Paradyn) are
00012  * under no obligation to provide either maintenance services,
00013  * update services, notices of latent defects, or correction of
00014  * defects for Paradyn.
00015  * 
00016  * This library is free software; you can redistribute it and/or
00017  * modify it under the terms of the GNU Lesser General Public
00018  * License as published by the Free Software Foundation; either
00019  * version 2.1 of the License, or (at your option) any later version.
00020  * 
00021  * This library is distributed in the hope that it will be useful,
00022  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00023  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00024  * Lesser General Public License for more details.
00025  * 
00026  * You should have received a copy of the GNU Lesser General Public
00027  * License along with this library; if not, write to the Free Software
00028  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
00029  */
00030 
00031 #if !defined(INTERVAL_TREE_H)
00032 #define INTERVAL_TREE_H
00033 
00034 #include <assert.h>
00035 #include <stdio.h>
00036 #include <vector>
00037 #include <map>
00038 
00039 template <class K, class V>
00040 class IntervalTree {
00041   typedef typename std::map<K, std::pair<K, V> > Tree;
00042   typedef typename Tree::const_iterator c_iter;
00043   typedef typename Tree::const_reverse_iterator c_r_iter;
00044   typedef typename Tree::iterator Iter;
00045 
00046  public:
00047   typedef typename std::pair<K, K> Range;
00048   typedef typename std::pair<Range, V> Entry;
00049   typedef typename Tree::iterator iterator;
00050   typedef typename Tree::const_iterator const_iterator;
00051 
00052   IntervalTree() {};
00053   ~IntervalTree() {};
00054 
00055   iterator begin() { return tree_.begin(); }
00056   iterator end() { return tree_.end(); }
00057   const_iterator begin() const { return tree_.begin(); }
00058   const_iterator end() const { return tree_.end(); }
00059 
00060   int size() const { return tree_.size(); }
00061   bool empty() const { return tree_.empty(); }
00062   void insert(K lb, K ub, V v) {
00063     tree_[lb] = std::make_pair(ub, v);
00064   }
00065 
00066   void remove(K lb) {
00067      erase(lb);
00068   }
00069 
00070   void erase(K lb) {
00071      tree_.erase(lb);
00072   }
00073 
00074   bool find(K key, V &value) const {
00075     K lb = 0;
00076     K ub = 0;
00077     V val;
00078     if (!precessor(key, lb, ub, val))
00079       return false;
00080     if (key < lb) return false;
00081     if (key >= ub) return false;
00082     value = val;
00083     return true;
00084   }
00085 
00086   bool find(K key, K &l, K &u, V &value) const {
00087     if (!precessor(key, l, u, value))
00088       return false;
00089     if (key < l) return false;
00090     if (key >= u) return false;
00091     return true;
00092   }
00093 
00094   bool precessor(K key, K &l, K &u, V &v) const {
00095     Entry e;
00096     if (!precessor(key, e)) {
00097       return false;
00098     }
00099     l = lb(e);
00100     u = ub(e);
00101     v = value(e);
00102     return true;
00103   }
00104 
00105   bool precessor(K key, Entry &e) const {
00106     if (tree_.empty()) return false;
00107     c_iter iter = tree_.lower_bound(key);
00108     if ((iter == tree_.end()) ||
00109     (iter->first != key)) {
00110       if (iter == tree_.begin()) {
00111     return false;
00112       }
00113       --iter;
00114     }
00115     if (iter->first > key) return false;
00116 
00117     lb(e) = iter->first;
00118     ub(e) = iter->second.first;
00119     value(e) = iter->second.second;
00120 
00121     assert(lb(e) <= key);
00122     if (ub(e) <= key) return false;
00123     return true;
00124   }
00125   void elements(std::vector<Entry> &buffer) const {
00126     buffer.clear();
00127     for (c_iter iter = tree_.begin();
00128      iter != tree_.end(); ++iter) {
00129       Entry e;
00130       lb(e) = iter->first;
00131       ub(e) = iter->second.first;
00132       value(e) = iter->second.second;
00133       buffer.push_back(e);
00134     }
00135   }
00136 
00137   K lowest() const { 
00138     if (tree_.empty()) return 0;
00139     c_iter iter = tree_.begin();
00140     return iter->first;
00141   }
00142 
00143   K highest() const { 
00144     if (tree_.empty()) return 0;
00145     c_r_iter iter = tree_.rbegin();
00146     return iter->second.first;
00147   }
00148 
00149   void clear() { tree_.clear(); }
00150 
00151   bool empty() { return tree_.empty(); }
00152 
00153   bool update(K lb, K newUB) {
00154      Iter iter = tree_.find(lb);
00155      if (iter == tree_.end()) return false;
00156      iter->second.first = newUB;
00157   }
00158 
00159  private:
00160   
00161   static V &value(Entry &e) { return e.second; }
00162   static K &lb(Entry &e) { return e.first.first; }
00163   static K &ub(Entry &e) { return e.first.second; }
00164 
00165   Tree tree_;
00166 };
00167 #endif
 All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Defines

Generated on 12 Jul 2013 for SymtabAPI by  doxygen 1.6.1