Commit 295ad3d9 authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

sum of multiplicities in Production/Transition's rules body

parent 45793b12
......@@ -12,12 +12,22 @@
#include "Production.hpp"
Production::BodyItem::BodyItem(state_t s, size_t u, size_t m):
state(s),
mult(m),
sum(u)
{
assert(m > 0);
}
// initial counter value
size_t Production::_cpt = 0;
Production::Production():
_body(),
_body(), // empty
_weight(),
_arity(0),
_id(_cpt++)
......@@ -25,7 +35,7 @@ _id(_cpt++)
Production::Production(const Weight& w):
_body(),
_body(), // empty
_weight(w),
_arity(0),
_id(_cpt++)
......@@ -35,7 +45,7 @@ _id(_cpt++)
Production::Production(LetterWeight* lw):
_body(),
_body(), // empty
_weight(lw),
_arity(0),
_id(_cpt++)
......@@ -67,7 +77,7 @@ _id(_cpt++)
Production::Production(state_t s, const Weight& w):
_body(),
_body(), // empty
_weight(w),
_arity(0),
_id(_cpt++)
......@@ -78,7 +88,7 @@ _id(_cpt++)
Production::Production(state_t s, LetterWeight* lw):
_body(),
_body(), // empty
_weight(lw),
_arity(0),
_id(_cpt++)
......@@ -170,11 +180,19 @@ size_t Production::multiplicity(size_t i) const
}
size_t Production::multiplicities(size_t i) const
{
assert (0 <= i);
assert (i < _body.size());
return (_body[i].sum);
}
void Production::push(state_t s, size_t m)
{
assert(m > 0);
_body.emplace_back(s, m); // _body.push_back(std::make_pair(s, m));
_arity += m;
_body.emplace_back(s, _arity, m); // _body.push_back(std::make_pair(s, m));
}
......@@ -232,7 +250,7 @@ std::ostream& operator<<(std::ostream& o, const Production& t)
else if (t.inner())
{
o << "( ";
for(StateMultList::const_iterator i = t._body.cbegin();
for(Body::const_iterator i = t._body.cbegin();
i != t._body.cend();
++i)
{
......
......@@ -25,21 +25,6 @@
#include "Weight.hpp"
struct StateMult
{
public:
StateMult(state_t s, size_t m=1):state(s), mult(m) { assert(m > 0); }
state_t state;
size_t mult;
};
// typedef std::vector<state_t> StateMultList;
//typedef std::vector<std::pair<state_t, size_t>> StateMultList;
typedef std::vector<StateMult> StateMultList;
// poor man's definition of iterators
// typedef std::vector<state_t>::iterator Transition_iterator;
// typedef std::vector<state_t>::const_iterator Transition_const_iterator;
......@@ -69,10 +54,30 @@ class Production
{
template<typename> friend class WFSM;
protected:
struct BodyItem
{
public:
BodyItem(state_t s, size_t u, size_t m=1);
/// state of body item
state_t state;
/// multiplicity for body item
size_t mult;
/// cumulated sum of multiplicities
size_t sum;
};
public:
// typedef StateMultList::iterator iterator;
// typedef StateMultList::const_iterator const_iterator;
// typedef Body::iterator iterator;
// typedef Body::const_iterator const_iterator;
// typedef std::vector<state_t> Body;
// typedef std::vector<std::pair<state_t, size_t>> Body;
typedef std::vector<BodyItem> Body;
/// production rule with unknown weight and empty body.
Production();
......@@ -158,6 +163,16 @@ public:
/// @param i index of children. must be between 0 and size - 1.
/// @return the multiplicity of the children i in the body of this rule.
size_t multiplicity(size_t i) const;
/// sum of the multiplicities in the body of this production rule
/// up to the given index (included).
/// @param i index of children. must be between 0 and size - 1.
/// @return the sum of multiplicities up to the children i in the body of this rule.
size_t multiplicities(size_t i) const;
/// the multiplicity values of elements of the body up to i (included) is 1.
/// @param i index of children. must be between 0 and size - 1.
inline bool nomult(size_t i) const { return (multiplicities(i) == i+1); }
/// add given state at the end of the body of this production rule,
/// with given multiplicity.
......@@ -172,10 +187,10 @@ public:
virtual size_t arity() const;
/// constant iterator pointing to the first state in the body of the production. rule.
StateMultList::const_iterator cbegin() const { return _body.cbegin(); }
Body::const_iterator cbegin() const { return _body.cbegin(); }
/// constant iterator pointing to the end of the body of the production rule.
StateMultList::const_iterator cend() const { return _body.cend(); }
Body::const_iterator cend() const { return _body.cend(); }
/// whether the given state belongs to the body of this production rule.
bool member(state_t) const;
......@@ -193,8 +208,11 @@ public:
friend std::ostream& operator<<(std::ostream&, const Production&);
protected:
StateMultList _body;
/// antecedents (source states) of the production (with resp. multiplicities)
Body _body;
/// weight attached to the production rule
Weight _weight;
/// 0 for terminal production and
......@@ -205,11 +223,13 @@ protected:
/// it reflects the order of creation.
/// @todo set id as param of constructor
size_t _id;
private:
/// counter for the creation of production rule's ids.
static size_t _cpt;
};
......
......@@ -90,7 +90,7 @@ std::ostream& operator<<(std::ostream& o, const Transition& t)
else if (t.inner())
{
o << "(";
StateMultList::const_iterator i = t._body.cbegin();
Body::const_iterator i = t._body.cbegin();
while(i != t._body.cend())
{
state_t s = i->state;
......
......@@ -113,7 +113,7 @@ Tlist<T>& WFSM<T>::add(state_t s, const T& t, bool initial)
Tlist<T>& tl = add(s, initial);
tl.add(t);
// register all states in the body
for (StateMultList::const_iterator i = t.cbegin(); i != t.cend(); i++)
for (Body::const_iterator i = t.cbegin(); i != t.cend(); i++)
{
state_t sub = i->state;
add(sub, false);
......@@ -132,7 +132,7 @@ Tlist<T>& WFSM<T>::add(state_t s, const T& t, bool initial)
// tl.emplace(std::make_tuple(std::forward<Args>(args)...));
// const T& t = tl.last();
//
// for (StateMultList::const_iterator i = t.cbegin(); i != t.cend(); i++)
// for (Body::const_iterator i = t.cbegin(); i != t.cend(); i++)
// {
// state_t sub = *i;
// add(sub, false);
......@@ -273,7 +273,7 @@ size_t WFSM<T>::resolution() const
{
res1 = Rational::lcm(res1, a);
// add states in the body of the transition to reach set
for (typename StateMultList::const_iterator i = t.cbegin();
for (typename Body::const_iterator i = t.cbegin();
i != t.cend(); i++)
reach->insert(i->state);
}
......@@ -326,7 +326,7 @@ std::set<state_t> WFSM<T>::allStates() const
const T& t = *it;
if (t.inner())
{
for (typename StateMultList::const_iterator is = t.cbegin();
for (typename Body::const_iterator is = t.cbegin();
is != t.cend(); is++)
res.insert(is->state);
}
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment