range_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_RANGE_ITERATOR_H__INCLUDED
21 #define TILEDARRAY_RANGE_ITERATOR_H__INCLUDED
22 
23 #include <TiledArray/error.h>
24 #include <iterator>
25 #include <vector>
26 
27 namespace TiledArray {
28 namespace detail {
29 
30 template <typename, typename>
31 class RangeIterator;
32 
33 } // namespace detail
34 } // namespace TiledArray
35 
36 namespace std {
37 template <typename T, typename Container>
38 void advance(
41 
42 template <typename T, typename Container>
46 
47 } // namespace std
48 
49 namespace TiledArray {
50 namespace detail {
51 
53 
61 template <typename T, typename Container>
63  public:
65 
66  // Standard iterator typedefs
67  typedef typename Container::index_type value_type;
68  typedef const value_type& reference;
69  typedef const value_type* pointer;
70  typedef std::input_iterator_tag iterator_category;
71  typedef std::ptrdiff_t difference_type;
72 
74 
77  : container_(other.container_), current_(other.current_) {}
78 
80 
83  RangeIterator(const T* v, const Container* c)
84  : container_(c), current_(v, v + c->rank()) {}
85 
87 
91  current_ = other.current_;
92  container_ = other.container_;
93 
94  return *this;
95  }
96 
97  const Container* container() const { return container_; }
98 
100 
104  TA_ASSERT(container_->includes(current_));
105  return current_;
106  }
107 
109 
113  TA_ASSERT(container_->includes(current_));
114  container_->increment(current_);
115  return *this;
116  }
117 
119 
123  TA_ASSERT(container_->includes(current_));
124  RangeIterator_ temp(*this);
125  container_->increment(current_);
126  return temp;
127  }
128 
130 
132  pointer operator->() const {
133  TA_ASSERT(container_->includes(current_));
134  return &current_;
135  }
136 
138  TA_ASSERT(container_->includes(current_));
139  container_->advance(current_, n);
140  }
141 
143  TA_ASSERT(container_ == other.container_);
144  TA_ASSERT(container_->includes(current_));
145  return container_->distance_to(current_, other.current_);
146  }
147 
148  private:
149  const Container* container_;
150  typename Container::index_type
151  current_;
152 
153  template <typename U, typename C>
154  friend bool operator==(const RangeIterator<U, C>& left_it,
155  const RangeIterator<U, C>& right_it);
156 
160  reference value() const { return current_; }
161 
162 }; // class RangeIterator
163 
165 
174 template <typename T, typename Container>
176  const RangeIterator<T, Container>& right_it) {
177  return (left_it.value() == right_it.value()) &&
178  (left_it.container() == right_it.container());
179 }
180 
182 
190 template <typename T, typename Container>
192  const RangeIterator<T, Container>& right_it) {
193  // return (left_it.value() != right_it.value()) ||
194  // (left_it.container() != right_it.container());
195  return !(left_it == right_it);
196 }
197 
198 } // namespace detail
199 } // namespace TiledArray
200 
201 namespace std {
202 template <typename T, typename Container>
203 void advance(
206  n) {
207  it.advance(n);
208 }
209 
210 template <typename T, typename Container>
214  return first.distance_to(last);
215 }
216 
217 } // namespace std
218 
219 #endif // TILEDARRAY_RANGE_ITERATOR_H__INCLUDED
Coordinate index iterate.
const value_type & reference
Iterator reference type.
pointer operator->() const
Pointer operator.
std::input_iterator_tag iterator_category
TiledArray::detail::RangeIterator< T, Container >::difference_type distance(const TiledArray::detail::RangeIterator< T, Container > &, const TiledArray::detail::RangeIterator< T, Container > &)
const value_type * pointer
Iterator pointer type.
RangeIterator_ & operator=(const RangeIterator_ &other)
Copy constructor.
auto rank(const DistArray< Tile, Policy > &a)
Definition: dist_array.h:1617
void advance(difference_type n)
#define TA_ASSERT(EXPR,...)
Definition: error.h:39
difference_type distance_to(const RangeIterator_ &other) const
RangeIterator_ operator++(int)
Increment operator.
void advance(TiledArray::detail::RangeIterator< T, Container > &, typename TiledArray::detail::RangeIterator< T, Container >::difference_type)
const Container * container() const
bool operator==(const TileReference< Impl > &a, const TileReference< Impl > &b)
comparison operator for TileReference objects
Definition: array_impl.h:120
std::ptrdiff_t difference_type
Iterator category tag.
reference operator*() const
Dereference operator.
RangeIterator_ & operator++()
Increment operator.
RangeIterator< T, Container > RangeIterator_
This class type.
RangeIterator(const T *v, const Container *c)
Construct an index iterator.
Container::index_type value_type
Iterator value type.
RangeIterator(const RangeIterator_ &other)
Copy constructor.
friend bool operator==(const RangeIterator< U, C > &left_it, const RangeIterator< U, C > &right_it)
bool operator!=(const TileReference< Impl > &a, const TileReference< Impl > &b)
inequality operator for TileReference objects
Definition: array_impl.h:126