Libosmium  2.17.0
Fast and flexible C++ library for working with OpenStreetMap data
Loading...
Searching...
No Matches
diff_iterator.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_DIFF_ITERATOR_HPP
2#define OSMIUM_DIFF_ITERATOR_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS IN THE SOFTWARE.
33
34*/
35
37
38#include <cassert>
39#include <cstddef>
40#include <iterator>
41#include <type_traits>
42#include <utility>
43
44namespace osmium {
45
46 class OSMObject;
47
56 template <typename TBasicIterator>
58
59
60 TBasicIterator m_prev;
61 TBasicIterator m_curr;
62 TBasicIterator m_next;
63
64 const TBasicIterator m_end;
65
67
68 void set_diff() const noexcept {
69 assert(m_curr != m_end);
70
71 const bool use_curr_for_prev = m_prev->type() != m_curr->type() || m_prev->id() != m_curr->id();
72 const bool use_curr_for_next = m_next == m_end || m_next->type() != m_curr->type() || m_next->id() != m_curr->id();
73
75 *(use_curr_for_prev ? m_curr : m_prev),
76 *m_curr,
77 *(use_curr_for_next ? m_curr : m_next)
78 };
79 }
80
81 public:
82
83 using iterator_category = std::input_iterator_tag;
85 using difference_type = std::ptrdiff_t;
88
89 DiffIterator(TBasicIterator begin, TBasicIterator end) :
90 m_prev(begin),
91 m_curr(begin),
92 m_next(begin == end ? begin : ++begin),
93 m_end(std::move(end)),
94 m_diff() {
95 }
96
98 m_prev = std::move(m_curr);
99 m_curr = m_next;
100
101 if (m_next != m_end) {
102 ++m_next;
103 }
104
105 return *this;
106 }
107
109 DiffIterator tmp{*this};
110 operator++();
111 return tmp;
112 }
113
114 bool operator==(const DiffIterator& rhs) const noexcept {
115 return m_curr == rhs.m_curr && m_end == rhs.m_end;
116 }
117
118 bool operator!=(const DiffIterator& rhs) const noexcept {
119 return !(*this == rhs);
120 }
121
122 reference operator*() const noexcept {
123 set_diff();
124 return m_diff;
125 }
126
127 pointer operator->() const noexcept {
128 set_diff();
129 return &m_diff;
130 }
131
132 }; // class DiffIterator
133
137 template <typename TBasicIterator>
139 TBasicIterator end) {
140 return DiffIterator<TBasicIterator>{begin, end};
141 }
142
143} // namespace osmium
144
145#endif // OSMIUM_DIFF_ITERATOR_HPP
Definition: diff_iterator.hpp:57
reference operator*() const noexcept
Definition: diff_iterator.hpp:122
std::ptrdiff_t difference_type
Definition: diff_iterator.hpp:85
osmium::DiffObject m_diff
Definition: diff_iterator.hpp:66
TBasicIterator m_curr
Definition: diff_iterator.hpp:61
bool operator==(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:114
DiffIterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:89
DiffIterator & operator++()
Definition: diff_iterator.hpp:97
void set_diff() const noexcept
Definition: diff_iterator.hpp:68
bool operator!=(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:118
TBasicIterator m_prev
Definition: diff_iterator.hpp:60
DiffIterator operator++(int)
Definition: diff_iterator.hpp:108
pointer operator->() const noexcept
Definition: diff_iterator.hpp:127
std::input_iterator_tag iterator_category
Definition: diff_iterator.hpp:83
TBasicIterator m_next
Definition: diff_iterator.hpp:62
const TBasicIterator m_end
Definition: diff_iterator.hpp:64
Definition: diff_object.hpp:66
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
DiffIterator< TBasicIterator > make_diff_iterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:138
Definition: location.hpp:551