|
| 1 | +/*license*/ |
| 2 | +#pragma once |
| 3 | +#include <cstddef> |
| 4 | +#include <iterator> |
| 5 | +#include <type_traits> |
| 6 | +#include <memory> |
| 7 | +#include <vector> |
| 8 | + |
| 9 | +// 要素のアドレスが動かない可変長配列。 |
| 10 | +// |
| 11 | +// std::vector だと push_back の再確保で既存要素が移動するため、 |
| 12 | +// x->vec.push_back(f()); // f() が同じプールに確保すると x->vec が無効化される |
| 13 | +// のような式が静かに壊れる。Ref は -> のたびに引き直すが、1 つの式の中では |
| 14 | +// 引き直さないので防げない。ここは固定長ブロックを継ぎ足すだけなので要素は動かない。 |
| 15 | +// |
| 16 | +// std::deque でも規格上は参照が保たれるが、MSVC は 8 バイトを超える型で |
| 17 | +// 1 ブロック 1 要素になり (deque の _Block_size)、ノード 1 個ごとに確保が走る。 |
| 18 | +// 既定のブロック要素数。計測して決める (build.py --chunk で上書きできる)。 |
| 19 | +#ifndef NAST_POOL_CHUNK |
| 20 | +#define NAST_POOL_CHUNK 8 |
| 21 | +#endif |
| 22 | + |
| 23 | +namespace brgen::nast { |
| 24 | + |
| 25 | + template <class T, std::size_t ChunkSize = NAST_POOL_CHUNK> |
| 26 | + struct StablePool { |
| 27 | + using value_type = T; |
| 28 | + static constexpr std::size_t chunk_size = ChunkSize; |
| 29 | + |
| 30 | + private: |
| 31 | + std::vector<std::unique_ptr<T[]>> chunks_; |
| 32 | + std::size_t size_ = 0; |
| 33 | + |
| 34 | + public: |
| 35 | + constexpr std::size_t size() const { |
| 36 | + return size_; |
| 37 | + } |
| 38 | + |
| 39 | + constexpr std::size_t capacity() const { |
| 40 | + return chunks_.size() * ChunkSize; |
| 41 | + } |
| 42 | + |
| 43 | + constexpr bool empty() const { |
| 44 | + return size_ == 0; |
| 45 | + } |
| 46 | + |
| 47 | + constexpr T& operator[](std::size_t i) { |
| 48 | + return chunks_[i / ChunkSize][i % ChunkSize]; |
| 49 | + } |
| 50 | + |
| 51 | + constexpr const T& operator[](std::size_t i) const { |
| 52 | + return chunks_[i / ChunkSize][i % ChunkSize]; |
| 53 | + } |
| 54 | + |
| 55 | + void push_back(T v) { |
| 56 | + if (size_ == capacity()) { |
| 57 | + chunks_.push_back(std::make_unique<T[]>(ChunkSize)); |
| 58 | + } |
| 59 | + (*this)[size_] = std::move(v); |
| 60 | + size_++; |
| 61 | + } |
| 62 | + |
| 63 | + void clear() { |
| 64 | + chunks_.clear(); |
| 65 | + size_ = 0; |
| 66 | + } |
| 67 | + |
| 68 | + void resize(std::size_t n) { |
| 69 | + while (size_ < n) { |
| 70 | + push_back(T{}); |
| 71 | + } |
| 72 | + // 縮小はブロックを返さない。要素のアドレスを動かさないため。 |
| 73 | + size_ = n; |
| 74 | + } |
| 75 | + |
| 76 | + // as_json が futils の Stringer に渡すので std::ranges::range を満たす必要がある。 |
| 77 | + // weakly_incrementable が difference_type と後置 ++ を要求する。 |
| 78 | + template <class P, class V> |
| 79 | + struct iter { |
| 80 | + using value_type = std::remove_const_t<V>; |
| 81 | + using difference_type = std::ptrdiff_t; |
| 82 | + |
| 83 | + P* pool = nullptr; |
| 84 | + std::size_t i = 0; |
| 85 | + |
| 86 | + constexpr V& operator*() const { |
| 87 | + return (*pool)[i]; |
| 88 | + } |
| 89 | + |
| 90 | + constexpr iter& operator++() { |
| 91 | + i++; |
| 92 | + return *this; |
| 93 | + } |
| 94 | + |
| 95 | + constexpr iter operator++(int) { |
| 96 | + auto copy = *this; |
| 97 | + i++; |
| 98 | + return copy; |
| 99 | + } |
| 100 | + |
| 101 | + constexpr bool operator!=(const iter& o) const { |
| 102 | + return i != o.i; |
| 103 | + } |
| 104 | + |
| 105 | + constexpr bool operator==(const iter& o) const { |
| 106 | + return i == o.i; |
| 107 | + } |
| 108 | + }; |
| 109 | + |
| 110 | + constexpr auto begin() { |
| 111 | + return iter<StablePool, T>{this, 0}; |
| 112 | + } |
| 113 | + |
| 114 | + constexpr auto end() { |
| 115 | + return iter<StablePool, T>{this, size_}; |
| 116 | + } |
| 117 | + |
| 118 | + constexpr auto begin() const { |
| 119 | + return iter<const StablePool, const T>{this, 0}; |
| 120 | + } |
| 121 | + |
| 122 | + constexpr auto end() const { |
| 123 | + return iter<const StablePool, const T>{this, size_}; |
| 124 | + } |
| 125 | + }; |
| 126 | + |
| 127 | +} // namespace brgen::nast |
0 commit comments