2007-02-01 05:28:10 +00:00
|
|
|
//===- llvm/ADT/IndexedMap.h - An index map implementation ------*- C++ -*-===//
|
2004-09-30 21:39:47 +00:00
|
|
|
//
|
2004-02-25 21:55:45 +00:00
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
2007-12-29 19:59:42 +00:00
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
2004-09-30 21:39:47 +00:00
|
|
|
//
|
2004-02-25 21:55:45 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
2007-02-01 05:28:10 +00:00
|
|
|
// This file implements an indexed map. The index map template takes two
|
2004-02-25 21:55:45 +00:00
|
|
|
// types. The first is the mapped type and the second is a functor
|
2004-02-26 04:07:12 +00:00
|
|
|
// that maps its argument to a size_t. On instantiation a "null" value
|
2004-02-25 21:55:45 +00:00
|
|
|
// can be provided to be used as a "does not exist" indicator in the
|
|
|
|
// map. A member function grow() is provided that given the value of
|
|
|
|
// the maximally indexed key (the argument of the functor) makes sure
|
|
|
|
// the map has enough space for it.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2007-02-01 05:28:10 +00:00
|
|
|
#ifndef LLVM_ADT_INDEXEDMAP_H
|
|
|
|
#define LLVM_ADT_INDEXEDMAP_H
|
2004-02-25 21:55:45 +00:00
|
|
|
|
2009-02-20 22:20:18 +00:00
|
|
|
#include <cassert>
|
2004-09-02 03:24:45 +00:00
|
|
|
#include <functional>
|
2004-02-25 21:55:45 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2009-09-06 08:55:57 +00:00
|
|
|
struct IdentityFunctor : public std::unary_function<unsigned, unsigned> {
|
2004-08-26 03:37:28 +00:00
|
|
|
unsigned operator()(unsigned Index) const {
|
|
|
|
return Index;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
template <typename T, typename ToIndexT = IdentityFunctor>
|
2007-02-01 05:32:05 +00:00
|
|
|
class IndexedMap {
|
2004-02-25 21:55:45 +00:00
|
|
|
typedef typename ToIndexT::argument_type IndexT;
|
|
|
|
typedef std::vector<T> StorageT;
|
|
|
|
StorageT storage_;
|
|
|
|
T nullVal_;
|
|
|
|
ToIndexT toIndex_;
|
|
|
|
|
2004-08-26 03:37:28 +00:00
|
|
|
public:
|
2007-02-01 05:32:05 +00:00
|
|
|
IndexedMap() : nullVal_(T()) { }
|
2004-02-25 21:55:45 +00:00
|
|
|
|
2007-02-01 05:32:05 +00:00
|
|
|
explicit IndexedMap(const T& val) : nullVal_(val) { }
|
2004-02-25 21:55:45 +00:00
|
|
|
|
|
|
|
typename StorageT::reference operator[](IndexT n) {
|
2004-08-26 03:37:28 +00:00
|
|
|
assert(toIndex_(n) < storage_.size() && "index out of bounds!");
|
|
|
|
return storage_[toIndex_(n)];
|
2004-02-25 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
typename StorageT::const_reference operator[](IndexT n) const {
|
2004-08-26 03:37:28 +00:00
|
|
|
assert(toIndex_(n) < storage_.size() && "index out of bounds!");
|
|
|
|
return storage_[toIndex_(n)];
|
2004-02-25 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
2011-01-09 03:05:46 +00:00
|
|
|
void reserve(typename StorageT::size_type s) {
|
|
|
|
storage_.reserve(s);
|
|
|
|
}
|
|
|
|
|
2011-01-09 21:58:20 +00:00
|
|
|
void resize(typename StorageT::size_type s) {
|
|
|
|
storage_.resize(s, nullVal_);
|
|
|
|
}
|
|
|
|
|
2004-02-25 21:55:45 +00:00
|
|
|
void clear() {
|
2004-08-26 03:37:28 +00:00
|
|
|
storage_.clear();
|
2004-02-25 21:55:45 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void grow(IndexT n) {
|
2004-08-26 03:37:28 +00:00
|
|
|
unsigned NewSize = toIndex_(n) + 1;
|
|
|
|
if (NewSize > storage_.size())
|
2011-01-09 21:58:20 +00:00
|
|
|
resize(NewSize);
|
2004-02-25 21:55:45 +00:00
|
|
|
}
|
2004-08-27 03:58:31 +00:00
|
|
|
|
2011-01-08 23:10:50 +00:00
|
|
|
bool inBounds(IndexT n) const {
|
|
|
|
return toIndex_(n) < storage_.size();
|
|
|
|
}
|
|
|
|
|
2004-08-27 03:58:31 +00:00
|
|
|
typename StorageT::size_type size() const {
|
|
|
|
return storage_.size();
|
|
|
|
}
|
2004-08-26 03:37:28 +00:00
|
|
|
};
|
2004-02-25 21:55:45 +00:00
|
|
|
|
|
|
|
} // End llvm namespace
|
|
|
|
|
|
|
|
#endif
|