2012-09-18 20:43:21 +02: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 15:42:51 +02: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 20:43:21 +02:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_MAPVECTOR_H
|
|
|
|
#define LLVM_ADT_MAPVECTOR_H
|
|
|
|
|
|
|
|
#include "llvm/ADT/ArrayRef.h"
|
|
|
|
#include "llvm/ADT/DenseMap.h"
|
|
|
|
#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.
|
|
|
|
template<typename KeyT, typename ValueT>
|
|
|
|
class MapVector {
|
|
|
|
typedef llvm::DenseMap<KeyT, unsigned> MapType;
|
2012-09-19 15:42:51 +02:00
|
|
|
typedef std::vector<std::pair<KeyT, ValueT> > VectorType;
|
2012-09-18 20:43:21 +02:00
|
|
|
typedef typename VectorType::size_type SizeType;
|
|
|
|
|
|
|
|
MapType Map;
|
|
|
|
VectorType Vector;
|
|
|
|
|
|
|
|
public:
|
2012-09-19 15:42:51 +02:00
|
|
|
typedef typename VectorType::iterator iterator;
|
|
|
|
typedef typename VectorType::const_iterator const_iterator;
|
2012-09-18 20:43:21 +02:00
|
|
|
|
|
|
|
SizeType size() const {
|
|
|
|
return Vector.size();
|
|
|
|
}
|
|
|
|
|
|
|
|
iterator begin() {
|
2012-09-19 15:42:51 +02:00
|
|
|
return Vector.begin();
|
2012-09-18 20:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator begin() const {
|
2012-09-19 15:42:51 +02:00
|
|
|
return Vector.begin();
|
2012-09-18 20:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iterator end() {
|
2012-09-19 15:42:51 +02:00
|
|
|
return Vector.end();
|
2012-09-18 20:43:21 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const_iterator end() const {
|
|
|
|
return Vector.end();
|
|
|
|
}
|
|
|
|
|
2012-09-19 15:42:51 +02:00
|
|
|
bool empty() const {
|
|
|
|
return Vector.empty();
|
2012-09-18 20:43:21 +02: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 15:42:51 +02:00
|
|
|
Vector.push_back(std::make_pair(Key, ValueT()));
|
2012-09-18 20:43:21 +02:00
|
|
|
I = Vector.size() - 1;
|
|
|
|
}
|
2012-09-19 15:42:51 +02:00
|
|
|
return Vector[I].second;
|
2012-09-18 20:43:21 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
#endif
|