Porytiles
Loading...
Searching...
No Matches
search_algorithm.hpp
Go to the documentation of this file.
1#pragma once
2
3#include <algorithm>
4#include <cctype>
5#include <format>
6#include <optional>
7#include <ostream>
8#include <string>
9
11
12// NOTE: DO NOT EDIT THIS FILE DIRECTLY. It is AUTO-GENERATED from config_schema.yaml.
13// To add new config values or make other changes, edit config_schema.yaml and regenerate via:
14//
15// uv run scripts/generate_config.py
16
17namespace porytiles {
18
26enum class SearchAlgorithm {
28 dfs,
30 bfs
31};
32
46[[nodiscard]] inline std::optional<SearchAlgorithm> search_algorithm_from_str(const std::string &str)
47{
48 // Phase 1: Exact match against C++ constant names
49 if (str == "dfs") {
50 return std::optional{SearchAlgorithm::dfs};
51 }
52 if (str == "bfs") {
53 return std::optional{SearchAlgorithm::bfs};
54 }
55
56 // Phase 2: Case-insensitive fuzzy match
57 std::string lower_str = str;
58 std::ranges::transform(
59 lower_str, lower_str.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
60
61 // Fuzzy names for dfs
62 if (lower_str == "dfs") {
63 return std::optional{SearchAlgorithm::dfs};
64 }
65 if (lower_str == "depth-first") {
66 return std::optional{SearchAlgorithm::dfs};
67 }
68 if (lower_str == "depth_first") {
69 return std::optional{SearchAlgorithm::dfs};
70 }
71 if (lower_str == "depthfirst") {
72 return std::optional{SearchAlgorithm::dfs};
73 }
74 // Fuzzy names for bfs
75 if (lower_str == "bfs") {
76 return std::optional{SearchAlgorithm::bfs};
77 }
78 if (lower_str == "breadth-first") {
79 return std::optional{SearchAlgorithm::bfs};
80 }
81 if (lower_str == "breadth_first") {
82 return std::optional{SearchAlgorithm::bfs};
83 }
84 if (lower_str == "breadthfirst") {
85 return std::optional{SearchAlgorithm::bfs};
86 }
87
88 return std::nullopt;
89}
90
99[[nodiscard]] inline std::string to_string(const SearchAlgorithm m)
100{
101 switch (m) {
103 return "dfs";
105 return "bfs";
106 }
107 panic("unhandled SearchAlgorithm value");
108}
109
115inline std::ostream &operator<<(std::ostream &os, const SearchAlgorithm m)
116{
117 return os << to_string(m);
118}
119
120} // namespace porytiles
121
122template <>
123struct std::formatter<porytiles::SearchAlgorithm> {
124 constexpr auto parse(std::format_parse_context &ctx)
125 {
126 return ctx.begin();
127 }
128
129 auto format(const porytiles::SearchAlgorithm &value, auto &ctx) const
130 {
131 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
132 }
133};
void panic(const StringViewSourceLoc &s)
Unconditionally terminates the program with a panic message.
Definition panic.cpp:43
std::ostream & operator<<(std::ostream &os, const PrimaryPairingMode m)
Stream insertion operator for PrimaryPairingMode.
SearchAlgorithm
Search algorithm used by BacktrackingStrategy.
@ dfs
Depth-first search with in-place mutation and undo.
@ bfs
Breadth-first search with dual-queue heuristic and visited-state deduplication.
std::string to_string(const PrimaryPairingMode m)
Converts a PrimaryPairingMode to its canonical string representation.
std::optional< SearchAlgorithm > search_algorithm_from_str(const std::string &str)
Parses a string into a SearchAlgorithm with fuzzy matching.
constexpr auto parse(std::format_parse_context &ctx)
auto format(const porytiles::SearchAlgorithm &value, auto &ctx) const