Vcsn  2.4
Be Rational
oneset.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <iostream>
4 #include <set>
5 #include <stdexcept>
6 
7 #include <vcsn/core/kind.hh>
10 #include <vcsn/misc/raise.hh>
11 
12 namespace vcsn
13 {
16  class oneset
17  {
18  public:
19  using self_t = oneset;
20  using value_t = bool;
21 
23 
24  oneset() = default;
25 
26  static symbol sname()
27  {
28  static auto res = symbol{"lao"};
29  return res;
30  }
31 
33  static oneset make(std::istream& is)
34  {
35  kind_t::make(is);
36  return {};
37  }
38 
40  bool open(bool o) const
41  {
42  return !o;
43  }
44 
45  static constexpr bool is_free()
46  {
47  // This is debatable. However, in Vcsn, if a labelset
48  // is_free, then we expect to be able to iterate on its genset,
49  // and I don't plan to provide a genset here.
50  return false;
51  }
52 
54  template <typename... Args>
55  value_t value(Args&&... args) const
56  {
57  return value_t{std::forward<Args>(args)...};
58  }
59 
61  static bool
62  equal(const value_t l, const value_t r)
63  {
64  return l == r;
65  }
66 
68  static bool less(const value_t l, const value_t r)
69  {
70  return l < r;
71  }
72 
73  static value_t special()
74  {
75  return false;
76  }
77 
79  static bool is_special(const value_t v)
80  {
81  return v == special();
82  }
83 
84  static constexpr bool
86  {
87  return false;
88  }
89 
90  static constexpr bool
92  {
93  return true;
94  }
95 
96  static constexpr bool
98  {
99  return false;
100  }
101 
102  static value_t one()
103  {
104  return true;
105  }
106 
107  static bool is_one(const value_t v)
108  {
109  return v == one();
110  }
111 
112  static value_t transpose(const value_t v)
113  {
114  return v;
115  }
116 
117  static bool is_letter(const value_t)
118  {
119  return false;
120  }
121 
123  {
124  return l && r;
125  }
126 
127  static std::ostream& print(const value_t, std::ostream& o = std::cout,
128  format = {})
129  {
130  return o;
131  }
132 
133  static size_t size(value_t)
134  {
135  return 0;
136  }
137 
138  static size_t hash(value_t v)
139  {
140  return hash_value(v);
141  }
142 
143  static value_t
145  {
146  return v;
147  }
148 
150  template <typename LabelSet>
151  value_t
152  conv(const LabelSet& ls,
153  typename LabelSet::value_t v) const
154  {
155  VCSN_REQUIRE(ls.is_one(v),
156  *this, ": conv: invalid label: ", to_string(ls, v));
157  return one();
158  }
159 
160 
161  value_t conv(std::istream& i, bool = true) const
162  {
163  if (i.peek() == '\\')
164  {
165  i.ignore();
166  int c = i.peek();
167  require(c == 'e',
168  *this, ": invalid label: unexpected \\", c);
169  i.ignore();
170  }
171  return one();
172  }
173 
174  template <typename Fun>
175  void convs(std::istream&, Fun) const
176  {
177  raise(*this, ": ranges not implemented");
178  }
179 
180  std::ostream&
181  print_set(std::ostream& o, format fmt = {}) const
182  {
183  switch (fmt.kind())
184  {
185  case format::latex:
186  o << "\\{\\varepsilon\\}";
187  break;
188  case format::sname:
189  o << sname();
190  break;
191  case format::text:
192  o << "{\\e}";
193  break;
194  case format::utf8:
195  o << "{ε}";
196  break;
197  case format::raw:
198  assert(0);
199  break;
200  }
201  return o;
202  }
203  };
204 
205  namespace detail
206  {
208  template <>
210  {
211  using type = oneset;
212  static type value(oneset)
213  {
214  return {};
215  }
216  };
217 
219  template <>
221  {
222  using type = oneset;
223  static type value(oneset)
224  {
225  return {};
226  }
227  };
228 
229  /*-------.
230  | Join. |
231  `-------*/
232 
233  template <>
235  {
236  using type = oneset;
237  static type join(const oneset&, const oneset&)
238  {
239  return {};
240  }
241  };
242  }
243 
245  inline
246  oneset
247  meet(const oneset&, const oneset&)
248  {
249  return {};
250  }
251 
253  template <typename RandomGenerator = std::default_random_engine>
254  typename oneset::value_t
255  random_label(const oneset& ls,
256  RandomGenerator& = RandomGenerator())
257  {
258  return ls.one();
259  }
260 }
static bool less(const value_t l, const value_t r)
Whether l < r.
Definition: oneset.hh:68
static type value(oneset)
Definition: oneset.hh:223
Print as is. For instance, don't try to escape labels.
Definition: format.hh:24
static constexpr bool is_expressionset()
Definition: oneset.hh:85
static bool equal(const value_t l, const value_t r)
Whether l == r.
Definition: oneset.hh:62
static bool is_special(const value_t v)
The special label is indistinguishable for the others.
Definition: oneset.hh:79
Print as a parsable type string.
Definition: format.hh:26
return res
Definition: multiply.hh:398
Implementation of labels are ones: there is a single instance of label.
Definition: oneset.hh:16
Print for LaTeX.
Definition: format.hh:22
static value_t mul(value_t l, value_t r)
Definition: oneset.hh:122
value_t value(Args &&...args) const
Value constructor.
Definition: oneset.hh:55
static constexpr bool is_free()
Definition: oneset.hh:45
static oneset make(std::istream &is)
Build from the description in is.
Definition: oneset.hh:33
bool value_t
Definition: oneset.hh:20
static bool is_one(const value_t v)
Definition: oneset.hh:107
static size_t size(value_t)
Definition: oneset.hh:133
static value_t special()
Definition: oneset.hh:73
static symbol sname()
Definition: oneset.hh:26
An input/output format for valuesets.
Definition: format.hh:13
weightset_mixin< detail::r_impl > r
Definition: fwd.hh:54
static type join(const oneset &, const oneset &)
Definition: oneset.hh:237
void require(Bool b, Args &&...args)
If b is not verified, raise an error with args as message.
Definition: raise.hh:91
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
The smallest nullableset which includes LabelSet.
Definition: labelset.hh:139
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
auto hash_value(const T &v) -> decltype(std::hash< T >
Following the naming convention of Boost.
Definition: functional.hh:30
static value_t transpose(const value_t v)
Definition: oneset.hh:112
void convs(std::istream &, Fun) const
Definition: oneset.hh:175
static value_t conv(self_t, value_t v)
Definition: oneset.hh:144
Definition: a-star.hh:8
static std::ostream & print(const value_t, std::ostream &o=std::cout, format={})
Definition: oneset.hh:127
#define VCSN_REQUIRE(Cond,...)
A macro similar to require.
Definition: raise.hh:111
Print as rich UTF-8 text, escaped.
Definition: format.hh:30
static value_t one()
Definition: oneset.hh:102
A structure that implements the computation of join(V1, V2).
Definition: join.hh:18
The LAW from a LAL.
Definition: labelset.hh:250
static constexpr bool has_one()
Definition: oneset.hh:91
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
static constexpr bool is_letterized()
Definition: oneset.hh:97
static bool is_letter(const value_t)
Definition: oneset.hh:117
value_t conv(const LabelSet &ls, typename LabelSet::value_t v) const
Convert from labelset to oneset.
Definition: oneset.hh:152
return v
Definition: multiply.hh:361
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: oneset.hh:181
std::string to_string(direction d)
Conversion to string.
Definition: direction.cc:7
oneset()=default
static size_t hash(value_t v)
Definition: oneset.hh:138
static void make(std::istream &is)
Definition: kind.hh:49
expressionset< Context >::value_t random_label(const expressionset< Context > &rs, RandomGenerator &gen=RandomGenerator())
Random label from expressionset: limited to a single label.
bool open(bool o) const
Does not make a lot of sense.
Definition: oneset.hh:40
auto meet(const expressionset< Ctx1 > &a, const expressionset< Ctx2 > &b) -> expressionset< meet_t< Ctx1, Ctx2 >>
The meet of two expressionsets.
value_t conv(std::istream &i, bool=true) const
Definition: oneset.hh:161