| 1 | //===- llvm/ADT/MapVector.h - Map w/ deterministic value order --*- C++ -*-===// | 
| 2 | // | 
| 3 | // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. | 
| 4 | // See https://llvm.org/LICENSE.txt for license information. | 
| 5 | // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception | 
| 6 | // | 
| 7 | //===----------------------------------------------------------------------===// | 
| 8 | /// | 
| 9 | /// \file | 
| 10 | /// This file implements a map that provides insertion order iteration. The | 
| 11 | /// interface is purposefully minimal. The key is assumed to be cheap to copy | 
| 12 | /// and 2 copies are kept, one for indexing in a DenseMap, one for iteration in | 
| 13 | /// a SmallVector. | 
| 14 | /// | 
| 15 | //===----------------------------------------------------------------------===// | 
| 16 |  | 
| 17 | #ifndef LLVM_ADT_MAPVECTOR_H | 
| 18 | #define LLVM_ADT_MAPVECTOR_H | 
| 19 |  | 
| 20 | #include "llvm/ADT/DenseMap.h" | 
| 21 | #include "llvm/ADT/SmallVector.h" | 
| 22 | #include <cassert> | 
| 23 | #include <cstddef> | 
| 24 | #include <iterator> | 
| 25 | #include <type_traits> | 
| 26 | #include <utility> | 
| 27 |  | 
| 28 | namespace llvm { | 
| 29 |  | 
| 30 | /// This class implements a map that also provides access to all stored values | 
| 31 | /// in a deterministic order. The values are kept in a SmallVector<*, 0> and the | 
| 32 | /// mapping is done with DenseMap from Keys to indexes in that vector. | 
| 33 | template <typename KeyT, typename ValueT, | 
| 34 |           typename MapType = DenseMap<KeyT, unsigned>, | 
| 35 |           typename VectorType = SmallVector<std::pair<KeyT, ValueT>, 0>> | 
| 36 | class MapVector { | 
| 37 |   MapType Map; | 
| 38 |   VectorType Vector; | 
| 39 |  | 
| 40 |   static_assert( | 
| 41 |       std::is_integral_v<typename MapType::mapped_type>, | 
| 42 |       "The mapped_type of the specified Map must be an integral type" ); | 
| 43 |  | 
| 44 | public: | 
| 45 |   using key_type = KeyT; | 
| 46 |   using value_type = typename VectorType::value_type; | 
| 47 |   using size_type = typename VectorType::size_type; | 
| 48 |  | 
| 49 |   using iterator = typename VectorType::iterator; | 
| 50 |   using const_iterator = typename VectorType::const_iterator; | 
| 51 |   using reverse_iterator = typename VectorType::reverse_iterator; | 
| 52 |   using const_reverse_iterator = typename VectorType::const_reverse_iterator; | 
| 53 |  | 
| 54 |   /// Clear the MapVector and return the underlying vector. | 
| 55 |   VectorType takeVector() { | 
| 56 |     Map.clear(); | 
| 57 |     return std::move(Vector); | 
| 58 |   } | 
| 59 |  | 
| 60 |   size_type size() const { return Vector.size(); } | 
| 61 |  | 
| 62 |   /// Grow the MapVector so that it can contain at least \p NumEntries items | 
| 63 |   /// before resizing again. | 
| 64 |   void reserve(size_type NumEntries) { | 
| 65 |     Map.reserve(NumEntries); | 
| 66 |     Vector.reserve(NumEntries); | 
| 67 |   } | 
| 68 |  | 
| 69 |   iterator begin() { return Vector.begin(); } | 
| 70 |   const_iterator begin() const { return Vector.begin(); } | 
| 71 |   iterator end() { return Vector.end(); } | 
| 72 |   const_iterator end() const { return Vector.end(); } | 
| 73 |  | 
| 74 |   reverse_iterator rbegin() { return Vector.rbegin(); } | 
| 75 |   const_reverse_iterator rbegin() const { return Vector.rbegin(); } | 
| 76 |   reverse_iterator rend() { return Vector.rend(); } | 
| 77 |   const_reverse_iterator rend() const { return Vector.rend(); } | 
| 78 |  | 
| 79 |   bool empty() const { | 
| 80 |     return Vector.empty(); | 
| 81 |   } | 
| 82 |  | 
| 83 |   std::pair<KeyT, ValueT>       &front()       { return Vector.front(); } | 
| 84 |   const std::pair<KeyT, ValueT> &front() const { return Vector.front(); } | 
| 85 |   std::pair<KeyT, ValueT>       &back()        { return Vector.back(); } | 
| 86 |   const std::pair<KeyT, ValueT> &back()  const { return Vector.back(); } | 
| 87 |  | 
| 88 |   void clear() { | 
| 89 |     Map.clear(); | 
| 90 |     Vector.clear(); | 
| 91 |   } | 
| 92 |  | 
| 93 |   void swap(MapVector &RHS) { | 
| 94 |     std::swap(Map, RHS.Map); | 
| 95 |     std::swap(Vector, RHS.Vector); | 
| 96 |   } | 
| 97 |  | 
| 98 |   ValueT &operator[](const KeyT &Key) { | 
| 99 |     std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(Key, 0); | 
| 100 |     std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair); | 
| 101 |     auto &I = Result.first->second; | 
| 102 |     if (Result.second) { | 
| 103 |       Vector.push_back(std::make_pair(Key, ValueT())); | 
| 104 |       I = Vector.size() - 1; | 
| 105 |     } | 
| 106 |     return Vector[I].second; | 
| 107 |   } | 
| 108 |  | 
| 109 |   // Returns a copy of the value.  Only allowed if ValueT is copyable. | 
| 110 |   ValueT lookup(const KeyT &Key) const { | 
| 111 |     static_assert(std::is_copy_constructible_v<ValueT>, | 
| 112 |                   "Cannot call lookup() if ValueT is not copyable." ); | 
| 113 |     typename MapType::const_iterator Pos = Map.find(Key); | 
| 114 |     return Pos == Map.end()? ValueT() : Vector[Pos->second].second; | 
| 115 |   } | 
| 116 |  | 
| 117 |   std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) { | 
| 118 |     std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0); | 
| 119 |     std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair); | 
| 120 |     auto &I = Result.first->second; | 
| 121 |     if (Result.second) { | 
| 122 |       Vector.push_back(std::make_pair(KV.first, KV.second)); | 
| 123 |       I = Vector.size() - 1; | 
| 124 |       return std::make_pair(std::prev(end()), true); | 
| 125 |     } | 
| 126 |     return std::make_pair(begin() + I, false); | 
| 127 |   } | 
| 128 |  | 
| 129 |   std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) { | 
| 130 |     // Copy KV.first into the map, then move it into the vector. | 
| 131 |     std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(KV.first, 0); | 
| 132 |     std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair); | 
| 133 |     auto &I = Result.first->second; | 
| 134 |     if (Result.second) { | 
| 135 |       Vector.push_back(std::move(KV)); | 
| 136 |       I = Vector.size() - 1; | 
| 137 |       return std::make_pair(std::prev(end()), true); | 
| 138 |     } | 
| 139 |     return std::make_pair(begin() + I, false); | 
| 140 |   } | 
| 141 |  | 
| 142 |   bool contains(const KeyT &Key) const { return Map.find(Key) != Map.end(); } | 
| 143 |  | 
| 144 |   size_type count(const KeyT &Key) const { return contains(Key) ? 1 : 0; } | 
| 145 |  | 
| 146 |   iterator find(const KeyT &Key) { | 
| 147 |     typename MapType::const_iterator Pos = Map.find(Key); | 
| 148 |     return Pos == Map.end()? Vector.end() : | 
| 149 |                             (Vector.begin() + Pos->second); | 
| 150 |   } | 
| 151 |  | 
| 152 |   const_iterator find(const KeyT &Key) const { | 
| 153 |     typename MapType::const_iterator Pos = Map.find(Key); | 
| 154 |     return Pos == Map.end()? Vector.end() : | 
| 155 |                             (Vector.begin() + Pos->second); | 
| 156 |   } | 
| 157 |  | 
| 158 |   /// Remove the last element from the vector. | 
| 159 |   void pop_back() { | 
| 160 |     typename MapType::iterator Pos = Map.find(Vector.back().first); | 
| 161 |     Map.erase(Pos); | 
| 162 |     Vector.pop_back(); | 
| 163 |   } | 
| 164 |  | 
| 165 |   /// Remove the element given by Iterator. | 
| 166 |   /// | 
| 167 |   /// Returns an iterator to the element following the one which was removed, | 
| 168 |   /// which may be end(). | 
| 169 |   /// | 
| 170 |   /// \note This is a deceivingly expensive operation (linear time).  It's | 
| 171 |   /// usually better to use \a remove_if() if possible. | 
| 172 |   typename VectorType::iterator erase(typename VectorType::iterator Iterator) { | 
| 173 |     Map.erase(Iterator->first); | 
| 174 |     auto Next = Vector.erase(Iterator); | 
| 175 |     if (Next == Vector.end()) | 
| 176 |       return Next; | 
| 177 |  | 
| 178 |     // Update indices in the map. | 
| 179 |     size_t Index = Next - Vector.begin(); | 
| 180 |     for (auto &I : Map) { | 
| 181 |       assert(I.second != Index && "Index was already erased!" ); | 
| 182 |       if (I.second > Index) | 
| 183 |         --I.second; | 
| 184 |     } | 
| 185 |     return Next; | 
| 186 |   } | 
| 187 |  | 
| 188 |   /// Remove all elements with the key value Key. | 
| 189 |   /// | 
| 190 |   /// Returns the number of elements removed. | 
| 191 |   size_type erase(const KeyT &Key) { | 
| 192 |     auto Iterator = find(Key); | 
| 193 |     if (Iterator == end()) | 
| 194 |       return 0; | 
| 195 |     erase(Iterator); | 
| 196 |     return 1; | 
| 197 |   } | 
| 198 |  | 
| 199 |   /// Remove the elements that match the predicate. | 
| 200 |   /// | 
| 201 |   /// Erase all elements that match \c Pred in a single pass.  Takes linear | 
| 202 |   /// time. | 
| 203 |   template <class Predicate> void remove_if(Predicate Pred); | 
| 204 | }; | 
| 205 |  | 
| 206 | template <typename KeyT, typename ValueT, typename MapType, typename VectorType> | 
| 207 | template <class Function> | 
| 208 | void MapVector<KeyT, ValueT, MapType, VectorType>::remove_if(Function Pred) { | 
| 209 |   auto O = Vector.begin(); | 
| 210 |   for (auto I = O, E = Vector.end(); I != E; ++I) { | 
| 211 |     if (Pred(*I)) { | 
| 212 |       // Erase from the map. | 
| 213 |       Map.erase(I->first); | 
| 214 |       continue; | 
| 215 |     } | 
| 216 |  | 
| 217 |     if (I != O) { | 
| 218 |       // Move the value and update the index in the map. | 
| 219 |       *O = std::move(*I); | 
| 220 |       Map[O->first] = O - Vector.begin(); | 
| 221 |     } | 
| 222 |     ++O; | 
| 223 |   } | 
| 224 |   // Erase trailing entries in the vector. | 
| 225 |   Vector.erase(O, Vector.end()); | 
| 226 | } | 
| 227 |  | 
| 228 | /// A MapVector that performs no allocations if smaller than a certain | 
| 229 | /// size. | 
| 230 | template <typename KeyT, typename ValueT, unsigned N> | 
| 231 | struct SmallMapVector | 
| 232 |     : MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>, | 
| 233 |                 SmallVector<std::pair<KeyT, ValueT>, N>> { | 
| 234 | }; | 
| 235 |  | 
| 236 | } // end namespace llvm | 
| 237 |  | 
| 238 | #endif // LLVM_ADT_MAPVECTOR_H | 
| 239 |  |