1 //===- llvm/ADT/MapVector.h - Map with 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/ArrayRef.h"
21 #include "llvm/ADT/DenseMap.h"
22 #include "llvm/ADT/STLExtras.h"
27 /// This class implements a map that also provides access to all stored values
28 /// in a deterministic order. The values are kept in a std::vector and the
29 /// mapping is done with DenseMap from Keys to indexes in that vector.
30 template<typename KeyT, typename ValueT,
31 typename MapType = llvm::DenseMap<KeyT, unsigned>,
32 typename VectorType = std::vector<std::pair<KeyT, ValueT> > >
34 typedef typename VectorType::size_type SizeType;
40 typedef typename VectorType::iterator iterator;
41 typedef typename VectorType::const_iterator const_iterator;
43 SizeType size() const {
48 return Vector.begin();
51 const_iterator begin() const {
52 return Vector.begin();
59 const_iterator end() const {
64 return Vector.empty();
72 ValueT &operator[](const KeyT &Key) {
73 std::pair<KeyT, unsigned> Pair = std::make_pair(Key, 0);
74 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
75 unsigned &I = Result.first->second;
77 Vector.push_back(std::make_pair(Key, ValueT()));
78 I = Vector.size() - 1;
80 return Vector[I].second;
83 ValueT lookup(const KeyT &Key) const {
84 typename MapType::const_iterator Pos = Map.find(Key);
85 return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
88 std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
89 std::pair<KeyT, unsigned> Pair = std::make_pair(KV.first, 0);
90 std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
91 unsigned &I = Result.first->second;
93 Vector.push_back(std::make_pair(KV.first, KV.second));
94 I = Vector.size() - 1;
95 return std::make_pair(llvm::prior(end()), true);
97 return std::make_pair(begin() + I, false);
100 unsigned count(const KeyT &Key) const {
101 typename MapType::const_iterator Pos = Map.find(Key);
102 return Pos == Map.end()? 0 : 1;
105 iterator find(const KeyT &Key) {
106 typename MapType::const_iterator Pos = Map.find(Key);
107 return Pos == Map.end()? Vector.end() :
108 (Vector.begin() + Pos->second);
111 const_iterator find(const KeyT &Key) const {
112 typename MapType::const_iterator Pos = Map.find(Key);
113 return Pos == Map.end()? Vector.end() :
114 (Vector.begin() + Pos->second);