1 //===- llvm/ADT/MapVector.h - Map w/ deterministic value order --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
17 #ifndef LLVM_ADT_MAPVECTOR_H
18 #define LLVM_ADT_MAPVECTOR_H
20 #include "llvm/ADT/DenseMap.h"
25 /// This class implements a map that also provides access to all stored values
26 /// in a deterministic order. The values are kept in a std::vector and the
27 /// mapping is done with DenseMap from Keys to indexes in that vector.
28 template<typename KeyT, typename ValueT,
29 typename MapType = llvm::DenseMap<KeyT, unsigned>,
30 typename VectorType = std::vector<std::pair<KeyT, ValueT> > >
32 typedef typename VectorType::size_type size_type;
38 typedef typename VectorType::iterator iterator;
39 typedef typename VectorType::const_iterator const_iterator;
40 typedef typename VectorType::reverse_iterator reverse_iterator;
41 typedef typename VectorType::const_reverse_iterator const_reverse_iterator;
43 size_type size() const { return Vector.size(); }
45 iterator begin() { return Vector.begin(); }
46 const_iterator begin() const { return Vector.begin(); }
47 iterator end() { return Vector.end(); }
48 const_iterator end() const { return Vector.end(); }
50 reverse_iterator rbegin() { return Vector.rbegin(); }
51 const_reverse_iterator rbegin() const { return Vector.rbegin(); }
52 reverse_iterator rend() { return Vector.rend(); }
53 const_reverse_iterator rend() const { return Vector.rend(); }
56 return Vector.empty();
59 std::pair<KeyT, ValueT> &front() { return Vector.front(); }
60 const std::pair<KeyT, ValueT> &front() const { return Vector.front(); }
61 std::pair<KeyT, ValueT> &back() { return Vector.back(); }
62 const std::pair<KeyT, ValueT> &back() const { return Vector.back(); }
69 ValueT &operator[](const KeyT &Key) {
70 std::pair<KeyT, unsigned> Pair = std::make_pair(Key, 0);
71 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
72 unsigned &I = Result.first->second;
74 Vector.push_back(std::make_pair(Key, ValueT()));
75 I = Vector.size() - 1;
77 return Vector[I].second;
80 ValueT lookup(const KeyT &Key) const {
81 typename MapType::const_iterator Pos = Map.find(Key);
82 return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
85 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
86 std::pair<KeyT, unsigned> Pair = std::make_pair(KV.first, 0);
87 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
88 unsigned &I = Result.first->second;
90 Vector.push_back(std::make_pair(KV.first, KV.second));
91 I = Vector.size() - 1;
92 return std::make_pair(std::prev(end()), true);
94 return std::make_pair(begin() + I, false);
97 size_type count(const KeyT &Key) const {
98 typename MapType::const_iterator Pos = Map.find(Key);
99 return Pos == Map.end()? 0 : 1;
102 iterator find(const KeyT &Key) {
103 typename MapType::const_iterator Pos = Map.find(Key);
104 return Pos == Map.end()? Vector.end() :
105 (Vector.begin() + Pos->second);
108 const_iterator find(const KeyT &Key) const {
109 typename MapType::const_iterator Pos = Map.find(Key);
110 return Pos == Map.end()? Vector.end() :
111 (Vector.begin() + Pos->second);
114 /// \brief Remove the last element from the vector.
116 typename MapType::iterator Pos = Map.find(Vector.back().first);
121 /// \brief Remove the element given by Iterator.
123 /// Returns an iterator to the element following the one which was removed,
124 /// which may be end().
126 /// \note This is a deceivingly expensive operation (linear time). It's
127 /// usually better to use \a remove_if() if possible.
128 typename VectorType::iterator erase(typename VectorType::iterator Iterator) {
129 Map.erase(Iterator->first);
130 auto Next = Vector.erase(Iterator);
131 if (Next == Vector.end())
134 // Update indices in the map.
135 size_t Index = Next - Vector.begin();
136 for (auto &I : Map) {
137 assert(I.second != Index && "Index was already erased!");
138 if (I.second > Index)
144 /// \brief Remove all elements with the key value Key.
146 /// Returns the number of elements removed.
147 size_type erase(const KeyT &Key) {
148 auto Iterator = find(Key);
149 if (Iterator == end())
155 /// \brief Remove the elements that match the predicate.
157 /// Erase all elements that match \c Pred in a single pass. Takes linear
159 template <class Predicate> void remove_if(Predicate Pred);
162 template <typename KeyT, typename ValueT, typename MapType, typename VectorType>
163 template <class Function>
164 void MapVector<KeyT, ValueT, MapType, VectorType>::remove_if(Function Pred) {
165 auto O = Vector.begin();
166 for (auto I = O, E = Vector.end(); I != E; ++I) {
168 // Erase from the map.
174 // Move the value and update the index in the map.
176 Map[O->first] = O - Vector.begin();
180 // Erase trailing entries in the vector.
181 Vector.erase(O, Vector.end());
184 } // end namespace llvm