vmmlib  1.7.0
 All Classes Namespaces Functions Pages
tensor3_iterator.hpp
1 /*
2  * Copyright (c) 2006-2014, Visualization and Multimedia Lab,
3  * University of Zurich <http://vmml.ifi.uzh.ch>,
4  * Eyescale Software GmbH,
5  * Blue Brain Project, EPFL
6  *
7  * This file is part of VMMLib <https://github.com/VMML/vmmlib/>
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions are met:
11  *
12  * Redistributions of source code must retain the above copyright notice, this
13  * list of conditions and the following disclaimer. Redistributions in binary
14  * form must reproduce the above copyright notice, this list of conditions and
15  * the following disclaimer in the documentation and/or other materials provided
16  * with the distribution. Neither the name of the Visualization and Multimedia
17  * Lab, University of Zurich nor the names of its contributors may be used to
18  * endorse or promote products derived from this software without specific prior
19  * written permission.
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
24  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
25  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
26  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
27  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
28  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
29  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
30  * POSSIBILITY OF SUCH DAMAGE.
31  */
32 
33 #ifndef __VMML__TENSOR3_ITERATOR__HPP__
34 #define __VMML__TENSOR3_ITERATOR__HPP__
35 
36 #include <vmmlib/matrix.hpp>
37 
38 namespace vmml
39 {
40 
41 
42 template< typename T >
44 {
45 public:
46 
47  typedef typename T::value_type value_type;
48  typedef typename T::pointer pointer;
49  typedef typename T::reference reference;
50  typedef typename std::forward_iterator_tag iterator_category;
51  typedef size_t difference_type;
52 
53  typedef typename matrix< T::ROWS, T::COLS, typename T::value_type >::iterator matrix_iterator;
54 
55  typedef typename T::front_slice_type slice_type;
56 
57  tensor3_iterator() : _tensor3( 0 ), _matrix_index( 0 ) {};
58 
59  tensor3_iterator( T& t_, bool begin_ ) : _tensor3( &t_ ), _matrix_index( 0 )
60  {
61  if ( begin_ )
62  {
63  _matrix_index = 0;
64  slice_type& slice_ = _tensor3->get_frontal_slice_fwd( _matrix_index );
65 
66  _matrix_it = slice_.begin();
67  _matrix_it_end = slice_.end();
68  }
69  else
70  {
71  _matrix_index = T::SLICES - 1;
72  slice_type& slice_ = _tensor3->get_frontal_slice_fwd( _matrix_index );
73 
74  _matrix_it = slice_.end();
75  _matrix_it_end = slice_.begin();
76  }
77  }
78 
79  value_type& operator*()
80  {
81  return *_matrix_it;
82  }
83 
84  const value_type& operator*() const
85  {
86  return *_matrix_it;
87  }
88 
89  bool operator==( const tensor3_iterator& it_ ) const
90  {
91  return it_._tensor3 == _tensor3
92  // && it_._matrix_index == _matrix_index
93  && it_._matrix_it == _matrix_it;
94 
95  }
96 
97  bool operator!=( const tensor3_iterator& it_ ) const
98  {
99  return ! operator==(it_);
100  }
101 
102 
103  void operator++()
104  {
105  if ( _tensor3 == 0 )
106  {
107  VMMLIB_ERROR( "attempt to increase singular iterator", VMMLIB_HERE );
108  }
109 
110  if ( _matrix_it != _matrix_it_end )
111  ++_matrix_it;
112 
113  if ( _matrix_it == _matrix_it_end && _matrix_index + 1 < T::SLICES )
114  {
115  ++_matrix_index;
116  //slice_type& slice_ = _tensor3->get_frontal_slice( _matrix_index );
117 
118  _matrix_it = _tensor3->get_frontal_slice_fwd( _matrix_index ).begin();
119  _matrix_it_end = _tensor3->get_frontal_slice_fwd( _matrix_index ).end();
120  }
121  }
122 
123 
124 protected:
125  matrix_iterator _matrix_it;
126  matrix_iterator _matrix_it_end;
127  T* _tensor3;
128  size_t _matrix_index;
129 
130 }; //end tensor3_iterator class
131 
132 
133 
134 
135 template< typename T >
137 {
138 public:
139 
140  typedef typename T::value_type value_type;
141  typedef typename T::pointer pointer;
142  typedef typename T::reference reference;
143  typedef typename std::forward_iterator_tag iterator_category;
144  typedef size_t difference_type;
145 
147 
148  typedef typename T::front_slice_type slice_type;
149 
150  tensor3_const_iterator() : _tensor3( 0 ), _matrix_index( 0 ) {};
151 
152  tensor3_const_iterator( const T& t_, bool begin_ ) : _tensor3( &t_ ), _matrix_index( 0 )
153  {
154  if ( begin_ )
155  {
156  _matrix_index = 0;
157  const slice_type& slice_ = _tensor3->get_frontal_slice_fwd( _matrix_index );
158 
159  _matrix_it = slice_.begin();
160  _matrix_it_end = slice_.end();
161  }
162  else
163  {
164  _matrix_index = T::SLICES - 1;
165  const slice_type& slice_ = _tensor3->get_frontal_slice_fwd( _matrix_index );
166 
167  _matrix_it = slice_.end();
168  _matrix_it_end = slice_.begin();
169  }
170  }
171 
172  const value_type& operator*() const
173  {
174  return *_matrix_it;
175  }
176 
177  bool operator==( const tensor3_const_iterator& it_ ) const
178  {
179  return it_._matrix_it == _matrix_it;
180 
181  }
182 
183  bool operator!=( const tensor3_const_iterator& it_ ) const
184  {
185  return it_._matrix_it != _matrix_it;
186  }
187 
188 
189  void operator++()
190  {
191  if ( _tensor3 == 0 )
192  {
193  VMMLIB_ERROR( "attempt to increase singular iterator", VMMLIB_HERE );
194  }
195 
196  if ( _matrix_it != _matrix_it_end )
197  ++_matrix_it;
198 
199  if ( _matrix_it == _matrix_it_end && _matrix_index + 1 < T::SLICES )
200  {
201  ++_matrix_index;
202  //const slice_type& slice_ = _tensor3->get_frontal_slice( _matrix_index );
203 
204  _matrix_it = _tensor3->get_frontal_slice_fwd( _matrix_index ).begin();
205  _matrix_it_end = _tensor3->get_frontal_slice_fwd( _matrix_index ).end();
206  }
207  }
208 
209 
210 protected:
211  matrix_iterator _matrix_it;
212  matrix_iterator _matrix_it_end;
213  const T* _tensor3;
214  size_t _matrix_index;
215 
216 }; //end tensor3_const_iterator class
217 
218 
219 
220 
221 
222 }// end vmml namespace
223 
224 #endif
225