GEOS 3.14.0dev
CoordinateSequenceIterator.h
1/**********************************************************************
2 *
3 * GEOS - Geometry Engine Open Source
4 * http://geos.osgeo.org
5 *
6 * Copyright (C) 2022 ISciences, LLC
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU Lesser General Public Licence as published
10 * by the Free Software Foundation.
11 * See the COPYING file for more information.
12 *
13 **********************************************************************/
14
15
16#pragma once
17
18#include <cstddef>
19#include <iterator>
20
21namespace geos {
22namespace geom {
23
24template<typename SequenceType, typename CoordinateType>
25class CoordinateSequenceIterator {
26
27public:
28 using iterator_category = std::random_access_iterator_tag;
29 using value_type = CoordinateType;
30 using reference = CoordinateType&;
31 using pointer = CoordinateType;
32 using difference_type = std::ptrdiff_t;
33
34private:
35 SequenceType* m_seq;
36 difference_type m_pos;
37
38public:
39 CoordinateSequenceIterator(SequenceType* seq) : m_seq(seq), m_pos(0) {}
40
41 CoordinateSequenceIterator(SequenceType* seq, std::size_t size) : m_seq(seq), m_pos(static_cast<difference_type>(size)) {}
42
43 reference operator*() const {
44 return m_seq->template getAt<value_type>(static_cast<std::size_t>(m_pos));
45 }
46
47 pointer operator->() const {
48 return &m_seq->template getAt<value_type>(static_cast<std::size_t>(m_pos));
49 }
50
51 CoordinateSequenceIterator& operator++() {
52 m_pos++;
53 return *this;
54 }
55
56 CoordinateSequenceIterator operator++(int) {
57 CoordinateSequenceIterator ret = *this;
58 m_pos++;
59 return ret;
60 }
61
62 CoordinateSequenceIterator& operator--() {
63 m_pos--;
64 return *this;
65 }
66
67 CoordinateSequenceIterator operator--(int) {
68 CoordinateSequenceIterator ret = *this;
69 m_pos--;
70 return ret;
71 }
72
73 difference_type operator-(const CoordinateSequenceIterator& other) const {
74 return this->m_pos - other.m_pos;
75 }
76
77 CoordinateSequenceIterator operator+(difference_type n) const {
78 return CoordinateSequenceIterator(m_seq, static_cast<std::size_t>(m_pos + n));
79 }
80
81 CoordinateSequenceIterator operator+=(difference_type n) {
82 this->m_pos += n;
83 return *this;
84 }
85
86 CoordinateSequenceIterator operator-(difference_type n) const {
87 return CoordinateSequenceIterator(m_seq, static_cast<std::size_t>(m_pos - n));
88 }
89
90 CoordinateSequenceIterator operator-=(difference_type n) {
91 this->m_pos -= n;
92 return *this;
93 }
94
95 CoordinateType& operator[](difference_type n) const {
96 return *(*this + n);
97 }
98
99 bool operator==(const CoordinateSequenceIterator& other) const {
100 return this->m_pos == other.m_pos;
101 }
102
103 bool operator!=(const CoordinateSequenceIterator& other) const {
104 return !(*this == other);
105 }
106
107 bool operator<(const CoordinateSequenceIterator& other) const {
108 return this->m_pos < other.m_pos;
109 }
110
111 bool operator<=(const CoordinateSequenceIterator& other) const {
112 return this->m_pos <= other.m_pos;
113 }
114
115 bool operator>(const CoordinateSequenceIterator& other) const {
116 return this->m_pos > other.m_pos;
117 }
118
119 bool operator>=(const CoordinateSequenceIterator& other) const {
120 return this->m_pos >= other.m_pos;
121 }
122
123};
124
125}
126}
bool operator<(const CoordinateXY &a, const CoordinateXY &b)
Strict weak ordering operator for Coordinate.
Definition Coordinate.h:458
Basic namespace for all GEOS functionalities.
Definition geos.h:39