Libosmium  2.4.1
Fast and flexible C++ library for working with OpenStreetMap data
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros
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-2015 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 namespace osmium {
41 
42  namespace util {
43 
47  template <typename T>
48  class DeltaEncode {
49 
51 
52  public:
53 
54  DeltaEncode(T value = 0) :
55  m_value(value) {
56  }
57 
58  void clear() {
59  m_value = 0;
60  }
61 
62  T update(T new_value) {
63  using std::swap;
64  swap(m_value, new_value);
65  return m_value - new_value;
66  }
67 
68  }; // class DeltaEncode
69 
73  template <typename T>
74  class DeltaDecode {
75 
77 
78  public:
79 
81  m_value(0) {
82  }
83 
84  void clear() {
85  m_value = 0;
86  }
87 
88  T update(T delta) {
89  m_value += delta;
90  return m_value;
91  }
92 
93  }; // class DeltaDecode
94 
95  template <typename TBaseIterator, typename TTransform, typename TValue>
96  class DeltaEncodeIterator : public std::iterator<std::input_iterator_tag, TValue> {
97 
98  typedef TValue value_type;
99 
100  TBaseIterator m_it;
101  TBaseIterator m_end;
102  TTransform m_trans;
103  value_type m_delta;
105 
106  public:
107 
108  DeltaEncodeIterator(TBaseIterator first, TBaseIterator last, TTransform& trans) :
109  m_it(first),
110  m_end(last),
111  m_trans(trans),
112  m_delta(m_it != m_end ? m_trans(m_it) : 0),
113  m_value(m_delta) {
114  }
115 
117  if (m_it != m_end) {
118  m_delta = m_value.update(m_trans(++m_it));
119  }
120  return *this;
121  }
122 
124  DeltaEncodeIterator tmp(*this);
125  operator++();
126  return tmp;
127  }
128 
129  value_type operator*() {
130  return m_delta;
131  }
132 
133  bool operator==(const DeltaEncodeIterator& rhs) const {
134  return m_it == rhs.m_it && m_end == rhs.m_end;
135  }
136 
137  bool operator!=(const DeltaEncodeIterator& rhs) const {
138  return !(*this == rhs);
139  }
140 
141  }; // class DeltaEncodeIterator
142 
143  } // namespace util
144 
145 } // namespace osmium
146 
147 #endif // OSMIUM_UTIL_DELTA_HPP
bool operator==(const DeltaEncodeIterator &rhs) const
Definition: delta.hpp:133
DeltaEncodeIterator operator++(int)
Definition: delta.hpp:123
T update(T new_value)
Definition: delta.hpp:62
void clear()
Definition: delta.hpp:84
Definition: delta.hpp:96
T m_value
Definition: delta.hpp:76
TValue value_type
Definition: delta.hpp:98
Definition: delta.hpp:74
TBaseIterator m_end
Definition: delta.hpp:101
TTransform m_trans
Definition: delta.hpp:102
DeltaEncode< value_type > m_value
Definition: delta.hpp:104
T m_value
Definition: delta.hpp:50
value_type m_delta
Definition: delta.hpp:103
Namespace for everything in the Osmium library.
Definition: assembler.hpp:55
value_type operator*()
Definition: delta.hpp:129
DeltaEncode(T value=0)
Definition: delta.hpp:54
Definition: delta.hpp:48
DeltaEncodeIterator(TBaseIterator first, TBaseIterator last, TTransform &trans)
Definition: delta.hpp:108
TBaseIterator m_it
Definition: delta.hpp:100
DeltaEncodeIterator & operator++()
Definition: delta.hpp:116
DeltaDecode()
Definition: delta.hpp:80
T update(T delta)
Definition: delta.hpp:88
void clear()
Definition: delta.hpp:58
bool operator!=(const DeltaEncodeIterator &rhs) const
Definition: delta.hpp:137