Libosmium 2.22.0 Fast and flexible C++ library for working with OpenStreetMap data

Loading...
Searching...
No Matches
collection.hpp
Go to the documentation of this file.
1#ifndef OSMIUM_MEMORY_COLLECTION_HPP
2#define OSMIUM_MEMORY_COLLECTION_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 <osmium/memory/item.hpp>
37
38#include <iosfwd>
39#include <iterator>
40#include <type_traits>
41
42 namespace osmium {
43
44 namespace memory {
45
46 template <typename TMember>
47 class CollectionIterator {
48
49 // This data_type is either 'unsigned char*' or 'const unsigned
50 // char*' depending on whether TMember is const. This allows this
51 // class to be used as an iterator and as a const_iterator.
52 using data_type = std::conditional_t<std::is_const<TMember>::value, const unsigned char*, unsigned char*>;
53
54 data_type m_data;
55
56 public:
57
58 using iterator_category = std::forward_iterator_tag;
59 using value_type = TMember;
60 using difference_type = std::ptrdiff_t;
61 using pointer = value_type*;
62 using reference = value_type&;
63
64 CollectionIterator() noexcept :
65 m_data(nullptr) {
66 }
67
68 explicit CollectionIterator(data_type data) noexcept :
69 m_data(data) {
70 }
71
72 CollectionIterator<TMember>& operator++() {
73 m_data = reinterpret_cast<TMember*>(m_data)->next();
74 return *static_cast<CollectionIterator<TMember>*>(this);
75 }
76
77 CollectionIterator<TMember> operator++(int) {
78 CollectionIterator<TMember> tmp{*this};
79 operator++();
80 return tmp;
81 }
82
83 bool operator==(const CollectionIterator<TMember>& rhs) const noexcept {
84 return m_data == rhs.m_data;
85 }
86
87 bool operator!=(const CollectionIterator<TMember>& rhs) const noexcept {
88 return !(*this == rhs);
89 }
90
91 unsigned char* data() const noexcept {
92 return m_data;
93 }
94
95 TMember& operator*() const noexcept {
96 return *reinterpret_cast<TMember*>(m_data);
97 }
98
99 TMember* operator->() const noexcept {
100 return reinterpret_cast<TMember*>(m_data);
101 }
102
103 template <typename TChar, typename TTraits>
104 void print(std::basic_ostream<TChar, TTraits>& out) const {
105 out << static_cast<const void*>(m_data);
106 }
107
108 }; // class CollectionIterator
109
110 template <typename TChar, typename TTraits, typename TMember>
111 inline std::basic_ostream<TChar, TTraits>& operator<<(std::basic_ostream<TChar, TTraits>& out, const CollectionIterator<TMember>& iter) {
112 iter.print(out);
113 return out;
114 }
115
116 template <typename TFilter, typename TMember>
117 class CollectionFilterIterator {
118
119 TFilter m_filter;
120 CollectionIterator<TMember> m_it;
121 CollectionIterator<TMember> m_end;
122
123 void advance() {
124 while (m_it != m_end) {
125 if (m_filter(*m_it)) {
126 break;
127 }
128 ++m_it;
129 }
130 }
131
132 public:
133
134 using iterator_category = std::forward_iterator_tag;
135 using value_type = const TMember;
136 using difference_type = std::ptrdiff_t;
137 using pointer = value_type*;
138 using reference = value_type&;
139
140 CollectionFilterIterator(TFilter filter, CollectionIterator<TMember> begin, CollectionIterator<TMember> end) :
141 m_filter(std::move(filter)),
142 m_it(begin),
143 m_end(end) {
144 advance();
145 }
146
147 CollectionFilterIterator& operator++() {
148 assert(m_it != m_end);
149 ++m_it;
150 advance();
151 return *this;
152 }
153
154 CollectionFilterIterator operator++(int) const {
155 CollectionFilterIterator tmp{*this};
156 operator++();
157 return tmp;
158 }
159
160 bool operator==(const CollectionFilterIterator& rhs) const noexcept {
161 return m_it == rhs.m_it && m_end == rhs.m_end;
162 }
163
164 bool operator!=(const CollectionFilterIterator& rhs) const noexcept {
165 return !(*this == rhs);
166 }
167
168 reference operator*() const noexcept {
169 assert(m_it != m_end);
170 return *m_it;
171 }
172
173 pointer operator->() const noexcept {
174 assert(m_it != m_end);
175 return &*m_it;
176 }
177
178 }; // class CollectionFilterIterator
179
180 template <typename TMember, osmium::item_type TCollectionItemType>
181 class Collection : public Item {
182
183 public:
184
185 using value_type = TMember;
186 using reference = TMember&;
187 using const_reference = const TMember&;
188 using iterator = CollectionIterator<TMember>;
189 using const_iterator = CollectionIterator<const TMember>;
190 using size_type = std::size_t;
191
192 static constexpr osmium::item_type itemtype = TCollectionItemType;
193
194 constexpr static bool is_compatible_to(const osmium::item_type t) noexcept {
195 return t == itemtype;
196 }
197
198 Collection() noexcept :
199 Item(sizeof(Collection<TMember, TCollectionItemType>), TCollectionItemType) {
200 }
201
207 bool empty() const noexcept {
208 return sizeof(Collection<TMember, TCollectionItemType>) == byte_size();
209 }
210
216 size_type size() const noexcept {
217 return static_cast<size_type>(std::distance(begin(), end()));
218 }
219
220 iterator begin() noexcept {
221 return iterator{data() + sizeof(Collection<TMember, TCollectionItemType>)};
222 }
223
224 iterator end() noexcept {
225 return iterator{data() + byte_size()};
226 }
227
228 const_iterator cbegin() const noexcept {
229 return const_iterator{data() + sizeof(Collection<TMember, TCollectionItemType>)};
230 }
231
232 const_iterator cend() const noexcept {
233 return const_iterator{data() + byte_size()};
234 }
235
236 const_iterator begin() const noexcept {
237 return cbegin();
238 }
239
240 const_iterator end() const noexcept {
241 return cend();
242 }
243
244 }; // class Collection
245
246 } // namespace memory
247
248} // namespace osmium
249
250#endif // OSMIUM_MEMORY_COLLECTION_HPP
osmium::memory::CollectionFilterIterator
Definition collection.hpp:117
osmium::memory::CollectionFilterIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition collection.hpp:134
osmium::memory::CollectionFilterIterator::operator==
bool operator==(const CollectionFilterIterator &rhs) const noexcept
Definition collection.hpp:160
osmium::memory::CollectionFilterIterator::operator->
pointer operator->() const noexcept
Definition collection.hpp:173
osmium::memory::CollectionFilterIterator::value_type
const TMember value_type
Definition collection.hpp:135
osmium::memory::CollectionFilterIterator::CollectionFilterIterator
CollectionFilterIterator(TFilter filter, CollectionIterator< TMember > begin, CollectionIterator< TMember > end)
Definition collection.hpp:140
osmium::memory::CollectionFilterIterator::m_it
CollectionIterator< TMember > m_it
Definition collection.hpp:120
osmium::memory::CollectionFilterIterator::operator!=
bool operator!=(const CollectionFilterIterator &rhs) const noexcept
Definition collection.hpp:164
osmium::memory::CollectionFilterIterator::pointer
value_type * pointer
Definition collection.hpp:137
osmium::memory::CollectionFilterIterator::operator++
CollectionFilterIterator operator++(int) const
Definition collection.hpp:154
osmium::memory::CollectionFilterIterator::operator*
reference operator*() const noexcept
Definition collection.hpp:168
osmium::memory::CollectionFilterIterator::m_filter
TFilter m_filter
Definition collection.hpp:119
osmium::memory::CollectionFilterIterator::reference
value_type & reference
Definition collection.hpp:138
osmium::memory::CollectionFilterIterator::m_end
CollectionIterator< TMember > m_end
Definition collection.hpp:121
osmium::memory::CollectionFilterIterator::operator++
CollectionFilterIterator & operator++()
Definition collection.hpp:147
osmium::memory::CollectionFilterIterator::difference_type
std::ptrdiff_t difference_type
Definition collection.hpp:136
osmium::memory::CollectionFilterIterator::advance
void advance()
Definition collection.hpp:123
osmium::memory::CollectionIterator
Definition collection.hpp:47
osmium::memory::CollectionIterator::operator==
bool operator==(const CollectionIterator< TMember > &rhs) const noexcept
Definition collection.hpp:83
osmium::memory::CollectionIterator::CollectionIterator
CollectionIterator(data_type data) noexcept
Definition collection.hpp:68
osmium::memory::CollectionIterator::data_type
std::conditional_t< std::is_const< TMember >::value, const unsigned char *, unsigned char * > data_type
Definition collection.hpp:52
osmium::memory::CollectionIterator::reference
value_type & reference
Definition collection.hpp:62
osmium::memory::CollectionIterator::operator*
TMember & operator*() const noexcept
Definition collection.hpp:95
osmium::memory::CollectionIterator::data
unsigned char * data() const noexcept
Definition collection.hpp:91
osmium::memory::CollectionIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition collection.hpp:58
osmium::memory::CollectionIterator::m_data
data_type m_data
Definition collection.hpp:54
osmium::memory::CollectionIterator::pointer
value_type * pointer
Definition collection.hpp:61
osmium::memory::CollectionIterator::operator++
CollectionIterator< TMember > & operator++()
Definition collection.hpp:72
osmium::memory::CollectionIterator::print
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition collection.hpp:104
osmium::memory::CollectionIterator::CollectionIterator
CollectionIterator() noexcept
Definition collection.hpp:64
osmium::memory::CollectionIterator::operator->
TMember * operator->() const noexcept
Definition collection.hpp:99
osmium::memory::CollectionIterator::value_type
TMember value_type
Definition collection.hpp:59
osmium::memory::CollectionIterator::operator!=
bool operator!=(const CollectionIterator< TMember > &rhs) const noexcept
Definition collection.hpp:87
osmium::memory::CollectionIterator::operator++
CollectionIterator< TMember > operator++(int)
Definition collection.hpp:77
osmium::memory::CollectionIterator::difference_type
std::ptrdiff_t difference_type
Definition collection.hpp:60
osmium::memory::Collection
Definition collection.hpp:181
osmium::memory::Collection::empty
bool empty() const noexcept
Definition collection.hpp:207
osmium::memory::Collection::reference
TMember & reference
Definition collection.hpp:186
osmium::memory::Collection::size_type
std::size_t size_type
Definition collection.hpp:190
osmium::memory::Collection::end
const_iterator end() const noexcept
Definition collection.hpp:240
osmium::memory::Collection::is_compatible_to
static constexpr bool is_compatible_to(const osmium::item_type t) noexcept
Definition collection.hpp:194
osmium::memory::Collection::itemtype
static constexpr osmium::item_type itemtype
Definition collection.hpp:192
osmium::memory::Collection::value_type
TMember value_type
Definition collection.hpp:185
osmium::memory::Collection::begin
const_iterator begin() const noexcept
Definition collection.hpp:236
osmium::memory::Collection::end
iterator end() noexcept
Definition collection.hpp:224
osmium::memory::Collection::size
size_type size() const noexcept
Definition collection.hpp:216
osmium::memory::Collection::cend
const_iterator cend() const noexcept
Definition collection.hpp:232
osmium::memory::Collection::cbegin
const_iterator cbegin() const noexcept
Definition collection.hpp:228
osmium::memory::Collection::begin
iterator begin() noexcept
Definition collection.hpp:220
osmium::memory::Collection::Collection
Collection() noexcept
Definition collection.hpp:198
osmium::memory::Collection::const_reference
const TMember & const_reference
Definition collection.hpp:187
osmium::memory::Item
Definition item.hpp:105
osmium::memory::Item::byte_size
item_size_type byte_size() const noexcept
Definition item.hpp:163
item.hpp
osmium::memory::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const CollectionIterator< TMember > &iter)
Definition collection.hpp:111
osmium
Namespace for everything in the Osmium library.
Definition assembler.hpp:53
osmium::item_type
item_type
Definition item_type.hpp:45
std
Definition location.hpp:555