/* * IdPool.hpp * * This is completely based on the work of Till Crueger, factored out from * World.cpp/hpp. * * Created on: Dec 23, 2011 * Author: heber */ #ifndef IDPOOL_HPP_ #define IDPOOL_HPP_ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include #include "CodePatterns/Range.hpp" /** This class represents a pool of id that can be defragmented. * * This is templated to have whatever pool size (depending on the variable * that stores the id). * * Note that the external class \a idpolicy decides upon how the next id is * produced, \sa IdPool_policy.hpp. */ template class IdPool : public idpolicy { // when trait is not of correct type this will produce an error typedef typename idpolicy::is_IdPool_policy check_for_IdPool_trait; public: /** Constructor for class IdPool. * * @param _currId initial id * @param _max_skips max skips before we really defragment * @param _max_size max size of distinct id ranges before we really defragment */ IdPool(const T _currId, const unsigned int _max_skips, const unsigned int _max_size); /** Destructor for class IdPool. * */ ~IdPool(); /** Reserves a specific \a id. * * @param id which id to reserve * @return true - \a id is reserved, false - \a id is already taken */ bool reserveId(T id); /** Release a reserved \a id. * * @param id id to release */ void releaseId(T id); /** Returns the next available id. * * @return free id that is reserved */ T getNextId(); private: enum Actions { release, reserve, NoAction }; /** Sets the last action. * * This also increases IdPool::numAtomDefragSkips when we switch from one * Action to another. * * @param _action new last action to set */ void setLastAction(const enum Actions _action) { if (_action != lastAction) ++numDefragSkips; lastAction = _action; } //!> contains the last action such that skip counter is only increased when we switch enum Actions lastAction; private: /** Defragment the id pool. * * It is up to this function whether we really defrag or not. * */ void defragIdPool(); private: //!> internal typedef for the pool typedef std::set > IdPool_t; //!> the id pool IdPool_t pool; //!> stores the next highest Id for atoms. This is the last resort of there is no pool. T currId; //!> size of the pool after last defrag, to skip some defrags size_t lastPoolSize; //!> current number of skips unsigned int numDefragSkips; //!> threshold after how many skips we reall defrag const unsigned int MAX_FRAGMENTATION_SKIPS; //!> threshold of how large the number of distinct ranges is before we defrag const unsigned int MAX_POOL_FRAGMENTATION; }; #endif /* IDPOOL_HPP_ */