/ lib / numpy / core / include / numpy / _neighborhood_iterator_imp.h
_neighborhood_iterator_imp.h
 1  #ifndef NUMPY_CORE_INCLUDE_NUMPY__NEIGHBORHOOD_IMP_H_
 2  #error You should not include this header directly
 3  #endif
 4  /*
 5   * Private API (here for inline)
 6   */
 7  static NPY_INLINE int
 8  _PyArrayNeighborhoodIter_IncrCoord(PyArrayNeighborhoodIterObject* iter);
 9  
10  /*
11   * Update to next item of the iterator
12   *
13   * Note: this simply increment the coordinates vector, last dimension
14   * incremented first , i.e, for dimension 3
15   * ...
16   * -1, -1, -1
17   * -1, -1,  0
18   * -1, -1,  1
19   *  ....
20   * -1,  0, -1
21   * -1,  0,  0
22   *  ....
23   * 0,  -1, -1
24   * 0,  -1,  0
25   *  ....
26   */
27  #define _UPDATE_COORD_ITER(c) \
28      wb = iter->coordinates[c] < iter->bounds[c][1]; \
29      if (wb) { \
30          iter->coordinates[c] += 1; \
31          return 0; \
32      } \
33      else { \
34          iter->coordinates[c] = iter->bounds[c][0]; \
35      }
36  
37  static NPY_INLINE int
38  _PyArrayNeighborhoodIter_IncrCoord(PyArrayNeighborhoodIterObject* iter)
39  {
40      npy_intp i, wb;
41  
42      for (i = iter->nd - 1; i >= 0; --i) {
43          _UPDATE_COORD_ITER(i)
44      }
45  
46      return 0;
47  }
48  
49  /*
50   * Version optimized for 2d arrays, manual loop unrolling
51   */
52  static NPY_INLINE int
53  _PyArrayNeighborhoodIter_IncrCoord2D(PyArrayNeighborhoodIterObject* iter)
54  {
55      npy_intp wb;
56  
57      _UPDATE_COORD_ITER(1)
58      _UPDATE_COORD_ITER(0)
59  
60      return 0;
61  }
62  #undef _UPDATE_COORD_ITER
63  
64  /*
65   * Advance to the next neighbour
66   */
67  static NPY_INLINE int
68  PyArrayNeighborhoodIter_Next(PyArrayNeighborhoodIterObject* iter)
69  {
70      _PyArrayNeighborhoodIter_IncrCoord (iter);
71      iter->dataptr = iter->translate((PyArrayIterObject*)iter, iter->coordinates);
72  
73      return 0;
74  }
75  
76  /*
77   * Reset functions
78   */
79  static NPY_INLINE int
80  PyArrayNeighborhoodIter_Reset(PyArrayNeighborhoodIterObject* iter)
81  {
82      npy_intp i;
83  
84      for (i = 0; i < iter->nd; ++i) {
85          iter->coordinates[i] = iter->bounds[i][0];
86      }
87      iter->dataptr = iter->translate((PyArrayIterObject*)iter, iter->coordinates);
88  
89      return 0;
90  }