source: src/IdPool_impl.hpp@ 8de597

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 8de597 was 5a4cbc, checked in by Frederik Heber <heber@…>, 13 years ago

VERBOSITY: Increased verbosity threshold of some outputs connected to FillRegularGridAction.

  • Property mode set to 100644
File size: 3.7 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, class idpolicy>
21IdPool<T,idpolicy>::IdPool(const T _currId, const unsigned int _max_skips, const unsigned int _max_size) :
22 lastAction(NoAction),
23 currId(_currId),
24 lastPoolSize(0),
25 numDefragSkips(0),
26 MAX_FRAGMENTATION_SKIPS(_max_skips),
27 MAX_POOL_FRAGMENTATION(_max_size)
28{}
29
30template <class T, class idpolicy>
31IdPool<T,idpolicy>::~IdPool()
32{}
33
34template <class T, class idpolicy>
35T IdPool<T,idpolicy>::getNextId()
36{
37 setLastAction(reserve);
38 return idpolicy::getNextId_impl(pool, currId);
39}
40
41template <class T, class idpolicy>
42void IdPool<T,idpolicy>::releaseId(T id)
43{
44 setLastAction(release);
45 pool.insert(makeRange(id,id+1));
46 defragIdPool();
47}
48
49template <class T, class idpolicy>
50bool IdPool<T,idpolicy>::reserveId(T id)
51{
52 setLastAction(reserve);
53 if(id>=currId ) {
54 range<T> newRange = makeRange(currId,id);
55 if(newRange.first<newRange.last)
56 pool.insert(newRange);
57 currId=id+1;
58 defragIdPool();
59 return true;
60 }
61 // look for a range that matches the request
62 for(typename IdPool_t::iterator iter=pool.begin();iter!=pool.end();++iter){
63 if(iter->isBefore(id)){
64 // we have covered all available ranges... nothing to be found here
65 break;
66 }
67 // no need to check first, since it has to be <=id, since otherwise we would have broken out
68 if(!iter->isBeyond(id)){
69 // we found a matching range... get the id from this range
70
71 // split up this range at the point of id
72 range<T> bottomRange = makeRange(iter->first,id);
73 range<T> topRange = makeRange(id+1,iter->last);
74 // remove this range
75 pool.erase(iter);
76 if(bottomRange.first<bottomRange.last){
77 pool.insert(bottomRange);
78 }
79 if(topRange.first<topRange.last){
80 pool.insert(topRange);
81 }
82 defragIdPool();
83 return true;
84 }
85 }
86 // this ID could not be reserved
87 return false;
88}
89
90template <class T, class idpolicy>
91void IdPool<T,idpolicy>::defragIdPool()
92{
93 // check if the situation is bad enough to make defragging neccessary
94 if((numDefragSkips<MAX_FRAGMENTATION_SKIPS) &&
95 (pool.size()<lastPoolSize+MAX_POOL_FRAGMENTATION)) {
96 return;
97 }
98 LOG(3, "DEBUG: Defragmenting id pool.");
99 for(typename IdPool_t::iterator iter = pool.begin();iter!=pool.end();) {
100 // see if this range is adjacent to the next one
101 typename IdPool_t::iterator next = iter;
102 next++;
103 if(next!=pool.end() && (next->first==iter->last)) {
104 // merge the two ranges
105 range<T> newRange = makeRange(iter->first,next->last);
106 pool.erase(iter);
107 pool.erase(next);
108 pair<typename IdPool_t::iterator,bool> res = pool.insert(newRange);
109 ASSERT(res.second,"Id-Pool was confused");
110 iter=res.first;
111 continue;
112 }
113 ++iter;
114 }
115 if(!pool.empty()) {
116 // check if the last range is at the border
117 typename IdPool_t::iterator iter = pool.end();
118 iter--;
119 if(iter->last==currId){
120 currId=iter->first;
121 pool.erase(iter);
122 }
123 }
124 lastPoolSize=pool.size();
125 numDefragSkips=0;
126}
127
128/**
129 * This define allows simple instantiation of the necessary singleton functions
130 * at a chosen place.
131 */
132#define CONSTRUCT_IDPOOL(name, idpolicy) \
133 template name IdPool< name, idpolicy >::getNextId(); \
134 template bool IdPool< name, idpolicy >::reserveId( name ); \
135 template void IdPool< name, idpolicy >::releaseId( name ); \
136 template void IdPool< name, idpolicy >::setLastAction(const enum Actions _action); \
137 template void IdPool< name, idpolicy >::defragIdPool() ;
138
139#endif /* IDPOOL_IMPL_HPP_ */
Note: See TracBrowser for help on using the repository browser.