Libosmium  2.15.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 (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 
36 #include <osmium/util/cast.hpp>
37 
38 #include <cassert>
39 #include <cstdint>
40 #include <type_traits>
41 #include <utility>
42 
43 namespace osmium {
44 
45  inline namespace util {
46 
50  template <typename TValue, typename TDelta = int64_t>
51  class DeltaEncode {
52 
53  "DeltaEncode value type must be some integer");
54 
55  "DeltaEncode delta type must be some signed integer");
56 
57  // Not a perfect check, because of signed vs. unsigned, but
58  // might find some problems.
59  "Delta type size should be larger or equal to value type size");
60 
61  TValue m_value;
62 
63  public:
64 
65  using value_type = TValue;
66  using delta_type = TDelta;
67 
68  explicit DeltaEncode(TValue value = 0) :
69  m_value(value) {
70  }
71 
72  void clear() noexcept {
73  m_value = 0;
74  }
75 
76  TValue value() const noexcept {
77  return m_value;
78  }
79 
80  TDelta update(TValue new_value) noexcept {
81  using std::swap;
82  swap(m_value, new_value);
83  // Checking the static_cast here doesn't help much, because
84  // the substraction can still lead to an overflow. This is
85  // dependend on the input data being "reasonable". XXX
86  return static_cast<TDelta>(m_value) -
87  static_cast<TDelta>(new_value);
88  }
89 
90  }; // class DeltaEncode
91 
95  template <typename TValue, typename TDelta = int64_t>
96  class DeltaDecode {
97 
98  "DeltaDecode value type must be some integer");
99 
100  "DeltaDecode delta type must be some signed integer");
101 
102  TValue m_value;
103 
104  public:
105 
106  using value_type = TValue;
107  using delta_type = TDelta;
108 
110  m_value(0) {
111  }
112 
113  void clear() noexcept {
114  m_value = 0;
115  }
116 
117  TValue update(TDelta delta) noexcept {
118  // Do not check for overflow. With real data this should not
119  // happen and if somebody is trying to trick us they can only
120  // create values this way they would also be able to generate
121  // without having an overflow.
122  m_value = static_cast<TValue>(
123  static_cast<TDelta>(m_value) + delta);
124  return m_value;
125  }
126 
127  }; // class DeltaDecode
128 
129  } // namespace util
130 
131 } // namespace osmium
132 
133 #endif // OSMIUM_UTIL_DELTA_HPP
DeltaEncode(TValue value=0)
Definition: delta.hpp:68
TDelta delta_type
Definition: delta.hpp:66
TValue m_value
Definition: delta.hpp:102
TDelta update(TValue new_value) noexcept
Definition: delta.hpp:80
Definition: delta.hpp:96
void swap(Buffer &lhs, Buffer &rhs)
Definition: buffer.hpp:885
TValue value_type
Definition: delta.hpp:106
TValue m_value
Definition: delta.hpp:61
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
Definition: delta.hpp:51
TValue update(TDelta delta) noexcept
Definition: delta.hpp:117
TValue value_type
Definition: delta.hpp:65
TValue value() const noexcept
Definition: delta.hpp:76
void clear() noexcept
Definition: delta.hpp:113
void clear() noexcept
Definition: delta.hpp:72
TDelta delta_type
Definition: delta.hpp:107
DeltaDecode()
Definition: delta.hpp:109