MongoDB C++ Driver 4.3.0
Loading...
Searching...
No Matches
iterators.hpp
Go to the documentation of this file.
1// Copyright 2009-present MongoDB, Inc.
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15#pragma once
16
17#include <bsoncxx/vector/iterators-fwd.hpp> // IWYU pragma: export
18
19//
20
21#include <cstdint>
22#include <iterator>
23
26
29
31
32namespace bsoncxx {
33namespace v_noabi {
34namespace vector {
35namespace iterators {
36
39template <typename Iterator>
40class packed_bit_element {
41 public:
43 using value_type = bool;
44
47
51
53 using iterator_category = std::random_access_iterator_tag;
54
56 using difference_type = std::ptrdiff_t;
57
60 constexpr reference operator*() const noexcept {
61 return {byte, bit};
62 }
63
65 constexpr bool operator==(packed_bit_element const& other) const noexcept {
66 return byte == other.byte && bit == other.bit;
67 }
68
70 constexpr bool operator!=(packed_bit_element const& other) const noexcept {
71 return byte != other.byte || bit != other.bit;
72 }
73
75 constexpr bool operator<(packed_bit_element const& other) const noexcept {
76 return byte < other.byte || (byte == other.byte && bit < other.bit);
77 }
78
80 constexpr bool operator<=(packed_bit_element const& other) const noexcept {
81 return byte < other.byte || (byte == other.byte && bit <= other.bit);
82 }
83
85 constexpr bool operator>(packed_bit_element const& other) const noexcept {
86 return byte > other.byte || (byte == other.byte && bit > other.bit);
87 }
88
90 constexpr bool operator>=(packed_bit_element const& other) const noexcept {
91 return byte > other.byte || (byte == other.byte && bit >= other.bit);
92 }
93
97 constexpr packed_bit_element operator+(difference_type const& other) const noexcept {
98 return {
99 byte + ((difference_type{bit} + other - ((difference_type{bit} + other) & 7)) / 8),
100 std::uint8_t((difference_type{bit} + other) & 7)};
101 }
102
106 constexpr packed_bit_element operator-(difference_type const& other) const noexcept {
107 return *this + (-other);
108 }
109
112 constexpr difference_type operator-(packed_bit_element const& other) const noexcept {
113 return {(byte - other.byte) * 8 + (difference_type{bit} - difference_type{other.bit})};
114 }
115
116 constexpr reference operator[](difference_type const& other) const noexcept {
117 return *(*this + other);
118 }
119
121 packed_bit_element& operator+=(difference_type const& other) noexcept {
122 return *this = *this + other;
123 }
124
126 packed_bit_element& operator-=(difference_type const& other) noexcept {
127 return *this = *this - other;
128 }
129
131 packed_bit_element& operator++() noexcept {
132 return *this += difference_type{1};
133 }
134
136 packed_bit_element& operator--() noexcept {
137 return *this -= difference_type{1};
138 }
139
141 packed_bit_element operator++(int) noexcept {
142 packed_bit_element prev = *this;
143 ++*this;
144 return prev;
145 }
146
148 packed_bit_element operator--(int) noexcept {
149 packed_bit_element prev = *this;
150 --*this;
151 return prev;
152 }
153
154 private:
155 friend class packed_bit_byte<Iterator>;
156 friend class accessor<formats::f_packed_bit>;
157 friend class accessor<formats::f_packed_bit const>;
158
159 constexpr packed_bit_element(Iterator byte_iter, std::uint8_t bit_index = 0) noexcept
160 : byte{byte_iter}, bit{bit_index} {}
161
162 Iterator byte;
163 std::uint8_t bit;
164};
165
168template <typename Iterator>
169class packed_bit_byte {
170 public:
172 using value_type = std::uint8_t;
173
177
181
183 using iterator_category = std::random_access_iterator_tag;
184
186 using difference_type = std::ptrdiff_t;
187
190 constexpr reference operator*() const noexcept {
191 return {byte, (byte + 1) == byte_end ? last_byte_mask : value_type{0xFFu}};
192 }
193
195 constexpr bool operator==(packed_bit_byte const& other) const noexcept {
196 return byte == other.byte;
197 }
198
200 constexpr bool operator!=(packed_bit_byte const& other) const noexcept {
201 return byte != other.byte;
202 }
203
205 constexpr bool operator<(packed_bit_byte const& other) const noexcept {
206 return byte < other.byte;
207 }
208
210 constexpr bool operator<=(packed_bit_byte const& other) const noexcept {
211 return byte <= other.byte;
212 }
213
215 constexpr bool operator>(packed_bit_byte const& other) const noexcept {
216 return byte > other.byte;
217 }
218
220 constexpr bool operator>=(packed_bit_byte const& other) const noexcept {
221 return byte >= other.byte;
222 }
223
227 constexpr packed_bit_byte operator+(difference_type const& other) const noexcept {
228 return {byte + other, byte_end, last_byte_mask};
229 }
230
234 constexpr packed_bit_byte operator-(difference_type const& other) const noexcept {
235 return *this + (-other);
236 }
237
240 constexpr difference_type operator-(packed_bit_byte const& other) const noexcept {
241 return {byte - other.byte};
242 }
243
244 constexpr reference operator[](difference_type const& other) const noexcept {
245 return *(*this + other);
246 }
247
249 packed_bit_byte& operator+=(difference_type const& other) noexcept {
250 return *this = *this + other;
251 }
252
254 packed_bit_byte& operator-=(difference_type const& other) noexcept {
255 return *this = *this - other;
256 }
257
259 packed_bit_byte& operator++() noexcept {
260 return *this += difference_type{1};
261 }
262
264 packed_bit_byte& operator--() noexcept {
265 return *this -= difference_type{1};
266 }
267
269 packed_bit_byte operator++(int) noexcept {
270 packed_bit_byte prev = *this;
271 ++*this;
272 return prev;
273 }
274
276 packed_bit_byte operator--(int) noexcept {
277 packed_bit_byte prev = *this;
278 --*this;
279 return prev;
280 }
281
282 private:
283 friend struct detail::format_traits<formats::f_packed_bit>;
284 friend struct detail::format_traits<formats::f_packed_bit const>;
285
286 constexpr packed_bit_byte(packed_bit_element<Iterator> element, packed_bit_element<Iterator> element_end)
287 : byte{element.byte},
288 byte_end{(element_end + 7u).byte},
289 last_byte_mask{value_type(0xFFu << (-element_end.bit & 7))} {}
290
291 constexpr packed_bit_byte(Iterator byte, Iterator byte_end, value_type last_byte_mask)
292 : byte{byte}, byte_end{byte_end}, last_byte_mask{last_byte_mask} {}
293
294 Iterator byte;
295 Iterator byte_end;
296 value_type last_byte_mask;
297};
298
299} // namespace iterators
300} // namespace vector
301} // namespace v_noabi
302} // namespace bsoncxx
303
305
Declares bsoncxx::v_noabi::vector::accessor.
The bsoncxx v_noabi macro guard postlude header.
The bsoncxx v_noabi macro guard prelude header.
Accessor for the contents of a valid BSON Binary Vector.
Definition accessor.hpp:53
Reference to a byte or partial byte within a vector of packed_bit elements. Allows access to each byt...
Definition elements.hpp:183
Reference to a single element in a packed_bit vector.
Definition elements.hpp:118
Iterator for bytes within a packed_bit vector.
Definition iterators.hpp:169
packed_bit_byte & operator++() noexcept
Pre-increment.
Definition iterators.hpp:259
packed_bit_byte & operator+=(difference_type const &other) noexcept
Advance this iterator forward by the indicated number of bytes.
Definition iterators.hpp:249
constexpr bool operator==(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:195
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
packed_bit_byte operator--(int) noexcept
Post-decrement.
Definition iterators.hpp:276
constexpr reference operator*() const noexcept
Dereference the byte iterator.
Definition iterators.hpp:190
elements::packed_bit_byte< Iterator > const reference
References to individual bytes are each bsoncxx::v_noabi::elements::packed_bit_byte,...
Definition iterators.hpp:176
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
constexpr bool operator!=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:200
constexpr bool operator>(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:215
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
packed_bit_byte & operator--() noexcept
Pre-decrement.
Definition iterators.hpp:264
constexpr bool operator<=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:210
std::random_access_iterator_tag iterator_category
This is a standard random-access iterator.
Definition iterators.hpp:183
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
constexpr bool operator>=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:220
std::uint8_t value_type
Values pointed to by this iterator are unsigned bytes.
Definition iterators.hpp:172
packed_bit_byte & operator-=(difference_type const &other) noexcept
Move this iterator backward by the indicated number of bytes.
Definition iterators.hpp:254
std::ptrdiff_t difference_type
A signed byte count.
Definition iterators.hpp:186
packed_bit_byte operator++(int) noexcept
Post-increment.
Definition iterators.hpp:269
constexpr bool operator<(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:205
Iterator for elements within a packed_bit vector.
Definition iterators.hpp:40
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
packed_bit_element operator--(int) noexcept
Post-decrement.
Definition iterators.hpp:148
std::ptrdiff_t difference_type
A signed bit count.
Definition iterators.hpp:56
constexpr bool operator==(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:65
packed_bit_element & operator+=(difference_type const &other) noexcept
Advance this iterator forward by the indicated number of bits.
Definition iterators.hpp:121
packed_bit_element & operator--() noexcept
Pre-decrement.
Definition iterators.hpp:136
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
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
elements::packed_bit_element< Iterator > const reference
References to individual bits are each bsoncxx::v_noabi::elements::packed_bit_element.
Definition iterators.hpp:46
std::random_access_iterator_tag iterator_category
This is a standard random-access iterator.
Definition iterators.hpp:53
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
packed_bit_element & operator-=(difference_type const &other) noexcept
Move this iterator backward by the indicated number of bits.
Definition iterators.hpp:126
packed_bit_element & operator++() noexcept
Pre-increment.
Definition iterators.hpp:131
bool value_type
Values pointed to by this iterator are single bits represented by bool.
Definition iterators.hpp:43
constexpr bool operator<=(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:80
constexpr bool operator>(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:85
constexpr bool operator!=(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:70
constexpr reference operator*() const noexcept
Dereference this bit iterator into a bit reference.
Definition iterators.hpp:60
packed_bit_element operator++(int) noexcept
Post-increment.
Definition iterators.hpp:141
constexpr bool operator<(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:75
constexpr bool operator>=(packed_bit_element const &other) const noexcept
Compare two bit iterators.
Definition iterators.hpp:90
For internal use only!
Declares entities in bsoncxx::v_noabi::vector::elements.
Declares entities in bsoncxx::v_noabi::vector::formats.
Forward declarations for bsoncxx::v_noabi::vector::iterators.
Declares supported BSON Binary Vector formats.
Declares iterator types for BSON Binary Vector.
Declarations related to the BSON Binary Vector subtype.
Declares entities whose ABI stability is NOT guaranteed.
The top-level namespace within which all bsoncxx library entities are declared.