src/AtomicDomain.h
e4a86274
 #ifndef __COGAPS_ATOMIC_DOMAIN_H__
 #define __COGAPS_ATOMIC_DOMAIN_H__
e5201463
 
338a383f
 #include "Archive.h"
e5201463
 
094c9c46
 #include <boost/unordered_set.hpp>
0733d393
 
338a383f
 #include <cstddef>
 #include <map>
0733d393
 #include <stdint.h>
 #include <vector>
e5201463
 
e4a86274
 class AtomicDomain;
 
 // Want the neighbors to be pointers, but can't use pointers since vector
 // is resized, shifted integers allow for 0 to be "null" and 1 to be the
 // first index. AtomicDomain is responsible for managing this correctly.
e5201463
 struct Atom
 {
8269c03c
 private:    
e4a86274
 
74377719
     friend class AtomicDomain;
e4a86274
     uint64_t leftNdx; // shift these up 1, 0 == no neighbor
     uint64_t rightNdx;
 
 public:    
 
e5201463
     uint64_t pos;
     float mass;
 
     Atom(uint64_t p, float m)
ee6cc296
         : leftNdx(0), rightNdx(0), pos(p), mass(m)
e5201463
     {}
 
338a383f
     bool operator==(const Atom &other) const
e5201463
     {
338a383f
         return pos == other.pos;
e5201463
     }
888a1fd4
 
     bool operator!=(const Atom &other) const
     {
         return !(pos == other.pos);
     }
8269c03c
 
     // serialization
dd3a9441
     friend Archive& operator<<(Archive &ar, Atom &a);
     friend Archive& operator>>(Archive &ar, Atom &a);
338a383f
 };
e5201463
 
a4f0bba1
 struct RawAtom
 {
     uint64_t pos;
     float mass;
 
     RawAtom() : pos(0), mass(0.f) {}
     RawAtom(uint64_t p, float m) : pos(p), mass(m) {}
 };
 
338a383f
 // data structure that holds atoms
 class AtomicDomain
 {
 private:
e5201463
 
0f279c94
     // size of atomic domain
     uint64_t mDomainSize;
 
338a383f
     // domain storage
     std::vector<Atom> mAtoms;
     std::map<uint64_t, uint64_t> mAtomPositions;
e5201463
 
094c9c46
     boost::unordered_set<uint64_t> mUsedPositions;
 
a4f0bba1
     mutable std::vector<RawAtom> mInsertCache;
     mutable std::vector<uint64_t> mEraseCache;
 
     mutable unsigned mInsertCacheIndex;
     mutable unsigned mEraseCacheIndex;
 
     Atom& _left(const Atom &atom);
     Atom& _right(const Atom &atom);
 
338a383f
 public:
 
0f279c94
     void setDomainSize(uint64_t size) { mDomainSize = size; }
 
094c9c46
     // access atoms
338a383f
     Atom front() const;
     Atom randomAtom() const;
     uint64_t randomFreePosition() const;
e4a86274
     uint64_t size() const;
 
     const Atom& left(const Atom &atom) const;
     const Atom& right(const Atom &atom) const;
     bool hasLeft(const Atom &atom) const;
     bool hasRight(const Atom &atom) const;
338a383f
 
     // modify domain
a9ce8660
     Atom insert(uint64_t pos, float mass);
888a1fd4
     void erase(uint64_t pos);
a4f0bba1
     void cacheInsert(uint64_t pos, float mass) const;
     void cacheErase(uint64_t pos) const;
338a383f
     void updateMass(uint64_t pos, float newMass);
a4f0bba1
     void flushCache();
     void resetCache(unsigned n);
888a1fd4
 
338a383f
     // serialization
     friend Archive& operator<<(Archive &ar, AtomicDomain &domain);
     friend Archive& operator>>(Archive &ar, AtomicDomain &domain);
 };
 
a4f0bba1
 #endif