Libosmium: include/osmium/memory/collection.hpp Source File

Libosmium 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
36#include <osmium/memory/item.hpp>
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>
48 class CollectionIterator {
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
55 data_type m_data;
56
57 public:
58
59 using iterator_category = std::forward_iterator_tag;
60 using value_type = TMember;
61 using difference_type = std::ptrdiff_t;
62 using pointer = value_type*;
63 using reference = value_type&;
64
65 CollectionIterator() noexcept :
66 m_data(nullptr) {
67 }
68
69 explicit CollectionIterator(data_type data) noexcept :
70 m_data(data) {
71 }
72
73 CollectionIterator<TMember>& operator++() {
74 m_data = reinterpret_cast<TMember*>(m_data)->next();
75 return *static_cast<CollectionIterator<TMember>*>(this);
76 }
77
78 CollectionIterator<TMember> operator++(int) {
79 CollectionIterator<TMember> tmp{*this};
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>
118 class CollectionFilterIterator {
119
120 TFilter m_filter;
121 CollectionIterator<TMember> m_it;
122 CollectionIterator<TMember> m_end;
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;
138 using pointer = value_type*;
139 using reference = value_type&;
140
141 CollectionFilterIterator(TFilter filter, CollectionIterator<TMember> begin, CollectionIterator<TMember> end) :
142 m_filter(std::move(filter)),
143 m_it(begin),
144 m_end(end) {
145 advance();
146 }
147
148 CollectionFilterIterator& operator++() {
149 assert(m_it != m_end);
150 ++m_it;
151 advance();
152 return *this;
153 }
154
155 CollectionFilterIterator operator++(int) const {
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&;
189 using iterator = CollectionIterator<TMember>;
190 using const_iterator = CollectionIterator<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 {
209 return sizeof(Collection<TMember, TCollectionItemType>) == byte_size();
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 {
230 return const_iterator{data() + sizeof(Collection<TMember, TCollectionItemType>)};
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
osmium::memory::CollectionFilterIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition collection.hpp:135
osmium::memory::CollectionFilterIterator::operator==
bool operator==(const CollectionFilterIterator &rhs) const noexcept
Definition collection.hpp:161
osmium::memory::CollectionFilterIterator::operator->
pointer operator->() const noexcept
Definition collection.hpp:174
osmium::memory::CollectionFilterIterator::value_type
const TMember value_type
Definition collection.hpp:136
osmium::memory::CollectionFilterIterator::CollectionFilterIterator
CollectionFilterIterator(TFilter filter, CollectionIterator< TMember > begin, CollectionIterator< TMember > end)
Definition collection.hpp:141
osmium::memory::CollectionFilterIterator< filter_type, const osmium::Tag >::m_it
CollectionIterator< const osmium::Tag > m_it
Definition collection.hpp:121
osmium::memory::CollectionFilterIterator::operator!=
bool operator!=(const CollectionFilterIterator &rhs) const noexcept
Definition collection.hpp:165
osmium::memory::CollectionFilterIterator::pointer
value_type * pointer
Definition collection.hpp:138
osmium::memory::CollectionFilterIterator::operator++
CollectionFilterIterator operator++(int) const
Definition collection.hpp:155
osmium::memory::CollectionFilterIterator::operator*
reference operator*() const noexcept
Definition collection.hpp:169
osmium::memory::CollectionFilterIterator< filter_type, const osmium::Tag >::m_filter
filter_type m_filter
Definition collection.hpp:120
osmium::memory::CollectionFilterIterator::reference
value_type & reference
Definition collection.hpp:139
osmium::memory::CollectionFilterIterator< filter_type, const osmium::Tag >::m_end
CollectionIterator< const osmium::Tag > m_end
Definition collection.hpp:122
osmium::memory::CollectionFilterIterator::operator++
CollectionFilterIterator & operator++()
Definition collection.hpp:148
osmium::memory::CollectionFilterIterator::difference_type
std::ptrdiff_t difference_type
Definition collection.hpp:137
osmium::memory::CollectionFilterIterator::advance
void advance()
Definition collection.hpp:124
osmium::memory::CollectionIterator
Definition collection.hpp:48
osmium::memory::CollectionIterator::operator==
bool operator==(const CollectionIterator< TMember > &rhs) const noexcept
Definition collection.hpp:84
osmium::memory::CollectionIterator::CollectionIterator
CollectionIterator(data_type data) noexcept
Definition collection.hpp:69
osmium::memory::CollectionIterator::data_type
std::conditional_t< std::is_const< TMember >::value, const unsigned char *, unsigned char * > data_type
Definition collection.hpp:53
osmium::memory::CollectionIterator::reference
value_type & reference
Definition collection.hpp:63
osmium::memory::CollectionIterator::operator*
TMember & operator*() const noexcept
Definition collection.hpp:96
osmium::memory::CollectionIterator< const TMember >::data
unsigned char * data() const noexcept
Definition collection.hpp:92
osmium::memory::CollectionIterator::iterator_category
std::forward_iterator_tag iterator_category
Definition collection.hpp:59
osmium::memory::CollectionIterator< const TMember >::m_data
data_type m_data
Definition collection.hpp:55
osmium::memory::CollectionIterator::pointer
value_type * pointer
Definition collection.hpp:62
osmium::memory::CollectionIterator::operator++
CollectionIterator< TMember > & operator++()
Definition collection.hpp:73
osmium::memory::CollectionIterator::print
void print(std::basic_ostream< TChar, TTraits > &out) const
Definition collection.hpp:105
osmium::memory::CollectionIterator::CollectionIterator
CollectionIterator() noexcept
Definition collection.hpp:65
osmium::memory::CollectionIterator::operator->
TMember * operator->() const noexcept
Definition collection.hpp:100
osmium::memory::CollectionIterator::value_type
TMember value_type
Definition collection.hpp:60
osmium::memory::CollectionIterator::operator!=
bool operator!=(const CollectionIterator< TMember > &rhs) const noexcept
Definition collection.hpp:88
osmium::memory::CollectionIterator::operator++
CollectionIterator< TMember > operator++(int)
Definition collection.hpp:78
osmium::memory::CollectionIterator::difference_type
std::ptrdiff_t difference_type
Definition collection.hpp:61
osmium::memory::Collection::empty
bool empty() const noexcept
Definition collection.hpp:208
osmium::memory::Collection::reference
TMember & reference
Definition collection.hpp:187
osmium::memory::Collection::size_type
std::size_t size_type
Definition collection.hpp:191
osmium::memory::Collection::end
const_iterator end() const noexcept
Definition collection.hpp:241
osmium::memory::Collection::is_compatible_to
static constexpr bool is_compatible_to(const osmium::item_type t) noexcept
Definition collection.hpp:195
osmium::memory::Collection::itemtype
static constexpr osmium::item_type itemtype
Definition collection.hpp:193
osmium::memory::Collection::iterator
CollectionIterator< TMember > iterator
Definition collection.hpp:189
osmium::memory::Collection::value_type
TMember value_type
Definition collection.hpp:186
osmium::memory::Collection::begin
const_iterator begin() const noexcept
Definition collection.hpp:237
osmium::memory::Collection::end
iterator end() noexcept
Definition collection.hpp:225
osmium::memory::Collection::size
size_type size() const noexcept
Definition collection.hpp:217
osmium::memory::Collection::const_iterator
CollectionIterator< const TMember > const_iterator
Definition collection.hpp:190
osmium::memory::Collection::cend
const_iterator cend() const noexcept
Definition collection.hpp:233
osmium::memory::Collection::cbegin
const_iterator cbegin() const noexcept
Definition collection.hpp:229
osmium::memory::Collection::begin
iterator begin() noexcept
Definition collection.hpp:221
osmium::memory::Collection::Collection
Collection() noexcept
Definition collection.hpp:199
osmium::memory::Collection::const_reference
const TMember & const_reference
Definition collection.hpp:188
osmium::memory::Item::Item
Item(item_size_type size=0, item_type type=item_type{}) noexcept
Definition item.hpp:128
osmium::memory::Item::byte_size
item_size_type byte_size() const noexcept
Definition item.hpp:163
osmium::memory::Item::CollectionIterator
friend class CollectionIterator
Definition item.hpp:114
item.hpp
osmium::memory
Definition osm_object_builder.hpp:66
osmium::memory::operator<<
std::basic_ostream< TChar, TTraits > & operator<<(std::basic_ostream< TChar, TTraits > &out, const CollectionIterator< TMember > &iter)
Definition collection.hpp:112
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:654