AIToolbox
A library that offers tools for AI problem solving.
Core.hpp
Go to the documentation of this file.
1 #ifndef AI_TOOLBOX_FACTORED_UTILS_HEADER_FILE
2 #define AI_TOOLBOX_FACTORED_UTILS_HEADER_FILE
3 
5 
6 namespace AIToolbox::Factored {
19  enum class TagErrors {
20  None,
21  NoElements,
23  IdTooHigh,
24  NotSorted,
25  Duplicates,
26  };
27 
42  std::pair<TagErrors, size_t> checkTag(const Factors & space, const PartialKeys & tag);
43 
52  PartialFactors removeFactor(const PartialFactors & pf, size_t f);
53 
57  template <typename Gen>
58  Factors makeRandomValue(const Factors & space, Gen & rnd) {
59  Factors retval(space.size());
60  for (size_t i = 0; i < space.size(); ++i) {
61  std::uniform_int_distribution<size_t> dist(0, space[i]-1);
62  retval[i] = dist(rnd);
63  }
64  return retval;
65  }
66 
75  bool match(const PartialFactors & lhs, const PartialFactors & rhs);
76 
85  bool match(const Factors & lhs, const PartialFactors & rhs);
86 
101  bool match(const PartialKeys & lhsK, const PartialValues & lhs, const PartialKeys & rhsK, const PartialValues & rhs);
102 
112  bool match(const PartialKeys & keys, const Factors & lhs, const Factors & rhs);
113 
128  bool match(const std::vector<std::pair<size_t, size_t>> & matches, const Factors & lhs, const Factors & rhs);
129 
137  void join(size_t S, PartialFactors * lhs, const PartialFactors & rhs);
138 
147  Factors join(const Factors & lhs, const Factors & rhs);
148 
158  PartialKeys join(size_t S, const PartialKeys & lhs, const PartialKeys & rhs);
159 
169  PartialFactors join(size_t S, const PartialFactors & lhs, const PartialFactors & rhs);
170 
183  void unsafe_join(PartialFactors * lhs, const PartialFactors & rhs);
184 
198  PartialFactors merge(const PartialFactors & lhs, const PartialFactors & rhs);
199 
219  PartialValues merge(const PartialKeys & lhsk, const PartialValues & lhs, const PartialKeys & rhsk, const PartialValues & rhs);
220 
237  PartialKeys merge(const PartialKeys & lhs, const PartialKeys & rhs, std::vector<std::pair<size_t, size_t>> * matches = nullptr);
238 
249  size_t factorSpace(const Factors & space);
250 
266  size_t factorSpacePartial(const PartialKeys & ids, const Factors & space);
267 
276 
290  Factors toFactors(size_t F, const PartialFactors & pf);
291 
306  Factors toFactors(const Factors & space, size_t id);
307 
320  void toFactors(const Factors & space, size_t id, Factors * out);
321 
338  PartialValues toFactorsPartial(const PartialKeys & ids, const Factors & space, size_t id);
339 
353  template <typename It>
354  void toFactorsPartial(It begin, const PartialKeys & ids, const Factors & space, size_t id) {
355  for (auto key : ids) {
356  *begin = id % space[key];
357  id /= space[key];
358  ++begin;
359  }
360  }
361 
392  size_t toIndex(const Factors & space, const Factors & f);
393 
406  size_t toIndex(const Factors & space, const PartialFactors & f);
407 
430  size_t toIndexPartial(const PartialKeys & ids, const Factors & space, const Factors & f);
431 
454  size_t toIndexPartial(const PartialKeys & ids, const Factors & space, const PartialFactors & pf);
455 
469  size_t toIndexPartial(const Factors & space, const PartialFactors & f);
470 
511  std::pair<size_t, size_t> toIndexPartialAndSkip(const PartialKeys & ids, const Factors & space, const Factors & f, size_t id);
512 
532  public:
545 
556 
572  PartialFactorsEnumerator(Factors f, const PartialKeys & factors, size_t factorToSkip, bool missing = false);
573 
589  PartialFactorsEnumerator(Factors f, size_t factorToSkip);
590 
602  size_t getFactorToSkipId() const;
603 
607  void advance();
608 
614  bool isValid() const;
615 
619  void reset();
620 
626  size_t size() const;
627 
644 
661 
662  private:
663  Factors F;
664  PartialFactors factors_;
665  size_t factorToSkipId_;
666  };
667 
703  public:
711  PartialIndexEnumerator(const Factors & F, size_t fixedFactor, size_t val);
712 
722  PartialIndexEnumerator(const Factors & F, const PartialKeys & factors, size_t fixedFactor, size_t val, bool missing = false);
723 
729  size_t operator*() const;
730 
734  void advance();
735 
741  bool isValid();
742 
746  void reset();
747 
748  private:
749  size_t len_;
750  size_t skip_;
751  size_t offset_;
752  size_t curr_, currLen_;
753  size_t max_;
754  };
755 }
756 
757 #endif
AIToolbox::Factored::join
void join(size_t S, PartialFactors *lhs, const PartialFactors &rhs)
This function appends the rhs to the lhs, assuming the original Factor for lhs has S elements.
AIToolbox::Factored::PartialFactors
std::pair< PartialKeys, PartialValues > PartialFactors
Definition: Types.hpp:65
AIToolbox::Factored::PartialKeys
std::vector< size_t > PartialKeys
Definition: Types.hpp:63
AIToolbox::Factored::TagErrors::IdTooHigh
@ IdTooHigh
AIToolbox::Factored::checkTag
std::pair< TagErrors, size_t > checkTag(const Factors &space, const PartialKeys &tag)
This function verifies whether a tag is correct w.r.t. a space.
AIToolbox::Factored::PartialIndexEnumerator::operator*
size_t operator*() const
This operator returns the current index.
AIToolbox::Factored::removeFactor
PartialFactors removeFactor(const PartialFactors &pf, size_t f)
This function removes the specified factor from the input PartialFactors.
AIToolbox::Factored::PartialValues
std::vector< size_t > PartialValues
Definition: Types.hpp:64
AIToolbox::Factored::toPartialFactors
PartialFactors toPartialFactors(const Factors &f)
This function converts Factors into the equivalent PartialFactors structure.
AIToolbox::Factored::toIndex
size_t toIndex(const Factors &space, const Factors &f)
This function converts the input factor in the input space to an unique index.
AIToolbox::Factored::PartialIndexEnumerator::advance
void advance()
This function advances the PartialFactorsEnumerator to the next index.
AIToolbox::Factored::PartialFactorsEnumerator::getFactorToSkipId
size_t getFactorToSkipId() const
This function returns the id of the factorToSkip inside the PartialFactorsEnumerator.
AIToolbox::Factored::toIndexPartialAndSkip
std::pair< size_t, size_t > toIndexPartialAndSkip(const PartialKeys &ids, const Factors &space, const Factors &f, size_t id)
This function avoids computing indeces multiple times if only a single index is changing.
AIToolbox::Factored::PartialIndexEnumerator::PartialIndexEnumerator
PartialIndexEnumerator(const Factors &F, size_t fixedFactor, size_t val)
Basic constructor.
AIToolbox::Factored::PartialFactorsEnumerator::advance
void advance()
This function advances the PartialFactorsEnumerator to the next possible combination.
AIToolbox::Factored::PartialFactorsEnumerator::reset
void reset()
This function resets the enumerator to the valid beginning (a fully zero PartialFactor).
AIToolbox::Factored::TagErrors::TooManyElements
@ TooManyElements
AIToolbox::Factored::toFactorsPartial
PartialValues toFactorsPartial(const PartialKeys &ids, const Factors &space, size_t id)
This function converts an index into the equivalent PartialValues of the input keys,...
AIToolbox::Factored::toIndexPartial
size_t toIndexPartial(const PartialKeys &ids, const Factors &space, const Factors &f)
This function converts the input factor in the input space to an unique index.
AIToolbox::Factored::makeRandomValue
Factors makeRandomValue(const Factors &space, Gen &rnd)
This function randomly generates a valid value inside the provided space.
Definition: Core.hpp:58
AIToolbox::Factored::merge
PartialFactors merge(const PartialFactors &lhs, const PartialFactors &rhs)
This function merges two PartialFactors together.
AIToolbox::Factored::TagErrors::NoElements
@ NoElements
AIToolbox::Factored::PartialIndexEnumerator::isValid
bool isValid()
This function returns whether it is safe to dereference the PartialFactorsEnumerator.
AIToolbox::Factored::factorSpace
size_t factorSpace(const Factors &space)
This function returns the multiplication of all elements of the input factor.
AIToolbox::Factored::Factors
std::vector< size_t > Factors
Definition: Types.hpp:62
AIToolbox::Factored::PartialFactorsEnumerator::operator*
PartialFactors & operator*()
This operator returns the current iteration in the values of the PartialFactors.
AIToolbox::Factored::factorSpacePartial
size_t factorSpacePartial(const PartialKeys &ids, const Factors &space)
This function returns the multiplication of all elements of the input factor.
AIToolbox::Factored::match
bool match(const PartialFactors &lhs, const PartialFactors &rhs)
This function returns whether the common factors in the inputs match in value.
AIToolbox::Factored::PartialFactorsEnumerator::PartialFactorsEnumerator
PartialFactorsEnumerator(Factors f, PartialKeys factors)
Basic constructor.
AIToolbox::Factored::toFactors
Factors toFactors(size_t F, const PartialFactors &pf)
This function converts PartialFactors into the equivalent Factors structure.
AIToolbox::Factored::PartialIndexEnumerator::reset
void reset()
This function resets the PartialIndexEnumerator to the first valid index.
AIToolbox::Factored
Definition: GraphUtils.hpp:12
AIToolbox::Factored::TagErrors::Duplicates
@ Duplicates
Types.hpp
AIToolbox::Factored::TagErrors
TagErrors
This enum contains all possible errors in a tag.
Definition: Core.hpp:19
AIToolbox::Factored::PartialFactorsEnumerator::isValid
bool isValid() const
This function returns whether this object has terminated advancing and can be dereferenced.
AIToolbox::Factored::PartialFactorsEnumerator::size
size_t size() const
This function returns the number of times that advance() can be called from the initial state.
AIToolbox::Factored::PartialFactorsEnumerator
This class enumerates all possible values for a PartialFactors.
Definition: Core.hpp:531
AIToolbox::Factored::TagErrors::NotSorted
@ NotSorted
AIToolbox::Factored::PartialIndexEnumerator
This class enumerates the indeces of all combinations where a value is fixed.
Definition: Core.hpp:702
AIToolbox::Factored::TagErrors::None
@ None
AIToolbox::Factored::unsafe_join
void unsafe_join(PartialFactors *lhs, const PartialFactors &rhs)
This function appends the rhs to the lhs.
AIToolbox::Factored::PartialFactorsEnumerator::operator->
PartialFactors * operator->()
This operator returns the current iteration in the values of the PartialFactors.