aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pythran/pythran/pythonic/include/itertools/product.hpp
blob: 7ca6f0c6a991f67108e9d9f491e793ea38858a2b (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
#ifndef PYTHONIC_INCLUDE_ITERTOOLS_PRODUCT_HPP
#define PYTHONIC_INCLUDE_ITERTOOLS_PRODUCT_HPP

#include "pythonic/include/utils/iterator.hpp"
#include "pythonic/include/utils/seq.hpp"
#include "pythonic/include/utils/int_.hpp"
#include "pythonic/include/itertools/common.hpp"
#include "pythonic/include/utils/functor.hpp"

#include <iterator>
#include <type_traits>

PYTHONIC_NS_BEGIN

namespace itertools
{
  namespace details
  {

    // FIXME : should be a combined_iterator_tag
    template <typename... Iters>
    struct product_iterator
        : std::iterator<std::forward_iterator_tag,
                        types::make_tuple_t<typename Iters::value_type...>> {

      std::tuple<typename Iters::iterator...> const it_begin;
      std::tuple<typename Iters::iterator...> const it_end;
      std::tuple<typename Iters::iterator...> it;
      bool end;

      product_iterator() = default;
      template <size_t... I>
      product_iterator(std::tuple<Iters...> &_iters,
                       utils::index_sequence<I...> const &);
      template <size_t... I>
      product_iterator(npos, std::tuple<Iters...> &_iters,
                       utils::index_sequence<I...> const &);
      types::make_tuple_t<typename Iters::value_type...> operator*() const;
      product_iterator &operator++();
      bool operator==(product_iterator const &other) const;
      bool operator!=(product_iterator const &other) const;
      bool operator<(product_iterator const &other) const;

    private:
      template <size_t N>
      void advance(utils::int_<N>);
      void advance(utils::int_<0>);
      template <size_t... I>
      types::make_tuple_t<typename Iters::value_type...>
      get_value(utils::index_sequence<I...> const &) const;
    };

    template <typename... Iters>
    struct product : utils::iterator_reminder<true, Iters...>,
                     product_iterator<Iters...> {

      using value_type = types::make_tuple_t<typename Iters::value_type...>;
      using iterator = product_iterator<Iters...>;

      iterator end_iter;

      product() = default;
      product(Iters const &... _iters);

      iterator &begin();
      iterator const &begin() const;
      iterator const &end() const;
    };
  }

  template <typename... Iter>
  details::product<typename std::remove_cv<
      typename std::remove_reference<Iter>::type>::type...>
  product(Iter &&... iters);

  DEFINE_FUNCTOR(pythonic::itertools, product);
}
PYTHONIC_NS_END

/* type inference stuff  {*/
#include "pythonic/include/types/combined.hpp"

template <class E, class... Iter>
struct __combined<E, pythonic::itertools::details::product<Iter...>> {
  using type =
      typename __combined<E, container<typename pythonic::itertools::details::
                                           product<Iter...>::value_type>>::type;
};

/* } */

#endif