2012-09-18 18:43:21 +00:00
|
|
|
//===- llvm/ADT/MapVector.h - Map with deterministic value order *- C++ -*-===//
|
|
|
|
//
|
|
|
|
// 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/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
2013-01-25 22:11:02 +00:00
|
|
|
#include "llvm/ADT/STLExtras.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 {
|
|
|
|
typedef typename VectorType::size_type SizeType;
|
|
|
|
|
|
|
|
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;
|
2012-09-18 18:43:21 +00:00
|
|
|
|
|
|
|
SizeType size() const {
|
|
|
|
return Vector.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator begin() {
|
2012-09-19 13:42:51 +00:00
|
|
|
return Vector.begin();
|
2012-09-18 18:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
2012-09-19 13:42:51 +00:00
|
|
|
return Vector.begin();
|
2012-09-18 18:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() {
|
2012-09-19 13:42:51 +00:00
|
|
|
return Vector.end();
|
2012-09-18 18:43:21 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator end() const {
|
|
|
|
return Vector.end();
|
|
|
|
}
|
|
|
|
|
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();
|
|
|
|
}
|
|
|
|
|
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;
|
|
|
|
return std::make_pair(llvm::prior(end()), true);
|
|
|
|
}
|
|
|
|
return std::make_pair(begin() + I, false);
|
|
|
|
}
|
|
|
|
|
2012-10-09 23:02:47 +00:00
|
|
|
unsigned count(const KeyT &Key) const {
|
|
|
|
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
|
|
|
}
|
2012-09-18 18:43:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|