1 //===- llvm/ADT/DenseMap.h - A dense map implmentation ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a dense map. A dense map template takes two
11 // types. The first is the mapped type and the second is a functor
12 // that maps its argument to a size_t. On instantiation a "null" value
13 // can be provided to be used as a "does not exist" indicator in the
14 // map. A member function grow() is provided that given the value of
15 // the maximally indexed key (the argument of the functor) makes sure
16 // the map has enough space for it.
18 //===----------------------------------------------------------------------===//
20 #ifndef LLVM_ADT_DENSEMAP_H
21 #define LLVM_ADT_DENSEMAP_H
29 struct IdentityFunctor : std::unary_function<unsigned, unsigned> {
30 unsigned operator()(unsigned Index) const {
35 template <typename T, typename ToIndexT = IdentityFunctor>
37 typedef typename ToIndexT::argument_type IndexT;
38 typedef std::vector<T> StorageT;
44 DenseMap() : nullVal_(T()) { }
46 explicit DenseMap(const T& val) : nullVal_(val) { }
48 typename StorageT::reference operator[](IndexT n) {
49 assert(toIndex_(n) < storage_.size() && "index out of bounds!");
50 return storage_[toIndex_(n)];
53 typename StorageT::const_reference operator[](IndexT n) const {
54 assert(toIndex_(n) < storage_.size() && "index out of bounds!");
55 return storage_[toIndex_(n)];
63 unsigned NewSize = toIndex_(n) + 1;
64 if (NewSize > storage_.size())
65 storage_.resize(NewSize, nullVal_);
68 typename StorageT::size_type size() const {
69 return storage_.size();
73 } // End llvm namespace