Vcsn  2.8
Be Rational
context.hh
Go to the documentation of this file.
1 #pragma once
2 
3 #include <memory>
4 #include <string>
5 
6 #include <vcsn/core/join.hh>
7 #include <vcsn/core/kind.hh>
8 #include <vcsn/core/rat/fwd.hh>
9 #include <vcsn/ctx/fwd.hh>
10 #include <vcsn/ctx/traits.hh>
11 #include <vcsn/misc/format.hh>
12 #include <vcsn/misc/stream.hh>
13 #include <vcsn/misc/symbol.hh>
14 
15 namespace vcsn
16 {
17 
18  template <typename LabelSet, typename WeightSet>
19  class context
20  {
21  public:
22  using labelset_t = LabelSet;
24  using labelset_ptr = std::shared_ptr<const labelset_t>;
25  using weightset_ptr = std::shared_ptr<const weightset_t>;
26 
27  using kind_t = typename labelset_t::kind_t;
28  enum
29  {
36  };
38  using label_t = typename labelset_t::value_t;
40  using weight_t = typename weightset_t::value_t;
41 
42  context(const context& that)
43  : context(that.ls_, that.ws_)
44  {}
45 
48  context(const labelset_ptr& ls, const weightset_ptr& ws)
49  : ls_{ls}
50  , ws_{ws}
51  {}
52 
56  context(const labelset_t& ls = {}, const weightset_t& ws = {})
57  : context(std::make_shared<const labelset_t>(ls),
58  std::make_shared<const weightset_t>(ws))
59  {}
60 
62  {
63  if (this != &that)
64  {
65  std::swap(ls_, that.ls_);
66  std::swap(ws_, that.ws_);
67  }
68  return *this;
69  }
70 
73  static symbol sname()
74  {
75  static auto res = symbol{"context<"
77  + ", "
79  + '>'};
80  return res;
81  }
82 
84  static context make(std::istream& is)
85  {
86  eat(is, "context<");
87  auto ls = labelset_t::make(is);
88  eat(is, ',');
89  while (isspace(is.peek()))
90  is.ignore();
91  auto ws = weightset_t::make(is);
92  eat(is, '>');
93  return {ls, ws};
94  }
95 
96  const labelset_ptr& labelset() const
97  {
98  return ls_;
99  }
100 
101  const weightset_ptr& weightset() const
102  {
103  return ws_;
104  }
105 
106  std::ostream&
107  print_set(std::ostream& o, format fmt = {}) const
108  {
109  labelset()->print_set(o, fmt);
110  switch (fmt.kind())
111  {
112  case format::latex:
113  o << "\\to";
114  break;
115  case format::sname:
116  o << ", ";
117  break;
118  case format::text:
119  o << " -> ";
120  break;
121  case format::utf8:
122  o << " → ";
123  break;
124  case format::raw:
125  assert(0);
126  break;
127  }
128  weightset()->print_set(o, fmt);
129  return o;
130  }
131 
132  static constexpr bool
134  {
135  return labelset_t::has_one();
136  }
137 
138  private:
141  };
142 
143 
145  template <typename LabelSet, typename WeightSet>
147  make_context(const LabelSet& ls, const WeightSet& ws)
148  {
149  return {ls, ws};
150  }
151 
152  template <typename LabelSet, typename WeightSet>
153  struct is_multitape<context<LabelSet, WeightSet>>
154  : is_multitape<LabelSet>
155  {};
156 
157  template <typename LabelSet, typename WeightSet>
158  struct number_of_tapes<context<LabelSet, WeightSet>>
159  : number_of_tapes<LabelSet>
160  {};
161 
162 
163  /*----------.
164  | meet_t. |
165  `----------*/
166 
167  template <typename... ValueSets>
168  using meet_t = decltype(meet(std::declval<ValueSets>()...));
169 
172  template <typename ValueSet>
173  auto
174  meet(const ValueSet& vs)
175  -> ValueSet
176  {
177  return vs;
178  }
179 
180  template <typename ValueSet1, typename ValueSet2, typename ValueSet3,
181  typename... VSs>
182  auto
183  meet(const ValueSet1& vs1, const ValueSet2& vs2, const ValueSet3& vs3,
184  const VSs&... vs)
185  -> decltype(meet(meet(vs1, vs2), vs3, vs...))
186  {
187  return meet(meet(vs1, vs2), vs3, vs...);
188  }
189 
190 
191  /*-------------------------.
192  | join(context, context). |
193  `-------------------------*/
194 
195  namespace detail
196  {
198  template <typename LS1, typename WS1,
199  typename LS2, typename WS2>
200  struct join_impl<context<LS1, WS1>, context<LS2, WS2>>
201  {
205 
206  static type join(const context<LS1, WS1>& ctx1,
207  const context<LS2, WS2>& ctx2)
208  {
209  // Don't use braces, otherwise the context constructor that
210  // takes a list-initializer for the labelset thinks both
211  // values here are letters for the labelset.
212  return type(vcsn::join(*ctx1.labelset(), *ctx2.labelset()),
213  vcsn::join(*ctx1.weightset(), *ctx2.weightset()));
214  }
215  };
216  }
217 
218  /*-------------------------.
219  | meet(context, context). |
220  `-------------------------*/
221 
223  template <typename LhsLabelSet, typename LhsWeightSet,
224  typename RhsLabelSet, typename RhsWeightSet>
225  auto
230  {
231  auto ls = meet(*a.labelset(), *b.labelset());
232  auto ws = join(*a.weightset(), *b.weightset());
233  return {ls, ws};
234  }
235 
236 }
Print as a parsable type string.
Definition: format.hh:26
decltype(join(std::declval< ValueSets >()...)) join_t
The type of the join of the ValueSets.
Definition: join.hh:78
auto meet(const expressionset< Ctx1 > &a, const expressionset< Ctx2 > &b) -> expressionset< meet_t< Ctx1, Ctx2 >>
The meet of two expressionsets.
typename labelset_t::value_t label_t
Type of transition labels, and type of expression atoms.
Definition: context.hh:38
static type join(const context< LS1, WS1 > &ctx1, const context< LS2, WS2 > &ctx2)
Definition: context.hh:206
typename weightset_t::value_t weight_t
Type of weights.
Definition: context.hh:40
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:147
weightset_ptr ws_
Definition: context.hh:140
labelset_ptr ls_
Definition: context.hh:139
typename labelset_t::kind_t kind_t
Definition: context.hh:27
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: context.hh:107
context & operator=(context &&that)
Definition: context.hh:61
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:21
static constexpr bool has_one()
Definition: context.hh:133
Ctx make_context(const std::string &name)
Build a context from its name.
Definition: make-context.hh:21
context(const context &that)
Definition: context.hh:42
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
static symbol sname()
The name of this context, built from its parameters.
Definition: context.hh:73
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Definition: join.hh:44
A structure that implements the computation of join(V1, V2).
Definition: join.hh:18
context(const labelset_t &ls={}, const weightset_t &ws={})
Build a context.
Definition: context.hh:56
Definition: a-star.hh:8
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:238
std::shared_ptr< const weightset_t > weightset_ptr
Definition: context.hh:25
Print as plain (ASCII) text, escaped.
Definition: format.hh:28
void swap(config::value &first, config::value &second)
context(const labelset_ptr &ls, const weightset_ptr &ws)
Definition: context.hh:48
symbol sname()
Definition: name.hh:65
decltype(meet(std::declval< ValueSets >()...)) meet_t
Definition: context.hh:168
Print as is. For instance, don&#39;t try to escape labels.
Definition: format.hh:24
Print for LaTeX.
Definition: format.hh:22
std::shared_ptr< const labelset_t > labelset_ptr
Definition: context.hh:24
const labelset_ptr & labelset() const
Definition: context.hh:96
static context make(std::istream &is)
Build from the description in is.
Definition: context.hh:84
Whether a ValueSet, or a context, is multitape.
Definition: traits.hh:117
const weightset_ptr & weightset() const
Definition: context.hh:101
return res
Definition: multiply.hh:399