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_CONTINUOUSRANGEMAP_H
00016 #define LLVM_CLANG_SERIALIZATION_CONTINUOUSRANGEMAP_H
00017 
00018 #include "clang/Basic/LLVM.h"
00019 #include "llvm/ADT/SmallVector.h"
00020 #include <algorithm>
00021 #include <utility>
00022 
00023 namespace clang {
00024 
00025 /// \brief A map from continuous integer ranges to some value, with a very
00026 /// specialized interface.
00027 ///
00028 /// CRM maps from integer ranges to values. The ranges are continuous, i.e.
00029 /// where one ends, the next one begins. So if the map contains the stops I0-3,
00030 /// the first range is from I0 to I1, the second from I1 to I2, the third from
00031 /// I2 to I3 and the last from I3 to infinity.
00032 ///
00033 /// Ranges must be inserted in order. Inserting a new stop I4 into the map will
00034 /// shrink the fourth range to I3 to I4 and add the new range I4 to inf.
00035 template <typename Int, typename V, unsigned InitialCapacity>
00036 class ContinuousRangeMap {
00037 public:
00038   typedef std::pair<Int, V> value_type;
00039   typedef value_type &reference;
00040   typedef const value_type &const_reference;
00041   typedef value_type *pointer;
00042   typedef const value_type *const_pointer;
00043 
00044 private:
00045   typedef SmallVector<value_type, InitialCapacity> Representation;
00046   Representation Rep;
00047 
00048   struct Compare {
00049     bool operator ()(const_reference L, Int R) const {
00050       return L.first < R;
00051     }
00052     bool operator ()(Int L, const_reference R) const {
00053       return L < R.first;
00054     }
00055     bool operator ()(Int L, Int R) const { 
00056       return L < R;
00057     }
00058     bool operator ()(const_reference L, const_reference R) const {
00059       return L.first < R.first;
00060     }
00061   };
00062 
00063 public:
00064   void insert(const value_type &Val) {
00065     if (!Rep.empty() && Rep.back() == Val)
00066       return;
00067 
00068     assert((Rep.empty() || Rep.back().first < Val.first) &&
00069            "Must insert keys in order.");
00070     Rep.push_back(Val);
00071   }
00072   
00073   void insertOrReplace(const value_type &Val) {
00074     iterator I = std::lower_bound(Rep.begin(), Rep.end(), Val, Compare());
00075     if (I != Rep.end() && I->first == Val.first) {
00076       I->second = Val.second;
00077       return;
00078     }
00079     
00080     Rep.insert(I, Val);
00081   }
00082 
00083   typedef typename Representation::iterator iterator;
00084   typedef typename Representation::const_iterator const_iterator;
00085 
00086   iterator begin() { return Rep.begin(); }
00087   iterator end() { return Rep.end(); }
00088   const_iterator begin() const { return Rep.begin(); }
00089   const_iterator end() const { return Rep.end(); }
00090 
00091   iterator find(Int K) {
00092     iterator I = std::upper_bound(Rep.begin(), Rep.end(), K, Compare());
00093     // I points to the first entry with a key > K, which is the range that
00094     // follows the one containing K.
00095     if (I == Rep.begin())
00096       return Rep.end();
00097     --I;
00098     return I;
00099   }
00100   const_iterator find(Int K) const {
00101     return const_cast<ContinuousRangeMap*>(this)->find(K);
00102   }
00103 
00104   reference back() { return Rep.back(); }
00105   const_reference back() const { return Rep.back(); }
00106   
00107   /// \brief An object that helps properly build a continuous range map
00108   /// from a set of values.
00109   class Builder {
00110     ContinuousRangeMap &Self;
00111     
00112     Builder(const Builder&) LLVM_DELETED_FUNCTION;
00113     Builder &operator=(const Builder&) LLVM_DELETED_FUNCTION;
00114     
00115   public:
00116     explicit Builder(ContinuousRangeMap &Self) : Self(Self) { }
00117     
00118     ~Builder() {
00119       std::sort(Self.Rep.begin(), Self.Rep.end(), Compare());
00120       std::unique(Self.Rep.begin(), Self.Rep.end(),
00121                   [](const_reference A, const_reference B) {
00122         // FIXME: we should not allow any duplicate keys, but there are a lot of
00123         // duplicate 0 -> 0 mappings to remove first.
00124         assert((A == B || A.first != B.first) &&
00125                "ContinuousRangeMap::Builder given non-unique keys");
00126         return A == B;
00127       });
00128     }
00129     
00130     void insert(const value_type &Val) {
00131       Self.Rep.push_back(Val);
00132     }
00133   };
00134   friend class Builder;
00135 };
00136 
00137 }
00138 
00139 #endif