Libosmium  2.17.0
Fast and flexible C++ library for working with OpenStreetMap data
Loading...
Searching...
No Matches
delta.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_UTIL_DELTA_HPP
2#define OSMIUM_UTIL_DELTA_HPP
3
4/*
5
6This file is part of Osmium (https://osmcode.org/libosmium).
7
8Copyright 2013-2021 Jochen Topf <jochen@topf.org> and others (see README).
9
10Boost Software License - Version 1.0 - August 17th, 2003
11
12Permission is hereby granted, free of charge, to any person or organization
13obtaining a copy of the software and accompanying documentation covered by
14this license (the "Software") to use, reproduce, display, distribute,
15execute, and transmit the Software, and to prepare derivative works of the
16Software, and to permit third-parties to whom the Software is furnished to
17do so, all subject to the following:
18
19The copyright notices in the Software and this entire statement, including
20the above license grant, this restriction and the following disclaimer,
21must be included in all copies of the Software, in whole or in part, and
22all derivative works of the Software, unless such copies or derivative
23works are solely in the form of machine-executable object code generated by
24a source language processor.
25
26THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
27IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
28FITNESS FOR A PARTICULAR PURPOSE, TITLE AND NON-INFRINGEMENT. IN NO EVENT
29SHALL THE COPYRIGHT HOLDERS OR ANYONE DISTRIBUTING THE SOFTWARE BE LIABLE
30FOR ANY DAMAGES OR OTHER LIABILITY, WHETHER IN CONTRACT, TORT OR OTHERWISE,
31ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
32DEALINGS 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
43namespace osmium {
44
45 inline namespace util {
46
50 template <typename TValue, typename TDelta = int64_t>
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>
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 TValue value() const noexcept {
128 return m_value;
129 }
130
131 }; // class DeltaDecode
132
133 } // namespace util
134
135} // namespace osmium
136
137#endif // OSMIUM_UTIL_DELTA_HPP
Definition: delta.hpp:96
TValue m_value
Definition: delta.hpp:102
DeltaDecode()
Definition: delta.hpp:109
void clear() noexcept
Definition: delta.hpp:113
TValue update(TDelta delta) noexcept
Definition: delta.hpp:117
TDelta delta_type
Definition: delta.hpp:107
TValue value() const noexcept
Definition: delta.hpp:127
TValue value_type
Definition: delta.hpp:106
Definition: delta.hpp:51
TValue m_value
Definition: delta.hpp:61
TValue value_type
Definition: delta.hpp:65
TDelta delta_type
Definition: delta.hpp:66
DeltaEncode(TValue value=0)
Definition: delta.hpp:68
TDelta update(TValue new_value) noexcept
Definition: delta.hpp:80
TValue value() const noexcept
Definition: delta.hpp:76
void clear() noexcept
Definition: delta.hpp:72
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53