clang API Documentation

ContinuousRangeMap.h
Go to the documentation of this file.
00001 //===--- ContinuousRangeMap.h - Map with int range as key -------*- C++ -*-===//
00002 //
00003 //                     The LLVM Compiler Infrastructure
00004 //
00005 // This file is distributed under the University of Illinois Open Source
00006 // License. See LICENSE.TXT for details.
00007 //
00008 //===----------------------------------------------------------------------===//
00009 //
00010 //  This file defines the ContinuousRangeMap class, which is a highly
00011 //  specialized container used by serialization.
00012 //
00013 //===----------------------------------------------------------------------===//
00014 
00015 #ifndef LLVM_CLANG_SERIALIZATION_CONTINUOUS_RANGE_MAP_H
00016 #define LLVM_CLANG_SERIALIZATION_CONTINUOUS_RANGE_MAP_H
00017 
00018 #include "llvm/ADT/SmallVector.h"
00019 #include <algorithm>
00020 #include <utility>
00021 
00022 namespace clang {
00023 
00024 /// \brief A map from continuous integer ranges to some value, with a very
00025 /// specialized interface.
00026 ///
00027 /// CRM maps from integer ranges to values. The ranges are continuous, i.e.
00028 /// where one ends, the next one begins. So if the map contains the stops I0-3,
00029 /// the first range is from I0 to I1, the second from I1 to I2, the third from
00030 /// I2 to I3 and the last from I3 to infinity.
00031 ///
00032 /// Ranges must be inserted in order. Inserting a new stop I4 into the map will
00033 /// shrink the fourth range to I3 to I4 and add the new range I4 to inf.
00034 template <typename Int, typename V, unsigned InitialCapacity>
00035 class ContinuousRangeMap {
00036 public:
00037   typedef std::pair<Int, V> value_type;
00038   typedef value_type &reference;
00039   typedef const value_type &const_reference;
00040   typedef value_type *pointer;
00041   typedef const value_type *const_pointer;
00042 
00043 private:
00044   typedef SmallVector<value_type, InitialCapacity> Representation;
00045   Representation Rep;
00046 
00047   struct Compare {
00048     bool operator ()(const_reference L, Int R) const {
00049       return L.first < R;
00050     }
00051     bool operator ()(Int L, const_reference R) const {
00052       return L < R.first;
00053     }
00054     bool operator ()(Int L, Int R) const { 
00055       return L < R;
00056     }
00057     bool operator ()(const_reference L, const_reference R) const {
00058       return L.first < R.first;
00059     }
00060   };
00061 
00062 public:
00063   void insert(const value_type &Val) {
00064     if (!Rep.empty() && Rep.back() == Val)
00065       return;
00066 
00067     assert((Rep.empty() || Rep.back().first < Val.first) &&
00068            "Must insert keys in order.");
00069     Rep.push_back(Val);
00070   }
00071   
00072   void insertOrReplace(const value_type &Val) {
00073     iterator I = std::lower_bound(Rep.begin(), Rep.end(), Val, Compare());
00074     if (I != Rep.end() && I->first == Val.first) {
00075       I->second = Val.second;
00076       return;
00077     }
00078     
00079     Rep.insert(I, Val);
00080   }
00081 
00082   typedef typename Representation::iterator iterator;
00083   typedef typename Representation::const_iterator const_iterator;
00084 
00085   iterator begin() { return Rep.begin(); }
00086   iterator end() { return Rep.end(); }
00087   const_iterator begin() const { return Rep.begin(); }
00088   const_iterator end() const { return Rep.end(); }
00089 
00090   iterator find(Int K) {
00091     iterator I = std::upper_bound(Rep.begin(), Rep.end(), K, Compare());
00092     // I points to the first entry with a key > K, which is the range that
00093     // follows the one containing K.
00094     if (I == Rep.begin())
00095       return Rep.end();
00096     --I;
00097     return I;
00098   }
00099   const_iterator find(Int K) const {
00100     return const_cast<ContinuousRangeMap*>(this)->find(K);
00101   }
00102 
00103   reference back() { return Rep.back(); }
00104   const_reference back() const { return Rep.back(); }
00105   
00106   /// \brief An object that helps properly build a continuous range map
00107   /// from a set of values.
00108   class Builder {
00109     ContinuousRangeMap &Self;
00110     
00111     Builder(const Builder&); // DO NOT IMPLEMENT
00112     Builder &operator=(const Builder&); // DO NOT IMPLEMENT
00113     
00114   public:
00115     explicit Builder(ContinuousRangeMap &Self) : Self(Self) { }
00116     
00117     ~Builder() {
00118       std::sort(Self.Rep.begin(), Self.Rep.end(), Compare());
00119     }
00120     
00121     void insert(const value_type &Val) {
00122       Self.Rep.push_back(Val);
00123     }
00124   };
00125   friend class Builder;
00126 };
00127 
00128 }
00129 
00130 #endif