2007-02-01 06:28:10 +01:00
|
|
|
//===- llvm/ADT/IndexedMap.h - An index map implementation ------*- C++ -*-===//
|
|
|
|
//
|
2019-01-19 09:50:56 +01:00
|
|
|
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
|
|
|
|
// See https://llvm.org/LICENSE.txt for license information.
|
|
|
|
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
|
2007-02-01 06:28:10 +01:00
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This file implements an indexed map. The index map template takes two
|
|
|
|
// types. The first is the mapped type and the second is a functor
|
|
|
|
// that maps its argument to a size_t. On instantiation a "null" value
|
|
|
|
// 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.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#ifndef LLVM_ADT_INDEXEDMAP_H
|
|
|
|
#define LLVM_ADT_INDEXEDMAP_H
|
|
|
|
|
2015-02-28 21:15:07 +01:00
|
|
|
#include "llvm/ADT/SmallVector.h"
|
2017-06-14 23:42:24 +02:00
|
|
|
#include "llvm/ADT/STLExtras.h"
|
2009-02-20 23:20:18 +01:00
|
|
|
#include <cassert>
|
2007-02-01 06:28:10 +01:00
|
|
|
|
|
|
|
namespace llvm {
|
|
|
|
|
2017-06-14 23:42:24 +02:00
|
|
|
template <typename T, typename ToIndexT = identity<unsigned>>
|
2007-02-01 06:32:05 +01:00
|
|
|
class IndexedMap {
|
2017-06-14 23:42:24 +02:00
|
|
|
using IndexT = typename ToIndexT::argument_type;
|
2015-03-02 23:20:22 +01: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.
|
2017-06-14 23:42:24 +02:00
|
|
|
using StorageT = SmallVector<T, 0>;
|
|
|
|
|
2007-02-01 06:28:10 +01:00
|
|
|
StorageT storage_;
|
|
|
|
T nullVal_;
|
|
|
|
ToIndexT toIndex_;
|
|
|
|
|
|
|
|
public:
|
2017-06-14 23:42:24 +02:00
|
|
|
IndexedMap() : nullVal_(T()) {}
|
2007-02-01 06:28:10 +01:00
|
|
|
|
2017-06-14 23:42:24 +02:00
|
|
|
explicit IndexedMap(const T& val) : nullVal_(val) {}
|
2007-02-01 06:28:10 +01:00
|
|
|
|
|
|
|
typename StorageT::reference operator[](IndexT n) {
|
|
|
|
assert(toIndex_(n) < storage_.size() && "index out of bounds!");
|
|
|
|
return storage_[toIndex_(n)];
|
|
|
|
}
|
|
|
|
|
|
|
|
typename StorageT::const_reference operator[](IndexT n) const {
|
|
|
|
assert(toIndex_(n) < storage_.size() && "index out of bounds!");
|
|
|
|
return storage_[toIndex_(n)];
|
|
|
|
}
|
|
|
|
|
2011-01-09 04:05:46 +01:00
|
|
|
void reserve(typename StorageT::size_type s) {
|
|
|
|
storage_.reserve(s);
|
|
|
|
}
|
|
|
|
|
2011-01-09 22:58:20 +01:00
|
|
|
void resize(typename StorageT::size_type s) {
|
|
|
|
storage_.resize(s, nullVal_);
|
|
|
|
}
|
|
|
|
|
2007-02-01 06:28:10 +01:00
|
|
|
void clear() {
|
|
|
|
storage_.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
void grow(IndexT n) {
|
|
|
|
unsigned NewSize = toIndex_(n) + 1;
|
|
|
|
if (NewSize > storage_.size())
|
2011-01-09 22:58:20 +01:00
|
|
|
resize(NewSize);
|
2007-02-01 06:28:10 +01:00
|
|
|
}
|
|
|
|
|
2011-01-09 00:10:50 +01:00
|
|
|
bool inBounds(IndexT n) const {
|
|
|
|
return toIndex_(n) < storage_.size();
|
|
|
|
}
|
|
|
|
|
2007-02-01 06:28:10 +01:00
|
|
|
typename StorageT::size_type size() const {
|
|
|
|
return storage_.size();
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2017-06-14 23:42:24 +02:00
|
|
|
} // end namespace llvm
|
2007-02-01 06:28:10 +01:00
|
|
|
|
2017-06-14 23:42:24 +02:00
|
|
|
#endif // LLVM_ADT_INDEXEDMAP_H
|