Libosmium  2.6.1
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>
118  class DeltaEncodeIterator : public std::iterator<std::input_iterator_tag, TValue> {
119 
120  TBaseIterator m_it;
121  TBaseIterator m_end;
122  TTransform m_trans;
124  TDelta m_delta;
125 
126  public:
127 
128  using value_type = TValue;
129  using delta_type = TDelta;
130 
131  DeltaEncodeIterator(TBaseIterator first, TBaseIterator last, TTransform& trans) :
132  m_it(first),
133  m_end(last),
134  m_trans(trans),
135  m_value(m_it != m_end ? m_trans(m_it) : 0),
136  m_delta(static_cast_with_assert<TDelta>(m_value.value())) {
137  }
138 
140  if (++m_it != m_end) {
141  m_delta = m_value.update(m_trans(m_it));
142  }
143  return *this;
144  }
145 
147  DeltaEncodeIterator tmp(*this);
148  operator++();
149  return tmp;
150  }
151 
152  TDelta operator*() {
153  return m_delta;
154  }
155 
156  bool operator==(const DeltaEncodeIterator& rhs) const {
157  return m_it == rhs.m_it && m_end == rhs.m_end;
158  }
159 
160  bool operator!=(const DeltaEncodeIterator& rhs) const {
161  return !(*this == rhs);
162  }
163 
164  }; // class DeltaEncodeIterator
165 
166  } // namespace util
167 
168 } // namespace osmium
169 
170 #endif // OSMIUM_UTIL_DELTA_HPP
TValue value() const noexcept
Definition: delta.hpp:71
TDelta delta_type
Definition: delta.hpp:129
DeltaEncodeIterator(TBaseIterator first, TBaseIterator last, TTransform &trans)
Definition: delta.hpp:131
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:128
Definition: delta.hpp:118
TTransform m_trans
Definition: delta.hpp:122
TDelta update(TValue new_value) noexcept
Definition: delta.hpp:75
Definition: delta.hpp:88
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:721
TValue value_type
Definition: delta.hpp:98
TValue m_value
Definition: delta.hpp:56
Namespace for everything in the Osmium library.
Definition: assembler.hpp:59
DeltaEncodeIterator operator++(int)
Definition: delta.hpp:146
bool operator!=(const DeltaEncodeIterator &rhs) const
Definition: delta.hpp:160
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
TDelta operator*()
Definition: delta.hpp:152
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:139
TBaseIterator m_end
Definition: delta.hpp:121
TDelta delta_type
Definition: delta.hpp:99
bool operator==(const DeltaEncodeIterator &rhs) const
Definition: delta.hpp:156
T static_cast_with_assert(const F value)
Definition: cast.hpp:63
DeltaDecode()
Definition: delta.hpp:101