00001
00002
00003
00004
00005 #ifndef moses_UniqueObject_h
00006 #define moses_UniqueObject_h
00007
00008 #include <iostream>
00009 #include <set>
00010
00011 template<class T> T const* uniqueObject(const T& x,int mode=0)
00012 {
00013 typedef std::set<T> Pool;
00014
00015 static Pool pool;
00016 static size_t Size=0;
00017
00018 if(mode==0) {
00019 std::pair<typename Pool::iterator,bool> p=pool.insert(x);
00020 if(p.second && (++Size%100000==0))
00021 std::cerr<<"uniqueObjects -- size: "<<Size<<" object size: "<<sizeof(T)<<"\n";
00022
00023 return &(*(p.first));
00024 } else {
00025 pool.clear();
00026 Size=0;
00027 return 0;
00028 }
00029 }
00030
00032 template<class T> class UniqueObjectManager
00033 {
00034 public:
00035 typedef T Object;
00036 private:
00037 typedef std::set<T> Pool;
00038 Pool pool;
00039 public:
00040 UniqueObjectManager() {}
00041
00042 void clear() {
00043 pool.clear();
00044 }
00045 size_t size() const {
00046 return pool.size();
00047 }
00048
00049 Object const * operator()(const Object& x) {
00050 #ifdef DEBUG
00051 std::pair<typename Pool::iterator,bool> p=pool.insert(x);
00052 if(p.second && (size()%100000==0))
00053 std::cerr<<"uniqueObjects -- size: "<<size()<<" object size: "<<sizeof(Object)<<"\n";
00054 return &(*(p.first));
00055 #else
00056 return &(*(pool.insert(x).first));
00057 #endif
00058 }
00059 };
00060
00061
00062
00063 #endif