Libosmium  2.20.0
Fast and flexible C++ library for working with OpenStreetMap data
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-2023 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) :
92 m_next(begin == end ? begin : ++begin),
93 m_end(std::move(end)) {
94 }
95
97 m_prev = std::move(m_curr);
98 m_curr = m_next;
99
100 if (m_next != m_end) {
101 ++m_next;
102 }
103
104 return *this;
105 }
106
108 DiffIterator tmp{*this};
109 operator++();
110 return tmp;
111 }
112
113 bool operator==(const DiffIterator& rhs) const noexcept {
114 return m_curr == rhs.m_curr && m_end == rhs.m_end;
115 }
116
117 bool operator!=(const DiffIterator& rhs) const noexcept {
118 return !(*this == rhs);
119 }
120
121 reference operator*() const noexcept {
122 set_diff();
123 return m_diff;
124 }
125
126 pointer operator->() const noexcept {
127 set_diff();
128 return &m_diff;
129 }
130
131 }; // class DiffIterator
132
136 template <typename TBasicIterator>
138 TBasicIterator end) {
140 }
141
142} // namespace osmium
143
144#endif // OSMIUM_DIFF_ITERATOR_HPP
Definition: diff_iterator.hpp:57
reference operator*() const noexcept
Definition: diff_iterator.hpp:121
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:113
DiffIterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:89
DiffIterator & operator++()
Definition: diff_iterator.hpp:96
void set_diff() const noexcept
Definition: diff_iterator.hpp:68
bool operator!=(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:117
TBasicIterator m_prev
Definition: diff_iterator.hpp:60
DiffIterator operator++(int)
Definition: diff_iterator.hpp:107
pointer operator->() const noexcept
Definition: diff_iterator.hpp:126
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
InputIterator< Reader > end(Reader &)
Definition: reader_iterator.hpp:47
InputIterator< Reader > begin(Reader &reader)
Definition: reader_iterator.hpp:43
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
DiffIterator< TBasicIterator > make_diff_iterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:137
Definition: location.hpp:555