Maze Builder Docs 8.10.23
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#include <vector>
13
16namespace mazes
17{
19 enum class algo : unsigned int
20 {
21 BINARY_TREE = 0,
22 SIDEWINDER = 1,
23 DFS = 2,
24 PRIMS = 3,
25 TOTAL = 4
26 };
27
29 constexpr std::array<std::string_view, static_cast<size_t>(algo::TOTAL)> ALGOS_LABELS_LOWERCASE = {
30 "binary_tree",
31 "sidewinder",
32 "dfs",
33 "prims"};
34
38 inline std::string_view to_sv_from_algo(algo a)
39 {
40 if (a == algo::TOTAL)
41 {
42 throw std::invalid_argument("Invalid algo: " + std::to_string(static_cast<unsigned int>(a)));
43 }
44 return ALGOS_LABELS_LOWERCASE.at(static_cast<size_t>(a));
45 }
46
50 inline algo to_algo_from_sv(std::string_view a)
51 {
52 if (const auto it = std::ranges::find(ALGOS_LABELS_LOWERCASE, a); it != ALGOS_LABELS_LOWERCASE.end())
53 {
54 return static_cast<algo>(std::distance(ALGOS_LABELS_LOWERCASE.cbegin(), it));
55 }
56 throw std::invalid_argument("Invalid algo: " + std::string{a});
57 }
58} // namespace mazes
59
60#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:29
algo
Enumeration of maze generation algorithms.
Definition algos.h:20
algo to_algo_from_sv(std::string_view a)
Convert a string_view to an algo enum.
Definition algos.h:50
std::string_view to_sv_from_algo(algo a)
Convert the algo enum to a string_view.
Definition algos.h:38