IndexedMap.h 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. #pragma once
  2. #ifdef __GNUC__
  3. #pragma GCC diagnostic push
  4. #pragma GCC diagnostic ignored "-Wunused-parameter"
  5. #endif
  6. //===- llvm/ADT/IndexedMap.h - An index map implementation ------*- C++ -*-===//
  7. //
  8. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  9. // See https://llvm.org/LICENSE.txt for license information.
  10. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  11. //
  12. //===----------------------------------------------------------------------===//
  13. ///
  14. /// \file
  15. /// This file implements an indexed map. The index map template takes two
  16. /// types. The first is the mapped type and the second is a functor
  17. /// that maps its argument to a size_t. On instantiation a "null" value
  18. /// can be provided to be used as a "does not exist" indicator in the
  19. /// map. A member function grow() is provided that given the value of
  20. /// the maximally indexed key (the argument of the functor) makes sure
  21. /// the map has enough space for it.
  22. ///
  23. //===----------------------------------------------------------------------===//
  24. #ifndef LLVM_ADT_INDEXEDMAP_H
  25. #define LLVM_ADT_INDEXEDMAP_H
  26. #include "llvm/ADT/SmallVector.h"
  27. #include "llvm/ADT/STLExtras.h"
  28. #include <cassert>
  29. namespace llvm {
  30. template <typename T, typename ToIndexT = identity<unsigned>>
  31. class IndexedMap {
  32. using IndexT = typename ToIndexT::argument_type;
  33. // Prefer SmallVector with zero inline storage over std::vector. IndexedMaps
  34. // can grow very large and SmallVector grows more efficiently as long as T
  35. // is trivially copyable.
  36. using StorageT = SmallVector<T, 0>;
  37. StorageT storage_;
  38. T nullVal_;
  39. ToIndexT toIndex_;
  40. public:
  41. IndexedMap() : nullVal_(T()) {}
  42. explicit IndexedMap(const T& val) : nullVal_(val) {}
  43. typename StorageT::reference operator[](IndexT n) {
  44. assert(toIndex_(n) < storage_.size() && "index out of bounds!");
  45. return storage_[toIndex_(n)];
  46. }
  47. typename StorageT::const_reference operator[](IndexT n) const {
  48. assert(toIndex_(n) < storage_.size() && "index out of bounds!");
  49. return storage_[toIndex_(n)];
  50. }
  51. void reserve(typename StorageT::size_type s) {
  52. storage_.reserve(s);
  53. }
  54. void resize(typename StorageT::size_type s) {
  55. storage_.resize(s, nullVal_);
  56. }
  57. void clear() {
  58. storage_.clear();
  59. }
  60. void grow(IndexT n) {
  61. unsigned NewSize = toIndex_(n) + 1;
  62. if (NewSize > storage_.size())
  63. resize(NewSize);
  64. }
  65. bool inBounds(IndexT n) const {
  66. return toIndex_(n) < storage_.size();
  67. }
  68. typename StorageT::size_type size() const {
  69. return storage_.size();
  70. }
  71. };
  72. } // end namespace llvm
  73. #endif // LLVM_ADT_INDEXEDMAP_H
  74. #ifdef __GNUC__
  75. #pragma GCC diagnostic pop
  76. #endif