23 std::string
quote(
const std::string& s)
26 || (s.size() == 1 && std::isalnum(s[0])))
31 static auto re = std::regex{
"['\\\\ \\[\\]()+,|<>]"};
32 return std::regex_replace(s, re,
"\\$&");
50 template <
typename Context, direction Dir>
97 for (
const auto& m: p)
107 while (getline(is, buf))
118 while (
auto m = ps.conv_monomial(is))
129 {
"default",
"monomials"},
134 (this->*(
map[format]))(is);
138 template <direction D = Dir>
140 -> std::enable_if_t<D == direction::forward, automaton_t>
146 template <direction D = Dir>
148 -> std::enable_if_t<D == direction::backward, automaton_t>
161 const auto& ls = *
ctx_.labelset();
163 s =
next_(s, ls.special());
170 res_->add_transition(s,
res_->post(), ls.special(), wgt);
178 assert(ts.size() == 0 || ts.size() == 1);
181 auto d =
res_->new_state();
182 res_->new_transition(s, d, l);
199 template <direction Dir,
typename PolynomialSet>
212 template <
typename PolynomialSet>
214 trie(
const PolynomialSet& ps,
const typename PolynomialSet::value_t& p)
216 auto t = detail::make_trie_builder<direction::forward>(ps);
226 template <
typename PolynomialSet>
227 mutable_automaton<detail::free_context<context_t_of<PolynomialSet>>>
228 cotrie(
const PolynomialSet& ps,
const typename PolynomialSet::value_t& p)
230 auto t = detail::make_trie_builder<direction::backward>(ps);
240 template <
typename PolynomialSet>
244 const auto& p = poly->
as<PolynomialSet>();
245 return trie(p.valueset(), p.value());
249 template <
typename PolynomialSet>
253 const auto& p = poly->
as<PolynomialSet>();
254 return cotrie(p.valueset(), p.value());
269 template <
typename PolynomialSet>
270 mutable_automaton<detail::free_context<context_t_of<PolynomialSet>>>
271 trie(
const PolynomialSet& ps, std::istream& is,
272 const std::string&
format =
"default")
274 auto t = detail::make_trie_builder<direction::forward>(ps);
285 template <
typename PolynomialSet>
286 mutable_automaton<detail::free_context<context_t_of<PolynomialSet>>>
287 cotrie(
const PolynomialSet& ps, std::istream& is,
288 const std::string&
format =
"default")
290 auto t = detail::make_trie_builder<direction::backward>(ps);
300 template <
typename Context,
typename Istream,
typename String>
303 const std::string&
format)
305 const auto& c = ctx->
as<Context>();
307 return trie(ps, is, format);
311 template <
typename Context,
typename Istream,
typename String>
314 const std::string&
format)
316 const auto& c = ctx->
as<Context>();
318 return cotrie(ps, is, format);
constant< type_t::one, Context > one
auto result() -> std::enable_if_t< D==direction::backward, automaton_t >
Get the result for a backward trie.
free_context< context< LabelSet, WeightSet > > make_free_context(const context< LabelSet, WeightSet > &c)
The free context for c.
automaton trie_stream(const context &ctx, std::istream &is, const std::string &format)
Bridge (trie).
mutable_automaton< detail::free_context< context_t_of< PolynomialSet > > > trie(const PolynomialSet &ps, const typename PolynomialSet::value_t &p)
Make a trie-like mutable_automaton for a finite series given as a polynomial.
Container::value_type front(const Container &container)
The first member of this Container.
Build a trie automaton (prefix-tree-like automaton).
void add(const monomial_t &m)
Add a monomial.
SharedPtr make_shared_ptr(Args &&...args)
Same as std::make_shared, but parameterized by the shared_ptr type, not the (pointed to) element_type...
void add_words(std::istream &is)
Add all the words (one per line) in this stream.
std::shared_ptr< detail::mutable_automaton_impl< Context >> mutable_automaton
std::shared_ptr< detail::transpose_automaton_impl< Aut >> transpose_automaton
An automaton wrapper that presents the transposed automaton.
mutable_automaton< context_t > automaton_t
The type of the result.
letter_t_of< context_t > letter_t
auto weight_of(const welement< Label, Weight > &m) -> decltype(m.weight())
The weight of a welement.
typename labelset_t_of< base_t< ValueSet >>::letter_t letter_t_of
An input/output format for valuesets.
auto make_word_polynomialset(const Ctx &ctx) -> word_polynomialset_t< Ctx >
The polynomialset of words of a labelset (not necessarily on words itself).
Provide a variadic mul on top of a binary mul(), and one().
auto result() -> std::enable_if_t< D==direction::forward, automaton_t >
Get the result for the forward trie.
const context_t & ctx_
The context of the automaton: letterized.
void add_(const word_t &lbl, const weight_t &wgt)
Add a monomial.
typename detail::labelset_t_of_impl< base_t< ValueSet >>::type labelset_t_of
trie_builder< free_context< context_t_of< PolynomialSet > >, Dir > make_trie_builder(const PolynomialSet &ps)
Instantiate a trie-builder for this type of polynomialset.
auto & as()
Downcast to the exact type.
state_t_of< automaton_t > state_t
std::conditional_t< Dir==direction::forward, automaton_t, transpose_automaton< automaton_t >> work_automaton_t
The type of the automaton we work on.
auto out(const Aut &aut, state_t_of< Aut > s)
Indexes of visible transitions leaving state s.
mutable_automaton< detail::free_context< context_t_of< PolynomialSet > > > cotrie(const PolynomialSet &ps, const typename PolynomialSet::value_t &p)
Make a cotrie-like mutable_automaton for a finite series given as a polynomial.
void add(const polynomial_t &p)
Add a polynomial.
word_t_of< context_t > word_t
trie_builder(const context_t &c)
void add(const word_t &l, const weight_t &w=weightset_t::one())
Add a monomial.
Template-less root for contexts.
void add(std::istream &is, const std::string &format)
Add all the monomials in this stream.
typename polynomialset_t::value_t polynomial_t
letterized_t< labelset_t >::value_t padding_
Padding, in case it is needed.
typename detail::weight_t_of_impl< base_t< ValueSet >>::type weight_t_of
work_automaton_t res_
The automaton being built.
typename detail::state_t_of_impl< base_t< ValueSet >>::type state_t_of
typename labelset_t_of< base_t< ValueSet >>::word_t word_t_of
weightset_t_of< context_t > weightset_t
typename letterized_traits< LabelSet >::labelset_t letterized_t
labelset_t_of< context_t > labelset_t
The input labelset, free/letterized or not.
auto map(const std::tuple< Ts... > &ts, Fun f) -> decltype(map_tuple_(f, ts, make_index_sequence< sizeof...(Ts)>()))
Map a function on a tuple, return tuple of the results.
void add_monomials(std::istream &is)
Add all the monomials (one per line) in this stream.
automaton cotrie_stream(const context &ctx, std::istream &is, const std::string &format)
Bridge (cotrie).
auto & as()
Extract wrapped typed value.
typename detail::weightset_t_of_impl< base_t< ValueSet >>::type weightset_t_of
auto conv(const ValueSet &vs, const std::string &str, Args &&...args) -> decltype(vs.conv(std::declval< std::istream & >(), std::forward< Args >(args)...))
Parse str via vs.conv.
A mapping from strings to Values.
typename polynomialset_t::monomial_t monomial_t
auto label_of(const welement< Label, Weight > &m) -> decltype(m.label())
The label of a welement.
std::string quote(const std::string &s)
Turn a label into a parsable label: escape special characters.
weight_t_of< context_t > weight_t
state_t next_(state_t s, letter_t l)
Follow a transition, possibly creating it.
law_t< LabelSet > make_wordset(const LabelSet &ls)
The wordset of a labelset.
Aut transpose(const transpose_automaton< Aut > &aut)
The transpose of a transpose automaton is the original automaton.