Maze Builder Docs 8.2.1
Loading...
Searching...
No Matches
algos.h
Go to the documentation of this file.
1#ifndef ALGOS_H
2#define ALGOS_H
3
4#include <algorithm>
5#include <array>
6#include <ranges>
7#include <stdexcept>
8#include <string>
9#include <string_view>
10#include <type_traits>
11#include <utility>
12
15namespace mazes
16{
18 enum class algo : unsigned int
19 {
20 BINARY_TREE = 0,
21 SIDEWINDER = 1,
22 DFS = 2,
23 PIXELS = 3,
24 STRINGIFY = 4,
25 WAVEFRONT_OBJECT = 5,
26 TOTAL = 6
27 };
28
30 constexpr std::array<std::string_view, static_cast<size_t>(algo::TOTAL)> ALGOS_LABELS_LOWERCASE = {
31 "binary_tree",
32 "sidewinder",
33 "dfs",
34 "pixels",
35 "stringify",
36 "wavefront_object"
37 };
38
42 inline std::string_view to_sv_from_algo(algo a)
43 {
44 if (a == algo::TOTAL)
45 {
46 throw std::invalid_argument("Invalid algo: " + std::to_string(static_cast<unsigned int>(a)));
47 }
48 return ALGOS_LABELS_LOWERCASE.at(static_cast<size_t>(a));
49 }
50
54 inline algo to_algo_from_sv(std::string_view a)
55 {
56 if (const auto it = std::ranges::find(ALGOS_LABELS_LOWERCASE, a); it != ALGOS_LABELS_LOWERCASE.end())
57 {
58 return static_cast<algo>(std::distance(ALGOS_LABELS_LOWERCASE.begin(), it));
59 }
60 throw std::invalid_argument("Invalid algo: " + std::string{a});
61 }
62} // namespace mazes
63
64#endif // ALGOS_H
constexpr std::array< std::string_view, static_cast< size_t >(algo::TOTAL)> ALGOS_LABELS_LOWERCASE
Array of string_view labels for the algo enum, in lowercase.
Definition algos.h:30
algo
Enumeration of maze generation algorithms.
Definition algos.h:19
algo to_algo_from_sv(std::string_view a)
Convert a string_view to an algo enum.
Definition algos.h:54
std::string_view to_sv_from_algo(algo a)
Convert the algo enum to a string_view.
Definition algos.h:42