fixed_size_value_store.h 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Part of the Carbon Language project, under the Apache License v2.0 with LLVM
  2. // Exceptions. See /LICENSE for license information.
  3. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  4. #ifndef CARBON_TOOLCHAIN_BASE_FIXED_SIZE_VALUE_STORE_H_
  5. #define CARBON_TOOLCHAIN_BASE_FIXED_SIZE_VALUE_STORE_H_
  6. #include "common/check.h"
  7. #include "common/move_only.h"
  8. #include "llvm/ADT/SmallVector.h"
  9. #include "llvm/ADT/StringRef.h"
  10. #include "toolchain/base/mem_usage.h"
  11. #include "toolchain/base/value_store.h"
  12. #include "toolchain/base/value_store_chunk.h"
  13. namespace Carbon {
  14. // A value store with a predetermined size.
  15. template <typename IdT, typename ValueT>
  16. class FixedSizeValueStore {
  17. public:
  18. using IdType = IdT;
  19. using ValueType = ValueStoreTypes<IdT, ValueT>::ValueType;
  20. using RefType = ValueStoreTypes<IdT, ValueT>::RefType;
  21. using ConstRefType = ValueStoreTypes<IdT, ValueT>::ConstRefType;
  22. // Makes a ValueStore of the specified size, but without initializing values.
  23. // Entries must be set before reading.
  24. static auto MakeForOverwriteWithExplicitSize(size_t size)
  25. -> FixedSizeValueStore {
  26. FixedSizeValueStore store;
  27. store.values_.resize_for_overwrite(size);
  28. return store;
  29. }
  30. // Makes a ValueStore of the same size as a source `ValueStoreT`, but without
  31. // initializing values. Entries must be set before reading.
  32. template <typename ValueStoreT>
  33. requires std::same_as<IdT, typename ValueStoreT::IdType>
  34. static auto MakeForOverwrite(const ValueStoreT& size_source)
  35. -> FixedSizeValueStore {
  36. FixedSizeValueStore store;
  37. store.values_.resize_for_overwrite(size_source.size());
  38. return store;
  39. }
  40. // Makes a ValueStore of the specified size, initialized to a default.
  41. static auto MakeWithExplicitSize(size_t size, ValueT default_value)
  42. -> FixedSizeValueStore {
  43. FixedSizeValueStore store;
  44. store.values_.resize(size, default_value);
  45. return store;
  46. }
  47. // Makes a ValueStore of the same size as a source `ValueStoreT`. This is
  48. // the safest constructor to use, since it ensures everything's initialized to
  49. // a default, and verifies a matching `IdT` for the size.
  50. template <typename ValueStoreT>
  51. requires std::same_as<IdT, typename ValueStoreT::IdType>
  52. explicit FixedSizeValueStore(const ValueStoreT& size_source,
  53. ValueT default_value) {
  54. values_.resize(size_source.size(), default_value);
  55. }
  56. // Move-only.
  57. FixedSizeValueStore(FixedSizeValueStore&&) noexcept = default;
  58. auto operator=(FixedSizeValueStore&&) noexcept
  59. -> FixedSizeValueStore& = default;
  60. // Sets the value for an ID.
  61. auto Set(IdT id, ValueType value) -> void {
  62. CARBON_DCHECK(id.index >= 0, "{0}", id);
  63. values_[id.index] = value;
  64. }
  65. // Returns a mutable value for an ID.
  66. auto Get(IdT id) -> RefType {
  67. CARBON_DCHECK(id.index >= 0, "{0}", id);
  68. return values_[id.index];
  69. }
  70. // Returns the value for an ID.
  71. auto Get(IdT id) const -> ConstRefType {
  72. CARBON_DCHECK(id.index >= 0, "{0}", id);
  73. return values_[id.index];
  74. }
  75. // Collects memory usage of the values.
  76. auto CollectMemUsage(MemUsage& mem_usage, llvm::StringRef label) const
  77. -> void {
  78. mem_usage.Collect(label.str(), values_);
  79. }
  80. auto size() const -> size_t { return values_.size(); }
  81. auto values() -> auto {
  82. return llvm::make_range(values_.begin(), values_.end());
  83. }
  84. private:
  85. // Allow default construction for `Make` functions.
  86. FixedSizeValueStore() = default;
  87. // Storage for the `ValueT` objects, indexed by the id.
  88. llvm::SmallVector<ValueT, 0> values_;
  89. };
  90. } // namespace Carbon
  91. #endif // CARBON_TOOLCHAIN_BASE_FIXED_SIZE_VALUE_STORE_H_