Libosmium  2.14.0
Fast and flexible C++ library for working with OpenStreetMap data
node_ref_list.hpp
Go to the documentation of this file.
1 #ifndef OSMIUM_OSM_NODE_REF_LIST_HPP
2 #define OSMIUM_OSM_NODE_REF_LIST_HPP
3 
4 /*
5 
6 This file is part of Osmium (http://osmcode.org/libosmium).
7 
8 Copyright 2013-2018 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 #include <osmium/osm/box.hpp>
38 #include <osmium/osm/item_type.hpp>
39 #include <osmium/osm/location.hpp>
40 #include <osmium/osm/node_ref.hpp>
41 
42 #include <cassert>
43 #include <cstddef>
44 #include <iterator>
45 
46 namespace osmium {
47 
53 
54  public:
55 
57  using reference = NodeRef&;
58  using const_reference = const NodeRef&;
59  using iterator = NodeRef*;
60  using const_iterator = const NodeRef*;
61  using const_reverse_iterator = std::reverse_iterator<const NodeRef*>;
62  using difference_type = std::ptrdiff_t;
63  using size_type = std::size_t;
64 
65  explicit NodeRefList(osmium::item_type itemtype) noexcept :
66  osmium::memory::Item(sizeof(NodeRefList), itemtype) {
67  }
68 
74  bool empty() const noexcept {
75  return sizeof(NodeRefList) == byte_size();
76  }
77 
83  size_type size() const noexcept {
84  const auto size_node_refs = byte_size() - sizeof(NodeRefList);
85  assert(size_node_refs % sizeof(NodeRef) == 0);
86  return size_node_refs / sizeof(NodeRef);
87  }
88 
98  const NodeRef& operator[](size_type n) const noexcept {
99  assert(n < size());
100  const NodeRef* node_ref = &*(cbegin());
101  return node_ref[n];
102  }
103 
113  NodeRef& operator[](size_type n) noexcept {
114  assert(n < size());
115  NodeRef* node_ref = &*(begin());
116  return node_ref[n];
117  }
118 
126  const NodeRef& front() const noexcept {
127  assert(!empty());
128  return operator[](0);
129  }
130 
138  const NodeRef& back() const noexcept {
139  assert(!empty());
140  return operator[](size() - 1);
141  }
142 
151  bool is_closed() const noexcept {
152  return ends_have_same_id();
153  }
154 
163  bool ends_have_same_id() const noexcept {
164  return front().ref() == back().ref();
165  }
166 
176  bool ends_have_same_location() const {
177  assert(front().location() && back().location());
178  return front().location() == back().location();
179  }
180 
187  osmium::Box envelope() const noexcept {
188  osmium::Box box;
189  for (const auto& node_ref : *this) {
190  box.extend(node_ref.location());
191  }
192  return box;
193  }
194 
196  iterator begin() noexcept {
197  return iterator(data() + sizeof(NodeRefList));
198  }
199 
201  iterator end() noexcept {
202  return iterator(data() + byte_size());
203  }
204 
206  const_iterator cbegin() const noexcept {
207  return const_iterator(data() + sizeof(NodeRefList));
208  }
209 
211  const_iterator cend() const noexcept {
212  return const_iterator(data() + byte_size());
213  }
214 
216  const_iterator begin() const noexcept {
217  return cbegin();
218  }
219 
221  const_iterator end() const noexcept {
222  return cend();
223  }
224 
226  const_reverse_iterator crbegin() const noexcept {
227  return const_reverse_iterator(cend());
228  }
229 
231  const_reverse_iterator crend() const noexcept {
232  return const_reverse_iterator(cbegin());
233  }
234 
235  }; // class NodeRefList
236 
237 } // namespace osmium
238 
239 #endif // OSMIUM_OSM_NODE_REF_LIST_HPP
const_iterator cbegin() const noexcept
Returns an iterator to the beginning.
Definition: node_ref_list.hpp:206
std::reverse_iterator< const NodeRef * > const_reverse_iterator
Definition: node_ref_list.hpp:61
bool is_closed() const noexcept
Definition: node_ref_list.hpp:151
const_reverse_iterator crbegin() const noexcept
Returns a reverse_iterator to the beginning.
Definition: node_ref_list.hpp:226
item_type
Definition: item_type.hpp:43
const NodeRef * const_iterator
Definition: node_ref_list.hpp:60
Box & extend(const Location &location) noexcept
Definition: box.hpp:100
bool ends_have_same_id() const noexcept
Definition: node_ref_list.hpp:163
const_iterator cend() const noexcept
Returns an iterator to the end.
Definition: node_ref_list.hpp:211
std::ptrdiff_t difference_type
Definition: node_ref_list.hpp:62
constexpr osmium::object_id_type ref() const noexcept
Definition: node_ref.hpp:71
bool empty() const noexcept
Definition: node_ref_list.hpp:74
Definition: item.hpp:103
NodeRef & operator[](size_type n) noexcept
Definition: node_ref_list.hpp:113
Namespace for everything in the Osmium library.
Definition: assembler.hpp:53
const_iterator begin() const noexcept
Returns an iterator to the beginning.
Definition: node_ref_list.hpp:216
osmium::Location & location() noexcept
Definition: node_ref.hpp:85
std::size_t size_type
Definition: node_ref_list.hpp:63
item_size_type byte_size() const noexcept
Definition: item.hpp:161
Definition: box.hpp:49
iterator begin() noexcept
Returns an iterator to the beginning.
Definition: node_ref_list.hpp:196
NodeRefList(osmium::item_type itemtype) noexcept
Definition: node_ref_list.hpp:65
iterator end() noexcept
Returns an iterator to the end.
Definition: node_ref_list.hpp:201
Definition: node_ref_list.hpp:52
const NodeRef & front() const noexcept
Definition: node_ref_list.hpp:126
osmium::Box envelope() const noexcept
Definition: node_ref_list.hpp:187
const NodeRef & back() const noexcept
Definition: node_ref_list.hpp:138
NodeRef * iterator
Definition: node_ref_list.hpp:59
Definition: node_ref.hpp:50
bool ends_have_same_location() const
Definition: node_ref_list.hpp:176
const_reverse_iterator crend() const noexcept
Returns a reverse_iterator to the end.
Definition: node_ref_list.hpp:231
const NodeRef & operator[](size_type n) const noexcept
Definition: node_ref_list.hpp:98
size_type size() const noexcept
Definition: node_ref_list.hpp:83
const_iterator end() const noexcept
Returns an iterator to the end.
Definition: node_ref_list.hpp:221