TiledArray  0.7.0
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 
23 #include <cstddef>
24 #include <TiledArray/madness.h>
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  private:
54  mutable T value_;
55  }; // class PointerProxy
56  } // namespace
57 
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 value_type;
79  typedef PointerProxy<value_type> pointer;
81  typedef std::input_iterator_tag iterator_category;
83  typedef Iter1 base_iterator1;
84  typedef Iter2 base_iterator2;
85 
87 
91  BinaryTransformIterator(Iter1 it1, Iter2 it2, Op op = Op()) :
92  it1_(it1), it2_(it2), op_(op)
93  { }
94 
96 
99  it1_(other.it1_), it2_(other.it2_), op_(other.op_)
100  { }
101 
103 
112  template <typename It1, typename It2>
114  it1_(other.it1_), it2_(other.it2_), op_(other.op_)
115  { }
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() { ++it1_; ++it2_; }
216 
218 
226  template <typename It1, typename It2>
227  bool equal(const BinaryTransformIterator<It1, It2, Op>& other) const {
228  return (it1_ == other.it1_) && (it2_ == other.it2_);
229  }
230 
232 
234  reference dereference() const { return op_(*it1_, *it2_); }
235 
236  base_iterator1 it1_;
237  base_iterator2 it2_;
238  Op op_;
239  }; // class BinaryTransformIterator
240 
241 
243 
249  template <typename Iter, typename Op>
251  private:
252  // Give access to other Transform iterator types.
253  template <typename, typename>
255 
256  public:
257  typedef ptrdiff_t difference_type;
258  typedef typename madness::detail::result_of<Op>::type value_type;
259  typedef PointerProxy<value_type> pointer;
261  typedef std::input_iterator_tag iterator_category;
263  typedef Iter base_iterator;
264 
266 
269  UnaryTransformIterator(Iter it, Op op = Op()) :
270  it_(it), op_(op)
271  { }
272 
274 
277  it_(other.it_), op_(other.op_)
278  { }
279 
281 
288  template <typename It>
290  it_(other.it_), op_(other.op_)
291  { }
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:
378 
380  void increment() { ++it_; }
381 
383 
389  template <typename It>
390  bool equal(const UnaryTransformIterator<It, Op>& other) const {
391  return it_ == other.it_;
392  }
393 
395 
397  template <typename It>
398  typename std::enable_if<! std::is_integral<It>::value, reference>::type
399  dereference(It it) const { return op_(*it); }
400 
401 
403 
405  template <typename It>
406  typename std::enable_if<std::is_integral<It>::value, reference>::type
407  dereference(It it) const { return op_(it); }
408 
409  base_iterator it_;
410  Op op_;
411  }; // class UnaryTransformIterator
412 
413 
415 
423  template <typename Iter1, typename Iter2, typename Op>
425  return BinaryTransformIterator<Iter1, Iter2, Op>(it1, it2, op);
426  }
427 
429 
435  template <typename Iter, typename Op>
437  return UnaryTransformIterator<Iter, Op>(it, op);
438  }
439 
440  } // namespace detail
441 } // namespace TiledArray
442 
443 #endif // TILEDARRAY_TRANSFORM_ITERATOR_H__INCLUDED
pointer operator->() const
Arrow dereference operator.
BinaryTransformIterator(const this_type &other)
Copy constructor.
this_type operator++(int)
Post-fix increment operator.
BinaryTransformIterator(const BinaryTransformIterator< It1, It2, Op > &other)
Copy conversion constructor.
UnaryTransformIterator< Iter, Op > this_type
This object type.
PointerProxy< value_type > pointer
Pointer type to iterator value.
UnaryTransformIterator(const this_type &other)
Copy constructor.
madness::detail::result_of< Op >::type value_type
Iterator dereference value type.
bool operator==(const UnaryTransformIterator< It, Op > &other) const
Equality operator.
this_type operator++(int)
Post-fix increment operator.
this_type & operator=(const UnaryTransformIterator< It, Op > &other)
Copy conversion operator.
BinaryTransformIterator(Iter1 it1, Iter2 it2, Op op=Op())
Constructor.
this_type & operator++()
Prefix increment operator.
UnaryTransformIterator(Iter it, Op op=Op())
Constructor.
bool operator!=(const BinaryTransformIterator< It1, It2, Op > &other) const
Inequality operator.
BinaryTransformIterator< Iter1, Iter2, Op > make_tran_it(Iter1 it1, Iter2 it2, Op op)
Binary Transform iterator factory.
pointer operator->()
Arrow dereference operator.
Iter1 base_iterator1
First base iterator type.
PointerProxy< value_type > pointer
Pointer type to iterator value.
std::input_iterator_tag iterator_category
Iterator category type.
Iter2 base_iterator2
Second base iterator type.
std::ptrdiff_t difference_type
Difference type.
this_type & operator=(const this_type &other)
Copy operator.
reference operator*()
Dereference operator.
Iter base_iterator
The base iterator type.
this_type & operator=(const this_type &other)
Copy operator.
bool operator!=(const UnaryTransformIterator< It, Op > &other) const
Inequality operator.
base_iterator2 base2() const
Second base iterator accessor.
bool operator==(const BinaryTransformIterator< It1, It2, Op > &other) const
Equality operator.
this_type & operator++()
Prefix increment operator.
value_type reference
Reference type to iterator value.
reference operator*() const
Dereference operator.
madness::detail::result_of< Op >::type value_type
Iterator dereference value type.
this_type & operator=(const BinaryTransformIterator< It1, It2, Op > &other)
Copy conversion operator.
base_iterator base() const
Base iterator accessor.
BinaryTransformIterator< Iter1, Iter2, Op > this_type
This object type.
value_type reference
Reference type to iterator value.
UnaryTransformIterator(const UnaryTransformIterator< It, Op > &other)
Copy conversion constructor.
base_iterator1 base1() const
First base iterator accessor.
std::input_iterator_tag iterator_category
Iterator category type.