transform_iterator.h
Go to the documentation of this file.
1 /*
2  * This file is a part of TiledArray.
3  * Copyright (C) 2013 Virginia Tech
4  *
5  * This program is free software: you can redistribute it and/or modify
6  * it under the terms of the GNU General Public License as published by
7  * the Free Software Foundation, either version 3 of the License, or
8  * (at your option) any later version.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program. If not, see <http://www.gnu.org/licenses/>.
17  *
18  */
19 
20 #ifndef TILEDARRAY_TRANSFORM_ITERATOR_H__INCLUDED
21 #define TILEDARRAY_TRANSFORM_ITERATOR_H__INCLUDED
22 
24 #include <cstddef>
25 
26 namespace TiledArray {
27 namespace detail {
28 
29 namespace {
30 
32 
38 template <typename T>
39 class PointerProxy {
40  public:
42 
44  PointerProxy(const T& value) : value_(value) {}
45 
47 
49  T* operator->() const { return &value_; }
50 
52  operator T*() const { return &value_; }
53 
54  private:
55  mutable T value_;
56 }; // class PointerProxy
57 } // namespace
58 
60 
69 template <typename Iter1, typename Iter2, typename Op>
71  private:
72  // Give access to other Transform iterator types.
73  template <typename, typename, typename>
75 
76  public:
77  typedef std::ptrdiff_t difference_type;
78  typedef typename madness::detail::result_of<Op>::type
80  typedef PointerProxy<value_type> pointer;
82  typedef std::input_iterator_tag
86  typedef Iter1 base_iterator1;
87  typedef Iter2 base_iterator2;
88 
90 
94  BinaryTransformIterator(Iter1 it1, Iter2 it2, Op op = Op())
95  : it1_(it1), it2_(it2), op_(op) {}
96 
98 
101  : it1_(other.it1_), it2_(other.it2_), op_(other.op_) {}
102 
104 
113  template <typename It1, typename It2>
115  : it1_(other.it1_), it2_(other.it2_), op_(other.op_) {}
116 
118 
121  this_type& operator=(const this_type& other) {
122  it1_ = other.it1_;
123  it2_ = other.it2_;
124  op_ = other.op_;
125 
126  return *this;
127  }
128 
130 
139  template <typename It1, typename It2>
141  it1_ = other.it1_;
142  it2_ = other.it2_;
143  op_ = other.op_;
144 
145  return *this;
146  }
147 
149 
152  increment();
153  return *this;
154  }
155 
157 
160  this_type tmp(*this);
161  increment();
162  return tmp;
163  }
164 
166 
174  template <typename It1, typename It2>
176  return equal(other);
177  }
178 
180 
188  template <typename It1, typename It2>
190  return !equal(other);
191  }
192 
194 
196  reference operator*() { return dereference(); }
197 
199 
201  pointer operator->() { return pointer(dereference()); }
202 
204 
206  base_iterator1 base1() const { return it1_; }
207 
209 
211  base_iterator2 base2() const { return it2_; }
212 
213  private:
215  void increment() {
216  ++it1_;
217  ++it2_;
218  }
219 
221 
229  template <typename It1, typename It2>
230  bool equal(const BinaryTransformIterator<It1, It2, Op>& other) const {
231  return (it1_ == other.it1_) && (it2_ == other.it2_);
232  }
233 
235 
237  reference dereference() const { return op_(*it1_, *it2_); }
238 
239  base_iterator1 it1_;
240  base_iterator2 it2_;
241  Op op_;
242 }; // class BinaryTransformIterator
243 
245 
251 template <typename Iter, typename Op>
253  private:
254  // Give access to other Transform iterator types.
255  template <typename, typename>
257 
258  public:
259  typedef ptrdiff_t difference_type;
260  typedef typename madness::detail::result_of<Op>::type
262  typedef PointerProxy<value_type> pointer;
264  typedef std::input_iterator_tag
267  typedef Iter base_iterator;
268 
270 
273  UnaryTransformIterator(Iter it, Op op = Op()) : it_(it), op_(op) {}
274 
276 
279  : it_(other.it_), op_(other.op_) {}
280 
282 
289  template <typename It>
291  : it_(other.it_), op_(other.op_) {}
292 
294 
297  this_type& operator=(const this_type& other) {
298  it_ = other.it_;
299  op_ = other.op_;
300 
301  return *this;
302  }
303 
305 
312  template <typename It>
314  it_ = other.it_;
315  op_ = other.op_;
316 
317  return *this;
318  }
319 
321 
324  increment();
325  return *this;
326  }
327 
329 
332  this_type tmp(*this);
333  increment();
334  return tmp;
335  }
336 
338 
344  template <typename It>
345  bool operator==(const UnaryTransformIterator<It, Op>& other) const {
346  return equal(other);
347  }
348 
350 
356  template <typename It>
357  bool operator!=(const UnaryTransformIterator<It, Op>& other) const {
358  return !equal(other);
359  }
360 
362 
364  reference operator*() const { return dereference(it_); }
365 
367 
370  pointer operator->() const { return pointer(dereference(it_)); }
371 
373 
375  base_iterator base() const { return it_; }
376 
377  private:
379  void increment() { ++it_; }
380 
382 
388  template <typename It>
389  bool equal(const UnaryTransformIterator<It, Op>& other) const {
390  return it_ == other.it_;
391  }
392 
394 
396  template <typename It>
397  std::enable_if_t<!std::is_integral_v<It>, reference> dereference(
398  It it) const {
399  return op_(*it);
400  }
401 
403 
405  template <typename It>
406  std::enable_if_t<std::is_integral_v<It>, reference> dereference(It it) const {
407  return op_(it);
408  }
409 
410  base_iterator it_;
411  Op op_;
412 }; // class UnaryTransformIterator
413 
415 
423 template <typename Iter1, typename Iter2, typename Op>
425  Op op) {
426  return BinaryTransformIterator<Iter1, Iter2, Op>(it1, it2, op);
427 }
428 
430 
436 template <typename Iter, typename Op>
438  return UnaryTransformIterator<Iter, Op>(it, op);
439 }
440 
441 } // namespace detail
442 } // namespace TiledArray
443 
444 #endif // TILEDARRAY_TRANSFORM_ITERATOR_H__INCLUDED
reference operator*() const
Dereference operator.
::blas::Op Op
Definition: blas.h:46
bool operator==(const BinaryTransformIterator< It1, It2, Op > &other) const
Equality operator.
this_type & operator=(const BinaryTransformIterator< It1, It2, Op > &other)
Copy conversion operator.
UnaryTransformIterator(const UnaryTransformIterator< It, Op > &other)
Copy conversion constructor.
base_iterator1 base1() const
First base iterator accessor.
PointerProxy< value_type > pointer
Pointer type to iterator value.
UnaryTransformIterator< Iter, Op > this_type
This object type.
value_type reference
Reference type to iterator value.
UnaryTransformIterator(const this_type &other)
Copy constructor.
PointerProxy< value_type > pointer
Pointer type to iterator value.
value_type reference
Reference type to iterator value.
this_type operator++(int)
Post-fix increment operator.
pointer operator->() const
Arrow dereference operator.
madness::detail::result_of< Op >::type value_type
Iterator dereference value type.
BinaryTransformIterator< Iter1, Iter2, Op > this_type
This object type.
bool operator!=(const BinaryTransformIterator< It1, It2, Op > &other) const
Inequality operator.
pointer operator->()
Arrow dereference operator.
this_type & operator=(const this_type &other)
Copy operator.
base_iterator base() const
Base iterator accessor.
BinaryTransformIterator< Iter1, Iter2, Op > make_tran_it(Iter1 it1, Iter2 it2, Op op)
Binary Transform iterator factory.
this_type & operator=(const UnaryTransformIterator< It, Op > &other)
Copy conversion operator.
BinaryTransformIterator(const this_type &other)
Copy constructor.
Iter1 base_iterator1
First base iterator type.
UnaryTransformIterator(Iter it, Op op=Op())
Constructor.
this_type & operator=(const this_type &other)
Copy operator.
this_type & operator++()
Prefix increment operator.
bool operator==(const UnaryTransformIterator< It, Op > &other) const
Equality operator.
reference operator*()
Dereference operator.
madness::detail::result_of< Op >::type value_type
Iterator dereference value type.
std::input_iterator_tag iterator_category
Iterator category type.
BinaryTransformIterator(const BinaryTransformIterator< It1, It2, Op > &other)
Copy conversion constructor.
Iter2 base_iterator2
Second base iterator type.
std::ptrdiff_t difference_type
Difference type.
bool operator!=(const UnaryTransformIterator< It, Op > &other) const
Inequality operator.
BinaryTransformIterator(Iter1 it1, Iter2 it2, Op op=Op())
Constructor.
this_type operator++(int)
Post-fix increment operator.
std::input_iterator_tag iterator_category
Iterator category type.
base_iterator2 base2() const
Second base iterator accessor.
this_type & operator++()
Prefix increment operator.