persistance-noop.h

Go to the documentation of this file.
00001 #ifndef PERSISTANCE_NOOP_H
00002 #define PERSISTANCE_NOOP_H
00003 
00004 #include <vector>
00005 #include <list>
00006 #include "hash_set.h"
00007 #include "hash_map.h"
00008 #include <cassert>
00009 
00014 struct PersistantStore {
00015   static PersistantStore& hackery() {
00016     return *(PersistantStore*)0x31337;
00017   }
00018 };
00019 
00020 template <class Datum> class PersistantData {
00021   private:
00022     Datum d;
00023 
00024   public:
00025     PersistantData(PersistantStore& store, const Datum& initial)
00026       : d(initial) { }
00027 
00028     const Datum& operator *() const {
00029       return d;
00030     }
00031     Datum& access() {
00032       return d;
00033     }
00034 };
00035 
00036 template <class Datum, size_t N> class PersistantArray {
00037   private:
00038     Datum data_[N];
00039 
00040   public:
00041     PersistantArray(PersistantStore& store) { }
00042     PersistantArray(PersistantStore& store, const Datum& initial) {
00043       for(size_t i = 0; i < N; i++) {
00044     data_[i] = initial;
00045       }
00046     }
00047 
00048     const Datum& operator[] (size_t i) const {
00049       assert(i < N);
00050       return data_[i];
00051     }
00052 
00053     Datum& access (size_t i) {
00054       assert(i < N);
00055       return data_[i];
00056     }
00057 
00058     /* \todo Not portable! */
00059     typedef __gnu_cxx::__normal_iterator<Datum*, PersistantArray> iterator;
00060     typedef __gnu_cxx::__normal_iterator<const Datum*, PersistantArray>
00061       const_iterator;
00062     typedef Datum* pointer;
00063 
00064     operator iterator() { return begin(); }
00065     operator const_iterator() const { return begin(); }
00066 
00067     iterator begin() { return iterator(data_); }
00068     const_iterator begin() const { return const_iterator(data_); }
00069     iterator end() { return iterator(data_ + N); }
00070     const_iterator end() const { return const_iterator(data_ + N); }
00071 
00072     iterator operator+ (size_t i) {
00073       assert(i <= N);
00074       return iterator(data_ + i);
00075     }
00076     const_iterator operator+ (size_t i) const {
00077       assert(i <= N);
00078       return const_iterator(data_ + i);
00079     }
00080 };
00081 
00082 template <class Datum> class PersistantVector : public std::vector<Datum> {
00083   private:
00084     typedef std::vector<Datum> super;
00085   public:
00086     typedef typename super::size_type size_type;
00087     typedef typename super::reference reference;
00088 
00089     reference access(size_type i) {
00090       return operator[](i);
00091     }
00092 
00093     PersistantVector(PersistantStore& store) { }
00094     PersistantVector(PersistantStore& store, size_type n) : super(n) { }
00095     PersistantVector(PersistantStore& store, size_type n, const Datum& t)
00096       : super(n, t) { }
00097 
00098     template <class InputIterator>
00099     PersistantVector(PersistantStore& store, InputIterator begin,
00100                      InputIterator end) : super(begin, end) { }
00101 
00102     reference access_front() {
00103       return access(0);
00104     }
00105     reference access_back() {
00106       return access(super::size() - 1);
00107     }
00108 
00109     PersistantStore& get_store() const { return PersistantStore::hackery(); }
00110 
00111   private:
00112     /* illegal */
00113     PersistantVector(const PersistantVector&);
00114     PersistantVector& operator=(const PersistantVector&);
00115 };
00116 
00117 
00118 template <class Datum> class PersistantMemoryPool {
00119   public:
00120     PersistantMemoryPool(PersistantStore& store) { }
00121 
00122     virtual ~PersistantMemoryPool() {
00123       for(typename datumset::iterator it = allocated_.begin();
00124       it != allocated_.end(); ++it) {
00125     delete *it;
00126       }
00127       /* datumset is cleared automatically */
00128     }
00129 
00130     void mark_allocated(Datum *key) {
00131       allocated_.insert(key);
00132     }
00133 
00134     void mark_deleted(Datum *key) {
00135       bool found = allocated_.erase(key);
00136       assert(found);
00137       delete key;
00138     }
00139 
00140   private:
00141     struct hashfcn {
00142       unsigned long long operator()(Datum *d) const { return (unsigned long long)d; }
00143     };
00144     typedef hashers::hash_set<Datum*,hashfcn> datumset;
00145 
00146     datumset allocated_;
00147 
00148   private:
00149     /* illegal */
00150     PersistantMemoryPool(const PersistantMemoryPool&);
00151     PersistantMemoryPool& operator = (const PersistantMemoryPool&);
00152 };
00153 
00157 template <class Value>
00158 class PersistantList : public std::list<Value> {
00162   public:
00163     struct iterator : public std::list<Value>::iterator {
00164       typedef typename std::list<Value>::iterator super;
00165       public:
00166         iterator() { }
00167         iterator(typename std::list<Value>::iterator it) : super(it) { }
00168 
00169         Value& access() const {
00170       return super::operator*();
00171         }
00172         const Value& operator*() const {
00173       return super::operator*();
00174         }
00175     };
00176 
00177     PersistantList(PersistantStore& store) { }
00178 
00179     PersistantStore& get_store() const { return PersistantStore::hackery(); }
00180 };
00181 
00182 
00183 template <class Key, class HashFcn = hashers::hash<Key>,
00184       class EqualKey = std::equal_to<Key> >
00185 class PersistantHashSet : public hashers::hash_set<Key, HashFcn, EqualKey> {
00186   public:
00187     PersistantHashSet(PersistantStore& store) { }
00188     PersistantStore& get_store() const { return PersistantStore::hackery(); }
00189 
00190   private:
00191     /* illegal */
00192     PersistantHashSet(const PersistantHashSet&);
00193     PersistantHashSet& operator = (const PersistantHashSet&);
00194 };
00195 
00196 template <class Key, class Value, class HashFcn = hashers::hash<Key>,
00197       class EqualKey = std::equal_to<Key> >
00198 class PersistantHashMap : public hashers::hash_map<Key, Value, HashFcn, EqualKey> {
00199   typedef hashers::hash_map<Key, Value, HashFcn, EqualKey> super;
00200   public:
00201     typedef typename super::iterator iterator;
00202 
00203     PersistantHashMap(PersistantStore& store) { }
00204 
00205     std::pair<iterator, bool> insert(const Key& k, const Value& v) {
00206       return super::insert(std::make_pair(k, v));
00207     }
00208 
00209     PersistantStore& get_store() const { return PersistantStore::hackery(); }
00210 
00211   private:
00212     /* illegal */
00213     PersistantHashMap(const PersistantHashMap&);
00214     PersistantHashMap& operator = (const PersistantHashMap&);
00215 };
00216 #endif

Generated on Mon May 24 09:53:30 2010 for TUMBLE by  doxygen 1.5.2