Porytiles
Loading...
Searching...
No Matches
packed_palette.cpp
Go to the documentation of this file.
2
3#include <algorithm>
4#include <ranges>
5
7
8namespace porytiles {
9
10PackedPalette::PackedPalette(std::size_t hardware_index, std::size_t capacity)
11 : hardware_index_{hardware_index}, capacity_{capacity}, color_set_{}, assigned_tile_ids_{},
12 cached_per_color_multiplicity_{}
13{
14}
15
16std::size_t PackedPalette::color_count() const
17{
18 return color_set_count(color_set_);
19}
20
22{
23 const std::size_t count = color_count();
24 return count >= capacity_ ? 0 : capacity_ - count;
25}
26
27bool PackedPalette::can_fit(const ColorSet &tile_colors) const
28{
29 return union_size(tile_colors) <= capacity_;
30}
31
32std::size_t PackedPalette::union_size(const ColorSet &tile_colors) const
33{
34 ColorSet combined = color_set_union(color_set_, tile_colors);
35 return color_set_count(combined);
36}
37
39{
40 assigned_tile_ids_.push_back(tile.id());
41 tile_colors_[tile.id()] = tile.color_set();
42 color_set_ = color_set_union(color_set_, tile.color_set());
43
44 // Incrementally update color counts for O(1) multiplicity lookups
45 for_each_color(tile.color_set(), [this](std::size_t color_idx) { cached_per_color_multiplicity_[color_idx]++; });
46}
47
49{
50 remove_tile(tile.id());
51}
52
54{
55 // Decrement color counts before removing the tile's colors
56 if (const auto it = tile_colors_.find(tile_id); it != tile_colors_.end()) {
57 for_each_color(it->second, [this](std::size_t color_idx) { cached_per_color_multiplicity_[color_idx]--; });
58 }
59
60 auto iter = std::ranges::find(assigned_tile_ids_, tile_id);
61 if (iter != assigned_tile_ids_.end()) {
62 assigned_tile_ids_.erase(iter);
63 }
64 tile_colors_.erase(tile_id);
65
66 // Rebuild color_set_ from remaining tiles
67 color_set_ = ColorSet{};
68 for (const auto &colors : tile_colors_ | std::views::values) {
69 color_set_ = color_set_union(color_set_, colors);
70 }
71}
72
73} // namespace porytiles
A set of colors represented as a bitset.
Definition color_set.hpp:26
Wraps a ColorSet with a tile ID for tracking during palette packing.
const ColorSet & color_set() const
const Id & id() const
std::variant< HintId, PrefilledPaletteId, RegularId, AnimId, PrimaryTileId > Id
Variant type for tile identification.
void remove_tile(const PackableTile &tile)
Removes a tile from this palette and recalculates the color set.
std::size_t union_size(const ColorSet &tile_colors) const
Computes the size of the union of this palette's colors with the given colors.
bool can_fit(const ColorSet &tile_colors) const
Checks if a tile's colors can fit in this palette.
void add_tile(const PackableTile &tile)
Adds a tile to this palette.
std::size_t remaining_capacity() const
PackedPalette(std::size_t hardware_index, std::size_t capacity=pal::max_size - 1)
Constructs a PackedPalette with a given index and capacity.
std::size_t color_count() const
void for_each_color(const ColorSet &set, Func &&func)
Iterates over each color index in a ColorSet.
ColorSet color_set_union(const ColorSet &a, const ColorSet &b)
Computes the union of two ColorSets.
Definition color_set.cpp:20
std::size_t color_set_count(const ColorSet &set)
Counts the number of colors in a ColorSet.
Definition color_set.cpp:44