| 1 |
/* |
| 2 |
* (C) Copyright 2013 ECMWF. |
| 3 |
* |
| 4 |
* This software is licensed under the terms of the Apache Licence Version 2.0 |
| 5 |
* which can be obtained at http://www.apache.org/licenses/LICENSE-2.0. |
| 6 |
* In applying this licence, ECMWF does not waive the privileges and immunities |
| 7 |
* granted to it by virtue of its status as an intergovernmental organisation |
| 8 |
* nor does it submit to any jurisdiction. |
| 9 |
*/ |
| 10 |
|
| 11 |
#pragma once |
| 12 |
|
| 13 |
#include <iterator> |
| 14 |
#include <memory> |
| 15 |
|
| 16 |
#include "atlas/grid/detail/grid/Grid.h" |
| 17 |
#include "atlas/util/Point.h" |
| 18 |
|
| 19 |
//--------------------------------------------------------------------------------------------------------------------- |
| 20 |
|
| 21 |
namespace atlas { |
| 22 |
namespace grid { |
| 23 |
|
| 24 |
//--------------------------------------------------------------------------------------------------------------------- |
| 25 |
|
| 26 |
class IteratorXY { |
| 27 |
using implementation_t = detail::grid::Grid::IteratorXY; |
| 28 |
|
| 29 |
public: |
| 30 |
using difference_type = implementation_t::difference_type; |
| 31 |
using iterator_category = implementation_t::iterator_category; |
| 32 |
using value_type = implementation_t::value_type; |
| 33 |
using pointer = implementation_t::pointer; |
| 34 |
using reference = implementation_t::reference; |
| 35 |
|
| 36 |
public: |
| 37 |
IteratorXY(std::unique_ptr<implementation_t> iterator): iterator_(std::move(iterator)) {} |
| 38 |
|
| 39 |
IteratorXY(const IteratorXY& iterator): iterator_(iterator.iterator_->clone()) {} |
| 40 |
|
| 41 |
bool next(value_type& xy) { return iterator_->next(xy); } |
| 42 |
|
| 43 |
reference operator*() const { return iterator_->operator*(); } |
| 44 |
|
| 45 |
const IteratorXY& operator++() { |
| 46 |
iterator_->operator++(); |
| 47 |
return *this; |
| 48 |
} |
| 49 |
|
| 50 |
const IteratorXY& operator+=(difference_type distance) { |
| 51 |
iterator_->operator+=(distance); |
| 52 |
return *this; |
| 53 |
} |
| 54 |
|
| 55 |
friend IteratorXY operator+(const IteratorXY& a, difference_type distance) { |
| 56 |
IteratorXY result(a); |
| 57 |
result += distance; |
| 58 |
return result; |
| 59 |
} |
| 60 |
|
| 61 |
friend difference_type operator-(const IteratorXY& last, const IteratorXY& first) { |
| 62 |
return first.iterator_->distance(*last.iterator_); |
| 63 |
} |
| 64 |
|
| 65 |
bool operator==(const IteratorXY& other) const { return iterator_->operator==(*other.iterator_); } |
| 66 |
bool operator!=(const IteratorXY& other) const { return iterator_->operator!=(*other.iterator_); } |
| 67 |
|
| 68 |
private: |
| 69 |
IteratorXY::difference_type distance(const IteratorXY& other) const { |
| 70 |
return iterator_->distance(*other.iterator_); |
| 71 |
} |
| 72 |
|
| 73 |
private: |
| 74 |
std::unique_ptr<implementation_t> iterator_; |
| 75 |
}; |
| 76 |
|
| 77 |
|
| 78 |
//--------------------------------------------------------------------------------------------------------------------- |
| 79 |
|
| 80 |
class IteratorLonLat { |
| 81 |
using implementation_t = detail::grid::Grid::IteratorLonLat; |
| 82 |
|
| 83 |
public: |
| 84 |
using difference_type = implementation_t::difference_type; |
| 85 |
using iterator_category = implementation_t::iterator_category; |
| 86 |
using value_type = implementation_t::value_type; |
| 87 |
using pointer = implementation_t::pointer; |
| 88 |
using reference = implementation_t::reference; |
| 89 |
|
| 90 |
public: |
| 91 |
IteratorLonLat(std::unique_ptr<implementation_t> iterator): iterator_(std::move(iterator)) {} |
| 92 |
|
| 93 |
IteratorLonLat(const IteratorLonLat& iterator): iterator_(iterator.iterator_->clone()) {} |
| 94 |
|
| 95 |
bool next(value_type& xy) { return iterator_->next(xy); } |
| 96 |
|
| 97 |
reference operator*() const { return iterator_->operator*(); } |
| 98 |
|
| 99 |
const IteratorLonLat& operator++() { |
| 100 |
iterator_->operator++(); |
| 101 |
return *this; |
| 102 |
} |
| 103 |
|
| 104 |
const IteratorLonLat& operator+=(difference_type distance) { |
| 105 |
iterator_->operator+=(distance); |
| 106 |
return *this; |
| 107 |
} |
| 108 |
|
| 109 |
friend IteratorLonLat operator+(const IteratorLonLat& a, difference_type distance) { |
| 110 |
IteratorLonLat result(a); |
| 111 |
result += distance; |
| 112 |
return result; |
| 113 |
} |
| 114 |
|
| 115 |
friend difference_type operator-(const IteratorLonLat& last, const IteratorLonLat& first) { |
| 116 |
return first.iterator_->distance(*last.iterator_); |
| 117 |
} |
| 118 |
|
| 119 |
bool operator==(const IteratorLonLat& other) const { return iterator_->operator==(*other.iterator_); } |
| 120 |
bool operator!=(const IteratorLonLat& other) const { return iterator_->operator!=(*other.iterator_); } |
| 121 |
|
| 122 |
private: |
| 123 |
difference_type distance(const IteratorLonLat& other) const { return iterator_->distance(*other.iterator_); } |
| 124 |
|
| 125 |
private: |
| 126 |
std::unique_ptr<implementation_t> iterator_; |
| 127 |
}; |
| 128 |
|
| 129 |
//--------------------------------------------------------------------------------------------------------------------- |
| 130 |
|
| 131 |
class IterateXY { |
| 132 |
public: |
| 133 |
using iterator = grid::IteratorXY; |
| 134 |
using const_iterator = iterator; |
| 135 |
using Grid = detail::grid::Grid; |
| 136 |
|
| 137 |
public: |
| 138 |
IterateXY(const Grid& grid): grid_(grid) {} |
| 139 |
iterator begin() const; |
| 140 |
iterator end() const; |
| 141 |
void access(size_t i, PointXY&); |
| 142 |
PointXY front() { return *begin(); } |
| 143 |
PointXY back() { return *(begin() + (grid_.size() - 1)); } |
| 144 |
|
| 145 |
private: |
| 146 |
const Grid& grid_; |
| 147 |
}; |
| 148 |
|
| 149 |
class IterateLonLat { |
| 150 |
public: |
| 151 |
using iterator = IteratorLonLat; |
| 152 |
using const_iterator = iterator; |
| 153 |
using Grid = detail::grid::Grid; |
| 154 |
|
| 155 |
public: |
| 156 |
IterateLonLat(const Grid& grid): grid_(grid) {} |
| 157 |
iterator begin() const; |
| 158 |
iterator end() const; |
| 159 |
PointLonLat front() { return *begin(); } |
| 160 |
PointLonLat back() { return *(begin() + (grid_.size() - 1)); } |
| 161 |
|
| 162 |
private: |
| 163 |
const Grid& grid_; |
| 164 |
}; |
| 165 |
|
| 166 |
} // namespace grid |
| 167 |
} // namespace atlas |
| 168 |
|
| 169 |
#ifndef DOXYGEN_SHOULD_SKIP_THIS |
| 170 |
namespace std { |
| 171 |
|
| 172 |
inline atlas::grid::IteratorXY::difference_type distance(const atlas::grid::IteratorXY& first, |
| 173 |
const atlas::grid::IteratorXY& last) { |
| 174 |
return last - first; |
| 175 |
} |
| 176 |
inline atlas::grid::IteratorLonLat::difference_type distance(const atlas::grid::IteratorLonLat& first, |
| 177 |
const atlas::grid::IteratorLonLat& last) { |
| 178 |
return last - first; |
| 179 |
} |
| 180 |
|
| 181 |
} // namespace std |
| 182 |
#endif |