blob: f57fd82f5ac99e3079f72d4d0c872026f9756f46 (
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
|
/* ---------------------------------------------------------------- */
/* Copyright 2004 (c) by RWTH Aachen - Lehrstuhl fuer Informatik VI */
/* Richard Zens */
/* ---------------------------------------------------------------- */
#ifndef UNIQUEOBJECT_HH_
#define UNIQUEOBJECT_HH_
#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
|