blob: 3751a2a93dffbe5520dda40593869012956a3605 (
plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
/*
* Recycler.h
*
* Created on: 2 Jan 2016
* Author: hieu
*/
#pragma once
#include <cstddef>
#include <deque>
#include <vector>
namespace Moses2
{
template<typename T>
class Recycler
{
public:
Recycler() :
m_currInd(0)
{
}
virtual ~Recycler()
{
}
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;
}
}
void Clear()
{
m_coll.clear();
m_currInd = m_all.size();
}
// call this for new objects when u 1st create it. It is assumed the object will be used right away
void Keep(const T& val)
{
m_all.push_back(val);
}
// call this for existing object to put back into queue for reuse
void Recycle(const T& val)
{
m_coll.push_back(val);
}
protected:
// all objects we're looking after
std::vector<T> m_all;
// pointer to the object that's just been given out.
// to give out another obj, must decrement THEN give out
size_t m_currInd;
// objects that have been give back to us
std::deque<T> m_coll;
};
} /* namespace Moses2 */
|