Libosmium  2.17.3
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 
6 This file is part of Osmium (https://osmcode.org/libosmium).
7 
8 Copyright 2013-2022 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 
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  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;
86  using pointer = 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 
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
void set_diff() const noexcept
Definition: diff_iterator.hpp:68
bool operator!=(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:117
DiffIterator & operator++()
Definition: diff_iterator.hpp:96
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 > begin(Reader &reader)
Definition: reader_iterator.hpp:43
InputIterator< Reader > end(Reader &)
Definition: reader_iterator.hpp:47
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