cartesian_product.h 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  1. #pragma once
  2. #include <util/generic/store_policy.h>
  3. #include <tuple>
  4. namespace NPrivate {
  5. template <typename... TContainers>
  6. struct TCartesianMultiplier {
  7. template <std::size_t... I>
  8. struct TCartesianMultiplierWithIndex {
  9. private:
  10. using THolders = std::tuple<TAutoEmbedOrPtrPolicy<TContainers>...>;
  11. using TValue = std::tuple<decltype(*std::begin(std::declval<TContainers&>()))...>;
  12. using TIteratorState = std::tuple<int, decltype(std::begin(std::declval<TContainers&>()))...>;
  13. using TSentinelState = std::tuple<int, decltype(std::end(std::declval<TContainers&>()))...>;
  14. struct TIterator;
  15. struct TSentinelCandidate {
  16. TSentinelState Iterators_;
  17. THolders* HoldersPtr_;
  18. };
  19. using TSentinel = std::conditional_t<std::is_same_v<TIteratorState, TSentinelState>,
  20. TIterator, TSentinelCandidate>;
  21. struct TIterator {
  22. private:
  23. //! Return value is true when iteration is not finished
  24. template <std::size_t position = sizeof...(TContainers)>
  25. void IncrementIteratorsTuple() {
  26. auto& currentIterator = std::get<position>(Iterators_);
  27. ++currentIterator;
  28. if (currentIterator != std::end(*std::get<position - 1>(*HoldersPtr_).Ptr())) {
  29. return;
  30. } else {
  31. currentIterator = std::begin(*std::get<position - 1>(*HoldersPtr_).Ptr());
  32. if constexpr (position != 1) {
  33. IncrementIteratorsTuple<position - 1>();
  34. } else {
  35. std::get<0>(Iterators_) = 1;
  36. }
  37. }
  38. }
  39. public:
  40. using difference_type = std::ptrdiff_t;
  41. using value_type = TValue;
  42. using pointer = TValue*;
  43. using reference = TValue&;
  44. using iterator_category = std::input_iterator_tag;
  45. TValue operator*() {
  46. return {*std::get<I + 1>(Iterators_)...};
  47. }
  48. TValue operator*() const {
  49. return {*std::get<I + 1>(Iterators_)...};
  50. }
  51. void operator++() {
  52. IncrementIteratorsTuple();
  53. }
  54. bool operator!=(const TSentinel& other) const {
  55. // not finished iterator VS sentinel (most frequent case)
  56. if (std::get<0>(Iterators_) != std::get<0>(other.Iterators_)) {
  57. return true;
  58. }
  59. // do not compare sentinels and finished iterators
  60. if (std::get<0>(other.Iterators_)) {
  61. return false;
  62. }
  63. // compare not finished iterators
  64. return ((std::get<I + 1>(Iterators_) != std::get<I + 1>(other.Iterators_)) || ...);
  65. }
  66. bool operator==(const TSentinel& other) const {
  67. return !(*this != other);
  68. }
  69. TIteratorState Iterators_;
  70. THolders* HoldersPtr_;
  71. };
  72. public:
  73. using iterator = TIterator;
  74. using const_iterator = TIterator;
  75. using value_type = typename TIterator::value_type;
  76. using reference = typename TIterator::reference;
  77. using const_reference = typename TIterator::reference;
  78. TIterator begin() const {
  79. bool isEmpty = !((std::begin(*std::get<I>(Holders_).Ptr()) != std::end(*std::get<I>(Holders_).Ptr())) && ...);
  80. return {TIteratorState{int(isEmpty), std::begin(*std::get<I>(Holders_).Ptr())...}, &Holders_};
  81. }
  82. TSentinel end() const {
  83. return {TSentinelState{1, std::end(*std::get<I>(Holders_).Ptr())...}, &Holders_};
  84. }
  85. mutable THolders Holders_;
  86. };
  87. template <std::size_t... I>
  88. static auto CartesianMultiply(TContainers&&... containers, std::index_sequence<I...>) {
  89. return TCartesianMultiplierWithIndex<I...>{{std::forward<TContainers>(containers)...}};
  90. }
  91. };
  92. }
  93. //! Usage: for (auto [ai, bi] : CartesianProduct(a, b)) {...}
  94. //! Equivalent: for (auto& ai : a) { for (auto& bi : b) {...} }
  95. template <typename... TContainers>
  96. auto CartesianProduct(TContainers&&... containers) {
  97. return NPrivate::TCartesianMultiplier<TContainers...>::CartesianMultiply(
  98. std::forward<TContainers>(containers)..., std::make_index_sequence<sizeof...(TContainers)>{});
  99. }