1 #ifndef __NUMERIC_LIMITED_COMBINATION_HPP__ 2 #define __NUMERIC_LIMITED_COMBINATION_HPP__ 6 #include <numeric/Combinatorics.hpp> 35 template<
typename AtomType>
38 typedef uint32_t CoreType;
39 typedef std::map<AtomType, size_t> AtomType2CountMap;
40 typedef std::vector<AtomType> AtomTypeList;
41 typedef std::vector<CoreType> CombinationDescriptor;
44 AtomType2CountMap mAtomTypeAvailablilityMap;
45 AtomTypeList mAtomTypeList;
47 std::vector<CoreType> mLimits;
48 std::vector<CoreType> mCurrentCombination;
60 CoreType currentType = 0;
61 typename AtomType2CountMap::const_iterator cit = mAtomTypeAvailablilityMap.begin();
62 for(; cit != mAtomTypeAvailablilityMap.end(); ++cit, ++currentType)
64 mAtomTypeList.push_back(cit->first);
65 size_t numberPerType = cit->second;
66 mLimits.at(currentType) = numberPerType;
72 throw std::runtime_error(
"numeric::LimitedCombination::prepare: preparation failed " 73 "check the given parameters");
81 std::vector<AtomType> mapToAtomTypes(
const std::vector<CoreType>& combination)
const 83 std::vector<AtomType> atomTypeList;
84 for(
size_t coreType = 0; coreType < combination.size(); ++coreType)
86 size_t count = combination[coreType];
87 for(
size_t i = 0; i < count; ++i)
89 atomTypeList.push_back( mAtomTypeList[coreType] );
95 bool increment(std::vector<uint32_t>& combination,
const std::vector<uint32_t>& limits)
const 97 for(
size_t pos = 0; pos < combination.size(); ++pos)
99 if( combination[pos] < limits[pos] )
108 for(
int lpos = pos-1; lpos >= 0; --lpos)
110 combination[lpos] = 0;
113 }
else if(pos == combination.size() - 1)
118 throw std::runtime_error(
"numeric::LimitedCombination::increment internal error -- " 119 " please check consistency of your input");
130 : mAtomTypeAvailablilityMap(countMap)
131 , mLimits(countMap.size(),0)
132 , mCurrentCombination(countMap.size(),0)
137 if(countMap.empty() || totalCount == 0)
139 throw std::invalid_argument(
"numeric::LimitedCombination no atoms to generate combination from -- check for empty map");
141 if(mSize > totalCount)
155 typename AtomType2CountMap::const_iterator cit = countMap.begin();
157 for(; cit != countMap.end(); ++cit)
159 count += cit->second;
170 std::vector<AtomType> atomTypeList = mapToAtomTypes(mCurrentCombination);
171 std::sort(atomTypeList.begin(), atomTypeList.end());
177 size_t combinationSize = 0;
178 std::vector<CoreType>::const_iterator cit = combination.begin();
179 for(; cit != combination.end(); ++cit)
181 combinationSize += *cit;
183 return combinationSize;
196 bool hasNext = increment(mCurrentCombination, mLimits);
205 if(combinationSize != mSize)
211 if(combinationSize > mSize)
217 if(combinationSize < mSize)
235 #endif // __NUMERIC_LIMITED_COMBINATION_HPP__ size_t getCombinationSize(const std::vector< CoreType > &combination) const
Definition: LimitedCombination.hpp:175
Compute combinatorics on a given set of limited but typed resources, e.g. for available resource A:2...
Definition: LimitedCombination.hpp:36
Definition: Combinatorics.hpp:82
Definition: Combinatorics.hpp:82
Mode
Definition: Combinatorics.hpp:82
bool next()
Definition: LimitedCombination.hpp:192
std::vector< AtomType > current() const
Definition: LimitedCombination.hpp:168
LimitedCombination(const AtomType2CountMap &countMap, size_t size, numeric::Mode mode)
Definition: LimitedCombination.hpp:129
static size_t totalNumberOfAtoms(const AtomType2CountMap &countMap)
Definition: LimitedCombination.hpp:153
Definition: Combinatorics.hpp:82