AIToolbox
A library that offers tools for AI problem solving.
Combinatorics.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_UTILS_COMBINATORICS_HEADER_FILE
2 #define AI_TOOLBOX_UTILS_COMBINATORICS_HEADER_FILE
3 
4 #include <vector>
5 #include <numeric>
6 #include <cassert>
7 
8 namespace AIToolbox {
12  unsigned nChooseK(unsigned n, unsigned k);
13 
20  unsigned starsBars(unsigned stars, unsigned bars);
21 
32  unsigned ballsBins(unsigned balls, unsigned bins);
33 
40  unsigned nonZeroStarsBars(unsigned stars, unsigned bars);
41 
53  unsigned nonZeroBallsBins(unsigned balls, unsigned bins);
54 
58  template <typename Index>
60  public:
61  using IdsStorage = std::vector<Index>;
62 
70  SubsetEnumerator(size_t elementsN, Index lowerBound, Index upperBound) :
71  lowerBound_(lowerBound), upperBound_(upperBound), ids_(elementsN)
72  {
73  if constexpr (std::is_integral_v<Index>) {
74  assert(upperBound_ > lowerBound_);
75  assert(static_cast<size_t>(upperBound_ - lowerBound_) >= elementsN);
76  } else
77  assert(static_cast<size_t>(std::distance(lowerBound_, upperBound_)) >= elementsN);
78  reset();
79  }
80 
107  auto advance() {
108  auto current = ids_.size() - 1;
109  auto ub = upperBound_ - 1;
110  while (current && ids_[current] == ub) --current, --ub;
111 
112  auto lowest = current; // Last element we need to change.
113  ub = ++ids_[current];
114 
115  while (++current != ids_.size()) ids_[current] = ++ub;
116 
117  return lowest;
118  }
119 
123  bool isValid() const {
124  return ids_.back() < upperBound_;
125  }
126 
130  void reset() {
131  std::iota(std::begin(ids_), std::end(ids_), lowerBound_);
132  }
133 
137  auto subsetsSize() const {
138  if constexpr (std::is_integral_v<Index>)
139  return nChooseK(upperBound_ - lowerBound_, ids_.size());
140  else
141  return nChooseK(std::distance(lowerBound_, upperBound_), ids_.size());
142  }
143 
147  auto size() const { return ids_.size(); }
148 
149 
158  const IdsStorage& operator*() const {
159  return ids_;
160  }
161 
170  const IdsStorage* operator->() const {
171  return &ids_;
172  }
173 
174  private:
175  Index lowerBound_, upperBound_;
176  IdsStorage ids_;
177  };
178 }
179 
180 #endif
AIToolbox::SubsetEnumerator::isValid
bool isValid() const
This function returns whether there are more subsets to be enumerated.
Definition: Combinatorics.hpp:123
AIToolbox::SubsetEnumerator
This class enumerates all possible vectors of finite subsets over N elements.
Definition: Combinatorics.hpp:59
AIToolbox::SubsetEnumerator::advance
auto advance()
This function advances the SubsetEnumerator to the next possible subset.
Definition: Combinatorics.hpp:107
AIToolbox::nonZeroBallsBins
unsigned nonZeroBallsBins(unsigned balls, unsigned bins)
Returns the number of balls/bins combinations.
AIToolbox::SubsetEnumerator::reset
void reset()
This function resets the enumerator to the valid beginning.
Definition: Combinatorics.hpp:130
AIToolbox
Definition: Experience.hpp:6
AIToolbox::ballsBins
unsigned ballsBins(unsigned balls, unsigned bins)
Returns the number of balls/bins combinations.
AIToolbox::SubsetEnumerator::operator*
const IdsStorage & operator*() const
This operator returns the current combination.
Definition: Combinatorics.hpp:158
AIToolbox::nonZeroStarsBars
unsigned nonZeroStarsBars(unsigned stars, unsigned bars)
Returns the number of stars/bars combinations.
AIToolbox::SubsetEnumerator::IdsStorage
std::vector< Index > IdsStorage
Definition: Combinatorics.hpp:61
AIToolbox::SubsetEnumerator::operator->
const IdsStorage * operator->() const
This operator returns a pointer to the current combination.
Definition: Combinatorics.hpp:170
AIToolbox::SubsetEnumerator::subsetsSize
auto subsetsSize() const
This function returns the number of subsets enumerated over.
Definition: Combinatorics.hpp:137
AIToolbox::nChooseK
unsigned nChooseK(unsigned n, unsigned k)
Returns (n k); i.e. n choose k.
AIToolbox::SubsetEnumerator::size
auto size() const
This function returns the size of the range covered.
Definition: Combinatorics.hpp:147
AIToolbox::SubsetEnumerator::SubsetEnumerator
SubsetEnumerator(size_t elementsN, Index lowerBound, Index upperBound)
Default constructor.
Definition: Combinatorics.hpp:70
AIToolbox::starsBars
unsigned starsBars(unsigned stars, unsigned bars)
Returns the number of stars/bars combinations.