Loading...
Searching...
No Matches
iterators.hpp
Go to the documentation of this file.
// Copyright 2009-present MongoDB, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#pragma once
#include <bsoncxx/vector/iterators-fwd.hpp> // IWYU pragma: export
//
#include <cstdint>
#include <iterator>
#include <bsoncxx/vector/accessor-fwd.hpp>
#include <bsoncxx/vector/detail-fwd.hpp>
#include <bsoncxx/vector/elements.hpp>
#include <bsoncxx/vector/formats.hpp>
#include <bsoncxx/config/prelude.hpp>
namespace bsoncxx {
namespace v_noabi {
namespace vector {
namespace iterators {
template <typename Iterator>
class packed_bit_element {
public:
using value_type = bool;
using reference = elements::packed_bit_element<Iterator> const;
using pointer = elements::packed_bit_element<Iterator> const*;
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
constexpr reference operator*() const noexcept {
return {byte, bit};
}
constexpr bool operator==(packed_bit_element const& other) const noexcept {
return byte == other.byte && bit == other.bit;
}
constexpr bool operator!=(packed_bit_element const& other) const noexcept {
return byte != other.byte || bit != other.bit;
}
constexpr bool operator<(packed_bit_element const& other) const noexcept {
return byte < other.byte || (byte == other.byte && bit < other.bit);
}
constexpr bool operator<=(packed_bit_element const& other) const noexcept {
return byte < other.byte || (byte == other.byte && bit <= other.bit);
}
constexpr bool operator>(packed_bit_element const& other) const noexcept {
return byte > other.byte || (byte == other.byte && bit > other.bit);
}
constexpr bool operator>=(packed_bit_element const& other) const noexcept {
return byte > other.byte || (byte == other.byte && bit >= other.bit);
}
constexpr packed_bit_element operator+(difference_type const& other) const noexcept {
return {
byte + ((difference_type{bit} + other - ((difference_type{bit} + other) & 7)) / 8),
std::uint8_t((difference_type{bit} + other) & 7)};
}
constexpr packed_bit_element operator-(difference_type const& other) const noexcept {
return *this + (-other);
}
constexpr difference_type operator-(packed_bit_element const& other) const noexcept {
return {(byte - other.byte) * 8 + (difference_type{bit} - difference_type{other.bit})};
}
constexpr reference operator[](difference_type const& other) const noexcept {
return *(*this + other);
}
packed_bit_element& operator+=(difference_type const& other) noexcept {
return *this = *this + other;
}
packed_bit_element& operator-=(difference_type const& other) noexcept {
return *this = *this - other;
}
packed_bit_element& operator++() noexcept {
return *this += difference_type{1};
}
packed_bit_element& operator--() noexcept {
return *this -= difference_type{1};
}
packed_bit_element operator++(int) noexcept {
packed_bit_element prev = *this;
++*this;
return prev;
}
packed_bit_element operator--(int) noexcept {
packed_bit_element prev = *this;
--*this;
return prev;
}
private:
friend class packed_bit_byte<Iterator>;
friend class accessor<formats::f_packed_bit>;
friend class accessor<formats::f_packed_bit const>;
constexpr packed_bit_element(Iterator byte_iter, std::uint8_t bit_index = 0) noexcept
: byte{byte_iter}, bit{bit_index} {}
Iterator byte;
std::uint8_t bit;
};
template <typename Iterator>
class packed_bit_byte {
public:
using value_type = std::uint8_t;
using reference = elements::packed_bit_byte<Iterator> const;
using pointer = elements::packed_bit_byte<Iterator> const*;
using iterator_category = std::random_access_iterator_tag;
using difference_type = std::ptrdiff_t;
constexpr reference operator*() const noexcept {
return {byte, (byte + 1) == byte_end ? last_byte_mask : value_type{0xFFu}};
}
constexpr bool operator==(packed_bit_byte const& other) const noexcept {
return byte == other.byte;
}
constexpr bool operator!=(packed_bit_byte const& other) const noexcept {
return byte != other.byte;
}
constexpr bool operator<(packed_bit_byte const& other) const noexcept {
return byte < other.byte;
}
constexpr bool operator<=(packed_bit_byte const& other) const noexcept {
return byte <= other.byte;
}
constexpr bool operator>(packed_bit_byte const& other) const noexcept {
return byte > other.byte;
}
constexpr bool operator>=(packed_bit_byte const& other) const noexcept {
return byte >= other.byte;
}
constexpr packed_bit_byte operator+(difference_type const& other) const noexcept {
return {byte + other, byte_end, last_byte_mask};
}
constexpr packed_bit_byte operator-(difference_type const& other) const noexcept {
return *this + (-other);
}
constexpr difference_type operator-(packed_bit_byte const& other) const noexcept {
return {byte - other.byte};
}
constexpr reference operator[](difference_type const& other) const noexcept {
return *(*this + other);
}
packed_bit_byte& operator+=(difference_type const& other) noexcept {
return *this = *this + other;
}
packed_bit_byte& operator-=(difference_type const& other) noexcept {
return *this = *this - other;
}
packed_bit_byte& operator++() noexcept {
return *this += difference_type{1};
}
packed_bit_byte& operator--() noexcept {
return *this -= difference_type{1};
}
packed_bit_byte operator++(int) noexcept {
packed_bit_byte prev = *this;
++*this;
return prev;
}
packed_bit_byte operator--(int) noexcept {
packed_bit_byte prev = *this;
--*this;
return prev;
}
private:
friend struct detail::format_traits<formats::f_packed_bit>;
friend struct detail::format_traits<formats::f_packed_bit const>;
constexpr packed_bit_byte(packed_bit_element<Iterator> element, packed_bit_element<Iterator> element_end)
: byte{element.byte},
byte_end{(element_end + 7u).byte},
last_byte_mask{value_type(0xFFu << (-element_end.bit & 7))} {}
constexpr packed_bit_byte(Iterator byte, Iterator byte_end, value_type last_byte_mask)
: byte{byte}, byte_end{byte_end}, last_byte_mask{last_byte_mask} {}
Iterator byte;
Iterator byte_end;
value_type last_byte_mask;
};
} // namespace iterators
} // namespace vector
} // namespace v_noabi
} // namespace bsoncxx
#include <bsoncxx/config/postlude.hpp>
accessor-fwd.hpp
Declares bsoncxx::v_noabi::vector::accessor.
postlude.hpp
The bsoncxx v_noabi macro guard postlude header.
prelude.hpp
The bsoncxx v_noabi macro guard prelude header.
bsoncxx::v_noabi::vector::accessor
Accessor for the contents of a valid BSON Binary Vector.
Definition accessor.hpp:53
bsoncxx::v_noabi::vector::elements::packed_bit_byte
Reference to a byte or partial byte within a vector of packed_bit elements. Allows access to each byt...
Definition elements.hpp:183
bsoncxx::v_noabi::vector::elements::packed_bit_element
Reference to a single element in a packed_bit vector.
Definition elements.hpp:118
bsoncxx::v_noabi::vector::iterators::packed_bit_byte
Iterator for bytes within a packed_bit vector.
Definition iterators.hpp:169
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator++
packed_bit_byte & operator++() noexcept
Pre-increment.
Definition iterators.hpp:259
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator+=
packed_bit_byte & operator+=(difference_type const &other) noexcept
Advance this iterator forward by the indicated number of bytes.
Definition iterators.hpp:249
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator==
constexpr bool operator==(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:195
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator-
constexpr packed_bit_byte operator-(difference_type const &other) const noexcept
Calculate a signed subtraction of a ptrdiff_t from this iterator, moving it backward or forward the i...
Definition iterators.hpp:234
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator--
packed_bit_byte operator--(int) noexcept
Post-decrement.
Definition iterators.hpp:276
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator*
constexpr reference operator*() const noexcept
Dereference the byte iterator.
Definition iterators.hpp:190
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::reference
elements::packed_bit_byte< Iterator > const reference
References to individual bytes are each bsoncxx::v_noabi::elements::packed_bit_byte,...
Definition iterators.hpp:176
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator-
constexpr difference_type operator-(packed_bit_byte const &other) const noexcept
Calculate the difference in position between two byte iterators If the two iterators do not point int...
Definition iterators.hpp:240
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator!=
constexpr bool operator!=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:200
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator>
constexpr bool operator>(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:215
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::pointer
elements::packed_bit_byte< Iterator > const * pointer
Element pointers aren't really a useful concept here, but this is defined for compatibility with stan...
Definition iterators.hpp:180
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator--
packed_bit_byte & operator--() noexcept
Pre-decrement.
Definition iterators.hpp:264
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator<=
constexpr bool operator<=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:210
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::iterator_category
std::random_access_iterator_tag iterator_category
This is a standard random-access iterator.
Definition iterators.hpp:183
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator+
constexpr packed_bit_byte operator+(difference_type const &other) const noexcept
Calculate a signed addition of this iterator with a ptrdiff_t, moving it forward or backward the indi...
Definition iterators.hpp:227
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator>=
constexpr bool operator>=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:220
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::value_type
std::uint8_t value_type
Values pointed to by this iterator are unsigned bytes.
Definition iterators.hpp:172
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator-=
packed_bit_byte & operator-=(difference_type const &other) noexcept
Move this iterator backward by the indicated number of bytes.
Definition iterators.hpp:254
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::difference_type
std::ptrdiff_t difference_type
A signed byte count.
Definition iterators.hpp:186
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator++
packed_bit_byte operator++(int) noexcept
Post-increment.
Definition iterators.hpp:269
bsoncxx::v_noabi::vector::iterators::packed_bit_byte::operator<
constexpr bool operator<(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:205
bsoncxx::v_noabi::vector::iterators::packed_bit_element
Iterator for elements within a packed_bit vector.
Definition iterators.hpp:40
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator-
constexpr difference_type operator-(packed_bit_element const &other) const noexcept
Calculate the difference in position between two bit iterators If the two iterators do not point into...
Definition iterators.hpp:112
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator--
packed_bit_element operator--(int) noexcept
Post-decrement.
Definition iterators.hpp:148
bsoncxx::v_noabi::vector::iterators::packed_bit_element::difference_type
std::ptrdiff_t difference_type
A signed bit count.
Definition iterators.hpp:56
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator==
constexpr bool operator==(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:65
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator+=
packed_bit_element & operator+=(difference_type const &other) noexcept
Advance this iterator forward by the indicated number of bits.
Definition iterators.hpp:121
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator--
packed_bit_element & operator--() noexcept
Pre-decrement.
Definition iterators.hpp:136
bsoncxx::v_noabi::vector::iterators::packed_bit_element::pointer
elements::packed_bit_element< Iterator > const * pointer
Element pointers aren't really a useful concept here, but this is defined for compatibility with stan...
Definition iterators.hpp:50
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator-
constexpr packed_bit_element operator-(difference_type const &other) const noexcept
Calculate a signed subtraction of a ptrdiff_t from this iterator, moving it backward or forward the i...
Definition iterators.hpp:106
bsoncxx::v_noabi::vector::iterators::packed_bit_element::reference
elements::packed_bit_element< Iterator > const reference
References to individual bits are each bsoncxx::v_noabi::elements::packed_bit_element.
Definition iterators.hpp:46
bsoncxx::v_noabi::vector::iterators::packed_bit_element::iterator_category
std::random_access_iterator_tag iterator_category
This is a standard random-access iterator.
Definition iterators.hpp:53
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator+
constexpr packed_bit_element operator+(difference_type const &other) const noexcept
Calculate a signed addition of this iterator with a ptrdiff_t, moving it forward or backward the indi...
Definition iterators.hpp:97
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator-=
packed_bit_element & operator-=(difference_type const &other) noexcept
Move this iterator backward by the indicated number of bits.
Definition iterators.hpp:126
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator++
packed_bit_element & operator++() noexcept
Pre-increment.
Definition iterators.hpp:131
bsoncxx::v_noabi::vector::iterators::packed_bit_element::value_type
bool value_type
Values pointed to by this iterator are single bits represented by bool.
Definition iterators.hpp:43
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator<=
constexpr bool operator<=(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:80
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator>
constexpr bool operator>(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:85
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator!=
constexpr bool operator!=(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:70
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator*
constexpr reference operator*() const noexcept
Dereference this bit iterator into a bit reference.
Definition iterators.hpp:60
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator++
packed_bit_element operator++(int) noexcept
Post-increment.
Definition iterators.hpp:141
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator<
constexpr bool operator<(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:75
bsoncxx::v_noabi::vector::iterators::packed_bit_element::operator>=
constexpr bool operator>=(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:90
detail-fwd.hpp
For internal use only!
elements.hpp
Declares entities in bsoncxx::v_noabi::vector::elements.
formats.hpp
Declares entities in bsoncxx::v_noabi::vector::formats.
iterators-fwd.hpp
Forward declarations for bsoncxx::v_noabi::vector::iterators.
bsoncxx::v_noabi::vector::formats
Declares supported BSON Binary Vector formats.
bsoncxx::v_noabi::vector::iterators
Declares iterator types for BSON Binary Vector.
bsoncxx::v_noabi::vector
Declarations related to the BSON Binary Vector subtype.
bsoncxx::v_noabi
Declares entities whose ABI stability is NOT guaranteed.
bsoncxx
The top-level namespace within which all bsoncxx library entities are declared.