MongoDB C++ Driver 4.1.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
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
117 packed_bit_element& operator+=(difference_type const& other) noexcept {
118 return *this = *this + other;
119 }
120
122 packed_bit_element& operator-=(difference_type const& other) noexcept {
123 return *this = *this - other;
124 }
125
127 packed_bit_element& operator++() noexcept {
128 return *this += difference_type{1};
129 }
130
132 packed_bit_element& operator--() noexcept {
133 return *this -= difference_type{1};
134 }
135
137 packed_bit_element operator++(int) noexcept {
138 packed_bit_element prev = *this;
139 ++*this;
140 return prev;
141 }
142
144 packed_bit_element operator--(int) noexcept {
145 packed_bit_element prev = *this;
146 --*this;
147 return prev;
148 }
149
150 private:
151 friend class packed_bit_byte<Iterator>;
152 friend class accessor<formats::f_packed_bit>;
153 friend class accessor<formats::f_packed_bit const>;
154
155 constexpr packed_bit_element(Iterator byte_iter, std::uint8_t bit_index = 0) noexcept
156 : byte{byte_iter}, bit{bit_index} {}
157
158 Iterator byte;
159 std::uint8_t bit;
160};
161
164template <typename Iterator>
165class packed_bit_byte {
166 public:
168 using value_type = std::uint8_t;
169
173
177
179 using iterator_category = std::random_access_iterator_tag;
180
182 using difference_type = std::ptrdiff_t;
183
186 constexpr reference operator*() const noexcept {
187 return {byte, (byte + 1) == byte_end ? last_byte_mask : value_type{0xFFu}};
188 }
189
191 constexpr bool operator==(packed_bit_byte const& other) const noexcept {
192 return byte == other.byte;
193 }
194
196 constexpr bool operator!=(packed_bit_byte const& other) const noexcept {
197 return byte != other.byte;
198 }
199
201 constexpr bool operator<(packed_bit_byte const& other) const noexcept {
202 return byte < other.byte;
203 }
204
206 constexpr bool operator<=(packed_bit_byte const& other) const noexcept {
207 return byte <= other.byte;
208 }
209
211 constexpr bool operator>(packed_bit_byte const& other) const noexcept {
212 return byte > other.byte;
213 }
214
216 constexpr bool operator>=(packed_bit_byte const& other) const noexcept {
217 return byte >= other.byte;
218 }
219
223 constexpr packed_bit_byte operator+(difference_type const& other) const noexcept {
224 return {byte + other, byte_end, last_byte_mask};
225 }
226
230 constexpr packed_bit_byte operator-(difference_type const& other) const noexcept {
231 return *this + (-other);
232 }
233
236 constexpr difference_type operator-(packed_bit_byte const& other) const noexcept {
237 return {byte - other.byte};
238 }
239
241 packed_bit_byte& operator+=(difference_type const& other) noexcept {
242 return *this = *this + other;
243 }
244
246 packed_bit_byte& operator-=(difference_type const& other) noexcept {
247 return *this = *this - other;
248 }
249
251 packed_bit_byte& operator++() noexcept {
252 return *this += difference_type{1};
253 }
254
256 packed_bit_byte& operator--() noexcept {
257 return *this -= difference_type{1};
258 }
259
261 packed_bit_byte operator++(int) noexcept {
262 packed_bit_byte prev = *this;
263 ++*this;
264 return prev;
265 }
266
268 packed_bit_byte operator--(int) noexcept {
269 packed_bit_byte prev = *this;
270 --*this;
271 return prev;
272 }
273
274 private:
275 friend struct detail::format_traits<formats::f_packed_bit>;
276 friend struct detail::format_traits<formats::f_packed_bit const>;
277
278 constexpr packed_bit_byte(packed_bit_element<Iterator> element, packed_bit_element<Iterator> element_end)
279 : byte{element.byte},
280 byte_end{(element_end + 7u).byte},
281 last_byte_mask{value_type(0xFFu << (-element_end.bit & 7u))} {}
282
283 constexpr packed_bit_byte(Iterator byte, Iterator byte_end, value_type last_byte_mask)
284 : byte{byte}, byte_end{byte_end}, last_byte_mask{last_byte_mask} {}
285
286 Iterator byte;
287 Iterator byte_end;
288 value_type last_byte_mask;
289};
290
291} // namespace iterators
292} // namespace vector
293} // namespace v_noabi
294} // namespace bsoncxx
295
297
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:169
Reference to a single element in a packed_bit vector.
Definition elements.hpp:104
Iterator for bytes within a packed_bit vector.
Definition iterators.hpp:165
packed_bit_byte & operator++() noexcept
Pre-increment.
Definition iterators.hpp:251
packed_bit_byte & operator+=(difference_type const &other) noexcept
Advance this iterator forward by the indicated number of bytes.
Definition iterators.hpp:241
constexpr bool operator==(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:191
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:230
packed_bit_byte operator--(int) noexcept
Post-decrement.
Definition iterators.hpp:268
constexpr reference operator*() const noexcept
Dereference the byte iterator.
Definition iterators.hpp:186
elements::packed_bit_byte< std::uint8_t * > const reference
Definition iterators.hpp:172
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:236
constexpr bool operator!=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:196
constexpr bool operator>(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:211
elements::packed_bit_byte< std::uint8_t * > const * pointer
Definition iterators.hpp:176
packed_bit_byte & operator--() noexcept
Pre-decrement.
Definition iterators.hpp:256
constexpr bool operator<=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:206
std::random_access_iterator_tag iterator_category
Definition iterators.hpp:179
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:223
constexpr bool operator>=(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:216
packed_bit_byte & operator-=(difference_type const &other) noexcept
Move this iterator backward by the indicated number of bytes.
Definition iterators.hpp:246
std::ptrdiff_t difference_type
Definition iterators.hpp:182
packed_bit_byte operator++(int) noexcept
Post-increment.
Definition iterators.hpp:261
constexpr bool operator<(packed_bit_byte const &other) const noexcept
Compare two byte iterators.
Definition iterators.hpp:201
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:144
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:117
packed_bit_element & operator--() noexcept
Pre-decrement.
Definition iterators.hpp:132
elements::packed_bit_element< std::uint8_t * > const * pointer
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< std::uint8_t * > const reference
Definition iterators.hpp:46
std::random_access_iterator_tag iterator_category
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:122
packed_bit_element & operator++() noexcept
Pre-increment.
Definition iterators.hpp:127
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:137
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.