mirror of
https://github.com/moses-smt/mosesdecoder.git
synced 2024-12-25 21:03:22 +03:00
Optimisation of bitvector - precalculate first gap pos
This commit is contained in:
parent
30a03237fa
commit
0fd3ef2263
@ -43,8 +43,10 @@ class WordsBitmap
|
||||
protected:
|
||||
const size_t m_size; /**< number of words in sentence */
|
||||
bool *m_bitmap; /**< ticks of words that have been done */
|
||||
size_t m_firstGap; /** Position of first gap, pre-calculated as it is consulted often */
|
||||
|
||||
WordsBitmap(); // not implemented
|
||||
WordsBitmap& operator= (const WordsBitmap& other);
|
||||
|
||||
//! set all elements to false
|
||||
void Initialize() {
|
||||
@ -56,9 +58,39 @@ protected:
|
||||
//sets elements by vector
|
||||
void Initialize(const std::vector<bool>& vector) {
|
||||
size_t vector_size = vector.size();
|
||||
bool gapFound = false;
|
||||
for (size_t pos = 0 ; pos < m_size ; pos++) {
|
||||
if (pos < vector_size && vector[pos] == true) m_bitmap[pos] = true;
|
||||
else m_bitmap[pos] = false;
|
||||
else {
|
||||
m_bitmap[pos] = false;
|
||||
if (!gapFound) {
|
||||
m_firstGap = pos;
|
||||
gapFound = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (!gapFound) m_firstGap = NOT_FOUND;
|
||||
}
|
||||
|
||||
/** Update the first gap, when bits are flipped */
|
||||
void UpdateFirstGap(size_t startPos, size_t endPos, bool value) {
|
||||
if (value) {
|
||||
//may remove gap
|
||||
if (startPos <= m_firstGap && m_firstGap <= endPos) {
|
||||
m_firstGap = NOT_FOUND;
|
||||
for (size_t i = endPos + 1 ; i < m_size; ++i) {
|
||||
if (!m_bitmap[i]) {
|
||||
m_firstGap = i;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
//setting positions to false, may add new gap
|
||||
if (startPos < m_firstGap) {
|
||||
m_firstGap = startPos;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@ -66,23 +98,24 @@ protected:
|
||||
public:
|
||||
//! create WordsBitmap of length size and initialise with vector
|
||||
WordsBitmap(size_t size, const std::vector<bool>& initialize_vector)
|
||||
:m_size (size) {
|
||||
:m_size (size), m_firstGap(0) {
|
||||
m_bitmap = (bool*) malloc(sizeof(bool) * size);
|
||||
Initialize(initialize_vector);
|
||||
}
|
||||
//! create WordsBitmap of length size and initialise
|
||||
WordsBitmap(size_t size)
|
||||
:m_size (size) {
|
||||
:m_size (size), m_firstGap(0) {
|
||||
m_bitmap = (bool*) malloc(sizeof(bool) * size);
|
||||
Initialize();
|
||||
}
|
||||
//! deep copy
|
||||
WordsBitmap(const WordsBitmap ©)
|
||||
:m_size (copy.m_size) {
|
||||
:m_size (copy.m_size), m_firstGap(copy.m_firstGap) {
|
||||
m_bitmap = (bool*) malloc(sizeof(bool) * m_size);
|
||||
for (size_t pos = 0 ; pos < copy.m_size ; pos++) {
|
||||
m_bitmap[pos] = copy.GetValue(pos);
|
||||
}
|
||||
m_firstGap = copy.m_firstGap;
|
||||
}
|
||||
~WordsBitmap() {
|
||||
free(m_bitmap);
|
||||
@ -99,13 +132,7 @@ public:
|
||||
|
||||
//! position of 1st word not yet translated, or NOT_FOUND if everything already translated
|
||||
size_t GetFirstGapPos() const {
|
||||
for (size_t pos = 0 ; pos < m_size ; pos++) {
|
||||
if (!m_bitmap[pos]) {
|
||||
return pos;
|
||||
}
|
||||
}
|
||||
// no starting pos
|
||||
return NOT_FOUND;
|
||||
return m_firstGap;
|
||||
}
|
||||
|
||||
|
||||
@ -141,12 +168,15 @@ public:
|
||||
//! set value at a particular position
|
||||
void SetValue( size_t pos, bool value ) {
|
||||
m_bitmap[pos] = value;
|
||||
UpdateFirstGap(pos, pos, value);
|
||||
}
|
||||
//! set value between 2 positions, inclusive
|
||||
void
|
||||
SetValue( size_t startPos, size_t endPos, bool value ) {
|
||||
for(size_t pos = startPos ; pos <= endPos ; pos++)
|
||||
for(size_t pos = startPos ; pos <= endPos ; pos++) {
|
||||
m_bitmap[pos] = value;
|
||||
}
|
||||
UpdateFirstGap(startPos, endPos, value);
|
||||
}
|
||||
|
||||
void
|
||||
|
@ -52,8 +52,16 @@ BOOST_AUTO_TEST_CASE(initialise)
|
||||
}
|
||||
|
||||
|
||||
bitvec[0] = true;
|
||||
bitvec[1] = true;
|
||||
WordsBitmap wbm3(7,bitvec);
|
||||
BOOST_CHECK_EQUAL(wbm3.GetFirstGapPos(),4);
|
||||
|
||||
WordsBitmap wbm4(4,bitvec);
|
||||
BOOST_CHECK_EQUAL(wbm4.GetFirstGapPos(),NOT_FOUND);
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_CASE(getset)
|
||||
{
|
||||
WordsBitmap wbm(6);
|
||||
@ -62,6 +70,7 @@ BOOST_AUTO_TEST_CASE(getset)
|
||||
BOOST_CHECK_EQUAL(wbm.GetValue(2),false);
|
||||
wbm.SetValue(2,true);
|
||||
BOOST_CHECK_EQUAL(wbm.GetValue(2),true);
|
||||
|
||||
|
||||
wbm.SetValue(1,3,true);
|
||||
BOOST_CHECK_EQUAL(wbm.GetValue(1),true);
|
||||
@ -110,6 +119,20 @@ BOOST_AUTO_TEST_CASE(positions)
|
||||
BOOST_CHECK_EQUAL(wbm.GetLastGapPos(), 9);
|
||||
BOOST_CHECK_EQUAL(wbm.GetLastPos(), 7);
|
||||
|
||||
WordsRange wr(2,4);
|
||||
wbm.SetValue(wr,true);
|
||||
BOOST_CHECK_EQUAL(wbm.GetFirstGapPos(),5);
|
||||
|
||||
WordsRange wr2(5,8);
|
||||
wbm.SetValue(wr2,true);
|
||||
BOOST_CHECK_EQUAL(wbm.GetFirstGapPos(),9);
|
||||
|
||||
wbm.SetValue(9,true);
|
||||
BOOST_CHECK_EQUAL(wbm.GetFirstGapPos(),NOT_FOUND);
|
||||
|
||||
wbm.SetValue(wr,false);
|
||||
BOOST_CHECK_EQUAL(wbm.GetFirstGapPos(),2);
|
||||
|
||||
WordsBitmap wbm2(2);
|
||||
wbm2.SetValue(0,true);
|
||||
wbm2.SetValue(1,true);
|
||||
@ -120,8 +143,8 @@ BOOST_AUTO_TEST_CASE(positions)
|
||||
BOOST_CHECK_EQUAL(wbm3.GetLastGapPos(), 4);
|
||||
BOOST_CHECK_EQUAL(wbm3.GetLastPos(), NOT_FOUND);
|
||||
|
||||
|
||||
}
|
||||
|
||||
|
||||
BOOST_AUTO_TEST_SUITE_END()
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user