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