17 #ifndef AWALI_LABELSET_TUPLESET_HH
18 # define AWALI_LABELSET_TUPLESET_HH
52 template <
typename Enable = void,
typename... ValueSets>
61 template <
typename... ValueSets>
66 using letter_t = std::tuple<
typename ValueSets::letter_t...>;
71 template <
typename... ValueSets>
78 template <
typename... ValueSets>
85 template <std::size_t... I>
89 template <std::
size_t I>
90 using valueset_t =
typename std::tuple_element<I, valuesets_t>::type;
97 using value_t = std::tuple<
typename ValueSets::value_t...>;
121 return sname_(indices);
126 return vname_(
full, indices);
129 static constexpr std::size_t
size()
131 return sizeof...(ValueSets);
134 template <std::size_t... I>
135 static constexpr
bool
138 return all_<valueset_t<I>::is_commutative_semiring()...>();
146 auto res = make_(is, indices);
161 return std::get<I>(sets());
169 return open_(o, indices);
173 template <
typename... Args>
176 return value_(args, indices);
183 return genset_(indices);
188 return is_free_(indices);
193 template <
typename Value, std::size_t... I>
195 letters_of_(
const Value& v, seq<I...>)
196 -> decltype(
internal::zip(valueset_t<I>::letters_of(std::get<I>(v))...))
198 return internal::zip(valueset_t<I>::letters_of(std::get<I>(v))...);
207 template <
typename Value>
210 -> decltype(letters_of_(v, indices))
212 return letters_of_(v, indices);
216 template <
typename... Args>
218 word(
const std::tuple<Args...>& v)
const
221 return word_(v, indices);
227 return equals_(l,
r, indices);
234 return less_than_(l,
r, indices);
240 return special_(indices);
246 return is_special_(l, indices);
252 return zero_(indices);
258 return is_zero_(l, indices);
261 static constexpr
bool
264 return has_one_(indices);
267 static constexpr
bool
270 return is_ratexpset_(indices);
273 static constexpr
bool
276 return is_letterized_(indices);
282 return one_(indices);
288 return is_one_(l, indices);
294 return show_one_(indices);
306 return add_(l,
r, indices);
312 return mul_(l,
r, indices);
318 return rdiv_(l,
r, indices);
324 return ldiv_(l,
r, indices);
330 return star_(l, indices);
338 template <
typename Value>
342 return delimit_(l, indices);
346 template <
typename Value>
350 return undelimit_(l, indices);
359 template <
typename LhsValue,
typename RhsValue>
361 concat(
const LhsValue& l,
const RhsValue&
r)
const
363 return concat_(l,
r, indices);
369 return transpose_(l, indices);
375 return hash_(v, indices);
386 conv(
const T& tset,
typename T::value_t v)
const
388 return conv_(tset, v, indices);
394 return v ? one() : zero();
401 value_t res = conv_(i, indices);
406 std::set<value_t>
convs(std::istream&)
const
408 raise(
"tupleset: ranges not implemented");
414 return print_set_(o,
format, indices);
418 template<
unsigned version = version::fsm_json>
422 version::check_fsmjson<version>();
426 return to_json<version>(indices);
431 template<
unsigned version = version::fsm_json>
435 version::check_fsmjson<version>();
439 return value_to_json<version>(v, indices);
445 template<
unsigned version = version::fsm_json>
449 version::check_fsmjson<version>();
450 return value_from_json<version>(p->
array()->
values, indices);
454 parse(
const std::string& s,
size_t& p,
bool fixed_domain=
true)
const {
457 # if AWALI_HAVE_CORRECT_LIST_INITIALIZER_ORDER
460 value_t r= parse_(s, p, fixed_domain, indices);
467 template <std::size_t... I>
468 void comma(
const std::string& s,
size_t& p,
char&
c)
const {
477 template <std::size_t... I>
481 # if AWALI_HAVE_CORRECT_LIST_INITIALIZER_ORDER
484 std::get<I>(sets_).
parse(s,p,fixed_domain))...);
486 value_t r = std::make_tuple((comma<I>(s, p,
c),
487 set<I>().
parse(s,p,fixed_domain))...);
495 const std::string&
format =
"text")
const
497 return print_(l, o,
format, indices);
501 template <std::size_t... I>
502 static std::string sname_(seq<I...>)
504 std::string res =
"lat<";
505 const char *sep =
"";
506 for (
auto n: {valueset_t<I>::sname()...})
516 template <
typename T, std::size_t... I>
518 conv_(
const T& tset,
typename T::value_t v, seq<I...>)
const
520 return std::make_tuple(std::get<I>(this->sets()).
conv(std::get<I>(tset.sets()),std::get<I>(v))...);
524 template <std::size_t... I>
525 std::string vname_(
bool full, seq<I...>)
const
527 std::string res =
"lat<";
528 const char *sep =
"";
529 for (
auto n: {set<I>().vname(
full)...})
539 template <std::size_t... I>
540 static tupleset make_(std::istream& i, seq<I...>)
542 # if AWALI_HAVE_CORRECT_LIST_INITIALIZER_ORDER
543 return tupleset{(eat_separator_<I>(i,
'<',
','),
544 valueset_t<I>::make(i))...};
547 ((eat_separator_<
sizeof...(ValueSets)-1 -I>(i,
'<',
','),
548 valueset_t<
sizeof...(ValueSets)-1 -I>::make(i))...);
552 template <std::size_t... I>
553 bool open_(
bool o, seq<I...>)
const
555 using swallow =
int[];
556 (void) swallow { set<I>().open(o)... };
557 std::swap(o, open__);
561 template <
typename... Args, std::size_t... I>
562 value_t value_(
const std::tuple<Args...>& args, seq<I...>)
const
564 return value_t{set<I>().value(std::get<I>(args))...};
567 template <std::size_t... I>
569 genset_(seq<I...>)
const
574 template <std::size_t... I>
575 static constexpr
bool
578 return all_<valueset_t<I>::is_free()...>();
581 template <
typename... Args, std::size_t... I>
583 word_(
const std::tuple<Args...>& l, seq<I...>)
const
585 return word_t{set<I>().word(std::get<I>(l))...};
588 template <std::size_t... I>
590 equals_(
const value_t& l,
const value_t& r, seq<I...>)
599 template <std::size_t... I>
601 less_than_(
const value_t& l,
const value_t& r, seq<I...>)
606 std::get<I>(l)))...})
614 template <std::size_t... I>
616 hash_(
const value_t& v, seq<I...>)
619 for (
auto h: {valueset_t<I>::hash(std::get<I>(v))...})
620 std::hash_combine(res, h);
624 template <std::size_t... I>
628 return std::make_tuple(valueset_t<I>::special()...);
631 template <std::size_t... I>
633 is_special_(
const value_t& l, seq<I...>)
635 for (
auto n: {valueset_t<I>::is_special(std::get<I>(l))...})
641 template <std::size_t... I>
643 zero_(seq<I...>)
const
645 return value_t{set<I>().zero()...};
648 template <std::size_t... I>
650 is_zero_(
const value_t& l, seq<I...>)
const
652 for (
auto n: {set<I>().is_zero(std::get<I>(l))...})
658 template <std::size_t... I>
659 static constexpr
bool
662 return all_<valueset_t<I>::has_one()...>();
665 template <std::size_t... I>
666 static constexpr
bool
667 is_ratexpset_(seq<I...>)
669 return all_<valueset_t<I>::is_ratexpset()...>();
672 template <std::size_t... I>
673 static constexpr
bool
674 is_letterized_(seq<I...>)
676 return all_<valueset_t<I>::is_letterized()...>();
679 template <std::size_t... I>
686 template <std::size_t... I>
688 is_one_(
const value_t& l, seq<I...>)
690 for (
auto n: {valueset_t<I>::is_one(std::get<I>(l))...})
696 template <std::size_t... I>
700 for (
auto n: {valueset_t<I>::show_one()...})
706 template <std::size_t... I>
708 add_(
const value_t& l,
const value_t& r, seq<I...>)
const
710 return value_t{set<I>().add(std::get<I>(l), std::get<I>(r))...};
713 template <std::size_t... I>
715 mul_(
const value_t& l,
const value_t& r, seq<I...>)
const
717 return value_t{set<I>().mul(std::get<I>(l), std::get<I>(r))...};
720 template <std::size_t... I>
722 rdiv_(
const value_t& l,
const value_t& r, seq<I...>)
const
724 return value_t{set<I>().rdiv(std::get<I>(l), std::get<I>(r))...};
727 template <std::size_t... I>
729 ldiv_(
const value_t& l,
const value_t& r, seq<I...>)
const
731 return value_t{set<I>().ldiv(std::get<I>(l), std::get<I>(r))...};
734 template <std::size_t... I>
736 star_(value_t
const& l, seq<I...>)
const
738 return value_t{set<I>().star(std::get<I>(l))...};
741 template <
typename Value, std::size_t... I>
743 delimit_(Value
const& l, seq<I...>)
const
745 return Value{set<I>().delimit(std::get<I>(l))...};
748 template <
typename Value, std::size_t... I>
750 undelimit_(Value
const& l, seq<I...>)
const
752 return Value{set<I>().undelimit(std::get<I>(l))...};
755 template <
typename LhsValue,
typename RhsValue, std::size_t... I>
757 concat_(
const LhsValue& l,
const RhsValue& r, seq<I...>)
const
759 return word_t{set<I>().concat(std::get<I>(l), std::get<I>(r))...};
762 template <std::size_t... I>
764 conv_(std::istream& i, seq<I...>)
const
766 # if AWALI_HAVE_CORRECT_LIST_INITIALIZER_ORDER
767 return std::make_tuple((eat_separator_<I>(i,
'[',
','),
768 set<I>().
conv(i))...);
772 std::get<
sizeof...(ValueSets)-1 - I>(sets_).
conv(i))...);
779 template <std::
size_t I>
781 eat_separator_(std::istream& i,
char first,
char tail)
783 eat(i, I == 0 ? first : tail);
784 while (isspace(i.peek()))
789 template <std::size_t... I>
791 print_(value_t
const& l, std::ostream& o,
792 const std::string&
format, seq<I...>)
const
796 using swallow =
int[];
799 (o << (I == 0 ?
'[' :
','),
800 set<I>().print(std::get<I>(l), o,
format),
808 template <std::size_t... I>
810 print_set_(std::ostream& o,
const std::string&
format,
813 const char *sep =
"";
816 else if (
format ==
"text")
822 raise(
"invalid format: ",
format);
823 using swallow =
int[];
826 (o << (I == 0 ?
"" : sep),
827 set<I>().print_set(o,
format),
835 template <unsigned version, std::size_t... I>
837 value_from_json(std::vector<json::node_t*> const& v, seq<I...>)
839 {return value_t{set<I>().template value_from_json<version>(v[I])...};}
841 template <unsigned version,std::size_t... I>
843 to_json(seq<I...>) const
848 json::object_t* obj = new json::object_t();
849 obj->push_back("labelKind", new json::string_t("Multi-tape"));
850 json::array_t* l = new json::array_t();
851 for (auto x: {this->set<I>().template to_json<version>()...})
853 obj->push_back("tapeLabels", l);
859 template <unsigned version,std::size_t... I>
861 value_to_json(value_t value, seq<I...> const) const
866 json::array_t* l = new json::array_t();
867 for (auto a : {this->set<I>().template value_to_json<version>(std::get<I>(value))...})
874 template <std::size_t... I>
876 transpose_(value_t const& l, seq<I...>) const
878 return value_t{(set<I>().transpose(std::get<I>(l)))...};
882 template <std::size_t... I>
884 meet_(const tupleset& rhs, seq<I...>) const
886 return tupleset{meet(set<I>(), rhs.set<I>())...};
890 template <std::size_t... I>
892 join_(const tupleset& rhs, seq<I...>) const
894 return tupleset{join(set<I>(), rhs.set<I>())...};
900 meet(const tupleset& lhs, const tupleset& rhs)
902 return lhs.meet_(rhs, indices);
908 meet(const tupleset& lhs, const b&)
916 meet(const b&, const tupleset& rhs)
924 join(const tupleset& lhs, const tupleset& rhs)
926 return lhs.join_(rhs, indices);
932 join(const tupleset& lhs, const b&)
940 join(const b&, const tupleset& rhs)
946 mutable bool open__ = false;
949 template<typename... Valuesets>
951 make_tupleset(std::tuple<Valuesets...> t) -> tupleset<Valuesets...> {
958 template <typename T1, typename T2>
959 struct concat_tupleset;
961 template <typename... T1, typename... T2>
962 struct concat_tupleset<tupleset<T1...>, tupleset<T2...>>
964 using type = tupleset<T1..., T2...>;
971 }//end of ns awali::stc
973 #endif // !AWALI_LABELSET_TUPLESET_HH
std::vector< node_t * > const & values
Definition: node.hh:436
virtual array_t const * array() const
Casts this node to array_t.
Definition: node.hh:207
The Boolean semring.
Definition: b.hh:38
bool value_t
Definition: b.hh:56
The semiring of complex numbers.
Definition: c.hh:44
The semiring of Natural numbers.
Definition: n.hh:34
The semiring of floating Numbers.
Definition: r.hh:35
A ValueSet which is a Cartesian product of ValueSets.
Definition: tupleset.hh:80
typename labelset_types< ValueSets... >::letter_t letter_t
A tuple of letters if meaningful, void otherwise.
Definition: tupleset.hh:100
tupleset(ValueSets... ls)
Definition: tupleset.hh:115
value_t rdiv(const value_t &l, const value_t &r) const
Definition: tupleset.hh:316
static constexpr bool has_one()
Definition: tupleset.hh:262
json::node_t * to_json() const
Definition: tupleset.hh:419
value_t conv(std::istream &i) const
Read one letter from i, return the corresponding value.
Definition: tupleset.hh:399
static constexpr bool is_ratexpset()
Definition: tupleset.hh:268
static constexpr bool is_commutative_semiring()
Definition: tupleset.hh:136
std::ostream & print_set(std::ostream &o, const std::string &format="text") const
Definition: tupleset.hh:412
genset_t genset() const
The generators. Meaningful for labelsets only.
Definition: tupleset.hh:181
value_t value(const std::tuple< Args... > &args) const
Construct a value.
Definition: tupleset.hh:174
value_t add(const value_t &l, const value_t &r) const
Definition: tupleset.hh:304
static constexpr bool is_free()
Definition: tupleset.hh:186
static std::string sname()
Definition: tupleset.hh:119
static bool equals(const value_t &l, const value_t &r)
Definition: tupleset.hh:225
value_t ldiv(const value_t &l, const value_t &r) const
Definition: tupleset.hh:322
value_t star(const value_t &l) const
Definition: tupleset.hh:328
std::tuple< typename ValueSets::value_t... > value_t
A tuple of values.
Definition: tupleset.hh:97
value_t value_from_json(json::node_t const *p) const
Definition: tupleset.hh:447
std::string vname(bool full=true) const
Definition: tupleset.hh:124
static constexpr bool is_letterized()
Definition: tupleset.hh:274
void comma(const std::string &s, size_t &p, char &c) const
Definition: tupleset.hh:468
tupleset(valuesets_t vs)
Definition: tupleset.hh:111
static value_t conv(self_type, value_t v)
Definition: tupleset.hh:379
static constexpr std::size_t size()
Definition: tupleset.hh:129
typename std::tuple_element< I, valuesets_t >::type valueset_t
The Ith valueset type.
Definition: tupleset.hh:90
word_t concat(const LhsValue &l, const RhsValue &r) const
Definition: tupleset.hh:361
value_t conv(const T &tset, typename T::value_t v) const
Definition: tupleset.hh:386
static tupleset make(std::istream &is)
Build from the description in is.
Definition: tupleset.hh:142
letter_t value_type
To be iterable.
Definition: tupleset.hh:107
static auto letters_of(const Value &v) -> decltype(letters_of_(v, indices))
Iterate over the letters of v.
Definition: tupleset.hh:209
static value_t special()
Definition: tupleset.hh:238
std::tuple< ValueSets... > valuesets_t
Definition: tupleset.hh:82
std::ostream & print(const value_t &l, std::ostream &o, const std::string &format="text") const
Definition: tupleset.hh:494
static value_t one()
Definition: tupleset.hh:280
Value undelimit(const Value &l) const
Remove first and last characters, that must be "special".
Definition: tupleset.hh:348
value_t parse(const std::string &s, size_t &p, bool fixed_domain=true) const
Definition: tupleset.hh:454
bool open(bool o) const
Whether unknown letters should be added, or rejected.
Definition: tupleset.hh:167
Value delimit(const Value &l) const
Add the special character first and last.
Definition: tupleset.hh:340
static bool less_than(const value_t &l, const value_t &r)
Whether l < r.
Definition: tupleset.hh:232
static bool is_one(const value_t &l)
Definition: tupleset.hh:286
std::set< value_t > convs(std::istream &) const
Definition: tupleset.hh:406
typename labelset_types< ValueSets... >::word_t word_t
A tuple of words if meaningful, void otherwise.
Definition: tupleset.hh:104
bool is_letter(const value_t &) const
Definition: tupleset.hh:298
static size_t hash(const value_t &v)
Definition: tupleset.hh:373
value_t zero() const
Definition: tupleset.hh:250
value_t conv(b, b::value_t v) const
Definition: tupleset.hh:392
bool is_zero(const value_t &l) const
Definition: tupleset.hh:256
static constexpr star_status_t star_status()
Definition: tupleset.hh:354
value_t transpose(const value_t &l) const
Definition: tupleset.hh:367
typename labelset_types< ValueSets... >::genset_t genset_t
A tuple of gensets if meaningful, void otherwise.
Definition: tupleset.hh:102
static bool is_special(const value_t &l)
Definition: tupleset.hh:244
static bool show_one()
Definition: tupleset.hh:292
bool is_tupleset_t
Definition: tupleset.hh:94
json::node_t * value_to_json(value_t v) const
Definition: tupleset.hh:432
const valuesets_t & sets() const
The componants valuesets, as a tuple.
Definition: tupleset.hh:152
value_t mul(const value_t &l, const value_t &r) const
Definition: tupleset.hh:310
auto word(const std::tuple< Args... > &v) const -> word_t
Convert to a word.
Definition: tupleset.hh:218
value_t parse_(const std::string &s, size_t &p, bool fixed_domain, seq< I... >) const
Definition: tupleset.hh:479
const valueset_t< I > & set() const
The Ith component valueset.
Definition: tupleset.hh:159
star_status_t
The different behaviours a weightset may have with respect to the star.
Definition: enums.hh:163
@ STARRABLE
The star of every element exists.
Definition: enums.hh:165
any_t word_t
Type for words; it is an alias to any_t since the precise type depends on the context (most of the ti...
Definition: typedefs.hh:67
auto make_inv_tuple() -> std::tuple<>
Definition: tuple.hh:179
node_t * parse(std::istream &, bool stop_after_metadata=false)
zip_sequences< Sequences... > zip(Sequences &&... seqs)
Definition: zip.hh:231
void genset_t
Definition: tupleset.hh:55
void word_t
Definition: tupleset.hh:57
void letter_t
Definition: tupleset.hh:56
std::tuple< typename ValueSets::word_t... > word_t
Definition: tupleset.hh:67
cross_sequences< Sequences... > cross(Sequences &&... seqs)
Definition: cross.hh:257
std::tuple< typename ValueSets::letter_t... > letter_t
Definition: tupleset.hh:66
A traits so that tupleset may define types that may exist.
Definition: tupleset.hh:54
constant< type_t::one, Label, Weight > one
Definition: fwd.hh:116
void eat(std::istream &is, char c)
Check lookahead character and advance.
Definition: stream.hh:62
RatExpSet::ratexp_t less_than(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: less_than.hh:166
RatExpSet::ratexp_t equals(const RatExpSet &rs, const typename RatExpSet::ratexp_t &v)
Definition: equal_visit.hh:153
auto format(const ValueSet &vs, const typename ValueSet::value_t &v, Args &&... args) -> std::string
Format v via vs.print.
Definition: stream.hh:109
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.
Definition: stream.hh:45
static const std::string full
Completely version of Awali as a std::string.
Definition: version.hh:42
Main namespace of Awali.
Definition: ato.hh:22
Exceptions thrown during parsing.
Definition: parse_exception.hh:26
Ignore its arguments.
Definition: raise.hh:34
marker type for labelsets where labels are tuples
Definition: kind.hh:84