aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/python/pythran/pythran/pythonic/numpy/ndindex.hpp
blob: 24010dc5b386cd2fc47f051faf47ffcdf336ed8b (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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
#ifndef PYTHONIC_NUMPY_NDINDEX_HPP
#define PYTHONIC_NUMPY_NDINDEX_HPP

#include "pythonic/include/numpy/ndindex.hpp"

#include "pythonic/utils/functor.hpp"

#include <numeric>

PYTHONIC_NS_BEGIN

namespace numpy
{
  template <size_t N>
  ndindex_iterator<N>::ndindex_iterator()
  {
  }

  template <size_t N>
  ndindex_iterator<N>::ndindex_iterator(types::array<long, N> const &shape,
                                        long first)
      : index(first), shape(shape)
  {
  }

  template <size_t N>
  types::array<long, N> ndindex_iterator<N>::operator*() const
  {
    types::array<long, N> out;
    long mult = 1;
    for (long j = N - 1; j > 0; j--) {
      out[j] = (index / mult) % shape[j];
      mult *= shape[j];
    }
    out[0] = index / mult;
    return out;
  }

  template <size_t N>
  ndindex_iterator<N> &ndindex_iterator<N>::operator++()
  {
    ++index;
    return *this;
  }

  template <size_t N>
  ndindex_iterator<N> &ndindex_iterator<N>::operator+=(long n)
  {
    index += n;
    return *this;
  }

  template <size_t N>
  bool ndindex_iterator<N>::operator!=(ndindex_iterator<N> const &other) const
  {
    return index != other.index;
  }

  template <size_t N>
  bool ndindex_iterator<N>::operator<(ndindex_iterator<N> const &other) const
  {
    return index < other.index;
  }

  template <size_t N>
  long ndindex_iterator<N>::operator-(ndindex_iterator<N> const &other) const
  {
    return index - other.index;
  }

  template <size_t N>
  _ndindex<N>::_ndindex()
  {
  }

  template <size_t N>
  _ndindex<N>::_ndindex(types::array<long, N> const &shape)
      : ndindex_iterator<N>(shape, 0), shape(shape),
        end_iter(shape, std::accumulate(shape.begin(), shape.end(), 1L,
                                        std::multiplies<long>()))
  {
  }

  template <size_t N>
  typename _ndindex<N>::iterator &_ndindex<N>::begin()
  {
    return *this;
  }

  template <size_t N>
  typename _ndindex<N>::iterator const &_ndindex<N>::begin() const
  {
    return *this;
  }

  template <size_t N>
  typename _ndindex<N>::iterator _ndindex<N>::end() const
  {
    return end_iter;
  }

  template <class... Types>
  _ndindex<sizeof...(Types)> ndindex(Types... args)
  {
    return {types::make_tuple(args...)};
  }

  template <size_t N>
  _ndindex<N> ndindex(types::array<long, N> const &args)
  {
    return {args};
  }
  template <class... Tys>
  _ndindex<sizeof...(Tys)> ndindex(types::pshape<Tys...> const &args)
  {
    return {args};
  }
}
PYTHONIC_NS_END

#endif