source: src/IdPool_impl.hpp@ b75386

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since b75386 was 3e4fb6, checked in by Frederik Heber <heber@…>, 13 years ago

Added template class IdPool used by World to manage defragmentable id pool.

  • this is stuff from World factored into own class (was doubly present in World anyway).
  • added short documentation to constructs/world.
  • Property mode set to 100644
File size: 3.8 KB
Line 
1/*
2 * IdPool_impl.hpp
3 *
4 * Created on: Dec 23, 2011
5 * Author: heber
6 */
7
8#ifndef IDPOOL_IMPL_HPP_
9#define IDPOOL_IMPL_HPP_
10
11// include config.h
12#ifdef HAVE_CONFIG_H
13#include <config.h>
14#endif
15
16#include "IdPool.hpp"
17
18#include "CodePatterns/Log.hpp"
19
20template <class T>
21IdPool<T>::IdPool(const T _currId, const unsigned int _max_skips, const unsigned int _max_size) :
22 currId(_currId),
23 MAX_FRAGMENTATION_SKIPS(_max_skips),
24 MAX_POOL_FRAGMENTATION(_max_size)
25{}
26
27template <class T>
28IdPool<T>::~IdPool()
29{}
30
31template <class T>
32T IdPool<T>::getNextId()
33{
34 setLastAction(reserve);
35 // try to find an Id in the pool;
36 if(!pool.empty()) {
37 typename IdPool_t::iterator iter=pool.begin();
38 T id = iter->first;
39 range<T> newRange = makeRange(id+1,iter->last);
40 // we wont use this iterator anymore, so we don't care about invalidating
41 pool.erase(iter);
42 if(newRange.first<newRange.last)
43 pool.insert(newRange);
44 return id;
45 }
46 // Nothing in the pool... we are out of luck
47 return currId++;
48}
49
50template <class T>
51void IdPool<T>::releaseId(T id)
52{
53 setLastAction(release);
54 pool.insert(makeRange(id,id+1));
55 defragIdPool();
56}
57
58template <class T>
59bool IdPool<T>::reserveId(T id)
60{
61 setLastAction(reserve);
62 if(id>=currId ) {
63 range<T> newRange = makeRange(currId,id);
64 if(newRange.first<newRange.last)
65 pool.insert(newRange);
66 currId=id+1;
67 defragIdPool();
68 return true;
69 }
70 // look for a range that matches the request
71 for(typename IdPool_t::iterator iter=pool.begin();iter!=pool.end();++iter){
72 if(iter->isBefore(id)){
73 // we have covered all available ranges... nothing to be found here
74 break;
75 }
76 // no need to check first, since it has to be <=id, since otherwise we would have broken out
77 if(!iter->isBeyond(id)){
78 // we found a matching range... get the id from this range
79
80 // split up this range at the point of id
81 range<T> bottomRange = makeRange(iter->first,id);
82 range<T> topRange = makeRange(id+1,iter->last);
83 // remove this range
84 pool.erase(iter);
85 if(bottomRange.first<bottomRange.last){
86 pool.insert(bottomRange);
87 }
88 if(topRange.first<topRange.last){
89 pool.insert(topRange);
90 }
91 defragIdPool();
92 return true;
93 }
94 }
95 // this ID could not be reserved
96 return false;
97}
98
99template <class T>
100void IdPool<T>::defragIdPool()
101{
102 // check if the situation is bad enough to make defragging neccessary
103 if((numDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
104 (pool.size()<lastPoolSize+MAX_POOL_FRAGMENTATION)) {
105 return;
106 }
107 LOG(1, "STATUS: Defragmenting id pool.");
108 for(typename IdPool_t::iterator iter = pool.begin();iter!=pool.end();) {
109 // see if this range is adjacent to the next one
110 typename IdPool_t::iterator next = iter;
111 next++;
112 if(next!=pool.end() && (next->first==iter->last)) {
113 // merge the two ranges
114 range<T> newRange = makeRange(iter->first,next->last);
115 pool.erase(iter);
116 pool.erase(next);
117 pair<typename IdPool_t::iterator,bool> res = pool.insert(newRange);
118 ASSERT(res.second,"Id-Pool was confused");
119 iter=res.first;
120 continue;
121 }
122 ++iter;
123 }
124 if(!pool.empty()) {
125 // check if the last range is at the border
126 typename IdPool_t::iterator iter = pool.end();
127 iter--;
128 if(iter->last==currId){
129 currId=iter->first;
130 pool.erase(iter);
131 }
132 }
133 lastPoolSize=pool.size();
134 numDefragSkips=0;
135}
136
137/**
138 * This define allows simple instantiation of the necessary singleton functions
139 * at a chosen place.
140 */
141#define CONSTRUCT_IDPOOL(name) \
142 template name IdPool< name >::getNextId(); \
143 template bool IdPool< name >::reserveId( name ); \
144 template void IdPool< name >::releaseId( name ); \
145 template void IdPool< name >::setLastAction(const enum Actions _action); \
146 template void IdPool< name >::defragIdPool() ;
147
148#endif /* IDPOOL_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.