2008-06-11 14:52:57 +04:00
|
|
|
/* ---------------------------------------------------------------- */
|
|
|
|
/* Copyright 2004 (c) by RWTH Aachen - Lehrstuhl fuer Informatik VI */
|
|
|
|
/* Richard Zens */
|
|
|
|
/* ---------------------------------------------------------------- */
|
2010-02-24 14:15:44 +03:00
|
|
|
#ifndef moses_UniqueObject_h
|
|
|
|
#define moses_UniqueObject_h
|
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
#include <iostream>
|
|
|
|
#include <set>
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
template<class T> T const* uniqueObject(const T& x,int mode=0)
|
|
|
|
{
|
2008-06-11 14:52:57 +04:00
|
|
|
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";
|
2011-02-24 16:14:42 +03:00
|
|
|
|
2008-06-11 14:52:57 +04:00
|
|
|
return &(*(p.first));
|
2011-02-24 16:14:42 +03:00
|
|
|
} else {
|
|
|
|
pool.clear();
|
|
|
|
Size=0;
|
|
|
|
return 0;
|
2008-06-11 14:52:57 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-06-29 02:29:46 +04:00
|
|
|
//! @todo what is this?
|
2011-02-24 16:14:42 +03:00
|
|
|
template<class T> class UniqueObjectManager
|
|
|
|
{
|
2008-06-11 14:52:57 +04:00
|
|
|
public:
|
|
|
|
typedef T Object;
|
|
|
|
private:
|
|
|
|
typedef std::set<T> Pool;
|
|
|
|
Pool pool;
|
|
|
|
public:
|
|
|
|
UniqueObjectManager() {}
|
|
|
|
|
2011-02-24 16:14:42 +03:00
|
|
|
void clear() {
|
|
|
|
pool.clear();
|
|
|
|
}
|
|
|
|
size_t size() const {
|
|
|
|
return pool.size();
|
|
|
|
}
|
2008-06-11 14:52:57 +04:00
|
|
|
|
|
|
|
Object const * operator()(const Object& x) {
|
|
|
|
#ifdef DEBUG
|
|
|
|
std::pair<typename Pool::iterator,bool> p=pool.insert(x);
|
|
|
|
if(p.second && (size()%100000==0))
|
2011-02-24 16:14:42 +03:00
|
|
|
std::cerr<<"uniqueObjects -- size: "<<size()<<" object size: "<<sizeof(Object)<<"\n";
|
2008-06-11 14:52:57 +04:00
|
|
|
return &(*(p.first));
|
|
|
|
#else
|
|
|
|
return &(*(pool.insert(x).first));
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
#endif
|