xtd 1.0.0
Loading...
Searching...
No Matches
bit_array.hpp
Go to the documentation of this file.
1
3#pragma once
4
5#include "generic/list.hpp"
6#include "../iclonable.hpp"
7#include "../iequatable.hpp"
8#include "../array.hpp"
9#include "../boolean.hpp"
10#include "../byte.hpp"
11#include "../index.hpp"
12#include "../int32.hpp"
13#include "../math.hpp"
14#include "../npos.hpp"
15#include "../object.hpp"
16#include "../usize.hpp"
17#include "../types.hpp"
18#include <bitset>
19#include <initializer_list>
20#include <limits>
21
23namespace xtd {
25 namespace collections {
43 private:
44 struct boolean_ref {
45 boolean_ref() noexcept = default;
46 bool& get_boolean_ref(bool value, xtd::usize index) noexcept;
47 const bool& get_boolean_ref(bool value, xtd::usize index) const noexcept;
48 void from_boolean(bit_array& parent) noexcept;
49
50 inline static constexpr xtd::usize npos = xtd::npos;
51 mutable xtd::usize index = npos;
52 mutable bool value = false;
53 };
54
55 public:
57
60 using value_type = bool;
74 using const_pointer = const value_type*;
76
78
84 explicit bit_array(xtd::usize length) noexcept;
85
91 bit_array(xtd::usize length, bool defaultValue) noexcept;
92
96 bit_array(std::initializer_list<bool> il) noexcept;
97
101 bit_array(const xtd::array<bool>& values) noexcept;
102
107 bit_array(const xtd::array<xtd::byte>& values) noexcept;
108
113 bit_array(const xtd::array<int32>& values) noexcept;
114
118 template<xtd::usize length>
119 bit_array(const std::bitset<length>& bit_set) noexcept : bit_array(length) {
120 for (auto index = xtd::usize {0}; index < length; ++index)
121 set(index, bit_set.test(index));
122 }
123
127 bit_array(const std::vector<bool>& booleans) noexcept;
129
131 bit_array(bit_array&& bits) = default;
132 bit_array(const bit_array& bits) = default;
133 auto operator=(bit_array&&) -> bit_array& = default;
134 auto operator=(const bit_array&) -> bit_array& = default;
136
138
142 template<xtd::usize length>
143 [[nodiscard]] auto bits() const noexcept -> std::bitset<length> {
144 auto result = std::bitset<length> {};
145 auto bits_count = xtd::math::min(length, count());
146 for (auto index = xtd::usize {0}; index < bits_count; ++index)
147 result[index] = self_[index];
148 return result;
149 }
150
153 [[nodiscard]] auto bits() const noexcept -> std::vector<bool> {
154 auto result = std::vector<bool> {};
155 for (auto index = xtd::usize {0}; index < count(); ++index)
156 result[index] = self_[index];
157 return result;
158 }
159
163 [[nodiscard]] auto count() const noexcept -> xtd::usize override;
164
168 [[nodiscard]] auto length() const noexcept -> xtd::usize;
172 auto length(xtd::usize value) -> void;
174
176
184 auto and_(const bit_array& value) -> const bit_array&;
185
188 [[nodiscard]] xtd::uptr<xtd::object> clone() const override;
189
194 auto copy_to(xtd::array<bool>& array, xtd::usize index) const -> void override;
195
199 [[nodiscard]] auto equals(const bit_array& value) const noexcept -> bool override;
200
204 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override;
205
211 [[nodiscard]] auto get(xtd::usize index) const -> bool;
212
218 [[nodiscard]] auto get(xtd::usize index) -> bool&;
219
222 [[nodiscard]] xtd::collections::generic::enumerator<bool> get_enumerator() const override;
223
226 [[nodiscard]] auto has_all_set() const noexcept -> bool;
227
230 [[nodiscard]] auto has_any_set() const noexcept -> bool;
231
236 auto left_shift(xtd::usize count) noexcept -> bit_array&;
237
241 auto not_() -> const bit_array&;
242
249 auto or_(const bit_array& value) -> const bit_array&;
250
255 auto right_shift(xtd::usize count) noexcept -> bit_array&;
256
262 auto set(xtd::usize index, bool value) -> void;
263
267 auto set_all(bool value) -> void;
268
271 [[nodiscard]] auto to_string() const noexcept -> xtd::string override;
272
279 auto xor_(const bit_array& value) -> const bit_array&;
281
283
289 auto operator [](xtd::usize index) const -> const bool&;
294 auto operator [](xtd::usize index) -> bool&;
299 auto operator [](const xtd::index& index) const -> const bool&;
304 auto operator [](const xtd::index& index) -> bool&;
305
310 auto operator ()(xtd::usize index) const -> const bool&;
315 auto operator ()(xtd::usize index) -> bool&;
320 auto operator ()(const xtd::index& index) const -> const bool&;
325 auto operator ()(const xtd::index& index) -> bool&;
326
333 auto operator &(const bit_array& value) const -> bit_array;
340 auto operator &=(const bit_array& value) -> bit_array&;
341
348 auto operator |(const bit_array& value) const -> bit_array;
355 auto operator |=(const bit_array& value) -> bit_array&;
356
363 auto operator ^(const bit_array& value) const -> bit_array;
370 auto operator ^=(const bit_array& value) -> bit_array&;
371
375 auto operator ~() const -> bit_array;
376
377 using xtd::collections::generic::icollection<bool>::operator >>;
382 auto operator >>(xtd::usize count) const noexcept -> bit_array;
383
388 auto operator >>=(xtd::usize count) noexcept -> bit_array&;
389
390 using xtd::collections::generic::icollection<bool>::operator <<;
395 auto operator <<(xtd::usize count) const noexcept -> bit_array;
396
401 auto operator <<=(xtd::usize count) noexcept -> bit_array&;
403
404 private:
405 auto is_read_only() const noexcept -> bool override;
406 auto is_synchronized() const noexcept -> bool override;
407 const object& sync_root() const noexcept override;
408
409 auto add(const bool&) -> void override;
410 auto clear() -> void override;
411 auto contains(const bool&) const noexcept -> bool override;
412 auto remove(const bool&) -> bool override;
413
414 auto flush() const noexcept -> void;
415 auto get_int32_array_length_from_bit_length(xtd::usize n) const noexcept -> xtd::usize;
416 auto get_list_length(xtd::usize length_) const noexcept -> xtd::usize;
417 auto get_list_position(xtd::usize index) const noexcept -> xtd::usize;
418 auto get_bit_position(xtd::usize index) const noexcept -> xtd::usize;
419 auto get_bit_value(xtd::usize index) const noexcept -> bool;
420 auto set_bit_value(xtd::usize index, bool value) noexcept -> void;
421
422 static constexpr xtd::usize bits_per_byte = 8;
423 static constexpr xtd::usize bits_per_int32 = 32;
424 static constexpr xtd::usize bytes_per_int32 = 4;
425 static constexpr xtd::usize bit_shift_per_int32 = 5;
426 mutable boolean_ref value_ref_;
427 xtd::collections::generic::list<xtd::int32> bit_array_;
428 xtd::usize length_ = 0;
429 };
430 }
431}
432
434template<xtd::usize size_>
435inline auto xtd::linq::enumerable::as_enumerable(const std::bitset<size_>& source) noexcept -> xtd::collections::generic::enumerable_generator<bool> {
436 for (const auto& item : xtd::collections::bit_array {source})
437 co_yield item;
438}
439template<xtd::usize size_>
441 for (const auto& item : xtd::collections::bit_array {source})
442 co_yield item;
443}
444
445template<xtd::usize size_>
446auto xtd::linq::enumerable::from(const std::bitset<size_>& source) noexcept -> xtd::collections::generic::enumerable_generator<bool> {
447 return as_enumerable(source);
448}
449template<xtd::usize size_>
450auto xtd::linq::enumerable::from(std::bitset<size_>& source) noexcept -> xtd::collections::generic::enumerable_generator<bool> {
451 return as_enumerable(source);
452}
Contains xtd::array class.
Contains xtd::boolean type.
Contains xtd::byte type.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Manages a compact array of bit values, which are represented as booleans, where true indicates that t...
Definition bit_array.hpp:42
bit_array(const xtd::array< xtd::byte > &values) noexcept
Initializes a new instance of the xtd::collections::bit_array class that contains bit values copied f...
typename xtd::collections::generic::list< int32 > base_type
Represents the list base type.
Definition bit_array.hpp:62
auto left_shift(xtd::usize count) noexcept -> bit_array &
Shifts all the bit values of the current xtd::collections::bit_array to the left on count bits.
const value_type * const_pointer
Represents the const pointer of list value type.
Definition bit_array.hpp:74
bit_array(std::initializer_list< bool > il) noexcept
Initializes a new instance of the xtd::collections::bit_array class that contains bit values copied f...
auto length() const noexcept -> xtd::usize
Gets the number of elements contained in the xtd::collections::bit_array.
xtd::usize size_type
Represents the list size type (usually xtd::usize).
Definition bit_array.hpp:64
xtd::ptrdiff difference_type
Represents the list difference type (usually xtd::ptrdiff).
Definition bit_array.hpp:66
auto not_() -> const bit_array &
Inverts all the bit values in the current xtd::collections::bit_array, so that elements set to true a...
auto and_(const bit_array &value) -> const bit_array &
Performs the bitwise AND operation on the elements in the current xtd::collections::bit_array against...
value_type & reference
Represents the reference of list value type.
Definition bit_array.hpp:68
auto right_shift(xtd::usize count) noexcept -> bit_array &
Shifts all the bit values of the current xtd::collections::bit_array to the right on count bits.
auto set_all(bool value) -> void
Sets all bits in the xtd::collections::bit_array to the specified value.
auto or_(const bit_array &value) -> const bit_array &
Performs the bitwise OR operation on the elements in the current xtd::collections::bit_array against ...
const value_type & const_reference
Represents the const reference of list value type.
Definition bit_array.hpp:70
auto set(xtd::usize index, bool value) -> void
Sets the value of the bit at a specific position in the xtd::collections::bit_array.
bit_array(xtd::usize length) noexcept
Initializes a new instance of the xtd::collections::bit_array class that can hold the specified numbe...
bit_array(const std::vector< bool > &booleans) noexcept
Initializes a new instance of the xtd::collections::bit_array class that contains bit values copied f...
auto bits() const noexcept -> std::vector< bool >
Returns a std::vector<bool> object containing the Booleans contained in the current xtd::collections:...
Definition bit_array.hpp:153
auto equals(const bit_array &value) const noexcept -> bool override
Determines whether this instance of xtd::collections::bit_array and a specified object,...
bit_array(const xtd::array< bool > &values) noexcept
Initializes a new instance of the xtd::collections::bit_array class that contains bit values copied f...
xtd::collections::generic::enumerator< bool > get_enumerator() const override
Returns an enumerator that iterates through a collection.
bit_array(xtd::usize length, bool defaultValue) noexcept
Initializes a new instance of the xtd::collections::bit_array class that can hold the specified numbe...
value_type * pointer
Represents the pointer of list value type.
Definition bit_array.hpp:72
bit_array(const std::bitset< length > &bit_set) noexcept
Initializes a new instance of the xtd::collections::bit_array class that contains bit values copied f...
Definition bit_array.hpp:119
auto has_any_set() const noexcept -> bool
Determines whether any bit in the xtd::collections::bit_array is set to true.
auto xor_(const bit_array &value) -> const bit_array &
Performs the bitwise exclusive OR operation on the elements in the current xtd::collections::bit_arra...
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
auto has_all_set() const noexcept -> bool
Determines whether all bits in the xtd::collections::bit_array are set to true.
bit_array(const xtd::array< int32 > &values) noexcept
Initializes a new instance of the xtd::collections::bit_array class that contains bit values copied f...
bool value_type
Represents the list value type.
Definition bit_array.hpp:60
auto count() const noexcept -> xtd::usize override
Gets the number of elements contained in the xtd::collections::bit_array.
auto copy_to(xtd::array< bool > &array, xtd::usize index) const -> void override
Copies the elements of the xtd::collections::bit_array to an xtd::array, starting at a particular xtd...
xtd::uptr< xtd::object > clone() const override
Creates a new object that is a copy of the current instance.
auto bits() const noexcept -> std::bitset< length >
Returns a std::bitset object containing the Booleans contained in the current xtd::collections::bit_a...
Definition bit_array.hpp:143
auto get(xtd::usize index) const -> bool
Gets the value of the bit at a specific position in the xtd::collections::bit_array.
Represents an enumerable generator that supports deferred, lazy iteration over a collection of a spec...
Definition enumerable_generator.hpp:44
typename xtd::linq::enumerable::list< type_t > list
Definition enumerable.hpp:47
Defines methods to manipulate generic collections.
Definition icollection.hpp:45
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:82
Supports cloning, which creates a new instance of a class with the same value as an existing instance...
Definition iclonable.hpp:21
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
static auto from(source_t &&source) noexcept
Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
static auto as_enumerable(source_t &&source) noexcept
Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
static auto min(xtd::byte a, xtd::byte b) noexcept -> xtd::byte
Returns the smaller of two 8-bit unsigned integers.
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:40
generic::icollection< xtd::any_object > icollection
Defines size, enumerators, and synchronization methods for all nongeneric collections.
Definition icollection.hpp:32
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
#define core_export_
Define shared library export.
Definition core_export.hpp:13
constexpr auto npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
xtd::unique_ptr_object< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
std::int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:25
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
Contains xtd::iclonable interface.
Contains xtd::iequatable interface.
Contains xtd::index struct.
Contains xtd::int32 type.
Contains xtd::collections::generic::list <type_t> class.
Contains xtd::math class.
The xtd::collections::generic namespace contains interfaces and classes that define generic collectio...
Definition comparer.hpp:16
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
Provides classes and interfaces that support queries that use Language-Integrated Query (LINQ).
Definition enumerable_.hpp:58
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
Contains xtd::npos constant.
Contains xtd::object class.
Contains xtd fundamental types.
Contains xtd::usize type.