Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-05-10 08:43:07

0001 //===- llvm/ADT/MapVector.h - Map w/ deterministic value order --*- C++ -*-===//
0002 //
0003 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
0004 // See https://llvm.org/LICENSE.txt for license information.
0005 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
0006 //
0007 //===----------------------------------------------------------------------===//
0008 ///
0009 /// \file
0010 /// This file implements a map that provides insertion order iteration. The
0011 /// interface is purposefully minimal. The key is assumed to be cheap to copy
0012 /// and 2 copies are kept, one for indexing in a DenseMap, one for iteration in
0013 /// a SmallVector.
0014 ///
0015 //===----------------------------------------------------------------------===//
0016 
0017 #ifndef LLVM_ADT_MAPVECTOR_H
0018 #define LLVM_ADT_MAPVECTOR_H
0019 
0020 #include "llvm/ADT/DenseMap.h"
0021 #include "llvm/ADT/SmallVector.h"
0022 #include <cassert>
0023 #include <cstddef>
0024 #include <iterator>
0025 #include <type_traits>
0026 #include <utility>
0027 
0028 namespace llvm {
0029 
0030 /// This class implements a map that also provides access to all stored values
0031 /// in a deterministic order. The values are kept in a SmallVector<*, 0> and the
0032 /// mapping is done with DenseMap from Keys to indexes in that vector.
0033 template <typename KeyT, typename ValueT,
0034           typename MapType = DenseMap<KeyT, unsigned>,
0035           typename VectorType = SmallVector<std::pair<KeyT, ValueT>, 0>>
0036 class MapVector {
0037   MapType Map;
0038   VectorType Vector;
0039 
0040   static_assert(
0041       std::is_integral_v<typename MapType::mapped_type>,
0042       "The mapped_type of the specified Map must be an integral type");
0043 
0044 public:
0045   using key_type = KeyT;
0046   using value_type = typename VectorType::value_type;
0047   using size_type = typename VectorType::size_type;
0048 
0049   using iterator = typename VectorType::iterator;
0050   using const_iterator = typename VectorType::const_iterator;
0051   using reverse_iterator = typename VectorType::reverse_iterator;
0052   using const_reverse_iterator = typename VectorType::const_reverse_iterator;
0053 
0054   /// Clear the MapVector and return the underlying vector.
0055   VectorType takeVector() {
0056     Map.clear();
0057     return std::move(Vector);
0058   }
0059 
0060   size_type size() const { return Vector.size(); }
0061 
0062   /// Grow the MapVector so that it can contain at least \p NumEntries items
0063   /// before resizing again.
0064   void reserve(size_type NumEntries) {
0065     Map.reserve(NumEntries);
0066     Vector.reserve(NumEntries);
0067   }
0068 
0069   iterator begin() { return Vector.begin(); }
0070   const_iterator begin() const { return Vector.begin(); }
0071   iterator end() { return Vector.end(); }
0072   const_iterator end() const { return Vector.end(); }
0073 
0074   reverse_iterator rbegin() { return Vector.rbegin(); }
0075   const_reverse_iterator rbegin() const { return Vector.rbegin(); }
0076   reverse_iterator rend() { return Vector.rend(); }
0077   const_reverse_iterator rend() const { return Vector.rend(); }
0078 
0079   bool empty() const {
0080     return Vector.empty();
0081   }
0082 
0083   std::pair<KeyT, ValueT>       &front()       { return Vector.front(); }
0084   const std::pair<KeyT, ValueT> &front() const { return Vector.front(); }
0085   std::pair<KeyT, ValueT>       &back()        { return Vector.back(); }
0086   const std::pair<KeyT, ValueT> &back()  const { return Vector.back(); }
0087 
0088   void clear() {
0089     Map.clear();
0090     Vector.clear();
0091   }
0092 
0093   void swap(MapVector &RHS) {
0094     std::swap(Map, RHS.Map);
0095     std::swap(Vector, RHS.Vector);
0096   }
0097 
0098   ValueT &operator[](const KeyT &Key) {
0099     std::pair<KeyT, typename MapType::mapped_type> Pair = std::make_pair(Key, 0);
0100     std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
0101     auto &I = Result.first->second;
0102     if (Result.second) {
0103       Vector.push_back(std::make_pair(Key, ValueT()));
0104       I = Vector.size() - 1;
0105     }
0106     return Vector[I].second;
0107   }
0108 
0109   // Returns a copy of the value.  Only allowed if ValueT is copyable.
0110   ValueT lookup(const KeyT &Key) const {
0111     static_assert(std::is_copy_constructible_v<ValueT>,
0112                   "Cannot call lookup() if ValueT is not copyable.");
0113     typename MapType::const_iterator Pos = Map.find(Key);
0114     return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
0115   }
0116 
0117   template <typename... Ts>
0118   std::pair<iterator, bool> try_emplace(const KeyT &Key, Ts &&...Args) {
0119     auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
0120     if (Inserted) {
0121       It->second = Vector.size();
0122       Vector.emplace_back(std::piecewise_construct, std::forward_as_tuple(Key),
0123                           std::forward_as_tuple(std::forward<Ts>(Args)...));
0124       return std::make_pair(std::prev(end()), true);
0125     }
0126     return std::make_pair(begin() + It->second, false);
0127   }
0128   template <typename... Ts>
0129   std::pair<iterator, bool> try_emplace(KeyT &&Key, Ts &&...Args) {
0130     auto [It, Inserted] = Map.insert(std::make_pair(Key, 0));
0131     if (Inserted) {
0132       It->second = Vector.size();
0133       Vector.emplace_back(std::piecewise_construct,
0134                           std::forward_as_tuple(std::move(Key)),
0135                           std::forward_as_tuple(std::forward<Ts>(Args)...));
0136       return std::make_pair(std::prev(end()), true);
0137     }
0138     return std::make_pair(begin() + It->second, false);
0139   }
0140 
0141   std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
0142     return try_emplace(KV.first, KV.second);
0143   }
0144   std::pair<iterator, bool> insert(std::pair<KeyT, ValueT> &&KV) {
0145     return try_emplace(std::move(KV.first), std::move(KV.second));
0146   }
0147 
0148   template <typename V>
0149   std::pair<iterator, bool> insert_or_assign(const KeyT &Key, V &&Val) {
0150     auto Ret = try_emplace(Key, std::forward<V>(Val));
0151     if (!Ret.second)
0152       Ret.first->second = std::forward<V>(Val);
0153     return Ret;
0154   }
0155   template <typename V>
0156   std::pair<iterator, bool> insert_or_assign(KeyT &&Key, V &&Val) {
0157     auto Ret = try_emplace(std::move(Key), std::forward<V>(Val));
0158     if (!Ret.second)
0159       Ret.first->second = std::forward<V>(Val);
0160     return Ret;
0161   }
0162 
0163   bool contains(const KeyT &Key) const { return Map.find(Key) != Map.end(); }
0164 
0165   size_type count(const KeyT &Key) const { return contains(Key) ? 1 : 0; }
0166 
0167   iterator find(const KeyT &Key) {
0168     typename MapType::const_iterator Pos = Map.find(Key);
0169     return Pos == Map.end()? Vector.end() :
0170                             (Vector.begin() + Pos->second);
0171   }
0172 
0173   const_iterator find(const KeyT &Key) const {
0174     typename MapType::const_iterator Pos = Map.find(Key);
0175     return Pos == Map.end()? Vector.end() :
0176                             (Vector.begin() + Pos->second);
0177   }
0178 
0179   /// Remove the last element from the vector.
0180   void pop_back() {
0181     typename MapType::iterator Pos = Map.find(Vector.back().first);
0182     Map.erase(Pos);
0183     Vector.pop_back();
0184   }
0185 
0186   /// Remove the element given by Iterator.
0187   ///
0188   /// Returns an iterator to the element following the one which was removed,
0189   /// which may be end().
0190   ///
0191   /// \note This is a deceivingly expensive operation (linear time).  It's
0192   /// usually better to use \a remove_if() if possible.
0193   typename VectorType::iterator erase(typename VectorType::iterator Iterator) {
0194     Map.erase(Iterator->first);
0195     auto Next = Vector.erase(Iterator);
0196     if (Next == Vector.end())
0197       return Next;
0198 
0199     // Update indices in the map.
0200     size_t Index = Next - Vector.begin();
0201     for (auto &I : Map) {
0202       assert(I.second != Index && "Index was already erased!");
0203       if (I.second > Index)
0204         --I.second;
0205     }
0206     return Next;
0207   }
0208 
0209   /// Remove all elements with the key value Key.
0210   ///
0211   /// Returns the number of elements removed.
0212   size_type erase(const KeyT &Key) {
0213     auto Iterator = find(Key);
0214     if (Iterator == end())
0215       return 0;
0216     erase(Iterator);
0217     return 1;
0218   }
0219 
0220   /// Remove the elements that match the predicate.
0221   ///
0222   /// Erase all elements that match \c Pred in a single pass.  Takes linear
0223   /// time.
0224   template <class Predicate> void remove_if(Predicate Pred);
0225 };
0226 
0227 template <typename KeyT, typename ValueT, typename MapType, typename VectorType>
0228 template <class Function>
0229 void MapVector<KeyT, ValueT, MapType, VectorType>::remove_if(Function Pred) {
0230   auto O = Vector.begin();
0231   for (auto I = O, E = Vector.end(); I != E; ++I) {
0232     if (Pred(*I)) {
0233       // Erase from the map.
0234       Map.erase(I->first);
0235       continue;
0236     }
0237 
0238     if (I != O) {
0239       // Move the value and update the index in the map.
0240       *O = std::move(*I);
0241       Map[O->first] = O - Vector.begin();
0242     }
0243     ++O;
0244   }
0245   // Erase trailing entries in the vector.
0246   Vector.erase(O, Vector.end());
0247 }
0248 
0249 /// A MapVector that performs no allocations if smaller than a certain
0250 /// size.
0251 template <typename KeyT, typename ValueT, unsigned N>
0252 struct SmallMapVector
0253     : MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
0254                 SmallVector<std::pair<KeyT, ValueT>, N>> {
0255 };
0256 
0257 } // end namespace llvm
0258 
0259 #endif // LLVM_ADT_MAPVECTOR_H