./
Coverage:
low: ≥ 0%
medium: ≥ 75.0%
high: ≥ 90.0%
Lines:
3 / 3
100.0%
Branches:
4 / 6
66.7%
Functions:
-
-%

src/layers/craters/generation/cell_cache.h
Line Branch Exec Source
1 // SPDX-License-Identifier: BSD-3-Clause
2
3 /// @file
4 /// @brief `CraterCellCache`, the craters of generated cells kept for reuse.
5 #pragma once
6
7 #include "terraingen/layers/craters/generation/population.h"
8 #include "terraingen/util/lru_cache.h"
9
10 #include <cstddef>
11 #include <cstdint>
12 #include <functional>
13 #include <memory>
14
15 namespace terraingen {
16
17 /// The craters drawn in each cell of each band, kept so tiles and point queries generate
18 /// each cell once.
19 ///
20 /// Holds about `maxCraters` craters at most, evicting least recently used cells; thread-safe.
21 /// Each thread also keeps the last cells it read in a small table of its own, so repeated point
22 /// queries skip the shared cache. Cells regenerate identically, so eviction never changes results.
23 1308 class CraterCellCache {
24 public:
25 /// @param maxCraters Most craters held across every cell; positive.
26 /// @throws std::invalid_argument if `maxCraters` is zero.
27 explicit CraterCellCache(std::size_t maxCraters = 4000000);
28
29 /// The craters of `cell` in the band of `exponent`, drawn by `generate` when not held.
30 std::shared_ptr<const CellCraters> Get(int exponent, const CellIndex& cell,
31 const std::function<CellCraters()>& generate);
32
33 private:
34 struct Key {
35 int exponent;
36 std::int32_t i;
37 std::int32_t j;
38
39 90132 bool operator==(const Key& other) const {
40
4/6
✓ Branch 0 taken 90134 times.
✗ Branch 1 not taken.
✓ Branch 2 taken 90134 times.
✗ Branch 3 not taken.
✓ Branch 4 taken 2 times.
✓ Branch 5 taken 90132 times.
90132 return exponent == other.exponent && i == other.i && j == other.j;
41 }
42 bool operator!=(const Key& other) const {
43 return !(*this == other);
44 }
45 };
46 struct KeyHash {
47 std::size_t operator()(const Key& key) const;
48 };
49
50 LruCache<Key, CellCraters, KeyHash> m_cache;
51 /// This cache among every one made, which the threads' tables of recent cells key on.
52 std::uint64_t m_id;
53 };
54
55 /// A source for `CraterGenerationOptions::cellCraters` that draws each cell with
56 /// `GenerateCellCraters` once and keeps it in `cache`; every argument must outlive it.
57 CellCraterSource CachedCellCraters(CraterCellCache& cache, const SurfaceCells& cells,
58 const SizeFrequencyDistribution& density,
59 const CraterAgeModel& ages, std::uint32_t seed);
60
61 /// `GenerateCraters` drawing each cell through `cache` (`CachedCellCraters`), or as `options`
62 /// say when `cache` is null.
63 /// @throws std::invalid_argument as `GenerateCraters`.
64 /// @throws std::domain_error as `GenerateCraters`.
65 CraterList GenerateCachedCraters(CraterCellCache* cache, const SurfaceCells& cells,
66 const Bounds2d& region, const SizeFrequencyDistribution& density,
67 const CraterAgeModel& ages, std::uint32_t seed,
68 CraterGenerationOptions options);
69
70 } // namespace terraingen
71