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