precalculate number of words covered

This commit is contained in:
Hieu Hoang 2015-10-26 09:50:27 +00:00
parent 52f4086dbc
commit 73d8dff5fe
2 changed files with 61 additions and 27 deletions

View File

@ -27,11 +27,47 @@ namespace Moses
TO_STRING_BODY(Bitmap);
Bitmap::Bitmap(size_t size, const std::vector<bool>& initializer)
:m_bitmap(initializer.begin(), initializer.end())
{
// The initializer may not be of the same length. Change to the desired
// length. If we need to add any elements, initialize them to false.
m_bitmap.resize(size, false);
m_numWordsCovered = std::count(m_bitmap.begin(), m_bitmap.end(), true);
// Find the first gap, and cache it.
std::vector<char>::const_iterator first_gap = std::find(
m_bitmap.begin(), m_bitmap.end(), false);
m_firstGap = (
(first_gap == m_bitmap.end()) ?
NOT_FOUND : first_gap - m_bitmap.begin());
}
//! Create Bitmap of length size and initialise.
Bitmap::Bitmap(size_t size)
:m_bitmap(size, false)
,m_firstGap(0)
,m_numWordsCovered(0)
{
}
//! Deep copy.
Bitmap::Bitmap(const Bitmap &copy)
:m_bitmap(copy.m_bitmap)
,m_firstGap(copy.m_firstGap)
,m_numWordsCovered(copy.m_numWordsCovered)
{
}
Bitmap::Bitmap(const Bitmap &copy, const Range &range)
:m_bitmap(copy.m_bitmap)
,m_firstGap(copy.m_firstGap)
,m_numWordsCovered(copy.m_numWordsCovered)
{
SetValue(range, true);
SetValueNonOverlap(range);
}
// for unordered_set in stack

View File

@ -53,6 +53,7 @@ class Bitmap
private:
std::vector<char> m_bitmap; //! Ticks of words in sentence that have been done.
size_t m_firstGap; //! Cached position of first gap, or NOT_FOUND.
size_t m_numWordsCovered;
Bitmap(); // not implemented
Bitmap& operator= (const Bitmap& other);
@ -81,48 +82,33 @@ private:
//! set value between 2 positions, inclusive
void
SetValue(Range const& range, bool value) {
SetValueNonOverlap(Range const& range) {
size_t startPos = range.GetStartPos();
size_t endPos = range.GetEndPos();
for(size_t pos = startPos ; pos <= endPos ; pos++) {
m_bitmap[pos] = value;
m_bitmap[pos] = true;
}
UpdateFirstGap(startPos, endPos, value);
m_numWordsCovered += range.GetNumWordsCovered();
UpdateFirstGap(startPos, endPos, true);
}
public:
//! Create Bitmap of length size, and initialise with vector.
Bitmap(size_t size, const std::vector<bool>& initializer)
:m_bitmap(initializer.begin(), initializer.end()), m_firstGap(0) {
// The initializer may not be of the same length. Change to the desired
// length. If we need to add any elements, initialize them to false.
m_bitmap.resize(size, false);
// Find the first gap, and cache it.
std::vector<char>::const_iterator first_gap = std::find(
m_bitmap.begin(), m_bitmap.end(), false);
m_firstGap = (
(first_gap == m_bitmap.end()) ?
NOT_FOUND : first_gap - m_bitmap.begin());
}
Bitmap(size_t size, const std::vector<bool>& initializer);
//! Create Bitmap of length size and initialise.
Bitmap(size_t size)
:m_bitmap(size, false), m_firstGap(0) {
}
Bitmap(size_t size);
//! Deep copy.
Bitmap(const Bitmap &copy)
:m_bitmap(copy.m_bitmap), m_firstGap(copy.m_firstGap) {
}
Bitmap(const Bitmap &copy);
Bitmap(const Bitmap &copy, const Range &range);
//! Count of words translated.
size_t GetNumWordsCovered() const {
return std::count(m_bitmap.begin(), m_bitmap.end(), true);
return m_numWordsCovered;
}
//! position of 1st word not yet translated, or NOT_FOUND if everything already translated
@ -160,8 +146,20 @@ public:
}
//! set value at a particular position
void SetValue( size_t pos, bool value ) {
m_bitmap[pos] = value;
UpdateFirstGap(pos, pos, value);
bool origValue = m_bitmap[pos];
if (origValue == value) {
// do nothing
}
else {
m_bitmap[pos] = value;
UpdateFirstGap(pos, pos, value);
if (value) {
++m_numWordsCovered;
}
else {
--m_numWordsCovered;
}
}
}
//! whether every word has been translated