Commit 9f1ca721 authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

refactoring of KeySIP like KeyS and KeySI

parent 0cdcfac0
......@@ -90,7 +90,6 @@
430045FF260496E6000E9898 /* ParsingEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FA6F722F97709003974DC /* ParsingEnv.cpp */; };
43004600260496E6000E9898 /* KeyS.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4373B456225163CC00D49D6C /* KeyS.cpp */; };
43004601260496E6000E9898 /* KeySI.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43552EDD22A6CFDE00C557BA /* KeySI.cpp */; };
43004602260496E6000E9898 /* KeySIP.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C692A22368A4CE0063ABAE /* KeySIP.cpp */; };
43004603260496E6000E9898 /* KeySIR.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 439D5228239E576500796CBA /* KeySIR.cpp */; };
43004604260496E6000E9898 /* KeySIO.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43CDB7932577899500956DDF /* KeySIO.cpp */; };
43004605260496E6000E9898 /* DuraList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C7232F2362E2D800D5E20E /* DuraList.cpp */; };
......@@ -503,7 +502,7 @@
4377138F231FBEE40089153C /* PointedRT.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 432F19E3204EE5D8002CD439 /* PointedRT.cpp */; };
43771390231FBF040089153C /* Label.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43979C6222A11762008BA390 /* Label.cpp */; };
437784B923881168007741A2 /* DuraList.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C7232F2362E2D800D5E20E /* DuraList.cpp */; };
437E8FFA2613382500B3150B /* RunTerm.tpp in Sources */ = {isa = PBXBuildFile; fileRef = 430046E42604CF3F000E9898 /* RunTerm.tpp */; };
437E8FFA2613382500B3150B /* RunTerm.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 430046E42604CF3F000E9898 /* RunTerm.cpp */; };
437EDA5C20EE419B008F0755 /* Rest.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437EDA5A20EE419B008F0755 /* Rest.cpp */; };
437FA6F922F97709003974DC /* ParsingEnv.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FA6F722F97709003974DC /* ParsingEnv.cpp */; };
437FC6C120BFFAC900AF0A2D /* InputSegmentMono.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 437FC6BF20BFFAC900AF0A2D /* InputSegmentMono.cpp */; };
......@@ -785,6 +784,7 @@
43D8484022A561BF007F7AE5 /* Transition.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4303B6D7229DE4B9003A91DF /* Transition.cpp */; };
43D8484122A561E0007F7AE5 /* Label.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43979C6222A11762008BA390 /* Label.cpp */; };
43D8484222A56208007F7AE5 /* SWTA.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 4356DABD22A45C8200E34F26 /* SWTA.cpp */; };
43DBCE4A26145F3800748B9B /* KeySIP.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43C692A22368A4CE0063ABAE /* KeySIP.cpp */; };
43DEAC0420A578F0007CBA83 /* SWTAFileIn.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43A378B02051557F00610A5C /* SWTAFileIn.cpp */; };
43E12D972211D83700B27B36 /* main.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43E12D962211D83700B27B36 /* main.cpp */; };
43E12DAF2211DFD900B27B36 /* TestWeight.cpp in Sources */ = {isa = PBXBuildFile; fileRef = 43E12DAE2211DFD900B27B36 /* TestWeight.cpp */; };
......@@ -1079,7 +1079,7 @@
3448BD311F0A484500B49FB8 /* Options.h */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.c.h; path = Options.h; sourceTree = "<group>"; };
43004671260496E6000E9898 /* squant2.2 */ = {isa = PBXFileReference; explicitFileType = "compiled.mach-o.executable"; includeInIndex = 0; path = squant2.2; sourceTree = BUILT_PRODUCTS_DIR; };
430046CC26049AEC000E9898 /* squant2.2.cpp */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.cpp.cpp; name = squant2.2.cpp; path = V2.2/targets/quant/squant2.2.cpp; sourceTree = "<group>"; };
430046E42604CF3F000E9898 /* RunTerm.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = RunTerm.tpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
430046E42604CF3F000E9898 /* RunTerm.cpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = RunTerm.cpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
430046E52604CF3F000E9898 /* RunTerm.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = RunTerm.hpp; sourceTree = "<group>"; };
430046F12604CF97000E9898 /* RunInner.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = RunInner.tpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
430046F22604CF97000E9898 /* RunInner.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = RunInner.hpp; sourceTree = "<group>"; };
......@@ -2232,7 +2232,7 @@
43EEB0A5224ACC56002A91E7 /* Runey.hpp */,
43C573C8224D2FD3003FA990 /* Runey.cpp */,
430046E52604CF3F000E9898 /* RunTerm.hpp */,
430046E42604CF3F000E9898 /* RunTerm.tpp */,
430046E42604CF3F000E9898 /* RunTerm.cpp */,
430046F22604CF97000E9898 /* RunInner.hpp */,
430046F12604CF97000E9898 /* RunInner.tpp */,
434F406422FB198300C3940E /* RunRanked.hpp */,
......@@ -2578,7 +2578,8 @@
43D0150B2522354C00E9F030 /* end2end-v2.txt */,
43D0150C2522354C00E9F030 /* end2end-v1.txt */,
);
path = E2E;
name = E2E;
path = squant2;
sourceTree = "<group>";
};
43E12D952211D83700B27B36 /* GTests */ = {
......@@ -3137,7 +3138,6 @@
430045FF260496E6000E9898 /* ParsingEnv.cpp in Sources */,
43004600260496E6000E9898 /* KeyS.cpp in Sources */,
43004601260496E6000E9898 /* KeySI.cpp in Sources */,
43004602260496E6000E9898 /* KeySIP.cpp in Sources */,
43004603260496E6000E9898 /* KeySIR.cpp in Sources */,
43004604260496E6000E9898 /* KeySIO.cpp in Sources */,
43004605260496E6000E9898 /* DuraList.cpp in Sources */,
......@@ -3340,7 +3340,7 @@
43F62EEA25DEC2F2006824CA /* AEVisitor.cpp in Sources */,
43E76050237475B4004426E6 /* SpanningElement.cpp in Sources */,
4321C81325A4ED4000486DAD /* CMND.cpp in Sources */,
437E8FFA2613382500B3150B /* RunTerm.tpp in Sources */,
437E8FFA2613382500B3150B /* RunTerm.cpp in Sources */,
43B4A9D4258418EB00FDD201 /* StaffDef.cpp in Sources */,
43D7B19C259A46C500D62892 /* Staffed.cpp in Sources */,
4331FC6A259B320500895F0F /* StaffRange.cpp in Sources */,
......@@ -3348,6 +3348,7 @@
43E6736E24AF303A008FA821 /* Notehead.cpp in Sources */,
43F1949B25223D6F00F0D2AB /* Beam.cpp in Sources */,
43E67596251CE77200EB8B2A /* TupletLabel.cpp in Sources */,
43DBCE4A26145F3800748B9B /* KeySIP.cpp in Sources */,
43CEA93925EFD1E5007C355B /* ValueFile.cpp in Sources */,
43E1AF3324ADCEA60018DFF1 /* Arpeggio.cpp in Sources */,
432CFF7924F9AAFD00036C17 /* Union.cpp in Sources */,
......
......@@ -39,6 +39,7 @@ _barlen(len)
}
// successor key (fs or ns or instance)
KeySI::KeySI(const KeySI& k, const RunInner<KeySI>& r, const ParsingEnv& env):
KeyS(k, (const RunInner<KeyS>&) r, env),
_barlen(k._barlen) // k is the parent (fs, ns) or matcher (instanciate)
......@@ -253,7 +254,6 @@ bool KeySI::complete() const
}
size_t KeySI::size() const
{
assert(_interval);
......@@ -343,8 +343,8 @@ void KeySI::RunsTerminal(const Transition& t,
RunFactory<KeySI>::terminalRun(*this, t, a, w, len, st);
}
}
else /// double bar (end of score)
if (SymbLabel::bar(a))
/// double bar (end of score)
else if (SymbLabel::bar(a) && (SymbLabel::arity(a) == 0))
{
if ((_interval->rduration() < _barlen) && (! _interval->inhabited()))
{
......
......@@ -25,6 +25,7 @@
namespace Parsing{
/// Key (augmented SWTA state) made of
/// - one state of a base WTA
/// - one interval (inherited attribute)
......@@ -59,12 +60,12 @@ public:
/// that will be the (complete) target of run r.
KeySI(const KeySI& k, const RunInner<KeySI>& r, const ParsingEnv& env);
/// @brief constructor of an instance of a key targeting a terminal run.
/// @param k target key for the run in 2 cases below. must be partial.
/// @param r a complete terminal run targeting the key constructed.
/// @param env input environment.
/// Construct a complete instance of k that will be the target of run r.
KeySI(const KeySI& k, const RunTerm& r, const ParsingEnv& env);
// @brief constructor of an instance of a key targeting a terminal run.
// @param k target key for the run in 2 cases below. must be partial.
// @param r a complete terminal run targeting the key constructed.
// @param env input environment.
// Construct a complete instance of k that will be the target of run r.
// KeySI(const KeySI& k, const RunTerm& r, const ParsingEnv& env);
/// @brief copy
KeySI(const KeySI& k);
......
......@@ -11,7 +11,6 @@
namespace Parsing{
// static
//pre_t KeySIP::P_UNKNOWN = -1;
......@@ -26,26 +25,24 @@ namespace Parsing{
// internal use
KeySIP::KeySIP(state_t s, IntervalTree* node, rtu_t len,
KeySIP::KeySIP(state_t s,
IntervalTree* node,
rtu_t barlen,
pre_t pre, pre_t post):
KeySI(s, node, len),
KeySI(s, node, barlen),
_pre(pre),
_post(post)
{ }
// copy
KeySIP::KeySIP(const KeySIP& k):
KeySI(k),
_pre(k._pre),
_post(k._post)
{ }
{
assert(node);
}
// top constructor (top state = first bar)
KeySIP::KeySIP(rtu_t len, const ParsingEnv& env, bool f_open,
KeySIP::KeySIP(rtu_t barlen,
const ParsingEnv& env,
bool f_open,
pre_t pre, pre_t post):
KeySI(len, env, f_open),
KeySI(barlen, env, f_open),
_pre(pre),
_post(post)
{
......@@ -53,130 +50,281 @@ _post(post)
}
// firstChild
KeySIP::KeySIP(const KeySIP& parent,
const Transition& tr,
// successor key (fs or ns or instance)
KeySIP::KeySIP(const KeySIP& k,
const RunInner<KeySIP>& r,
const ParsingEnv& env):
KeySI(parent, tr, env),
_pre(parent._pre),
_post(P_UNKNOWN)
KeySI(k, (const RunInner<KeySI>&) r, env),
_pre(k._pre), // k is the parent (fs, ns) or matcher (instanciate)
_post(k._post)
{
assert(r.inner());
const Transition& t = r.origin();
assert(t.inner());
assert(0 < t.size());
assert(t.size() <= t.arity());
// first child, k is parent
if (r.empty())
{
assert(r.partial());
assert(_state == t.state(0)); // check downcast of KeyS
init_firstchild(k, t, env);
}
// instanciate head (inner case)
else if (r.complete())
{
assert(r.size() == t.size());
assert(_state == k._state); // check downcast of KeyS
assert(r.size() > 0);
const KeySIP* last = r.key(r.size()-1);
assert(last);
init_instanciate(k, *last, t, env);
}
// next sibling
else
{
assert(r.partial());
size_t i = r.size(); // index of next sibling in r
assert(0 < i); // there is at least one previous sibling
assert(i < t.size()); // r partial
assert(_state == t.state(i)); // check downcast of KeyS
const KeySIP* last = r.key(i-1);
assert(last);
init_nextsibling(k, *last, t, i, env);
}
}
void KeySIP::init_firstchild(const KeySIP& parent,
const Transition& t,
const ParsingEnv& env)
{
assert(P_KNOWN(parent._pre));
label_t a = t.label();
assert((! SymbLabel::bar(a)) || (t.arity() == 2));
assert((! SymbLabel::bar(a)) || (t.multiplicity(0) == 1));
if (SymbLabel::tuple(a) || SymbLabel::bar(a)) // || SymbLabel::rest(a))
{
_pre = parent._pre;
_post = P_UNKNOWN;
}
else /// @todo cases grace and fork
{
ERROR("KeySIP.firstChild: unexpected symbol in tr: _state -> {}", t);
}
assert(P_KNOWN(_pre));
// construction of sub-interval is the same as in KeySI
/// @todo cases grace and fork
}
// nextSibling
KeySIP::KeySIP(const KeySIP& previous,
size_t i,
const KeySIP& parent,
const Transition& tr,
const ParsingEnv& env):
KeySI(previous, i, parent, tr, env),
_pre(previous._post),
_post(P_UNKNOWN)
void KeySIP::init_nextsibling(const KeySIP& parent,
const KeySIP& previous,
const Transition& t,
size_t i, // index of next
const ParsingEnv& env)
{
assert(t.inner());
assert(previous.complete());
// construction of sub-interval is the same as in KeySI
label_t a = t.label();
assert((! SymbLabel::bar(a)) || (t.arity() == 2));
assert((! SymbLabel::bar(a)) || (t.multiplicity(0) == 1));
if (SymbLabel::tuple(a) || SymbLabel::bar(a))
{
_pre = previous._post;
_post = P_UNKNOWN;
}
/// @todo cases grace and fork
else
{
ERROR("KeySIP.nextSibling: unexpected symbol in tr: _state -> {}", t);
}
assert(P_KNOWN(_pre));
}
// instanciate: copy and propagation of post
KeySIP::KeySIP(const KeySIP& head,
label_t a,
const KeySIP& lastchild,
const ParsingEnv& env):
KeySI(head, a, lastchild, env),
_pre(head._pre),
_post(lastchild._post)
void KeySIP::init_instanciate(const KeySIP& head,
const KeySIP& last,
const Transition& t,
const ParsingEnv& env)
{
assert(head.partial());
assert(lastchild.complete());
// this key is now complete
assert(P_KNOWN(this->_pre));
assert(P_KNOWN(this->_post));
assert(head.partial()); // will be copied and completed
assert(last.complete());
assert(P_KNOWN(head._pre));
_pre = head._pre; // copy
assert(P_KNOWN(last._post));
_post = last._post; // up-propagation of post
assert(this->complete());
}
// complete leaf: copy and computation of post
KeySIP::KeySIP(const KeySIP& head,
label_t a,
const ParsingEnv& env):
KeySI(head, a, env),
_pre(head._pre)
// copy
KeySIP::KeySIP(const KeySIP& k):
KeySI(k),
_pre(k._pre),
_post(k._post)
{ }
// copy and instanciate.
KeySIP::KeySIP(const KeySIP& k, pre_t post):
KeySI(k),
_pre(k._pre),
_post(post)
{
assert(SymbLabel::terminal(a));
assert(_interval);
// case of terminal bar symbol (double bar)
assert(k.partial());
assert(P_KNOWN(post));
assert(this->complete());
}
assert(P_KNOWN(_pre));
if (P_KNOWN(head._post))
{
ERROR("KeySIP : instance as leaf of complete ptr {}", head);
_post = head._post;
assert(_post == _interval->rsize());
}
else
KeySIP::~KeySIP()
{ }
KeySIP& KeySIP::operator= (const KeySIP& p)
{
if(this != &p)
{
if (! _interval->aligned())
{
assert(SymbLabel::bar(a));
assert(env.segment);
_interval->align(env.segment);
}
_post = _interval->rsize();
}
assert(P_KNOWN(_post));
KeySI::operator=(p);
_pre = p._pre;
_post = p._post;
};
return *this;
}
Run<KeySIP>* KeySIP::failRun(const ParsingEnv& env) const
bool KeySIP::operator==(const KeySIP& p) const
{
assert(env.wta);
const Transition& t1 = env.dummyTransition_one();
assert(t1.weight().one());
if (! _interval->aligned())
{
assert(env.segment);
_interval->align(env.segment);
}
label_t a = SymbLabel::make_fail(this->size());
return (KeySI::operator==(p) &&
(_pre == p._pre) &&
(_post == p._post));
}
return new Run<KeySIP>(t1, a, t1.weight());
bool KeySIP::operator!=(const KeySIP& p) const
{
return (! (*this == p));
}
bool KeySIP::instance(const KeySIP& p) const
{
assert(_pre != P_UNKNOWN);
assert(p._pre != P_UNKNOWN);
return (KeySI::instance(p) && ((p._post == P_UNKNOWN) ||
(_post == p._post)));
}
bool KeySIP::complete() const
{
assert(_pre != P_UNKNOWN);
return (KeySI::complete() && (_post != P_UNKNOWN));
}
size_t KeySIP::size() const
{
assert(_pre != P_UNKNOWN);
assert(_interval);
assert(_interval->aligned());
return _pre + _interval->lsize();
}
void KeySIP::leaves(const Transition& tr,
const ParsingEnv& env,
std::queue<KeySIP>& q) const
void KeySIP::runs(std::stack<RunInner<KeySIP>*>& si,
std::stack<std::pair<const KeySIP*, RunTerm*>>& st,
ParsingEnv& env) const
{
assert(this->partial());
assert(q.empty());
size_t nbl = leaf(tr, env);
if (nbl == 0)
RunFactory<KeySIP>::runs(*this, si, st, env);
}
void KeySIP::RunsTerminal(const Transition& t,
std::stack<std::pair<const KeySIP*, RunTerm*>>& st,
const ParsingEnv& env) const
{
assert(P_KNOWN(_pre));
assert(_interval);
const label_t& a = t.label();
assert(SymbLabel::terminal(a));
const KeySIP* knew = nullptr;
if (! _interval->aligned())
{
assert(SymbLabel::bar(a));
WARN("KeySIP RunsTerminal: unaligned interval {} for {}",
*(_interval), SymbLabel::to_string(a));
assert(env.segment);
_interval->align(env.segment);
}
if (P_KNOWN(_post))
{
WARN("KeySIP : instance as leaf of complete key {}", *this);
if (_post == _interval->rsize())
{
knew = this;
}
else
return;
}
else
{
//copy with _post = _interval->rsize();
knew = new KeySIP(*this, _interval->rsize());
}
assert(knew);
assert(P_KNOWN(knew->_post));
Weight w = terminalWeight(a, t, env); // terminalWeight(a, t, env);
assert(! w.unknown());
/// filter term label
if (SymbLabel::note(a) || SymbLabel::rest(a) ||
SymbLabel::continuation(a) || SymbLabel::dot(a))
{
if ((_post != P_UNKNOWN) && (_post != _interval->rsize()))
return; // add nothing
assert(_pre != P_UNKNOWN);
size_t n = SymbLabel::nbEvents(a);
if (n == _pre + _interval->lsize())
{
// n points = grace-notes + grace-rests + 1 note or -1
RunFactory<KeySIP>::terminalRun(*knew, t, a, w, n, st);
}
}
/// double bar (end of score)
else if (SymbLabel::bar(a) && (SymbLabel::arity(a) == 0))
{
TRACE("KeySIP: terminal bar interval: {} ({}aligned) for transition {}",
*(_interval),
((! _interval->aligned())?"not ":""),
t);
// && (DBL_APPROX_EQUAL(_interval->rend(), _interval->rbegin()))
if ((_interval->rduration() < _barlen) && (! _interval->inhabited()))
{
// 0 or -1 point to read in interval
RunFactory<KeySIP>::terminalRun(*knew, t, a, w, 0, st);
}
}
else if (SymbLabel::dummy(a))
{
ERROR("KeySIP: leaves called for 0 leaf");
ERROR("KeySIP: dummy symbol unexpected");
}
else if (nbl == 1)
else
{
ERROR("KeySIP: leaves called for 1 leaf, call cstr instead");
label_t a = tr.label();
q.emplace(*this, a, env);
ERROR("KeySIP: unexpected terminal symbol {}", SymbLabel::to_string(a));
}
}
// KeySIP::innerWeight = KeySI::innerWeight
Weight KeySIP::terminalWeight(label_t a,
const Transition& tr,
......@@ -185,7 +333,7 @@ Weight KeySIP::terminalWeight(label_t a,
assert(tr.terminal());
assert(SymbLabel::terminal(a));
assert(this->complete());
assert(this->leaf(tr, env) != 0);
// assert(this->leaf(tr, env) != 0);
// tweight = transition weight = complexity
Weight tweight = tr.weight(); // copy
......@@ -240,116 +388,60 @@ Weight KeySIP::terminalWeight(label_t a,
}
// KeySIP::filterInnerLabel: same as for KeySI
/// we do not filter out grace rests
bool KeySIP::filterTerminalLabel(const label_t& a,
const ParsingEnv& env) const
void KeySIP::RunsInner(const Transition& t,
std::stack<RunInner<KeySIP>*>& si,
const ParsingEnv& env) const
{
assert(SymbLabel::terminal(a));
assert(_interval);
if (! _interval->aligned())
assert(State::isWTA(_state));
const label_t& a = t.label();
assert(SymbLabel::inner(a));
if (SymbLabel::tuple(a))
{
WARN("KeySIP: unaligned interval {} for filtering {}",
*(_interval), SymbLabel::to_string(a));
assert(env.segment);
_interval->align(env.segment);
assert(_interval);
// we do not divide empty intervals
if (_interval->inhabited())
RunFactory<KeySIP>::innerRun(*this, t, si);
}
/// double bar (end of score)
if (SymbLabel::bar(a))
else if (SymbLabel::grace(a))
{
assert(_interval);
// && (DBL_APPROX_EQUAL(_interval->rend(), _interval->rbegin()))
return ((_interval->rduration() < _barlen) &&
(! _interval->inhabited()));
// 0 or -1 point to read in interval
assert(SymbLabel::arity(a) == 2);
ERROR("KeySIP: outside g.n. not supported, label={}",
SymbLabel::to_string(a));
}
else if (SymbLabel::dummy(a))
else if (SymbLabel::fork(a))
{
WARN("KeySIP: filter dummy symbol");
return false;