osrm-backend/include/protozero/iterators.hpp
Michael Krasnyk 0c6dee4bef Squashed 'third_party/libosmium/' changes from d5ecf4d..c1f34c4
c1f34c4 Release v2.11.0
d3b72e0 Updated change log.
2982b8d Update embedded Protozero to version 1.5.1.
cc1ab2a Add non-const WayNodeList::operator[].
3da372e Add missing example to examples/README.md.
30604ba Add OSMIUM_USE_SLOW_MERCATOR_PROJECTION define.
47a92e0 Clearer CheckOrder handler doc.
f11106d Formatting fixes.
a870737 Use faster implementation of web mercator projection.
041bb42 Test cleanups.
8933bc5 Cleanup *Map::get() functions.
6b989ca Document that (Multipolygon)Collectors only work with unique Ids.
8fb5bd2 Updated included Protozero to version 1.5.0.
76e153d Removed Makefile.
35d7ec9 Update copyright date.
a7f8126 Rename guard define to common scheme.
a923c69 Cleanup I/O tests.
d353993 Add Map::get_noexcept() method for all index maps.
94fa5ac Add const overload for mmap_vector_base::operator[].
3cf9184 Add default constructed "invalid" Coordinates.
358f170 Add Tile constructor from web mercator coordinates.
006aa4c Add index::RelationsMap(Stash|Index) classes.
9cc842e Updated catch to v1.5.9.
bd8c3b6 Use initializer_list trick instead of recursive template.
2c82a6f Merge pull request #183 from daniel-j-h/rvalue-apply
0bf5404 Implements rvalue handler support for apply, resolves #180.
ccaab08 Merge pull request #182 from AMDmi3/freebsd-endianess
bffe626 Handle endianess on FreeBSD properly
7250222 Code formatting and test cleanup.
6652436 Merge pull request #179 from oxidase/add_match_key_std_regex
afadf5b Rename centroid variables and function in example.
8355284 Add envelope() functions to NodeRefList, Way, and Area.
fc83d2e Remove unnecessary include.
9ddd00e Add match_key<std::regex> tag
9c54a53 Update README. Moved some infos to manual.
89a90a6 Update readme and developer docs.
c3446ec Simplify subitem iteration code and made it more flexible.
542b07c Add some static_asserts.
f0fd690 Memory reporting on M68k doesn't work properly.
e8957c6 Compare doubles in test using Approx().
58ae4a6 Add amenity_list example.
53783f8 Fix doxygen config for reproducible builds.
de4e52d Release v2.10.3
0cc42a2 ObjectPointerCollection constructor can't be noexcept.
4472dfb Round out ObjectPointerCollection implementation and test it.
28cb35d Build with XCode 8 and GCC 6 on travis.
03e3e66 Upgrade to new protozero version 1.4.5.
2102c2f Add assertion in queue handling code.

git-subtree-dir: third_party/libosmium
git-subtree-split: c1f34c45507e233a2b9028663906679c610fe179
2017-01-20 14:05:21 +01:00

329 lines
8.5 KiB
C++

