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