Commit 2a45a74e authored by Florent Jacquemard's avatar Florent Jacquemard
Browse files

some copy constructors for Records

separation TableRecords (from ParseTable)
parent 9f7531d8
......@@ -1552,6 +1552,8 @@
43E2728D2315696C000D2DF4 /* TreePosition.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = TreePosition.hpp; sourceTree = "<group>"; };
43E27291231589B5000D2DF4 /* LRT.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LRT.cpp; sourceTree = "<group>"; };
43E27292231589B5000D2DF4 /* LRT.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = LRT.hpp; sourceTree = "<group>"; };
43E2B536267E32EB004B4803 /* TableRecords.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = TableRecords.tpp; sourceTree = "<group>"; };
43E2B537267E32EB004B4803 /* TableRecords.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = TableRecords.hpp; sourceTree = "<group>"; };
43E3FDD120237FA300DBB754 /* Rune.tpp */ = {isa = PBXFileReference; explicitFileType = sourcecode.cpp.cpp; path = Rune.tpp; sourceTree = "<group>"; xcLanguageSpecificationIdentifier = xcode.lang.cpp; };
43E3FDD220237FA300DBB754 /* Rune.hpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.h; path = Rune.hpp; sourceTree = "<group>"; };
43E6736324AF2E46008FA821 /* LeafNote.cpp */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.cpp.cpp; path = LeafNote.cpp; sourceTree = "<group>"; };
......@@ -2214,10 +2216,6 @@
isa = PBXGroup;
children = (
43770CD1235863A700CC8869 /* CMakeLists.txt */,
437FA6F822F97709003974DC /* ParsingEnv.hpp */,
437FA6F722F97709003974DC /* ParsingEnv.cpp */,
433278E2260DC6CA00D6AD17 /* RunFactory.hpp */,
433278E1260DC6CA00D6AD17 /* RunFactory.tpp */,
43552EE122A6DFCD00C557BA /* Key.hpp */,
43552EE022A6DFCD00C557BA /* Key.tpp */,
43552ED422A6ADA800C557BA /* Ranked.hpp */,
......@@ -2234,6 +2232,8 @@
43CDB7932577899500956DDF /* KeySIO.cpp */,
4352BE2E25E40EF600F045BC /* KeySV.hpp */,
4352BE2D25E40EF600F045BC /* KeySV.cpp */,
433278E2260DC6CA00D6AD17 /* RunFactory.hpp */,
433278E1260DC6CA00D6AD17 /* RunFactory.tpp */,
43979C6B22A11DBC008BA390 /* RunFilterMode.hpp */,
43847D7D23F5980C00059835 /* RunFilterMode.cpp */,
439EA0BB224CCF91006E742E /* RunFilter.hpp */,
......@@ -2260,12 +2260,16 @@
43D0E404230173F9001C12C1 /* RecordQueue.tpp */,
43472DF722F86A0500BF65CF /* TableInstances.hpp */,
43472DF622F86A0500BF65CF /* TableInstances.tpp */,
43E2B537267E32EB004B4803 /* TableRecords.hpp */,
43E2B536267E32EB004B4803 /* TableRecords.tpp */,
431A339222FC8A2F0072DEC8 /* TableInstPair.hpp */,
431A339122FC8A2F0072DEC8 /* TableInstPair.tpp */,
4354C4E122FEB70A001E78DD /* TableA.hpp */,
4354C4E222FEB70A001E78DD /* TableA.tpp */,
4352B25922FC1EB600259764 /* TableParse.hpp */,
4352B25822FC1EB600259764 /* TableParse.tpp */,
437FA6F822F97709003974DC /* ParsingEnv.hpp */,
437FA6F722F97709003974DC /* ParsingEnv.cpp */,
4316CD1025F3832E00359F78 /* export */,
);
path = parsing;
......
......@@ -35,9 +35,15 @@ public:
/// be set later to a non-NULL ptr with setKey.
RecordOne(RunCompare comp, const RunTerm* failrun);
/// copy
RecordOne(const RecordOne&);
/// deleting a RecordOne frees the best run contained.
virtual ~RecordOne();
/// @brief assignement.
virtual RecordOne<K>& operator= (const RecordOne<K>& rec);
virtual Recordo* clone() const;
/// @brief add a run to the record or delete.
......@@ -48,7 +54,6 @@ public:
virtual size_t addTerm(const RunTerm* r);
virtual size_t addInner(const RunInnero* r);
/// @brief returns the 1-th best run of the record.
/// @param n rank, must be 1 for RecordOne.
/// @return the n-th best run of the record or, if there is none,
......@@ -65,7 +70,7 @@ protected:
private:
/// Common code to addTerm and addInner
/// Common code for addTerm and addInner
/// In this class of Recoreds, there is no distinction between the 2 types
/// of Runs.
size_t generic_add(const Run* r);
......@@ -83,8 +88,3 @@ private:
#endif /* RecordOne_hpp */
/// @}
......@@ -16,6 +16,13 @@ _best(nullptr) // initialize with worst run
{ }
template<class K>
RecordOne<K>::RecordOne(const RecordOne<K>& rec):
Record<K>(rec),
_best(rec._best)
{ }
template<class K>
RecordOne<K>::~RecordOne()
{
......@@ -35,6 +42,18 @@ RecordOne<K>::~RecordOne()
}
template<class K>
RecordOne<K>& RecordOne<K>::operator= (const RecordOne<K>& rec)
{
if(this != &rec)
{
Record<K>::operator=(rec);
_best = rec._best;
};
return *this;
}
template<class K>
Recordo* RecordOne<K>::clone() const
{
......@@ -47,6 +66,7 @@ template<class K>
const Run* RecordOne<K>::best(size_t n)
{
assert(n == 1);
// std::string s = SymbLabel::to_string(_best->origin().label());
assert ((_best == nullptr) || _best->complete());
assert ((_best == nullptr) || (! _best->weight().unknown()));
const K* key = this->_key;
......@@ -57,7 +77,7 @@ const Run* RecordOne<K>::best(size_t n)
if (_best == nullptr)
{
TRACE("RecordOne[{}].best : no best run", *key);
const Run* fail = this->_fail;
const RunTerm* fail = this->_fail;
assert((fail == nullptr) || fail->fail());
if (fail == nullptr)
......@@ -124,7 +144,7 @@ size_t RecordOne<K>::generic_add(const Run* r)
if (r->partial())
{
ERROR("RecordOne[{}].add: reject partial run {}", thiskey, *r);
ERROR("RecordOne[{}].add: partial run {} rejected", thiskey, *r);
delete r;
return 5;
}
......
......@@ -55,7 +55,7 @@ public:
Record() { assert(false); }
/// @brief empty record associated to k.
/// @brief empty record associated to a key of type K.
/// @param comp function for comparing Runs over K.
/// @param failrun fixed run returned by best when there is no best run.
/// @todo delete failrun if not necessary (replaced by method)
......@@ -63,6 +63,9 @@ public:
/// be set later to a non-NULL ptr with setKey.
Record(RunCompare comp, const RunTerm* failrun);
/// copy
Record(const Record&);
virtual ~Record();
......@@ -107,7 +110,7 @@ public:
protected:
/// copy of the key associated to the record.
/// key associated to the record.
const K* _key;
/// comparison function.
......
......@@ -28,6 +28,16 @@ _nb_best_rejected(0)
}
template<class K>
Record<K>::Record(const Record& rec):
_key(rec._key),
_comp(rec._comp),
_fail(rec._fail),
_nb_cand(rec._nb_cand),
_nb_best_rejected(rec._nb_best_rejected)
{ }
template<class K>
Record<K>::~Record()
{
......
......@@ -265,7 +265,7 @@ const Transition& RunFactory<K>::barTransition2(state_t state, ParsingEnv& env)
template<class K>
RunTerm* RunFactory<K>::failRun(const K& k,
const ParsingEnv& env)
const ParsingEnv& env)
{
assert(env.wta);
const Transition& t0 = env.dummyTransition_zero();
......
......@@ -17,22 +17,40 @@ RunInnero::RunInnero(const Transition& tr):
Run(tr)
{ }
RunInnero::RunInnero(const Transition& tr, label_t lab, const Weight& w):
Run(tr, lab, w)
{ }
RunInnero::RunInnero(const RunTerm& r):
Run((const Run&) r) // downcast
{ }
RunInnero::RunInnero(const RunInnero& r):
Run(r)
{ }
RunInnero::~RunInnero()
{ }
} // end namespace parsing
bool RunInnero::terminal() const
{
assert(arity() > 0);
return false;
}
bool RunInnero::inner() const
{
assert(arity() > 0);
return true;
}
} // end namespace parsing
......@@ -49,6 +49,13 @@ public:
virtual RunInnero* clone() const=0;
/// this run is terminal (leaf).
virtual bool terminal() const;
/// this run is inner.
virtual bool inner() const;
};
......
......@@ -124,6 +124,20 @@ bool RunTerm::complete() const
}
bool RunTerm::terminal() const
{
assert(arity() == 0);
return true;
}
bool RunTerm::inner() const
{
assert(arity() == 0);
return false;
}
void RunTerm::print(std::ostream& o) const
{
SymbLabel::print(_label, o);
......
......@@ -73,6 +73,12 @@ public:
/// a terminal run is complete
virtual bool complete() const;
/// this run is terminal (leaf).
virtual bool terminal() const;
/// this run is inner.
virtual bool inner() const;
/// Visitor
virtual size_t addto(Recordo* rec) const;
......
......@@ -120,6 +120,14 @@ size_t Run::arity() const
}
//bool Run::complete() const
//{
// ERROR("Run (abstract class): complete() called (origin={})",
// SymbLabel::to_string(_origin.label()));
// return false;
//}
//template<class K>
bool Run::filtered() const
{
......
......@@ -38,6 +38,10 @@ template<class K> class Record;
//const Run* Record<K>::best(size_t);
/// abstract class for runs,
/// which are compact representations of parse trees,
/// as a tuple of pointers to subruns.
///
/// a run is associated with as an augmented transition, with
/// - a label
/// - an arity value (number of children expected)
......@@ -46,12 +50,6 @@ template<class K> class Record;
/// (pointer to a row in parse table)
/// - a weight (updatable)
/// - a list of relative durations
///
/// Runs are compact representations of parse trees,
/// as a tuple of pointers to subruns.
///
/// A run is complete when the length of the list of children is the arity.
/// It is partial otherwise.
class Run
{
public:
......@@ -126,10 +124,10 @@ public:
bool filtered() const;
/// this run is terminal (leaf).
bool terminal() const { return (arity() == 0); }
virtual bool terminal() const = 0;
/// this run is inner.
bool inner() const { return (arity() > 0); }
virtual bool inner() const = 0;
/// this run is a ranked run
virtual bool ranked() const { return false; }
......
......@@ -12,7 +12,6 @@
#ifndef TableInstances_hpp
#define TableInstances_hpp
#include <stdio.h>
#include <assert.h>
#include <unordered_map> // and unordered_multimap
......@@ -31,7 +30,7 @@ template<class K, class R, class H> using MapInstances =
/// table of instances:
/// undorered mapping associating to partial Keys of one or several complete instances.
/// undorered mapping associating to partial Keys one or several complete instances.
/// @code multimap: Key -> Record* @endcode where
/// - Key of type K (partial key)
/// - Record* over K: pointers to records associated to complete instances of Key
......@@ -130,7 +129,6 @@ private:
// separated definition of template class
#include "TableInstances.tpp"
#endif /* TableInstances_hpp */
/// @}
......@@ -26,6 +26,7 @@
#include "RunInner.hpp"
#include "Recordey.hpp"
#include "RecordQueue.hpp"
//#include "TableRecords.hpp"
#include "TableInstances.hpp"
#include "ParsingEnv.hpp"
#include "TableA.hpp"
......@@ -38,8 +39,8 @@ template<class K, class R, class H> using MapRecord =
template<class K, class R, class H> using MapIterator =
typename MapRecord<K,R,H>::iterator;
template<class K, class R, class H> using Entry =
std::pair<const K*, R*>;
//template<class K, class R, class H> using Entry =
// std::pair<const K*, R*>;
/// parse table.
......@@ -187,6 +188,7 @@ private: // table access and construction
//const RunTerm* failRun(const K& head, label_t a) const;
/// @param k key must be complete.
/// @todo SUPPR (not used)
R* at(const K& k);
R* at(const K& k, bool& newrec);
......@@ -361,8 +363,8 @@ private: // table access and construction
/// @param tr nullary transition of base SWATA.
/// @return the number of runs added to the table
size_t addLeafComplete(const K& k,
R* rec,
const Transition& tr);
R* rec,
const Transition& tr);
// particular case of add(k, r, it)
......
......@@ -148,7 +148,7 @@ const Run* TableParse<K,R,H>::bestPartial(const K& k, size_t n)
size_t l = 1;
// for all record rec associated to a complete instance k' of k in instance table
for(typename MapInstances<K,R,H>::iterator rit = range.first;
rit != range.second; ++rit)
rit != range.second; ++rit)
{
assert(rit->first == k);
R* rec = rit->second;
......@@ -290,7 +290,6 @@ size_t TableParse<K,R,H>::add(const K& k, R* rec)
MapIterator<K,R,H> it = addemplace(kc, newrec);
assert(it->first == kc);
R* recc = &(it->second);
assert(recc);
assert(recc->key() != nullptr);
assert(*(recc->key()) == kc);
assert(_instances);
......@@ -375,6 +374,7 @@ size_t TableParse<K,R,H>::addTable(const K& k,
assert(*(rec->key()) == k);
//assert((rec->state == 1) || (rec->state == 2));
assert(r);
assert(r->terminal() || r->inner());
assert(r->complete());
assert(! r->weight().unknown());
......@@ -560,7 +560,8 @@ size_t TableParse<K,R,H>::addUpdateFinal(const K& k,
assert(nextrec);
const Run* nextbest = nextrec->best(1);
assert(nextbest);
assert(nextbest->complete());
if (nextbest->fail())
{
WARN("TableParse[{}].addUpdate: fail sibling in {}", k, *r);
......@@ -591,7 +592,7 @@ size_t TableParse<K,R,H>::addInstanciate(const K& k,
{
assert(k.partial());
assert(r);
TRACE("TableParse[{}].addIntanciate run {}", k, *r);
TRACE("TableParse[{}].addInstanciate run {}", k, *r);
assert(r->inner());
assert(r->complete());
assert(! r->weight().unknown());
......
//
// TableRecords.hpp
// squant2
//
// Created by Florent Jacquemard on 19/06/2021.
// Copyright © 2021 Florent Jacquemard. All rights reserved.
//
/// @addtogroup parsing
/// @{
#ifndef TableRecords_hpp
#define TableRecords_hpp
#include <stdio.h>
#include <assert.h>
#include <unordered_map> // and unordered_multimap
#include <queue>
#include "trace.hpp"
#include "Runey.hpp"
//#include "ParsingEnv.hpp"
namespace parsing{
template<class K, class R, class H> using MapRecord =
std::unordered_map<K,R,H>;
template<class K, class R, class H> using MapIterator =
typename MapRecord<K,R,H>::iterator;
/// main table for parsing.
///
/// Parameters:
/// - K = concrete key class (index rows in the table)
/// - R = record class (values in table)
/// - H = KeyHasher (for keys)
template<class K, class R, class H>
class TableRecords
{
public:
/// concrete table.
/// @param env the parsing environment must not be NULL
/// @param comp binary function for comparing runs
TableRecords(ParsingEnv& env, RunCompare comp);
/// delete all the complete keys and associated records
/// stored in the table.
/// @warning they must not be freed elsewhere.
virtual ~TableRecords();
bool empty() const;
/// @brief n-th best run targeting the key (augmented state) k.
/// @param k key can be partial or complete.
/// @param n (as in n-best) is the rank of the run with target the key
/// of the record. must be larger or equal to 1 (default).
/// @return if k is complete: the n-th best run targeting k or NULL if there is none.
/// if k is partial: the n-th best run targeting one complete instance of k
/// reachable by the augmented automaton, or a failRun if it does not exist.
/// @warning the table can be modified (lazy addition of needed entries)
virtual const Run& best(const K& k, size_t n=1);
/// get record associated to the given key,
/// it is created if there was none.
/// @param k a complete key.
/// @return the record associated to the given key.
R& find(const K& k);
/// add run r to the record rec.
/// @param k a complete key.
/// @param rec the entry for k in the map table.
/// @param r omplete run
/// @return the number of runs added to the table
size_t add(const K& k, R* rec, Run* r);
virtual size_t nb_entries() const { return _nb_rows; }
virtual size_t nb_runs() const { return _nb_runs; }
private: // data
/// main table
MapRecord<K,R,H> _table;
/// stats.
size_t _nb_rows;
/// stats.
size_t _nb_runs;
private: // table access and construction
/// @brief find or create an entry in the table.
/// @param k key of the entry. must be complete.
/// @param newrec flag set to true iff the returned record
/// has been created and added to table in this call.
/// @return a iterator to the entry in table
/// associated with key k.
MapIterator<K,R,H> addemplace(const K& k,
bool& newrec);
/// @brief find or create an entry in the table.
/// @param head arg. of constructor of complete instance. must be partial.
/// @param r an inner run used to complete k. must ne non-null and complete.
/// @return an iterator to the entry in table associated with
/// the instance of head build by the constructor of complete instance
/// called with the given arguments (+ env).
/// @todo TBR deprecated
MapIterator<K,R,H> addemplace(const K& head,
RunInner<K>* r,
bool& newrec);
};
} // end namespace parsing
// separated definitions of template class
#include "TableRecords.tpp"
#endif /* TableRecords_hpp */
/// @}
//
// TableRecords.tpp
// squant2
//
// Created by Florent Jacquemard on 19/06/2021.
// Copyright © 2021 Florent Jacquemard. All rights reserved.
//
namespace parsing{
// addComplete(k)
// k complete.
template <class K, class R, class H>
R& TableRecords<K,R,H>::find(const K& k)
{
assert(k.complete());
bool newrec = false;
MapIterator<K,R,H> it = addemplace(k, newrec);
R& rec = it->second;
assert(it->first == k);
assert(rec.key() != nullptr);
assert(*(rec.key()) == k);
if (newrec) // record rec is new and empty and has no key
{
TRACE("TableParse[{}].addComplete: new record in table for key {}", k);
size_t ret = addComplete(k, rec);
TRACE("TableParse[{}].addComplete: {} runs added", k, ret);
}
else // k and record already done, skip
{
TRACE("TableParse[{}].addComplete: complete key already in table,\
skip", k);
}
return rec;
}
template<class K, class R, class H>
void TableRecords<K,R,H>::initRecord(const K& k, R& rec)
{