xtd 0.2.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 "../int32.hpp"
12#include "../math.hpp"
13#include "../npos.hpp"
14#include "../object.hpp"
15#include "../size.hpp"
16#include "../types.hpp"
17#include <bitset>
18#include <initializer_list>
19#include <limits>
20
22namespace xtd {
24 namespace collections {
41 class core_export_ bit_array : public xtd::object, public xtd::iequatable<bit_array>, public xtd::collections::generic::icollection<bool>, public xtd::iclonable {
42 private:
43 struct boolean_ref {
44 boolean_ref() noexcept = default;
45 bool& get_boolean_ref(bool value, xtd::size index) noexcept;
46 const bool& get_boolean_ref(bool value, xtd::size index) const noexcept;
47 void from_boolean(bit_array& parent) noexcept;
48
49 inline static constexpr xtd::size npos = xtd::npos;
50 inline static constexpr xtd::size bpos = xtd::bpos;
51 inline static constexpr xtd::size epos = xtd::epos;
52 mutable xtd::size index = npos;
53 mutable bool value = false;
54 };
55
56 public:
58
61 using value_type = bool;
75 using const_pointer = const value_type*;
77
79
85 explicit bit_array(xtd::size length) noexcept;
86
92 bit_array(xtd::size length, bool defaultValue) noexcept;
93
97 bit_array(std::initializer_list<bool> il) noexcept;
98
102 bit_array(const xtd::array<bool>& values) noexcept;
103
108 bit_array(const xtd::array<xtd::byte>& values) noexcept;
109
114 bit_array(const xtd::array<int32>& values) noexcept;
115
119 template<xtd::size length>
120 bit_array(const std::bitset<length>& bit_set) noexcept : bit_array(length) {
121 for (auto index = xtd::size {0}; index < length; ++index)
122 set(index, bit_set.test(index));
123 }
124
128 bit_array(const std::vector<bool>& booleans) noexcept;
130
132 bit_array(bit_array&& bits) = default;
133 bit_array(const bit_array& bits) = default;
134 auto operator=(bit_array&&) -> bit_array& = default;
135 auto operator=(const bit_array&) -> bit_array& = default;
137
139
143 template<xtd::size length>
144 auto bits() const noexcept -> std::bitset<length> {
145 auto result = std::bitset<length> {};
146 auto bits_count = xtd::math::min(length, count());
147 for (auto index = xtd::size {0}; index < bits_count; ++index)
148 result[index] = self_[index];
149 return result;
150 }
151
154 auto bits() const noexcept -> std::vector<bool> {
155 auto result = std::vector<bool> {};
156 for (auto index = xtd::size {0}; index < count(); ++index)
157 result[index] = self_[index];
158 return result;
159 }
160
164 auto count() const noexcept -> xtd::size override;
165
169 auto length() const noexcept -> xtd::size;
173 auto length(xtd::size value) -> void;
175
177
185 auto and_(const bit_array& value) -> const bit_array&;
186
189 xtd::uptr<xtd::object> clone() const override;
190
195 auto copy_to(xtd::array<bool>& array, xtd::size index) const -> void override;
196
200 auto equals(const bit_array& value) const noexcept -> bool override;
201
205 auto equals(const object& obj) const noexcept -> bool override;
206
212 auto get(xtd::size index) const -> bool;
213
219 auto get(xtd::size index) -> bool&;
220
224
227 auto has_all_set() const noexcept -> bool;
228
231 auto has_any_set() const noexcept -> bool;
232
237 auto left_shift(xtd::size count) noexcept -> bit_array&;
238
242 auto not_() -> const bit_array&;
243
250 auto or_(const bit_array& value) -> const bit_array&;
251
256 auto right_shift(xtd::size count) noexcept -> bit_array&;
257
263 auto set(xtd::size index, bool value) -> void;
264
268 auto set_all(bool value) -> void;
269
272 auto to_string() const noexcept -> xtd::string override;
273
280 auto xor_(const bit_array& value) -> const bit_array&;
282
284
290 auto operator [](xtd::size index) const -> const bool&;
291
296 auto operator [](xtd::size index) -> bool&;
297
304 auto operator &(const bit_array& value) const -> bit_array;
311 auto operator &=(const bit_array& value) -> bit_array&;
312
319 auto operator |(const bit_array& value) const -> bit_array;
326 auto operator |=(const bit_array& value) -> bit_array&;
327
334 auto operator ^(const bit_array& value) const -> bit_array;
341 auto operator ^=(const bit_array& value) -> bit_array&;
342
346 auto operator ~() const -> bit_array;
347
348 using xtd::collections::generic::icollection<bool>::operator >>;
353 auto operator >>(xtd::size count) const noexcept -> bit_array;
354
359 auto operator >>=(xtd::size count) noexcept -> bit_array&;
360
361 using xtd::collections::generic::icollection<bool>::operator <<;
366 auto operator <<(xtd::size count) const noexcept -> bit_array;
367
372 auto operator <<=(xtd::size count) noexcept -> bit_array&;
374
375 private:
376 auto is_read_only() const noexcept -> bool override;
377 auto is_synchronized() const noexcept -> bool override;
378 const object& sync_root() const noexcept override;
379
380 auto add(const bool&) -> void override;
381 auto clear() -> void override;
382 auto contains(const bool&) const noexcept -> bool override;
383 auto remove(const bool&) -> bool override;
384
385 auto flush() const noexcept -> void;
386 auto get_int32_array_length_from_bit_length(xtd::size n) const noexcept -> xtd::size;
387 auto get_list_length(xtd::size length_) const noexcept -> xtd::size;
388 auto get_list_position(xtd::size index) const noexcept -> xtd::size;
389 auto get_bit_position(xtd::size index) const noexcept -> xtd::size;
390 auto get_bit_value(xtd::size index) const noexcept -> bool;
391 auto set_bit_value(xtd::size index, bool value) noexcept -> void;
392
393 static constexpr xtd::size bits_per_byte = 8;
394 static constexpr xtd::size bits_per_int32 = 32;
395 static constexpr xtd::size bytes_per_int32 = 4;
396 static constexpr xtd::size bit_shift_per_int32 = 5;
397 mutable boolean_ref value_ref_;
398 xtd::collections::generic::list<int32> bit_array_;
399 xtd::size length_ = 0;
400 };
401 }
402}
403
405template<xtd::size size_>
406inline auto xtd::linq::enumerable::as_enumerable(std::bitset<size_> source) {
407 return xtd::collections::bit_array {source};
408}
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:41
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...
const value_type * const_pointer
Represents the const pointer of list value type.
Definition bit_array.hpp:75
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::size
Gets the number of elements contained in the xtd::collections::bit_array.
xtd::ptrdiff difference_type
Represents the list difference type (usually xtd::ptrdiff).
Definition bit_array.hpp:67
typename xtd::collections::generic::list< int32 > base_type
Represents the list base type.
Definition bit_array.hpp:63
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...
xtd::size size_type
Represents the list size type (usually xtd::size).
Definition bit_array.hpp:65
value_type & reference
Represents the reference of list value type.
Definition bit_array.hpp:69
auto set(xtd::size index, bool value) -> void
Sets the value of the bit at a specific position in the xtd::collections::bit_array.
auto set_all(bool value) -> void
Sets all bits in the xtd::collections::bit_array to the specified value.
auto copy_to(xtd::array< bool > &array, xtd::size index) const -> void override
Copies the elements of the xtd::collections::bit_array to an xtd::array, starting at a particular xtd...
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:71
bit_array(xtd::size length, bool defaultValue) 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:154
auto equals(const bit_array &value) const noexcept -> bool override
Determines whether this instance of xtd::collections::bit_array and a specified object,...
auto get(xtd::size index) const -> bool
Gets the value of the bit at a specific position in the xtd::collections::bit_array.
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.
value_type * pointer
Represents the pointer of list value type.
Definition bit_array.hpp:73
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:120
auto count() const noexcept -> xtd::size override
Gets the number of elements contained in the xtd::collections::bit_array.
auto has_any_set() const noexcept -> bool
Determines whether any bit in the xtd::collections::bit_array is set to true.
auto right_shift(xtd::size count) noexcept -> bit_array &
Shifts all the bit values of the current xtd::collections::bit_array to the right on count bits.
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 left_shift(xtd::size count) noexcept -> bit_array &
Shifts all the bit values of the current xtd::collections::bit_array to the left on count bits.
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
bit_array(xtd::size length) noexcept
Initializes a new instance of the xtd::collections::bit_array class that can hold the specified numbe...
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:61
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:144
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
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:22
static xtd::byte min(xtd::byte a, xtd::byte b) noexcept
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:45
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 xtd::size npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
constexpr xtd::size epos
Represents the index of the last valid element in a collection.
Definition epos.hpp:33
constexpr xtd::size bpos
Represents the index of the firsy valid element in a collection.
Definition bpos.hpp:25
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
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:23
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
@ n
The N key.
Definition console_key.hpp:114
Contains xtd::iclonable interface.
Contains xtd::iequatable interface.
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:50
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::size type.