blob: 789c5e1b3f0c7b93dff588d40b7a0f115b349933 (
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
|
/* ---------------------------------------------------------------- */
/* Copyright 2004 (c) by RWTH Aachen - Lehrstuhl fuer Informatik VI */
/* Richard Zens */
/* ---------------------------------------------------------------- */
#ifndef moses_UniqueObject_h
#define moses_UniqueObject_h
#include <iostream>
#include <set>
template<class T> T const* uniqueObject(const T& x,int mode=0)
{
typedef std::set<T> Pool;
static Pool pool;
static size_t Size=0;
if(mode==0) {
std::pair<typename Pool::iterator,bool> p=pool.insert(x);
if(p.second && (++Size%100000==0))
std::cerr<<"uniqueObjects -- size: "<<Size<<" object size: "<<sizeof(T)<<"\n";
return &(*(p.first));
} else {
pool.clear();
Size=0;
return 0;
}
}
template<class T> class UniqueObjectManager
{
public:
typedef T Object;
private:
typedef std::set<T> Pool;
Pool pool;
public:
UniqueObjectManager() {}
void clear() {
pool.clear();
}
size_t size() const {
return pool.size();
}
Object const * operator()(const Object& x) {
#ifdef DEBUG
std::pair<typename Pool::iterator,bool> p=pool.insert(x);
if(p.second && (size()%100000==0))
std::cerr<<"uniqueObjects -- size: "<<size()<<" object size: "<<sizeof(Object)<<"\n";
return &(*(p.first));
#else
return &(*(pool.insert(x).first));
#endif
}
};
#endif
|