Libosmium 2.22.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
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 Jochen Topf <jochen@topf.org> and others (see README).
9
10 Boost Software License - Version 1.0 - August 17th, 2003
11
12 Permission is hereby granted, free of charge, to any person or organization
13 obtaining a copy of the software and accompanying documentation covered by
14 this license (the "Software") to use, reproduce, display, distribute,
15 execute, and transmit the Software, and to prepare derivative works of the
16 Software, and to permit third-parties to whom the Software is furnished to
17 do so, all subject to the following:
18
19 The copyright notices in the Software and this entire statement, including
20 the above license grant, this restriction and the following disclaimer,
21 must be included in all copies of the Software, in whole or in part, and
22 all derivative works of the Software, unless such copies or derivative
23 works are solely in the form of machine-executable object code generated by
24 a source language processor.
25
26 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28 FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29 SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30 FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32 DEALINGS IN THE SOFTWARE.
33
34*/
35
36#include <osmium/osm/diff_object.hpp>
37
38#include <cassert>
39#include <cstddef>
40#include <iterator>
41#include <type_traits>
42#include <utility>
43
44 namespace osmium {
45
46 class OSMObject;
47
56 template <typename TBasicIterator>
57 class DiffIterator {
58
59
60 TBasicIterator m_prev;
61 TBasicIterator m_curr;
62 TBasicIterator m_next;
63
64 TBasicIterator m_end;
65
66 mutable osmium::DiffObject m_diff;
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
74 m_diff = osmium::DiffObject{
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;
84 using value_type = const osmium::DiffObject;
85 using difference_type = std::ptrdiff_t;
86 using pointer = value_type*;
87 using reference = value_type&;
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 }
95
96 DiffIterator& operator++() {
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
107 DiffIterator operator++(int) {
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>
137 inline DiffIterator<TBasicIterator> make_diff_iterator(TBasicIterator begin,
138 TBasicIterator end) {
139 return DiffIterator<TBasicIterator>{begin, end};
140 }
141
142} // namespace osmium
143
144#endif // OSMIUM_DIFF_ITERATOR_HPP
osmium::DiffIterator
Definition diff_iterator.hpp:57
osmium::DiffIterator::operator*
reference operator*() const noexcept
Definition diff_iterator.hpp:121
osmium::DiffIterator::difference_type
std::ptrdiff_t difference_type
Definition diff_iterator.hpp:85
osmium::DiffIterator::m_diff
osmium::DiffObject m_diff
Definition diff_iterator.hpp:66
osmium::DiffIterator::m_end
TBasicIterator m_end
Definition diff_iterator.hpp:64
osmium::DiffIterator::m_curr
TBasicIterator m_curr
Definition diff_iterator.hpp:61
osmium::DiffIterator::operator==
bool operator==(const DiffIterator &rhs) const noexcept
Definition diff_iterator.hpp:113
osmium::DiffIterator::DiffIterator
DiffIterator(TBasicIterator begin, TBasicIterator end)
Definition diff_iterator.hpp:89
osmium::DiffIterator::operator++
DiffIterator & operator++()
Definition diff_iterator.hpp:96
osmium::DiffIterator::set_diff
void set_diff() const noexcept
Definition diff_iterator.hpp:68
osmium::DiffIterator::operator!=
bool operator!=(const DiffIterator &rhs) const noexcept
Definition diff_iterator.hpp:117
osmium::DiffIterator::m_prev
TBasicIterator m_prev
Definition diff_iterator.hpp:60
osmium::DiffIterator::operator++
DiffIterator operator++(int)
Definition diff_iterator.hpp:107
osmium::DiffIterator::operator->
pointer operator->() const noexcept
Definition diff_iterator.hpp:126
osmium::DiffIterator::iterator_category
std::input_iterator_tag iterator_category
Definition diff_iterator.hpp:83
osmium::DiffIterator::m_next
TBasicIterator m_next
Definition diff_iterator.hpp:62
osmium::DiffObject
Definition diff_object.hpp:66
diff_object.hpp
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::make_diff_iterator
DiffIterator< TBasicIterator > make_diff_iterator(TBasicIterator begin, TBasicIterator end)
Definition diff_iterator.hpp:137
std
Definition location.hpp:555