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