xtd 0.2.0
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 "../object.hpp"
14#include "../size.hpp"
15#include "../types.hpp"
16#include <bitset>
17#include <initializer_list>
18#include <limits>
19
21namespace xtd {
23 namespace collections {
41 private:
42 struct boolean_ref {
43 boolean_ref() noexcept = default;
44 bool& get_boolean_ref(bool value, xtd::size index) noexcept;
45 const bool& get_boolean_ref(bool value, xtd::size index) const noexcept;
46 void from_boolean(bit_array& parent) noexcept;
47
48 static constexpr xtd::size npos = std::numeric_limits<xtd::size>::max();
49 mutable xtd::size index = npos;
50 mutable bool value = false;
51 };
52
53 public:
55
58 using value_type = bool;
74 using const_pointer = const value_type*;
80
82
88 explicit bit_array(xtd::size length) noexcept;
89
95 bit_array(xtd::size length, bool defaultValue) noexcept;
96
100 bit_array(std::initializer_list<bool> il) noexcept;
101
105 bit_array(const xtd::array<bool>& values) noexcept;
106
111 bit_array(const xtd::array<xtd::byte>& values) noexcept;
112
117 bit_array(const xtd::array<int32>& values) noexcept;
118
122 template<xtd::size length>
123 bit_array(const std::bitset<length>& bit_set) noexcept : bit_array(length) {
124 for (auto index = xtd::size {0}; index < length; ++index)
125 set(index, bit_set.test(index));
126 }
127
131 bit_array(const std::vector<bool>& booleans) noexcept;
133
135 bit_array(bit_array&& bits) = default;
136 bit_array& operator=(bit_array&&) = default;
137 bit_array(const bit_array& bits) = default;
138 bit_array& operator=(const bit_array&) = default;
140
142
144
151
154 template<xtd::size length>
155 std::bitset<length> bits() const noexcept {
156 auto result = std::bitset<length> {};
157 auto bits_count = xtd::math::min(length, count());
158 for (auto index = xtd::size {0}; index < bits_count; ++index)
159 result[index] = at(index);
160 return result;
161 }
162
165 std::vector<bool> bits() const noexcept {
166 auto result = std::vector<bool> {};
167 for (auto index = xtd::size {0}; index < count(); ++index)
168 result[index] = at(index);
169 return result;
170 }
171
175
179
183 xtd::size count() const noexcept override;
184
187 virtual bool empty() const noexcept {return !length();}
188
195
199 xtd::size length() const noexcept;
203 void length(xtd::size value);
204
208 bool is_read_only() const noexcept override;
209
224 bool is_synchronized() const noexcept override;
225
247 const object& sync_root() const noexcept override;
249
251
259 const bit_array& and_(const bit_array& value);
260
266 bool at(xtd::size index) const;
267
273 bool& at(xtd::size index);
274
277 xtd::uptr<xtd::object> clone() const override;
278
283 void copy_to(xtd::array<bool>& array, xtd::size index) const override;
284
288 bool equals(const bit_array& value) const noexcept override;
289
293 bool equals(const object& obj) const noexcept override;
294
300 bool get(xtd::size index) const;
301
307 bool& get(xtd::size index);
308
311 xtd::collections::generic::enumerator<bool> get_enumerator() const override;
312
315 bool has_all_set() const noexcept;
316
319 bool has_any_set() const noexcept;
320
325 bit_array& left_shift(xtd::size count) noexcept;
326
330 const bit_array& not_();
331
338 const bit_array& or_(const bit_array& value);
339
344 bit_array& right_shift(xtd::size count) noexcept;
345
351 void set(xtd::size index, bool value);
352
356 void set_all(bool value);
357
360 xtd::string to_string() const noexcept override;
361
368 const bit_array& xor_(const bit_array& value);
370
372
378 const bool& operator [](xtd::size index) const;
379
384 bool& operator [](xtd::size index);
385
390 bit_array operator >>(xtd::size count) const noexcept;
391
396 bit_array& operator >>=(xtd::size count) noexcept;
397
402 bit_array operator <<(xtd::size count) const noexcept;
403
408 bit_array& operator <<=(xtd::size count) noexcept;
410
411 private:
412 void add(const bool&) override;
413 void clear() override;
414 bool contains(const bool&) const noexcept override;
415 bool remove(const bool&) override;
416
417 void flush() const noexcept;
418 size get_int32_array_length_from_bit_length(size n) const noexcept;
419 xtd::size get_list_length(xtd::size length_) const noexcept;
420 xtd::size get_list_position(xtd::size index) const noexcept;
421 xtd::size get_bit_position(xtd::size index) const noexcept;
422 bool get_bit_value(xtd::size index) const noexcept;
423 void set_bit_value(xtd::size index, bool value) noexcept;
424
425 static constexpr xtd::size bits_per_byte = 8;
426 static constexpr xtd::size bits_per_int32 = 32;
427 static constexpr xtd::size bytes_per_int32 = 4;
428 static constexpr xtd::size bit_shift_per_int32 = 5;
429 mutable boolean_ref value_ref_;
430 xtd::collections::generic::list<int32> bit_array_;
431 xtd::size length_ = 0;
432 };
433 }
434}
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:59
Manages a compact array of bit values, which are represented as booleans, where true indicates that t...
Definition bit_array.hpp:40
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
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...
const_iterator begin() const noexcept override
Returns an iterator to the first element of the enumarable.
Definition bit_array.hpp:147
iterator end() noexcept override
Returns an iterator to the element following the last element of the enumarable.
Definition bit_array.hpp:194
xtd::size count() const noexcept override
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:66
std::bitset< length > bits() const noexcept
Returns a std::bitset object containing the Booleans contained in the current xtd::collections::bit_a...
Definition bit_array.hpp:155
xtd::size size_type
Represents the list size type (usually xtd::size).
Definition bit_array.hpp:64
typename xtd::collections::generic::ienumerable< bool >::const_iterator const_iterator
Represents the const iterator of list value type.
Definition bit_array.hpp:78
typename xtd::collections::generic::ienumerable< bool >::iterator iterator
Represents the iterator of list value type.
Definition bit_array.hpp:76
value_type & reference
Represents the reference of list value type.
Definition bit_array.hpp:68
xtd::size length() const noexcept
Gets the number of elements contained in the xtd::collections::bit_array.
const value_type & const_reference
Represents the const reference of list value type.
Definition bit_array.hpp:70
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...
iterator begin() noexcept override
Returns an iterator to the first element of the enumarable.
Definition bit_array.hpp:150
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...
typename xtd::collections::generic::list< int32 >::allocator_type allocator_type
Represents the list allocator type.
Definition bit_array.hpp:60
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:123
std::vector< bool > bits() const noexcept
Returns a std::vector<bool> object containing the Booleans contained in the current xtd::collections:...
Definition bit_array.hpp:165
bit_array(xtd::size length) noexcept
Initializes a new instance of the xtd::collections::bit_array class that can hold the specified numbe...
const_iterator cend() const noexcept override
Returns an iterator to the element following the last element of the enumarable.
Definition bit_array.hpp:178
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:58
const_iterator cbegin() const noexcept override
Returns an iterator to the first element of the enumarable.
Definition bit_array.hpp:174
const_iterator end() const noexcept override
Returns an iterator to the element following the last element of the enumarable.
Definition bit_array.hpp:191
virtual const_iterator end() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:170
virtual const_iterator begin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:155
virtual const_iterator cbegin() const
Returns an iterator to the first element of the enumarable.
Definition enumerable_iterators.hpp:162
virtual const_iterator cend() const
Returns an iterator to the element following the last element of the enumarable.
Definition enumerable_iterators.hpp:166
typename xtd::linq::enumerable::list< type_t > list
Represents the list value type.
Definition enumerable.hpp:47
Defines methods to manipulate generic collections.
Definition icollection.hpp:45
typename xtd::collections::generic::extensions::enumerable_iterators< type_t, xtd::collections::generic::ienumerable< type_t > >::iterator iterator
Represents the iterator of xtd::collections::generic::ienumerable value type.
Definition ienumerable.hpp:44
typename xtd::collections::generic::extensions::enumerable_iterators< type_t, xtd::collections::generic::ienumerable< type_t > >::const_iterator const_iterator
Represents the const iterator of xtd::collections::generic::ienumerable value type.
Definition ienumerable.hpp:46
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:80
typename xtd::collections::generic::helpers::allocator< typename std::conditional< std::is_same< bool, value_type >::value, xtd::byte, value_type >::type > allocator_type
Represents the list allocator type.
Definition list.hpp:127
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:44
#define core_export_
Define shared library export.
Definition core_export.hpp:13
int32_t int32
Represents a 32-bit signed integer.
Definition int32.hpp:23
std::unique_ptr< type_t > uptr
The xtd::uptr object is a unique pointer.
Definition uptr.hpp:25
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
Contains xtd::collections::generic::list <value_t> class.
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition xtd_about_box.hpp:10
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38