Algorithms Library Toolkit
A toolkit for algorithms, especially for algorithms on formal languages
symbol_or_epsilon.hpp
Go to the documentation of this file.
1
6#pragma once
7
8#include <optional>
9
10#include <alib/tuple>
11
12#include <core/normalize.hpp>
13
15
17
18namespace common {
19
23template < class SymbolType = DefaultSymbolType >
25 std::optional < SymbolType > m_symbol;
26
27public:
33 explicit symbol_or_epsilon ( SymbolType symbol ) : m_symbol ( std::move ( symbol ) ) {
34 }
35
36 explicit symbol_or_epsilon ( ) = default;
37
41 const SymbolType & getSymbol ( ) const & {
42 if ( ! m_symbol )
43 throw exception::CommonException ( "The symbol is epsilon" );
44 else
45 return m_symbol.value ( );
46 }
47
52 if ( ! m_symbol )
53 throw exception::CommonException ( "The symbol is epsilon" );
54 else
55 return std::move ( m_symbol.value ( ) );
56 }
57
58 bool is_epsilon ( ) const {
59 return ! m_symbol;
60 }
61
62 auto operator <=> ( const symbol_or_epsilon & other ) const {
63 return m_symbol <=> other.m_symbol;
64 }
65
66 bool operator == ( const symbol_or_epsilon & other ) const {
67 return m_symbol == other.m_symbol;
68 }
69
70 auto operator <=> ( const SymbolType & other ) const {
71 return m_symbol <=> other;
72 }
73
74 bool operator == ( const SymbolType & other ) const {
75 return m_symbol == other;
76 }
77
79 ++ m_symbol.value ( );
80
81 return *this;
82 }
83};
84
85template < class SymbolType >
87 out << "(symbol_or_epsilon ";
88 if ( symbol.is_epsilon ( ) )
89 out << "#E";
90 else
91 out << symbol.getSymbol ( );
92 out << ")";
93 return out;
94}
95
96} /* namespace common */
97
Definition: symbol_or_epsilon.hpp:24
const SymbolType & getSymbol() const &
Definition: symbol_or_epsilon.hpp:41
SymbolType && getSymbol() &&
Definition: symbol_or_epsilon.hpp:51
auto operator<=>(const symbol_or_epsilon &other) const
Definition: symbol_or_epsilon.hpp:62
symbol_or_epsilon(SymbolType symbol)
Definition: symbol_or_epsilon.hpp:33
symbol_or_epsilon< SymbolType > & operator++()
Definition: symbol_or_epsilon.hpp:78
bool operator==(const symbol_or_epsilon &other) const
Definition: symbol_or_epsilon.hpp:66
bool is_epsilon() const
Definition: symbol_or_epsilon.hpp:58
Basic exception from which all other exceptions are derived.
Definition: CommonException.h:21
Definition: ostream.h:14
typename T::SymbolType SymbolType
Definition: ReachableStates.h:176
Definition: Permutation.hpp:18
ext::ostream & operator<<(ext::ostream &out, const common::ranked_symbol< SymbolType > &symbol)
Definition: ranked_symbol.hpp:93
Definition: FordFulkerson.hpp:16