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
|
|
|
|
2012-04-20 20:05:28 +00:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2015-02-28 20:15:07 +00:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
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
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2012-04-20 20:05:28 +00:00
|
|
|
template <typename T, typename ToIndexT = llvm::identity<unsigned> >
|
2007-02-01 05:32:05 +00:00
|
|
|
class IndexedMap {
|
2004-02-25 21:55:45 +00:00
|
|
|
typedef typename ToIndexT::argument_type IndexT;
|
2015-03-02 22:20:22 +00:00
|
|
|
// Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
|
|
|
|
// can grow very large and SmallVector grows more efficiently as long as T
|
|
|
|
// is trivially copyable.
|
2015-02-28 20:15:07 +00:00
|
|
|
typedef SmallVector<T, 0> StorageT;
|
2004-02-25 21:55:45 +00:00
|
|
|
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
|