BlackCat_Tensors
A GPU-supported autograd and linear algebra library, designed for neural network construction
multidimensional_iterator.h
Go to the documentation of this file.
1 /* Project: BlackCat_Tensors
2  * Author: JosephJaspers
3  * Copyright 2018
4  *
5  * This Source Code Form is subject to the terms of the Mozilla Public
6  * License, v. 2.0. If a copy of the MPL was not distributed with this
7  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 
9 #ifndef tensor_iterator_dim_H_
10 #define tensor_iterator_dim_H_
11 
12 #include "common.h"
13 
14 namespace bc {
15 namespace tensors {
16 namespace iterators {
17 
18 template<direction direction, class Tensor>
20 
23  using iterator_category = std::random_access_iterator_tag;
24  using value_type = decltype(std::declval<Tensor>().slice(0));
25  using difference_type = int;
27 
30 
32  tensor(tensor_), index(index_) {}
33 
34 #define BC_ND_Iter_Compare(sign, rev) \
35  BCINLINE \
36  bool operator sign (const Iterator& iter) { \
37  if (direction == direction::forward) \
38  return index sign iter.index; \
39  else \
40  return index rev iter.index; \
41  } \
42  BCINLINE \
43  bool operator sign (int p_index) { \
44  if (direction == direction::forward) \
45  return index sign p_index; \
46  else \
47  return index rev p_index; \
48  }
53 #undef BC_ND_Iter_Compare
54 
55  BCINLINE operator bc::size_t () const { return index; }
56 
57  BCINLINE bool operator == (const Iterator& iter) {
58  return index == iter.index;
59  }
60  BCINLINE bool operator != (const Iterator& iter) {
61  return index != iter.index;
62  }
63 
64  BCINLINE Iterator& operator = (int index_) { this->index = index_; return *this; }
65 
66  BCINLINE Iterator& operator ++ () { return *this += direction; }
67  BCINLINE Iterator& operator -- () { return *this += direction; }
68 
69  BCINLINE Iterator operator ++(int) { return Iterator(tensor, index++); }
70  BCINLINE Iterator operator --(int) { return Iterator(tensor, index--); }
71 
72  BCINLINE Iterator& operator += (int dist) { index += dist*direction; return *this; }
73  BCINLINE Iterator& operator -= (int dist) { index -= dist*direction; return *this; }
74 
75  BCINLINE Iterator operator + (int dist) const { return Iterator(tensor, index+dist*direction); }
76  BCINLINE Iterator operator - (int dist) const { return Iterator(tensor, index-dist*direction); }
77 
78  BCINLINE Iterator& operator += (const Iterator& dist) { return *this += dist.index; }
79  BCINLINE Iterator& operator -= (const Iterator& dist) { return *this -= dist.index; }
80  BCINLINE Iterator operator + (const Iterator& dist) const { return Iterator(tensor, *this+dist.index); }
81  BCINLINE Iterator operator - (const Iterator& dist) const { return Iterator(tensor, *this-dist.index); }
82 
83  const value_type operator*() const { return this->tensor[this->index]; }
84  value_type operator*() { return this->tensor[this->index]; }
85 
86 };
87 
88 
89 template<class Tensor>
90 auto iter_begin(Tensor& derived) {
92 }
93 
94 template<class Tensor>
95 auto iter_end(Tensor& derived) {
96  return Multidimensional_Iterator<direction::forward, Tensor>(derived, derived.outer_dim());
97 }
98 
99 template<class Tensor>
100 auto iter_rbegin(Tensor& derived) {
101  return Multidimensional_Iterator<direction::reverse, Tensor>(derived, derived.outer_dim()-1);
102 }
103 
104 template<class Tensor>
105 auto iter_rend(Tensor& derived) {
107 }
108 
109 }
110 }
111 }
112 
113 #endif /* tensor_iterator_dim_H_ */
Multidimensional_Iterator< direction, Tensor > Iterator
Definition: multidimensional_iterator.h:22
value_type reference
Definition: multidimensional_iterator.h:26
#define BCINLINE
Definition: common.h:96
BCINLINE Iterator & operator-=(int dist)
Definition: multidimensional_iterator.h:73
BCINLINE Iterator & operator+=(int dist)
Definition: multidimensional_iterator.h:72
value_type operator*()
Definition: multidimensional_iterator.h:84
BCINLINE Iterator & operator++()
Definition: multidimensional_iterator.h:66
int difference_type
Definition: multidimensional_iterator.h:25
BCINLINE Iterator operator-(int dist) const
Definition: multidimensional_iterator.h:76
const value_type operator*() const
Definition: multidimensional_iterator.h:83
auto iter_rbegin(Tensor &derived)
Definition: multidimensional_iterator.h:100
BCINLINE Iterator & operator--()
Definition: multidimensional_iterator.h:67
BCINLINE Iterator operator+(int dist) const
Definition: multidimensional_iterator.h:75
Tensor & tensor
Definition: multidimensional_iterator.h:28
bc::size_t index
Definition: multidimensional_iterator.h:29
int size_t
Definition: common.h:283
auto iter_end(Tensor &derived)
Definition: multidimensional_iterator.h:95
auto iter_rend(Tensor &derived)
Definition: multidimensional_iterator.h:105
Definition: multidimensional_iterator.h:19
direction
Definition: common.h:18
auto iter_begin(Tensor &derived)
Definition: multidimensional_iterator.h:90
#define BC_ND_Iter_Compare(sign, rev)
Definition: multidimensional_iterator.h:34
BCINLINE bool operator==(const Iterator &iter)
Definition: multidimensional_iterator.h:57
BCINLINE Iterator & operator=(int index_)
Definition: multidimensional_iterator.h:64
std::random_access_iterator_tag iterator_category
Definition: multidimensional_iterator.h:23
BCINLINE Multidimensional_Iterator(Tensor &tensor_, bc::size_t index_=0)
Definition: multidimensional_iterator.h:31
BCINLINE bool operator!=(const Iterator &iter)
Definition: multidimensional_iterator.h:60
decltype(std::declval< Tensor >().slice(0)) value_type
Definition: multidimensional_iterator.h:24
The Evaluator determines if an expression needs to be greedily optimized.
Definition: algorithms.h:22