TiledArray  0.7.0
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>
32 
33  } // namespace detail
34 } // namespace TiledArray
35 
36 namespace std {
37  template <typename T, typename Container>
40 
41  template <typename T, typename Container>
45 
46 } // namespace std
47 
48 namespace TiledArray {
49  namespace detail {
50 
52 
60  template <typename T, typename Container>
61  class RangeIterator {
62  public:
64 
65  // Standard iterator typedefs
66  typedef std::vector<T> value_type;
67  typedef const value_type& reference;
68  typedef const value_type* pointer;
69  typedef std::input_iterator_tag iterator_category;
70  typedef std::ptrdiff_t difference_type;
71 
73 
75  RangeIterator(const RangeIterator_& other) :
76  container_(other.container_), current_(other.current_) {
77  }
78 
80 
83  RangeIterator(const T* v, const Container* c) :
84  container_(c), current_(v, v + c->rank())
85  { }
86 
88 
92  current_ = other.current_;
93  container_ = other.container_;
94 
95  return *this;
96  }
97 
98  const Container* container() const { return container_; }
99 
101 
103  reference operator*() const { return current_; }
104 
106 
110  container_->increment(current_);
111  return *this;
112  }
113 
115 
119  RangeIterator_ temp(*this);
120  container_->increment(current_);
121  return temp;
122  }
123 
125 
127  pointer operator->() const { return & current_; }
128 
129 
131  container_->advance(current_, n);
132  }
133 
135  TA_ASSERT(container_ == other.container_);
136  return container_->distance_to(current_, other.current_);
137  }
138 
139  private:
140 
141  const Container* container_;
142  std::vector<T> current_;
143  }; // class RangeIterator
144 
146 
155  template <typename T, typename Container>
157  const RangeIterator<T, Container>& right_it)
158  {
159  return ((*left_it) == (*right_it)) &&
160  (left_it.container() == right_it.container());
161  }
162 
164 
172  template <typename T, typename Container>
174  const RangeIterator<T, Container>& right_it)
175  {
176  return ((*left_it) != (*right_it)) ||
177  (left_it.container() != right_it.container());
178  }
179 
180  } // namespace detail
181 } // namespace TiledArray
182 
183 namespace std {
184  template <typename T, typename Container>
187  {
188  it.advance(n);
189  }
190 
191  template <typename T, typename Container>
195  {
196  return first.distance_to(last);
197  }
198 
199 } // namespace std
200 
201 #endif // TILEDARRAY_RANGE_ITERATOR_H__INCLUDED
difference_type distance_to(const RangeIterator_ &other) const
bool operator!=(const TileReference< Impl > &a, const TileReference< Impl > &b)
inequality operator for TileReference objects
Definition: array_impl.h:129
pointer operator->() const
Pointer operator.
RangeIterator(const T *v, const Container *c)
Construct an index iterator.
const value_type & reference
Iterator reference type.
RangeIterator_ & operator=(const RangeIterator_ &other)
Copy constructor.
STL namespace.
TiledArray::detail::RangeIterator< T, Container >::difference_type distance(const TiledArray::detail::RangeIterator< T, Container > &, const TiledArray::detail::RangeIterator< T, Container > &)
std::ptrdiff_t difference_type
Iterator category tag.
void advance(difference_type n)
void advance(TiledArray::detail::RangeIterator< T, Container > &, typename TiledArray::detail::RangeIterator< T, Container >::difference_type)
RangeIterator< T, Container > RangeIterator_
This class type.
reference operator*() const
Dereference operator.
#define TA_ASSERT(a)
Definition: error.h:107
const Container * container() const
RangeIterator(const RangeIterator_ &other)
Copy constructor.
std::vector< T > value_type
Iterator value type.
bool operator==(const TileReference< Impl > &a, const TileReference< Impl > &b)
comparison operator for TileReference objects
Definition: array_impl.h:122
RangeIterator_ operator++(int)
Increment operator.
std::input_iterator_tag iterator_category
RangeIterator_ & operator++()
Increment operator.
const value_type * pointer
Iterator pointer type.
Coordinate index iterate.