Libosmium  2.15.1
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-2019 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 
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;
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  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_object.hpp:66
DiffIterator< TBasicIterator > make_diff_iterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:138
InputIterator< Reader > end(Reader &)
Definition: reader_iterator.hpp:47
const TBasicIterator m_end
Definition: diff_iterator.hpp:64
Definition: location.hpp:550
Definition: diff_iterator.hpp:57
InputIterator< Reader > begin(Reader &reader)
Definition: reader_iterator.hpp:43
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
osmium::DiffObject m_diff
Definition: diff_iterator.hpp:66
bool operator==(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:114
void set_diff() const noexcept
Definition: diff_iterator.hpp:68
DiffIterator & operator++()
Definition: diff_iterator.hpp:97
std::input_iterator_tag iterator_category
Definition: diff_iterator.hpp:83
TBasicIterator m_prev
Definition: diff_iterator.hpp:60
reference operator*() const noexcept
Definition: diff_iterator.hpp:122
DiffIterator operator++(int)
Definition: diff_iterator.hpp:108
TBasicIterator m_curr
Definition: diff_iterator.hpp:61
pointer operator->() const noexcept
Definition: diff_iterator.hpp:127
TBasicIterator m_next
Definition: diff_iterator.hpp:62
std::ptrdiff_t difference_type
Definition: diff_iterator.hpp:85
bool operator!=(const DiffIterator &rhs) const noexcept
Definition: diff_iterator.hpp:118
DiffIterator(TBasicIterator begin, TBasicIterator end)
Definition: diff_iterator.hpp:89