-
Notifications
You must be signed in to change notification settings - Fork 295
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'optimize-priority-queue'
- Loading branch information
Showing
5 changed files
with
181 additions
and
109 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
Copyright (c) 2011-2023 The plumed team | ||
(see the PEOPLE file at the root of the distribution for a list of names) | ||
See http://www.plumed.org for more information. | ||
This file is part of plumed, version 2. | ||
plumed is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
plumed is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with plumed. If not, see <http://www.gnu.org/licenses/>. | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ | ||
|
||
#include "MergeVectorTools.h" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,127 @@ | ||
/* +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ | ||
Copyright (c) 2011-2023 The plumed team | ||
(see the PEOPLE file at the root of the distribution for a list of names) | ||
See http://www.plumed.org for more information. | ||
This file is part of plumed, version 2. | ||
plumed is free software: you can redistribute it and/or modify | ||
it under the terms of the GNU Lesser General Public License as published by | ||
the Free Software Foundation, either version 3 of the License, or | ||
(at your option) any later version. | ||
plumed is distributed in the hope that it will be useful, | ||
but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | ||
GNU Lesser General Public License for more details. | ||
You should have received a copy of the GNU Lesser General Public License | ||
along with plumed. If not, see <http://www.gnu.org/licenses/>. | ||
+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ */ | ||
#ifndef __PLUMED_tools_MergeVectorTools_h | ||
#define __PLUMED_tools_MergeVectorTools_h | ||
|
||
#include "small_vector/small_vector.h" | ||
#include <vector> | ||
#include <algorithm> | ||
|
||
namespace PLMD { | ||
|
||
namespace mergeVectorTools { | ||
|
||
/// Merge sorted vectors. | ||
/// Takes a vector of pointers to containers and merge them. | ||
/// Containers should be already sorted. | ||
/// The content is appended to the result vector. | ||
/// Optionally, uses a priority_queue implementation. | ||
template<class C> | ||
static void mergeSortedVectors(const C* const* vecs, std::size_t size, std::vector<typename C::value_type> & result) { | ||
|
||
/// local class storing the range of remaining objects to be pushed | ||
class Entry | ||
{ | ||
typename C::const_iterator fwdIt,endIt; | ||
|
||
public: | ||
explicit Entry(C const& v) : fwdIt(v.begin()), endIt(v.end()) {} | ||
/// check if this vector still contains something to be pushed | ||
bool empty() const { return fwdIt == endIt; } | ||
/// to allow using a priority_queu, which selects the highest element. | ||
/// we here (counterintuitively) define < as > | ||
bool operator< (Entry const& rhs) const { return top() > rhs.top(); } | ||
const auto & top() const { return *fwdIt; } | ||
void next() { ++fwdIt;}; | ||
}; | ||
|
||
// preprocessing | ||
{ | ||
std::size_t maxsize=0; | ||
for(unsigned i=0; i<size; i++) { | ||
// find the largest | ||
maxsize=std::max(maxsize,vecs[i]->size()); | ||
} | ||
// this is just to decrease the number of reallocations on push_back | ||
result.reserve(maxsize); | ||
// if vectors are empty we are done | ||
if(maxsize==0) return; | ||
} | ||
|
||
// start | ||
// heap containing the ranges to be pushed | ||
// avoid allocations when it's small | ||
gch::small_vector<Entry,32> heap; | ||
|
||
{ | ||
for(unsigned i=0; i<size; i++) { | ||
if(!vecs[i]->empty()) { | ||
// add entry at the end of the array | ||
heap.emplace_back(Entry(*vecs[i])); | ||
} | ||
} | ||
// make a sorted heap | ||
std::make_heap(std::begin(heap),std::end(heap)); | ||
} | ||
|
||
// first iteration, to avoid an extra "if" in main iteration | ||
// explanations are below | ||
{ | ||
std::pop_heap(std::begin(heap), std::end(heap)); | ||
auto & tmp=heap.back(); | ||
const auto val=tmp.top(); | ||
// here, we append inconditionally | ||
result.push_back(val); | ||
tmp.next(); | ||
if(tmp.empty()) heap.pop_back(); | ||
else std::push_heap(std::begin(heap), std::end(heap)); | ||
} | ||
|
||
while(!heap.empty()) { | ||
// move entry with the smallest element to the back of the array | ||
std::pop_heap(std::begin(heap), std::end(heap)); | ||
// entry | ||
auto & tmp=heap.back(); | ||
// element | ||
const auto val=tmp.top(); | ||
// if the element is larger than the current largest element, | ||
// push it to result | ||
if(val > result.back()) result.push_back(val); | ||
// move forward the used entry | ||
tmp.next(); | ||
// if this entry is exhausted, remove it from the array | ||
if(tmp.empty()) heap.pop_back(); | ||
// otherwise, sort again the array | ||
else std::push_heap(std::begin(heap), std::end(heap)); | ||
} | ||
} | ||
|
||
template<class C> | ||
static void mergeSortedVectors(const std::vector<C*> & vecs, std::vector<typename C::value_type> & result) { | ||
mergeSortedVectors(vecs.data(),vecs.size(),result); | ||
} | ||
} | ||
|
||
} | ||
|
||
#endif | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
0c8be3e
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Found broken examples in automatic/ADAPTIVE_PATH.tmp
Found broken examples in automatic/ANGLES.tmp
Found broken examples in automatic/ANN.tmp
Found broken examples in automatic/AROUND.tmp
Found broken examples in automatic/CAVITY.tmp
Found broken examples in automatic/CLUSTER_DIAMETER.tmp
Found broken examples in automatic/CLUSTER_DISTRIBUTION.tmp
Found broken examples in automatic/CLUSTER_PROPERTIES.tmp
Found broken examples in automatic/CONSTANT.tmp
Found broken examples in automatic/CONTACT_MATRIX.tmp
Found broken examples in automatic/CONVERT_TO_FES.tmp
Found broken examples in automatic/COORDINATIONNUMBER.tmp
Found broken examples in automatic/DFSCLUSTERING.tmp
Found broken examples in automatic/DISTANCE_FROM_CONTOUR.tmp
Found broken examples in automatic/DUMPCUBE.tmp
Found broken examples in automatic/DUMPGRID.tmp
Found broken examples in automatic/EDS.tmp
Found broken examples in automatic/EMMI.tmp
Found broken examples in automatic/ENVIRONMENTSIMILARITY.tmp
Found broken examples in automatic/FIND_CONTOUR.tmp
Found broken examples in automatic/FIND_CONTOUR_SURFACE.tmp
Found broken examples in automatic/FIND_SPHERICAL_CONTOUR.tmp
Found broken examples in automatic/FOURIER_TRANSFORM.tmp
Found broken examples in automatic/FUNCPATHGENERAL.tmp
Found broken examples in automatic/FUNCPATHMSD.tmp
Found broken examples in automatic/FUNNEL.tmp
Found broken examples in automatic/FUNNEL_PS.tmp
Found broken examples in automatic/GHBFIX.tmp
Found broken examples in automatic/GPROPERTYMAP.tmp
Found broken examples in automatic/HBOND_MATRIX.tmp
Found broken examples in automatic/HISTOGRAM.tmp
Found broken examples in automatic/INCLUDE.tmp
Found broken examples in automatic/INCYLINDER.tmp
Found broken examples in automatic/INENVELOPE.tmp
Found broken examples in automatic/INSPHERE.tmp
Found broken examples in automatic/INTERPOLATE_GRID.tmp
Found broken examples in automatic/LOCAL_AVERAGE.tmp
Found broken examples in automatic/LOCAL_Q3.tmp
Found broken examples in automatic/LOCAL_Q4.tmp
Found broken examples in automatic/LOCAL_Q6.tmp
Found broken examples in automatic/MAZE_OPTIMIZER_BIAS.tmp
Found broken examples in automatic/MAZE_RANDOM_ACCELERATION_MD.tmp
Found broken examples in automatic/MAZE_SIMULATED_ANNEALING.tmp
Found broken examples in automatic/MAZE_STEERED_MD.tmp
Found broken examples in automatic/MULTICOLVARDENS.tmp
Found broken examples in automatic/OUTPUT_CLUSTER.tmp
Found broken examples in automatic/PAMM.tmp
Found broken examples in automatic/PARABETARMSD.tmp
Found broken examples in automatic/PATH.tmp
Found broken examples in automatic/PCAVARS.tmp
Found broken examples in automatic/PIV.tmp
Found broken examples in automatic/PLUMED.tmp
Found broken examples in automatic/PYCVINTERFACE.tmp
Found broken examples in automatic/PYTHONFUNCTION.tmp
Found broken examples in automatic/QUATERNION.tmp
Found broken examples in automatic/REWEIGHT_BIAS.tmp
Found broken examples in automatic/REWEIGHT_METAD.tmp
Found broken examples in automatic/SPRINT.tmp
Found broken examples in automatic/TETRAHEDRALPORE.tmp
Found broken examples in automatic/TORSION.tmp
Found broken examples in automatic/TORSIONS.tmp
Found broken examples in automatic/WHAM_HISTOGRAM.tmp
Found broken examples in automatic/WHAM_WEIGHTS.tmp
Found broken examples in AnalysisPP.md
Found broken examples in CollectiveVariablesPP.md
Found broken examples in MiscelaneousPP.md