Libosmium  2.7.2
Fast and flexible C++ library for working with OpenStreetMap data
delta.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_UTIL_DELTA_HPP
2 #define OSMIUM_UTIL_DELTA_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2016 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 <iterator>
37 #include <type_traits>
38 #include <utility>
39 
40 #include <osmium/util/cast.hpp>
41 
42 namespace osmium {
43 
44  namespace util {
45 
49  template <typename TValue, typename TDelta = int64_t>
50  class DeltaEncode {
51 
52  "DeltaEncode value type must be some integer");
53 
54  "DeltaEncode delta type must be some signed integer");
55 
56  TValue m_value;
57 
58  public:
59 
60  using value_type = TValue;
61  using delta_type = TDelta;
62 
63  explicit DeltaEncode(TValue value = 0) :
64  m_value(value) {
65  }
66 
67  void clear() noexcept {
68  m_value = 0;
69  }
70 
71  TValue value() const noexcept {
72  return m_value;
73  }
74 
75  TDelta update(TValue new_value) noexcept {
76  using std::swap;
77  swap(m_value, new_value);
78  return static_cast_with_assert<TDelta>(m_value) -
79  static_cast_with_assert<TDelta>(new_value);
80  }
81 
82  }; // class DeltaEncode
83 
87  template <typename TValue, typename TDelta = int64_t>
88  class DeltaDecode {
89 
90  "DeltaDecode value type must be some integer");
91 
92  "DeltaDecode delta type must be some signed integer");
93 
94  TValue m_value;
95 
96  public:
97 
98  using value_type = TValue;
99  using delta_type = TDelta;
100 
102  m_value(0) {
103  }
104 
105  void clear() noexcept {
106  m_value = 0;
107  }
108 
109  TValue update(TDelta delta) noexcept {
110  m_value = static_cast_with_assert<TValue>(
111  static_cast_with_assert<TDelta>(m_value) + delta);
112  return m_value;
113  }
114 
115  }; // class DeltaDecode
116 
117  template <typename TBaseIterator, typename TTransform, typename TValue, typename TDelta = int64_t>
119 
120  TBaseIterator m_it;
121  TBaseIterator m_end;
122  TTransform m_trans;
124  TDelta m_delta;
125 
126  public:
127 
128  using iterator_category = std::input_iterator_tag;
129  using value_type = TValue;
130  using difference_type = std::ptrdiff_t;
131  using pointer = value_type*;
133 
134  using delta_type = TDelta;
135 
136  DeltaEncodeIterator(TBaseIterator first, TBaseIterator last, TTransform& trans) :
137  m_it(first),
138  m_end(last),
139  m_trans(trans),
140  m_value(m_it != m_end ? m_trans(m_it) : 0),
141  m_delta(static_cast_with_assert<TDelta>(m_value.value())) {
142  }
143 
145  if (++m_it != m_end) {
146  m_delta = m_value.update(m_trans(m_it));
147  }
148  return *this;
149  }
150 
152  DeltaEncodeIterator tmp(*this);
153  operator++();
154  return tmp;
155  }
156 
157  TDelta operator*() {
158  return m_delta;
159  }
160 
161  bool operator==(const DeltaEncodeIterator& rhs) const {
162  return m_it == rhs.m_it && m_end == rhs.m_end;
163  }
164 
165  bool operator!=(const DeltaEncodeIterator& rhs) const {
166  return !(*this == rhs);
167  }
168 
169  }; // class DeltaEncodeIterator
170 
171  } // namespace util
172 
173 } // namespace osmium
174 
175 #endif // OSMIUM_UTIL_DELTA_HPP
TDelta delta_type
Definition: delta.hpp:134
DeltaEncodeIterator(TBaseIterator first, TBaseIterator last, TTransform &trans)
Definition: delta.hpp:136
DeltaEncode(TValue value=0)
Definition: delta.hpp:63
TDelta delta_type
Definition: delta.hpp:61
TValue m_value
Definition: delta.hpp:94
TValue value_type
Definition: delta.hpp:129
Definition: delta.hpp:118
value_type * pointer
Definition: delta.hpp:131
TTransform m_trans
Definition: delta.hpp:122
TDelta update(TValue new_value) noexcept
Definition: delta.hpp:75
value_type & reference
Definition: delta.hpp:132
Definition: delta.hpp:88
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:731
TValue value_type
Definition: delta.hpp:98
TValue m_value
Definition: delta.hpp:56
Namespace for everything in the Osmium library.
Definition: assembler.hpp:66
std::ptrdiff_t difference_type
Definition: delta.hpp:130
DeltaEncodeIterator operator++(int)
Definition: delta.hpp:151
bool operator!=(const DeltaEncodeIterator &rhs) const
Definition: delta.hpp:165
Definition: delta.hpp:50
TValue update(TDelta delta) noexcept
Definition: delta.hpp:109
TValue value_type
Definition: delta.hpp:60
DeltaEncode< TValue, TDelta > m_value
Definition: delta.hpp:123
TValue value() const noexcept
Definition: delta.hpp:71
TDelta operator*()
Definition: delta.hpp:157
std::input_iterator_tag iterator_category
Definition: delta.hpp:128
void clear() noexcept
Definition: delta.hpp:105
TBaseIterator m_it
Definition: delta.hpp:120
void clear() noexcept
Definition: delta.hpp:67
TDelta m_delta
Definition: delta.hpp:124
DeltaEncodeIterator & operator++()
Definition: delta.hpp:144
TBaseIterator m_end
Definition: delta.hpp:121
TDelta delta_type
Definition: delta.hpp:99
bool operator==(const DeltaEncodeIterator &rhs) const
Definition: delta.hpp:161
T static_cast_with_assert(const F value)
Definition: cast.hpp:63
DeltaDecode()
Definition: delta.hpp:101