2014-01-06 19:52:42 +00:00
|
|
|
//===- llvm/ADT/MapVector.h - Map w/ deterministic value order --*- C++ -*-===//
|
2012-09-18 18:43:21 +00:00
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2012-09-19 13:42:51 +00:00
|
|
|
// This file implements a map that provides insertion order iteration. The
|
|
|
|
// interface is purposefully minimal. The key is assumed to be cheap to copy
|
|
|
|
// and 2 copies are kept, one for indexing in a DenseMap, one for iteration in
|
|
|
|
// a std::vector.
|
2012-09-18 18:43:21 +00:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_MAPVECTOR_H
|
|
|
|
#define LLVM_ADT_MAPVECTOR_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2014-12-31 23:33:21 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2012-09-18 18:43:21 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
|
|
|
/// This class implements a map that also provides access to all stored values
|
|
|
|
/// in a deterministic order. The values are kept in a std::vector and the
|
|
|
|
/// mapping is done with DenseMap from Keys to indexes in that vector.
|
2012-10-09 17:49:42 +00:00
|
|
|
template<typename KeyT, typename ValueT,
|
|
|
|
typename MapType = llvm::DenseMap<KeyT, unsigned>,
|
2012-10-09 17:51:56 +00:00
|
|
|
typename VectorType = std::vector<std::pair<KeyT, ValueT> > >
|
2012-09-18 18:43:21 +00:00
|
|
|
class MapVector {
|
2014-06-20 10:26:56 +00:00
|
|
|
typedef typename VectorType::size_type size_type;
|
2012-09-18 18:43:21 +00:00
|
|
|
|
|
|
|
MapType Map;
|
|
|
|
VectorType Vector;
|
|
|
|
|
|
|
|
public:
|
2012-09-19 13:42:51 +00:00
|
|
|
typedef typename VectorType::iterator iterator;
|
|
|
|
typedef typename VectorType::const_iterator const_iterator;
|
2014-10-27 17:20:53 +00:00
|
|
|
typedef typename VectorType::reverse_iterator reverse_iterator;
|
|
|
|
typedef typename VectorType::const_reverse_iterator const_reverse_iterator;
|
2012-09-18 18:43:21 +00:00
|
|
|
|
2014-10-27 17:20:53 +00:00
|
|
|
size_type size() const { return Vector.size(); }
|
2012-09-18 18:43:21 +00:00
|
|
|
|
2014-10-27 17:20:53 +00:00
|
|
|
iterator begin() { return Vector.begin(); }
|
|
|
|
const_iterator begin() const { return Vector.begin(); }
|
|
|
|
iterator end() { return Vector.end(); }
|
|
|
|
const_iterator end() const { return Vector.end(); }
|
2012-09-18 18:43:21 +00:00
|
|
|
|
2014-10-27 17:20:53 +00:00
|
|
|
reverse_iterator rbegin() { return Vector.rbegin(); }
|
|
|
|
const_reverse_iterator rbegin() const { return Vector.rbegin(); }
|
|
|
|
reverse_iterator rend() { return Vector.rend(); }
|
|
|
|
const_reverse_iterator rend() const { return Vector.rend(); }
|
2012-09-18 18:43:21 +00:00
|
|
|
|
2012-09-19 13:42:51 +00:00
|
|
|
bool empty() const {
|
|
|
|
return Vector.empty();
|
2012-09-18 18:43:21 +00:00
|
|
|
}
|
|
|
|
|
2013-02-18 16:03:04 +00:00
|
|
|
std::pair<KeyT, ValueT> &front() { return Vector.front(); }
|
|
|
|
const std::pair<KeyT, ValueT> &front() const { return Vector.front(); }
|
|
|
|
std::pair<KeyT, ValueT> &back() { return Vector.back(); }
|
|
|
|
const std::pair<KeyT, ValueT> &back() const { return Vector.back(); }
|
|
|
|
|
2012-10-09 17:49:42 +00:00
|
|
|
void clear() {
|
|
|
|
Map.clear();
|
|
|
|
Vector.clear();
|
|
|
|
}
|
|
|
|
|
2015-03-25 00:50:21 +00:00
|
|
|
void swap(MapVector &RHS) {
|
|
|
|
std::swap(Map, RHS.Map);
|
|
|
|
std::swap(Vector, RHS.Vector);
|
|
|
|
}
|
|
|
|
|
2012-09-18 18:43:21 +00:00
|
|
|
ValueT &operator[](const KeyT &Key) {
|
|
|
|
std::pair<KeyT, unsigned> Pair = std::make_pair(Key, 0);
|
|
|
|
std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
|
|
|
|
unsigned &I = Result.first->second;
|
|
|
|
if (Result.second) {
|
2012-09-19 13:42:51 +00:00
|
|
|
Vector.push_back(std::make_pair(Key, ValueT()));
|
2012-09-18 18:43:21 +00:00
|
|
|
I = Vector.size() - 1;
|
|
|
|
}
|
2012-09-19 13:42:51 +00:00
|
|
|
return Vector[I].second;
|
2012-09-18 18:43:21 +00:00
|
|
|
}
|
2012-10-09 23:02:47 +00:00
|
|
|
|
2012-12-19 11:08:33 +00:00
|
|
|
ValueT lookup(const KeyT &Key) const {
|
|
|
|
typename MapType::const_iterator Pos = Map.find(Key);
|
|
|
|
return Pos == Map.end()? ValueT() : Vector[Pos->second].second;
|
|
|
|
}
|
|
|
|
|
2013-01-25 22:11:02 +00:00
|
|
|
std::pair<iterator, bool> insert(const std::pair<KeyT, ValueT> &KV) {
|
|
|
|
std::pair<KeyT, unsigned> Pair = std::make_pair(KV.first, 0);
|
|
|
|
std::pair<typename MapType::iterator, bool> Result = Map.insert(Pair);
|
|
|
|
unsigned &I = Result.first->second;
|
|
|
|
if (Result.second) {
|
|
|
|
Vector.push_back(std::make_pair(KV.first, KV.second));
|
|
|
|
I = Vector.size() - 1;
|
2014-03-02 12:27:27 +00:00
|
|
|
return std::make_pair(std::prev(end()), true);
|
2013-01-25 22:11:02 +00:00
|
|
|
}
|
|
|
|
return std::make_pair(begin() + I, false);
|
|
|
|
}
|
|
|
|
|
2014-06-20 10:26:56 +00:00
|
|
|
size_type count(const KeyT &Key) const {
|
2012-10-09 23:02:47 +00:00
|
|
|
typename MapType::const_iterator Pos = Map.find(Key);
|
|
|
|
return Pos == Map.end()? 0 : 1;
|
|
|
|
}
|
2012-11-15 17:45:50 +00:00
|
|
|
|
|
|
|
iterator find(const KeyT &Key) {
|
|
|
|
typename MapType::const_iterator Pos = Map.find(Key);
|
|
|
|
return Pos == Map.end()? Vector.end() :
|
|
|
|
(Vector.begin() + Pos->second);
|
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator find(const KeyT &Key) const {
|
|
|
|
typename MapType::const_iterator Pos = Map.find(Key);
|
|
|
|
return Pos == Map.end()? Vector.end() :
|
|
|
|
(Vector.begin() + Pos->second);
|
|
|
|
}
|
2013-02-18 16:03:04 +00:00
|
|
|
|
2013-02-19 18:26:07 +00:00
|
|
|
/// \brief Remove the last element from the vector.
|
|
|
|
void pop_back() {
|
|
|
|
typename MapType::iterator Pos = Map.find(Vector.back().first);
|
2013-02-18 16:03:04 +00:00
|
|
|
Map.erase(Pos);
|
2013-02-19 18:26:07 +00:00
|
|
|
Vector.pop_back();
|
2013-02-18 16:03:04 +00:00
|
|
|
}
|
2014-06-19 15:52:37 +00:00
|
|
|
|
|
|
|
/// \brief Remove the element given by Iterator.
|
2014-07-15 18:32:30 +00:00
|
|
|
///
|
2014-06-19 15:52:37 +00:00
|
|
|
/// Returns an iterator to the element following the one which was removed,
|
|
|
|
/// which may be end().
|
2014-07-15 18:32:30 +00:00
|
|
|
///
|
|
|
|
/// \note This is a deceivingly expensive operation (linear time). It's
|
|
|
|
/// usually better to use \a remove_if() if possible.
|
2014-06-19 15:52:37 +00:00
|
|
|
typename VectorType::iterator erase(typename VectorType::iterator Iterator) {
|
2014-07-15 18:32:30 +00:00
|
|
|
Map.erase(Iterator->first);
|
|
|
|
auto Next = Vector.erase(Iterator);
|
|
|
|
if (Next == Vector.end())
|
|
|
|
return Next;
|
|
|
|
|
|
|
|
// Update indices in the map.
|
|
|
|
size_t Index = Next - Vector.begin();
|
|
|
|
for (auto &I : Map) {
|
|
|
|
assert(I.second != Index && "Index was already erased!");
|
|
|
|
if (I.second > Index)
|
|
|
|
--I.second;
|
|
|
|
}
|
|
|
|
return Next;
|
2014-06-19 15:52:37 +00:00
|
|
|
}
|
2014-07-15 20:24:56 +00:00
|
|
|
|
2014-10-07 21:15:51 +00:00
|
|
|
/// \brief Remove all elements with the key value Key.
|
|
|
|
///
|
|
|
|
/// Returns the number of elements removed.
|
|
|
|
size_type erase(const KeyT &Key) {
|
|
|
|
auto Iterator = find(Key);
|
|
|
|
if (Iterator == end())
|
|
|
|
return 0;
|
|
|
|
erase(Iterator);
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2014-07-15 20:24:56 +00:00
|
|
|
/// \brief Remove the elements that match the predicate.
|
|
|
|
///
|
|
|
|
/// Erase all elements that match \c Pred in a single pass. Takes linear
|
|
|
|
/// time.
|
|
|
|
template <class Predicate> void remove_if(Predicate Pred);
|
2012-09-18 18:43:21 +00:00
|
|
|
};
|
|
|
|
|
2014-07-15 20:24:56 +00:00
|
|
|
template <typename KeyT, typename ValueT, typename MapType, typename VectorType>
|
|
|
|
template <class Function>
|
|
|
|
void MapVector<KeyT, ValueT, MapType, VectorType>::remove_if(Function Pred) {
|
|
|
|
auto O = Vector.begin();
|
|
|
|
for (auto I = O, E = Vector.end(); I != E; ++I) {
|
|
|
|
if (Pred(*I)) {
|
|
|
|
// Erase from the map.
|
|
|
|
Map.erase(I->first);
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (I != O) {
|
|
|
|
// Move the value and update the index in the map.
|
|
|
|
*O = std::move(*I);
|
|
|
|
Map[O->first] = O - Vector.begin();
|
|
|
|
}
|
|
|
|
++O;
|
|
|
|
}
|
|
|
|
// Erase trailing entries in the vector.
|
|
|
|
Vector.erase(O, Vector.end());
|
|
|
|
}
|
|
|
|
|
2014-12-31 23:33:21 +00:00
|
|
|
/// \brief A MapVector that performs no allocations if smaller than a certain
|
|
|
|
/// size.
|
|
|
|
template <typename KeyT, typename ValueT, unsigned N>
|
2015-01-05 08:55:19 +00:00
|
|
|
struct SmallMapVector
|
|
|
|
: MapVector<KeyT, ValueT, SmallDenseMap<KeyT, unsigned, N>,
|
|
|
|
SmallVector<std::pair<KeyT, ValueT>, N>> {
|
2015-01-01 13:54:05 +00:00
|
|
|
};
|
2014-12-31 23:33:21 +00:00
|
|
|
|
2014-07-15 18:06:56 +00:00
|
|
|
} // end namespace llvm
|
2012-09-18 18:43:21 +00:00
|
|
|
|
|
|
|
#endif
|