reglibcpp  2.0.0
A C++ implementation of models for regular languages
utils.h
Go to the documentation of this file.
1 // Copyright 2017, 2018 Tom Kranz
2 //
3 // This file is part of reglibcpp.
4 //
5 // reglibcpp is free software: you can redistribute it and/or modify
6 // it under the terms of the GNU General Public License as published by
7 // the Free Software Foundation, either version 3 of the License, or
8 // (at your option) any later version.
9 //
10 // reglibcpp is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 // GNU General Public License for more details.
14 //
15 // You should have received a copy of the GNU General Public License
16 // along with reglibcpp. If not, see <https://www.gnu.org/licenses/>.
17 
18 #ifndef REG_UTILS_H
19 #define REG_UTILS_H
20 #include <codecvt>
23 
24 #include <locale>
25 
26 #include <unordered_set>
27 
28 #include <functional>
29 
30 #include <valarray>
31 
32 namespace reg {
33 
36 
42 template <class C, class T>
43 size_t index_of(C const& container, T const& element) {
45  "C must be a container with T as value_type.");
46  return static_cast<size_t>(
47  std::distance(container.begin(),
48  std::find(container.begin(), container.end(), element)));
49 }
50 
53 
57  inline state_not_found(char const* what_arg = "Cannot find the given state.")
58  : std::invalid_argument(what_arg) {}
60  inline state_not_found(std::string&& what_arg)
61  : std::invalid_argument(std::move(what_arg)) {}
64  inline state_not_found(std::string const& state)
65  : std::invalid_argument("There is no state named " + state) {}
68  inline state_not_found(size_t state_index)
69  : std::invalid_argument("There is no state with index " +
70  std::to_string(state_index)) {}
71 };
72 
77  char const* what_arg = "Cannot find the given symbol.")
78  : std::invalid_argument(what_arg) {}
80  inline symbol_not_found(std::string&& what_arg)
81  : std::invalid_argument(std::move(what_arg)) {}
84  inline symbol_not_found(char32_t symbol)
85  : std::invalid_argument("There is no symbol named " +
86  converter.to_bytes(symbol)) {}
89  inline symbol_not_found(size_t symbol_index)
90  : std::invalid_argument("There is no symbol with index " +
91  std::to_string(symbol_index)) {}
92 };
93 } // namespace reg
94 
95 namespace std {
96 template <>
97 struct hash<valarray<bool>> {
98  inline size_t operator()(valarray<bool> const& value) const {
99  size_t hash = 0;
100  for (bool v : value) {
101  hash <<= 1;
102  hash |= v;
103  }
104  return hash;
105  }
106 };
107 
108 template <>
109 struct equal_to<valarray<bool>> {
110  inline bool operator()(valarray<bool> const& lhs,
111  valarray<bool> const& rhs) const {
112  return (lhs.size() == rhs.size() && (lhs == rhs).min() == true);
113  }
114 };
115 } // namespace std
116 
117 #endif
state_not_found(std::string const &state)
Constructs an exception object incorporating the given state name in its message. ...
Definition: utils.h:64
symbol_not_found(std::string &&what_arg)
Definition: utils.h:80
Signals that an input symbol was used that the FA doesn't recognize.
Definition: utils.h:74
T min(T... args)
size_t index_of(C const &container, T const &element)
Basically Java&#39;s List interface&#39;s indexOf, but as a non-member function and returning the container&#39;s...
Definition: utils.h:43
symbol_not_found(size_t symbol_index)
Constructs an exception object incorporating the given symbol index in its message.
Definition: utils.h:89
T hash(T... args)
state_not_found(char const *what_arg="Cannot find the given state.")
Constructs an exception object with the given message.
Definition: utils.h:57
std::wstring_convert< std::codecvt_utf8< char32_t >, char32_t > converter
Converts between UTF-8-encoded and UTF-32-encoded strings.
Definition: utils.h:52
T operator()(T... args)
Signals that a state was mentioned that isn't part of the FA.
Definition: utils.h:55
symbol_not_found(char const *what_arg="Cannot find the given symbol.")
Constructs an exception object with the given message.
Definition: utils.h:76
Where this library lives.
Definition: dfa.cpp:48
state_not_found(size_t state_index)
Constructs an exception object incorporating the given state index in its message.
Definition: utils.h:68
state_not_found(std::string &&what_arg)
Definition: utils.h:60
T operator()(T... args)
symbol_not_found(char32_t symbol)
Constructs an exception object incorporating the given symbol in its message.
Definition: utils.h:84