| [3e4fb6] | 1 | /*
 | 
|---|
 | 2 |  * IdPool.hpp
 | 
|---|
 | 3 |  *
 | 
|---|
 | 4 |  *  This is completely based on the work of Till Crueger, factored out from
 | 
|---|
 | 5 |  *  World.cpp/hpp.
 | 
|---|
 | 6 |  *
 | 
|---|
 | 7 |  *  Created on: Dec 23, 2011
 | 
|---|
 | 8 |  *      Author: heber
 | 
|---|
 | 9 |  */
 | 
|---|
 | 10 | 
 | 
|---|
 | 11 | #ifndef IDPOOL_HPP_
 | 
|---|
 | 12 | #define IDPOOL_HPP_
 | 
|---|
 | 13 | 
 | 
|---|
 | 14 | // include config.h
 | 
|---|
 | 15 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 16 | #include <config.h>
 | 
|---|
 | 17 | #endif
 | 
|---|
 | 18 | 
 | 
|---|
 | 19 | #include <set>
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | #include "CodePatterns/Range.hpp"
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | /** This class represents a pool of id that can be defragmented.
 | 
|---|
 | 24 |  *
 | 
|---|
 | 25 |  * This is templated to have whatever pool size (depending on the variable
 | 
|---|
 | 26 |  * that stores the id).
 | 
|---|
 | 27 |  *
 | 
|---|
| [b97a60] | 28 |  * Note that the external class \a idpolicy decides upon how the next id is
 | 
|---|
 | 29 |  * produced, \sa IdPool_policy.hpp.
 | 
|---|
| [3e4fb6] | 30 |  */
 | 
|---|
| [b97a60] | 31 | template <class T, class idpolicy>
 | 
|---|
 | 32 | class IdPool : public idpolicy {
 | 
|---|
 | 33 |   // when trait is not of correct type this will produce an error
 | 
|---|
 | 34 |   typedef typename idpolicy::is_IdPool_policy check_for_IdPool_trait;
 | 
|---|
| [3e4fb6] | 35 | public:
 | 
|---|
 | 36 |   /** Constructor for class IdPool.
 | 
|---|
 | 37 |    *
 | 
|---|
 | 38 |    * @param _currId initial id
 | 
|---|
 | 39 |    * @param _max_skips max skips before we really defragment
 | 
|---|
 | 40 |    * @param _max_size max size of distinct id ranges before we really defragment
 | 
|---|
 | 41 |    */
 | 
|---|
 | 42 |   IdPool(const T _currId, const unsigned int _max_skips, const unsigned int _max_size);
 | 
|---|
 | 43 | 
 | 
|---|
 | 44 |   /** Destructor for class IdPool.
 | 
|---|
 | 45 |    *
 | 
|---|
 | 46 |    */
 | 
|---|
 | 47 |   ~IdPool();
 | 
|---|
 | 48 | 
 | 
|---|
 | 49 |   /** Reserves a specific \a id.
 | 
|---|
 | 50 |    *
 | 
|---|
 | 51 |    * @param id which id to reserve
 | 
|---|
 | 52 |    * @return true - \a id is reserved, false - \a id is already taken
 | 
|---|
 | 53 |    */
 | 
|---|
 | 54 |   bool reserveId(T id);
 | 
|---|
 | 55 | 
 | 
|---|
 | 56 |   /** Release a reserved \a id.
 | 
|---|
 | 57 |    *
 | 
|---|
 | 58 |    * @param id id to release
 | 
|---|
 | 59 |    */
 | 
|---|
 | 60 |   void releaseId(T id);
 | 
|---|
 | 61 | 
 | 
|---|
| [b97a60] | 62 |   /** Returns the next available id.
 | 
|---|
 | 63 |    *
 | 
|---|
 | 64 |    * @return free id that is reserved
 | 
|---|
 | 65 |    */
 | 
|---|
 | 66 |   T getNextId();
 | 
|---|
 | 67 | 
 | 
|---|
| [3e4fb6] | 68 | private:
 | 
|---|
 | 69 |   enum Actions {
 | 
|---|
 | 70 |     release,
 | 
|---|
 | 71 |     reserve,
 | 
|---|
 | 72 |     NoAction
 | 
|---|
 | 73 |   };
 | 
|---|
 | 74 | 
 | 
|---|
 | 75 |   /** Sets the last action.
 | 
|---|
 | 76 |    *
 | 
|---|
 | 77 |    * This also increases IdPool::numAtomDefragSkips when we switch from one
 | 
|---|
 | 78 |    * Action to another.
 | 
|---|
 | 79 |    *
 | 
|---|
 | 80 |    * @param _action new last action to set
 | 
|---|
 | 81 |    */
 | 
|---|
 | 82 |   void setLastAction(const enum Actions _action)
 | 
|---|
 | 83 |   {
 | 
|---|
 | 84 |     if (_action != lastAction)
 | 
|---|
 | 85 |       ++numDefragSkips;
 | 
|---|
 | 86 |     lastAction = _action;
 | 
|---|
 | 87 |   }
 | 
|---|
 | 88 | 
 | 
|---|
 | 89 |   //!> contains the last action such that skip counter is only increased when we switch
 | 
|---|
 | 90 |   enum Actions lastAction;
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 | private:
 | 
|---|
 | 93 |   /** Defragment the id pool.
 | 
|---|
 | 94 |    *
 | 
|---|
 | 95 |    * It is up to this function whether we really defrag or not.
 | 
|---|
 | 96 |    *
 | 
|---|
 | 97 |    */
 | 
|---|
 | 98 |   void defragIdPool();
 | 
|---|
 | 99 | 
 | 
|---|
 | 100 | private:
 | 
|---|
 | 101 |   //!> internal typedef for the pool
 | 
|---|
 | 102 |   typedef std::set<range<T> > IdPool_t;
 | 
|---|
 | 103 | 
 | 
|---|
 | 104 |   //!> the id pool
 | 
|---|
 | 105 |   IdPool_t pool;
 | 
|---|
 | 106 | 
 | 
|---|
 | 107 |   //!> stores the next highest Id for atoms. This is the last resort of there is no pool.
 | 
|---|
 | 108 |   T currId;
 | 
|---|
 | 109 | 
 | 
|---|
 | 110 |   //!> size of the pool after last defrag, to skip some defrags
 | 
|---|
 | 111 |   size_t lastPoolSize;
 | 
|---|
 | 112 | 
 | 
|---|
 | 113 |   //!> current number of skips
 | 
|---|
 | 114 |   unsigned int numDefragSkips;
 | 
|---|
 | 115 | 
 | 
|---|
 | 116 |   //!> threshold after how many skips we reall defrag
 | 
|---|
 | 117 |   const unsigned int MAX_FRAGMENTATION_SKIPS;
 | 
|---|
 | 118 | 
 | 
|---|
 | 119 |   //!> threshold of how large the number of distinct ranges is before we defrag
 | 
|---|
 | 120 |   const unsigned int MAX_POOL_FRAGMENTATION;
 | 
|---|
 | 121 | 
 | 
|---|
 | 122 | };
 | 
|---|
 | 123 | 
 | 
|---|
 | 124 | #endif /* IDPOOL_HPP_ */
 | 
|---|