Vcsn  2.1
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  if (fmt == format::latex)
110  o << "\\rightarrow";
111  else
112  o << ", ";
113  return weightset()->print_set(o, fmt);
114  }
115 
116  static constexpr bool
118  {
119  return labelset_t::has_one();
120  }
121 
122  private:
125  };
126 
127 
129  template <typename LabelSet, typename WeightSet>
131  make_context(const LabelSet& ls, const WeightSet& ws)
132  {
133  return {ls, ws};
134  }
135 
136 
137 
138  /*-----------------.
139  | join_t, meet_t. |
140  `-----------------*/
141 
142  template <typename... ValueSets>
143  using meet_t = decltype(meet(std::declval<ValueSets>()...));
144 
145 
146  /*-------------------------.
147  | Variadic join and meet. |
148  `-------------------------*/
149 
152  template <typename ValueSet>
153  auto
154  meet(const ValueSet& vs)
155  -> ValueSet
156  {
157  return vs;
158  }
159 
160  template <typename ValueSet1, typename ValueSet2, typename ValueSet3,
161  typename... VSs>
162  auto
163  meet(const ValueSet1& vs1, const ValueSet2& vs2, const ValueSet3& vs3,
164  const VSs&... vs)
165  -> decltype(meet(meet(vs1, vs2), vs3, vs...))
166  {
167  return meet(meet(vs1, vs2), vs3, vs...);
168  }
169 
170 
171  /*-------------------------.
172  | join(context, context). |
173  `-------------------------*/
174 
175  namespace detail
176  {
178  template <typename LS1, typename WS1,
179  typename LS2, typename WS2>
180  struct join_impl<context<LS1, WS1>, context<LS2, WS2>>
181  {
185 
186  static type join(const context<LS1, WS1>& ctx1,
187  const context<LS2, WS2>& ctx2)
188  {
189  // Don't use braces, otherwise the context constructor that
190  // takes a list-initializer for the labelset thinks both
191  // values here are letters for the labelset.
192  return type(vcsn::join(*ctx1.labelset(), *ctx2.labelset()),
193  vcsn::join(*ctx1.weightset(), *ctx2.weightset()));
194  }
195  };
196  }
197 
198  /*-------------------------.
199  | meet(context, context). |
200  `-------------------------*/
201 
203  template <typename LhsLabelSet, typename LhsWeightSet,
204  typename RhsLabelSet, typename RhsWeightSet>
205  auto
210  {
211  auto ls = meet(*a.labelset(), *b.labelset());
212  auto ws = join(*a.weightset(), *b.weightset());
213  return {ls, ws};
214  }
215 
216 }
std::shared_ptr< const weightset_t > weightset_ptr
Definition: context.hh:24
static symbol sname()
The name of this context, built from its parameters.
Definition: context.hh:72
weightset_ptr ws_
Definition: context.hh:124
context & operator=(context &&that)
Definition: context.hh:60
typename labelset_t::value_t label_t
Type of transition labels, and type of expression atoms.
Definition: context.hh:37
std::ostream & print_set(std::ostream &o, format fmt={}) const
Definition: context.hh:106
A structure that implements the computation of join(V1, V2).
Definition: join.hh:18
std::string type(const automaton &a)
The implementation type of a.
Definition: others.cc:197
auto join(const ValueSet &vs) -> ValueSet
The join of a single valueset.
Definition: join.hh:44
context(const context &that)
Definition: context.hh:41
std::istringstream is
The input stream: the specification to translate.
Definition: translate.cc:372
char eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.cc:37
decltype(meet(std::declval< ValueSets >()...)) meet_t
Definition: context.hh:143
typename labelset_t::kind_t kind_t
Definition: context.hh:26
context(const labelset_ptr &ls, const weightset_ptr &ws)
Definition: context.hh:47
An input/output format.
Definition: format.hh:11
const weightset_ptr & weightset() const
Definition: context.hh:100
Provide a variadic mul on top of a binary mul(), and one().
Definition: fwd.hh:46
decltype(join(std::declval< ValueSets >()...)) join_t
The type of the join of the ValueSets.
Definition: join.hh:79
std::shared_ptr< const labelset_t > labelset_ptr
Definition: context.hh:23
static constexpr bool has_one()
Definition: context.hh:117
labelset_ptr ls_
Definition: context.hh:123
symbol sname()
Definition: name.hh:67
boost::flyweight< std::string, boost::flyweights::no_tracking, boost::flyweights::intermodule_holder > symbol
An internalized string.
Definition: symbol.hh:23
Ctx make_context(const std::string &name)
Definition: make-context.hh:20
context(const labelset_t &ls={}, const weightset_t &ws={})
Build a context.
Definition: context.hh:55
auto meet(const expressionset< Ctx1 > &a, const expressionset< Ctx2 > &b) -> expressionset< meet_t< Ctx1, Ctx2 >>
The meet of two expressionsets.
const labelset_ptr & labelset() const
Definition: context.hh:95
static type join(const context< LS1, WS1 > &ctx1, const context< LS2, WS2 > &ctx2)
Definition: context.hh:186
typename weightset_t::value_t weight_t
Type of weights.
Definition: context.hh:39
static context make(std::istream &is)
Build from the description in is.
Definition: context.hh:83