recycler unused bitmaps

This commit is contained in:
Hieu Hoang 2015-12-30 13:40:52 +00:00
parent e7247534ce
commit 48b26851e0
4 changed files with 38 additions and 37 deletions

View File

@ -25,46 +25,35 @@ Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
namespace Moses2
{
Bitmap::Bitmap(size_t size, const std::vector<bool>& initializer)
:m_bitmap(initializer.begin(), initializer.end())
Bitmap::Bitmap(size_t size)
:m_bitmap(size)
{
}
void Bitmap::Init(const std::vector<bool>& initializer)
{
// 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);
std::fill(m_bitmap.begin(), m_bitmap.end(), false);
std::copy(initializer.begin(), initializer.end(), m_bitmap.begin());
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_bitmap.begin(), m_bitmap.end(), false);
m_firstGap = (
(first_gap == m_bitmap.end()) ?
NOT_FOUND : first_gap - m_bitmap.begin());
(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)
void Bitmap::Init(const Bitmap &copy, const Range &range)
{
}
std::copy(copy.m_bitmap.begin(), copy.m_bitmap.end(), m_bitmap.begin());
m_firstGap = copy.m_firstGap;
m_numWordsCovered = copy.m_numWordsCovered;
//! 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)
{
SetValueNonOverlap(range);
}

View File

@ -95,15 +95,10 @@ private:
public:
//! Create Bitmap of length size, and initialise with vector.
explicit Bitmap(size_t size, const std::vector<bool>& initializer);
//! Create Bitmap of length size and initialise.
explicit Bitmap(size_t size);
//! Deep copy.
explicit Bitmap(const Bitmap &copy);
explicit Bitmap(const Bitmap &copy, const Range &range);
void Init(const std::vector<bool>& initializer);
void Init(const Bitmap &copy, const Range &range);
//! Count of words translated.
size_t GetNumWordsCovered() const {

View File

@ -18,20 +18,29 @@ Bitmaps::~Bitmaps()
void Bitmaps::Init(size_t inputSize, const std::vector<bool> &initSourceCompleted)
{
m_initBitmap = new Bitmap(inputSize, initSourceCompleted);
m_initBitmap = new Bitmap(inputSize);
m_initBitmap->Init(initSourceCompleted);
m_coll[m_initBitmap];
}
const Bitmap &Bitmaps::GetNextBitmap(const Bitmap &bm, const Range &range)
{
Bitmap *newBM = new Bitmap(bm, range);
Bitmap *newBM;
if (m_recycler.empty()) {
newBM = new Bitmap(bm.GetSize());
}
else {
newBM = m_recycler.top();
m_recycler.pop();
}
newBM->Init(bm, range);
Coll::const_iterator iter = m_coll.find(newBM);
if (iter == m_coll.end()) {
m_coll[newBM] = NextBitmaps();
return *newBM;
} else {
delete newBM;
m_recycler.push(newBM);
return *iter->first;
}
}
@ -63,6 +72,12 @@ void Bitmaps::Clear()
delete bm;
}
m_coll.clear();
while (!m_recycler.empty()) {
const Bitmap *bm = m_recycler.top();
delete bm;
m_recycler.pop();
}
}
}

View File

@ -3,6 +3,7 @@
#include <boost/unordered_set.hpp>
#include <boost/unordered_map.hpp>
#include <set>
#include <stack>
#include "Bitmap.h"
#include "Util2.h"
@ -15,7 +16,8 @@ class Bitmaps
typedef boost::unordered_map<const Bitmap*, NextBitmaps, UnorderedComparer<Bitmap>, UnorderedComparer<Bitmap> > Coll;
//typedef std::set<const Bitmap*, OrderedComparer<Bitmap> > Coll;
Coll m_coll;
const Bitmap *m_initBitmap;
Bitmap *m_initBitmap;
std::stack<Bitmap*> m_recycler;
const Bitmap &GetNextBitmap(const Bitmap &bm, const Range &range);
public: