Libosmium 2.22.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
6 This file is part of Osmium (https://osmcode.org/libosmium).
7
8 Copyright 2013-2025 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 <cassert>
37#include <cstdint>
38#include <type_traits>
39#include <utility>
40
41 namespace osmium {
42
43 inline namespace util {
44
48 template <typename TValue, typename TDelta = int64_t>
49 class DeltaEncode {
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>
94 class DeltaDecode {
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
107 DeltaDecode() :
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
osmium::DeltaDecode
Definition delta.hpp:94
osmium::util::DeltaDecode::m_value
TValue m_value
Definition delta.hpp:100
osmium::util::DeltaDecode::DeltaDecode
DeltaDecode()
Definition delta.hpp:107
osmium::util::DeltaDecode::clear
void clear() noexcept
Definition delta.hpp:111
osmium::util::DeltaDecode::update
TValue update(TDelta delta) noexcept
Definition delta.hpp:115
osmium::util::DeltaDecode::delta_type
TDelta delta_type
Definition delta.hpp:105
osmium::util::DeltaDecode::value
TValue value() const noexcept
Definition delta.hpp:125
osmium::util::DeltaDecode::value_type
TValue value_type
Definition delta.hpp:104
osmium::DeltaEncode
Definition delta.hpp:49
osmium::util::DeltaEncode::m_value
TValue m_value
Definition delta.hpp:59
osmium::util::DeltaEncode::value_type
TValue value_type
Definition delta.hpp:63
osmium::util::DeltaEncode::delta_type
TDelta delta_type
Definition delta.hpp:64
osmium::util::DeltaEncode::DeltaEncode
DeltaEncode(TValue value=0)
Definition delta.hpp:66
osmium::util::DeltaEncode::update
TDelta update(TValue new_value) noexcept
Definition delta.hpp:78
osmium::util::DeltaEncode::value
TValue value() const noexcept
Definition delta.hpp:74
osmium::util::DeltaEncode::clear
void clear() noexcept
Definition delta.hpp:70
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53