mosesdecoder/contrib/other-builds/moses2/Recycler.h

75 lines
1.0 KiB
C
Raw Normal View History

2016-01-03 02:03:08 +03:00
/*
* Recycler.h
*
* Created on: 2 Jan 2016
* Author: hieu
*/
#pragma once
2016-01-05 17:34:59 +03:00
#include <cstddef>
2016-01-03 02:03:08 +03:00
#include <deque>
2016-01-05 17:34:59 +03:00
#include <vector>
2016-01-03 02:03:08 +03:00
2016-03-31 23:00:16 +03:00
namespace Moses2
{
2016-01-03 02:03:08 +03:00
template<typename T>
2016-03-31 23:00:16 +03:00
class Recycler
{
2016-01-03 02:03:08 +03:00
public:
2016-03-31 23:00:16 +03:00
Recycler() :
m_currInd(0)
{
}
virtual ~Recycler()
{
}
2016-01-03 02:03:08 +03:00
2016-03-31 23:00:16 +03:00
T Get()
{
if (!m_coll.empty()) {
T &obj = m_coll.back();
m_coll.pop_back();
return obj;
}
else if (m_currInd) {
--m_currInd;
T &obj = m_all[m_currInd];
return obj;
}
else {
return NULL;
}
}
2016-01-05 17:34:59 +03:00
2016-03-31 23:00:16 +03:00
void Clear()
{
m_coll.clear();
m_currInd = m_all.size();
}
2016-01-05 17:34:59 +03:00
2016-03-31 23:00:16 +03:00
void Keep(const T& val)
{
m_all.push_back(val);
}
2016-01-05 17:34:59 +03:00
2016-03-31 23:00:16 +03:00
void Recycle(const T& val)
{
m_coll.push_back(val);
}
2016-01-03 02:03:08 +03:00
protected:
2016-03-31 23:00:16 +03:00
// all objects we're looking after
std::vector<T> m_all;
2016-01-05 17:34:59 +03:00
2016-03-31 23:00:16 +03:00
// pointer to the object that's just been given out.
// to give out another obj, must decrement THEN give out
size_t m_currInd;
2016-01-05 17:34:59 +03:00
2016-03-31 23:00:16 +03:00
// objects that have been give back to us
std::deque<T> m_coll;
2016-01-03 02:03:08 +03:00
};
} /* namespace Moses2 */