#ifndef PROTOZERO_ITERATORS_HPP
#define PROTOZERO_ITERATORS_HPP
/*****************************************************************************
protozero - Minimalistic protocol buffer decoder and encoder in C++.
This file is from https://github.com/mapbox/protozero where you can find more
documentation.
*****************************************************************************/
/**
* @file iterators.hpp
*
* @brief Contains the iterators for access to packed repeated fields.
*/
#include <cstring>
#include <iterator>
#include <utility>
#include <protozero/config.hpp>
#include <protozero/varint.hpp>
#if PROTOZERO_BYTE_ORDER != PROTOZERO_LITTLE_ENDIAN
# include <protozero/byteswap.hpp>
#endif
namespace protozero {
/**
* A range of iterators based on std::pair. Created from beginning and
* end iterators. Used as a return type from some pbf_reader methods
* that is easy to use with range-based for loops.
*/
template <typename T, typename P = std::pair<T, T>>
class iterator_range :
#ifdef PROTOZERO_STRICT_API
protected
#else
public
#endif
P {
public:
/// The type of the iterators in this range.
using iterator = T;
/// The value type of the underlying iterator.
using value_type = typename std::iterator_traits<T>::value_type;
/**
* Default constructor. Create empty iterator_range.
*/
constexpr iterator_range() :
P(iterator{}, iterator{}) {
}
/**
* Create iterator range from two iterators.
*
* @param first_iterator Iterator to beginning or range.
* @param last_iterator Iterator to end or range.
*/
constexpr iterator_range(iterator&& first_iterator, iterator&& last_iterator) :
P(std::forward<iterator>(first_iterator),
std::forward<iterator>(last_iterator)) {
}
/// Return iterator to beginning of range.
constexpr iterator begin() const noexcept {
return this->first;
}
/// Return iterator to end of range.
constexpr iterator end() const noexcept {
return this->second;
}
/// Return iterator to beginning of range.
constexpr iterator cbegin() const noexcept {
return this->first;
}
/// Return iterator to end of range.
constexpr iterator cend() const noexcept {
return this->second;
}
/// Return true if this range is empty.
constexpr std::size_t empty() const noexcept {
return begin() == end();
}
/**
* Get element at the beginning of the range.
*
* @pre Range must not be empty.
*/
value_type front() const {
protozero_assert(!empty());
return *(this->first);
}
/**
* Advance beginning of range by one.
*
* @pre Range must not be empty.
*/
void drop_front() {
protozero_assert(!empty());
++this->first;
}
/**
* Swap the contents of this range with the other.
*
* @param other Other range to swap data with.
*/
void swap(iterator_range& other) noexcept {
using std::swap;
swap(this->first, other.first);
swap(this->second, other.second);
}
}; // struct iterator_range
/**
* Swap two iterator_ranges.
*
* @param lhs First range.
* @param rhs Second range.
*/
template <typename T>
inline void swap(iterator_range<T>& lhs, iterator_range<T>& rhs) noexcept {
lhs.swap(rhs);
}
/**
* A forward iterator used for accessing packed repeated fields of fixed
* length (fixed32, sfixed32, float, double).
*/
template <typename T>
class const_fixed_iterator {
/// Pointer to current iterator position
const char* m_data;
/// Pointer to end iterator position
const char* m_end;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
const_fixed_iterator() noexcept :
m_data(nullptr),
m_end(nullptr) {
}
const_fixed_iterator(const char* data, const char* end) noexcept :
m_data(data),
m_end(end) {
}
const_fixed_iterator(const const_fixed_iterator&) noexcept = default;
const_fixed_iterator(const_fixed_iterator&&) noexcept = default;
const_fixed_iterator& operator=(const const_fixed_iterator&) noexcept = default;
const_fixed_iterator& operator=(const_fixed_iterator&&) noexcept = default;
~const_fixed_iterator() noexcept = default;
value_type operator*() const {
value_type result;
std::memcpy(&result, m_data, sizeof(value_type));
#if PROTOZERO_BYTE_ORDER != PROTOZERO_LITTLE_ENDIAN
detail::byteswap_inplace(&result);
#endif
return result;
}
const_fixed_iterator& operator++() {
m_data += sizeof(value_type);
return *this;
}
const_fixed_iterator operator++(int) {
const const_fixed_iterator tmp(*this);
++(*this);
return tmp;
}
bool operator==(const const_fixed_iterator& rhs) const noexcept {
return m_data == rhs.m_data && m_end == rhs.m_end;
}
bool operator!=(const const_fixed_iterator& rhs) const noexcept {
return !(*this == rhs);
}
}; // class const_fixed_iterator
/**
* A forward iterator used for accessing packed repeated varint fields
* (int32, uint32, int64, uint64, bool, enum).
*/
template <typename T>
class const_varint_iterator {
protected:
/// Pointer to current iterator position
const char* m_data;
/// Pointer to end iterator position
const char* m_end;
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
const_varint_iterator() noexcept :
m_data(nullptr),
m_end(nullptr) {
}
const_varint_iterator(const char* data, const char* end) noexcept :
m_data(data),
m_end(end) {
}
const_varint_iterator(const const_varint_iterator&) noexcept = default;
const_varint_iterator(const_varint_iterator&&) noexcept = default;
const_varint_iterator& operator=(const const_varint_iterator&) noexcept = default;
const_varint_iterator& operator=(const_varint_iterator&&) noexcept = default;
~const_varint_iterator() noexcept = default;
value_type operator*() const {
const char* d = m_data; // will be thrown away
return static_cast<value_type>(decode_varint(&d, m_end));
}
const_varint_iterator& operator++() {
skip_varint(&m_data, m_end);
return *this;
}
const_varint_iterator operator++(int) {
const const_varint_iterator tmp(*this);
++(*this);
return tmp;
}
bool operator==(const const_varint_iterator& rhs) const noexcept {
return m_data == rhs.m_data && m_end == rhs.m_end;
}
bool operator!=(const const_varint_iterator& rhs) const noexcept {
return !(*this == rhs);
}
}; // class const_varint_iterator
/**
* A forward iterator used for accessing packed repeated svarint fields
* (sint32, sint64).
*/
template <typename T>
class const_svarint_iterator : public const_varint_iterator<T> {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = T;
using difference_type = std::ptrdiff_t;
using pointer = value_type*;
using reference = value_type&;
const_svarint_iterator() noexcept :
const_varint_iterator<T>() {
}
const_svarint_iterator(const char* data, const char* end) noexcept :
const_varint_iterator<T>(data, end) {
}
const_svarint_iterator(const const_svarint_iterator&) = default;
const_svarint_iterator(const_svarint_iterator&&) = default;
const_svarint_iterator& operator=(const const_svarint_iterator&) = default;
const_svarint_iterator& operator=(const_svarint_iterator&&) = default;
~const_svarint_iterator() = default;
value_type operator*() const {
const char* d = this->m_data; // will be thrown away
return static_cast<value_type>(decode_zigzag64(decode_varint(&d, this->m_end)));
}
const_svarint_iterator& operator++() {
skip_varint(&this->m_data, this->m_end);
return *this;
}
const_svarint_iterator operator++(int) {
const const_svarint_iterator tmp(*this);
++(*this);
return tmp;
}
}; // class const_svarint_iterator
} // end namespace protozero
#endif // PROTOZERO_ITERATORS_HPP