Maze Builder Docs 8.10.23
Loading...
Searching...
No Matches
cell_pool.h
Go to the documentation of this file.
1#ifndef CELL_POOL_H
2#define CELL_POOL_H
3
4#include <cstddef>
5#include <cstdint>
6#include <memory>
7#include <span>
8#include <unordered_map>
9#include <vector>
10
13namespace mazes
14{
15 class cell;
16
19 class cell_pool final
20 {
21 public:
22 cell_pool() = default;
23 cell_pool(const cell_pool &) = delete;
24 cell_pool &operator=(const cell_pool &) = delete;
25
29 void acquire(std::uint8_t rows, std::uint8_t cols, std::uint8_t levels);
30
32 void release() noexcept;
33
35 [[nodiscard]] std::span<const std::shared_ptr<cell>> view() const noexcept;
36
38 [[nodiscard]] std::shared_ptr<cell> at(int index) const noexcept;
39
41 [[nodiscard]] int index_of(const cell *c) const noexcept;
42
43 [[nodiscard]] std::size_t size() const noexcept { return m_active; }
44
45 [[nodiscard]] std::size_t capacity() const noexcept { return m_cells.size(); }
46
47 private:
48 std::vector<std::shared_ptr<cell>> m_cells;
49 std::unordered_map<const cell *, int> m_indices;
50 std::size_t m_active{0};
51 };
52} // namespace mazes
53
54#endif // CELL_POOL_H
std::span< const std::shared_ptr< cell > > view() const noexcept
Active cells in index order (level-major, then row, then column).
void release() noexcept
Unlink all active cells and deactivate them, keeping the allocated cells for reuse.
int index_of(const cell *c) const noexcept
Linear index of an active cell, or -1.
void acquire(std::uint8_t rows, std::uint8_t cols, std::uint8_t levels)
Prepare rows * cols * levels cells, allocating only what the pool is missing. Every active cell is un...
std::shared_ptr< cell > at(int index) const noexcept
Active cell at a linear index, or nullptr.
Cell class with links to other cells.
Definition cell.h:18