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 SizeType;
38 typedef typename VectorType::iterator iterator;
39 typedef typename VectorType::const_iterator const_iterator;
41 SizeType size() const {
46 return Vector.begin();
49 const_iterator begin() const {
50 return Vector.begin();
57 const_iterator end() const {
62 return Vector.empty();
65 std::pair<KeyT, ValueT> &front() { return Vector.front(); }
66 const std::pair<KeyT, ValueT> &front() const { return Vector.front(); }
67 std::pair<KeyT, ValueT> &back() { return Vector.back(); }
68 const std::pair<KeyT, ValueT> &back() const { return Vector.back(); }
75 ValueT &operator[](const KeyT &Key) {
76 std::pair<KeyT, unsigned> Pair = std::make_pair(Key, 0);
77 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
78 unsigned &I = Result.first->second;
80 Vector.push_back(std::make_pair(Key, ValueT()));
81 I = Vector.size() - 1;
83 return Vector[I].second;
86 ValueT lookup(const KeyT &Key) const {
87 typename MapType::const_iterator Pos = Map.find(Key);
88 return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
91 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
92 std::pair<KeyT, unsigned> Pair = std::make_pair(KV.first, 0);
93 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
94 unsigned &I = Result.first->second;
96 Vector.push_back(std::make_pair(KV.first, KV.second));
97 I = Vector.size() - 1;
98 return std::make_pair(std::prev(end()), true);
100 return std::make_pair(begin() + I, false);
103 unsigned count(const KeyT &Key) const {
104 typename MapType::const_iterator Pos = Map.find(Key);
105 return Pos == Map.end()? 0 : 1;
108 iterator find(const KeyT &Key) {
109 typename MapType::const_iterator Pos = Map.find(Key);
110 return Pos == Map.end()? Vector.end() :
111 (Vector.begin() + Pos->second);
114 const_iterator find(const KeyT &Key) const {
115 typename MapType::const_iterator Pos = Map.find(Key);
116 return Pos == Map.end()? Vector.end() :
117 (Vector.begin() + Pos->second);
120 /// \brief Remove the last element from the vector.
122 typename MapType::iterator Pos = Map.find(Vector.back().first);