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/*
13 * NOTE: DO NOT EDIT THIS FILE DIRECTLY. It is AUTO-GENERATED from config_schema.yaml.
14 * To add new config values or make other changes, edit config_schema.yaml and regenerate via:
15 *
16 * uv run scripts/generate_config.py
17 */
18
19namespace porytiles {
20
30enum class SearchAlgorithm {
34 dfs,
38 bfs
39};
40
56[[nodiscard]] inline std::optional<SearchAlgorithm> search_algorithm_from_str(const std::string &str)
57{
58 // Phase 1: Exact match against C++ constant names
59 if (str == "dfs") {
60 return std::optional{ SearchAlgorithm::dfs };
61 }
62 if (str == "bfs") {
63 return std::optional{ SearchAlgorithm::bfs };
64 }
65
66 // Phase 2: Case-insensitive fuzzy match
67 std::string lower_str = str;
68 std::ranges::transform(
69 lower_str, lower_str.begin(), [](unsigned char c) { return static_cast<char>(std::tolower(c)); });
70
71 // Fuzzy names for dfs
72 if (lower_str == "dfs") {
73 return std::optional{ SearchAlgorithm::dfs };
74 }
75 if (lower_str == "depth-first") {
76 return std::optional{ SearchAlgorithm::dfs };
77 }
78 if (lower_str == "depth_first") {
79 return std::optional{ SearchAlgorithm::dfs };
80 }
81 if (lower_str == "depthfirst") {
82 return std::optional{ SearchAlgorithm::dfs };
83 }
84 // Fuzzy names for bfs
85 if (lower_str == "bfs") {
86 return std::optional{ SearchAlgorithm::bfs };
87 }
88 if (lower_str == "breadth-first") {
89 return std::optional{ SearchAlgorithm::bfs };
90 }
91 if (lower_str == "breadth_first") {
92 return std::optional{ SearchAlgorithm::bfs };
93 }
94 if (lower_str == "breadthfirst") {
95 return std::optional{ SearchAlgorithm::bfs };
96 }
97
98 return std::nullopt;
99}
100
111[[nodiscard]] inline std::string to_string(const SearchAlgorithm m)
112{
113 switch (m) {
115 return "dfs";
117 return "bfs";
118 }
119 panic("unhandled SearchAlgorithm value");
120}
121
129inline std::ostream &operator<<(std::ostream &os, const SearchAlgorithm m)
130{
131 return os << to_string(m);
132}
133
134} // namespace porytiles
135
136template <>
137struct std::formatter<porytiles::SearchAlgorithm> {
138 constexpr auto parse(std::format_parse_context &ctx)
139 {
140 return ctx.begin();
141 }
142
143 auto format(const porytiles::SearchAlgorithm &value, auto &ctx) const
144 {
145 return std::format_to(ctx.out(), "{}", porytiles::to_string(value));
146 }
147};
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