| 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 | * | 
|---|
| 28 | */ | 
|---|
| 29 | template <class T> | 
|---|
| 30 | class IdPool { | 
|---|
| 31 | public: | 
|---|
| 32 | /** Constructor for class IdPool. | 
|---|
| 33 | * | 
|---|
| 34 | * @param _currId initial id | 
|---|
| 35 | * @param _max_skips max skips before we really defragment | 
|---|
| 36 | * @param _max_size max size of distinct id ranges before we really defragment | 
|---|
| 37 | */ | 
|---|
| 38 | IdPool(const T _currId, const unsigned int _max_skips, const unsigned int _max_size); | 
|---|
| 39 |  | 
|---|
| 40 | /** Destructor for class IdPool. | 
|---|
| 41 | * | 
|---|
| 42 | */ | 
|---|
| 43 | ~IdPool(); | 
|---|
| 44 |  | 
|---|
| 45 | /** Returns the next available id. | 
|---|
| 46 | * | 
|---|
| 47 | * @return free id that is reserved | 
|---|
| 48 | */ | 
|---|
| 49 | T getNextId(); | 
|---|
| 50 |  | 
|---|
| 51 | /** Reserves a specific \a id. | 
|---|
| 52 | * | 
|---|
| 53 | * @param id which id to reserve | 
|---|
| 54 | * @return true - \a id is reserved, false - \a id is already taken | 
|---|
| 55 | */ | 
|---|
| 56 | bool reserveId(T id); | 
|---|
| 57 |  | 
|---|
| 58 | /** Release a reserved \a id. | 
|---|
| 59 | * | 
|---|
| 60 | * @param id id to release | 
|---|
| 61 | */ | 
|---|
| 62 | void releaseId(T id); | 
|---|
| 63 |  | 
|---|
| 64 | private: | 
|---|
| 65 | enum Actions { | 
|---|
| 66 | release, | 
|---|
| 67 | reserve, | 
|---|
| 68 | NoAction | 
|---|
| 69 | }; | 
|---|
| 70 |  | 
|---|
| 71 | /** Sets the last action. | 
|---|
| 72 | * | 
|---|
| 73 | * This also increases IdPool::numAtomDefragSkips when we switch from one | 
|---|
| 74 | * Action to another. | 
|---|
| 75 | * | 
|---|
| 76 | * @param _action new last action to set | 
|---|
| 77 | */ | 
|---|
| 78 | void setLastAction(const enum Actions _action) | 
|---|
| 79 | { | 
|---|
| 80 | if (_action != lastAction) | 
|---|
| 81 | ++numDefragSkips; | 
|---|
| 82 | lastAction = _action; | 
|---|
| 83 | } | 
|---|
| 84 |  | 
|---|
| 85 | //!> contains the last action such that skip counter is only increased when we switch | 
|---|
| 86 | enum Actions lastAction; | 
|---|
| 87 |  | 
|---|
| 88 | private: | 
|---|
| 89 | /** Defragment the id pool. | 
|---|
| 90 | * | 
|---|
| 91 | * It is up to this function whether we really defrag or not. | 
|---|
| 92 | * | 
|---|
| 93 | */ | 
|---|
| 94 | void defragIdPool(); | 
|---|
| 95 |  | 
|---|
| 96 | private: | 
|---|
| 97 | //!> internal typedef for the pool | 
|---|
| 98 | typedef std::set<range<T> > IdPool_t; | 
|---|
| 99 |  | 
|---|
| 100 | //!> the id pool | 
|---|
| 101 | IdPool_t pool; | 
|---|
| 102 |  | 
|---|
| 103 | //!> stores the next highest Id for atoms. This is the last resort of there is no pool. | 
|---|
| 104 | T currId; | 
|---|
| 105 |  | 
|---|
| 106 | //!> size of the pool after last defrag, to skip some defrags | 
|---|
| 107 | size_t lastPoolSize; | 
|---|
| 108 |  | 
|---|
| 109 | //!> current number of skips | 
|---|
| 110 | unsigned int numDefragSkips; | 
|---|
| 111 |  | 
|---|
| 112 | //!> threshold after how many skips we reall defrag | 
|---|
| 113 | const unsigned int MAX_FRAGMENTATION_SKIPS; | 
|---|
| 114 |  | 
|---|
| 115 | //!> threshold of how large the number of distinct ranges is before we defrag | 
|---|
| 116 | const unsigned int MAX_POOL_FRAGMENTATION; | 
|---|
| 117 |  | 
|---|
| 118 | }; | 
|---|
| 119 |  | 
|---|
| 120 | #endif /* IDPOOL_HPP_ */ | 
|---|