xtd 1.0.0
Loading...
Searching...
No Matches
fixed_array.hpp
Go to the documentation of this file.
1
4#pragma once
5#include "array.hpp"
6#include "iequatable.hpp"
7#include "optional.hpp"
8#include "npos.hpp"
9#include "string.hpp"
12#include <concepts>
13
15namespace xtd {
26 template<typename type_t, xtd::usize len>
27 class fixed_array : public xtd::object, public xtd::collections::generic::ienumerable<type_t>, public xtd::iequatable<fixed_array<type_t, len>> {
28 public:
30
33 using value_type = type_t;
45 using const_pointer = const value_type*;
51
53
57 fixed_array() = default;
61 fixed_array(std::initializer_list<type_t> items) {
62 auto index = xtd::usize {0};
63 for (const auto& item : items)
64 items_[index++] = item;
65 }
66
67
69 fixed_array(fixed_array&&) = default;
70 fixed_array& operator =(fixed_array&&) = default;
71 fixed_array(const fixed_array&) = default;
72 fixed_array& operator =(const fixed_array&) = default;
74
76
80 [[nodiscard]] auto count() const noexcept -> size_type {return size();}
81
85 [[nodiscard]] auto data() const noexcept -> const_pointer {return items_;}
89 [[nodiscard]] auto data() noexcept -> pointer {return items_;}
90
93 [[nodiscard]] virtual auto empty() const noexcept -> bool {return len == xtd::usize {0};}
94
97 [[nodiscard]] virtual auto items() const noexcept -> const_base_type {return items_;}
100 [[nodiscard]] virtual auto items() noexcept -> base_type {return items_;}
101
105 [[nodiscard]] virtual auto length() const noexcept -> size_type {return len;}
106
109 [[nodiscard]] auto size() const noexcept -> size_type {return length();}
111
113
117 auto clear() noexcept -> void {fill(value_type {});}
118
121 [[nodiscard]] auto contains(const value_type& value) const noexcept -> bool {
122 for (const auto& item : items_)
123 if (xtd::collections::generic::helpers::equator<type_t> {}(reinterpret_cast<const value_type&>(item), value)) return true;
124 return false;
125 }
126
130 auto copy_to(xtd::array<value_type>& array) const -> void {
131 copy_to(0, array, 0);
132 }
133
138 auto copy_to(xtd::array<value_type>& array, size_type array_index) const -> void {
139 return copy_to(0, array, array_index);
140 }
141
146 auto copy_to(const size_type index, xtd::array<value_type>& array, size_type array_index) const -> void {
147 return copy_to(index, array, array_index, length() - index);
148 }
149
155 auto copy_to(const size_type index, xtd::array<value_type>& array, size_type array_index, size_type count) const -> void {
158 for (auto i = index; i < (index + count); ++i)
159 array[array_index++] = self_[i];
160 }
161
165 [[nodiscard]] auto equals(const object & obj) const noexcept -> bool override {return dynamic_cast<const fixed_array<value_type, len>*>(&obj) && equals(static_cast<const fixed_array<value_type, len>&>(obj));}
170 [[nodiscard]] auto equals(const fixed_array & rhs) const noexcept -> bool override {
171 if (count() != rhs.count()) return false;
172 for (size_type i = 0; i < count(); i++)
173 if (!xtd::collections::generic::helpers::equator<type_t> {}(items_[i], rhs.items_[i])) return false;
174 return true;
175 }
176
179 auto fill(const value_type& value) -> void {
180 for (auto& item : *this)
181 item = value;
182 }
183
186 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
187 struct fixed_array_enumerator : public xtd::collections::generic::ienumerator < value_type > {
188 explicit fixed_array_enumerator(const fixed_array & items) : items_(items) {}
189
190 [[nodiscard]] const value_type& current() const override {
192 return items_[index_];
193 }
194 bool move_next() override {return ++index_ < items_.count();}
195 void reset() override {index_ = xtd::npos;}
196
197 private:
198 size_type index_ = xtd::npos;
199 const fixed_array& items_;
200 };
201 return {new_ptr<fixed_array_enumerator>(*this)};
202 }
203
207 [[nodiscard]] auto index_of(const value_type& value) const noexcept -> size_type {return index_of(value, 0, length());}
213 [[nodiscard]] auto index_of(const value_type& value, size_type index) const -> size_type {return index_of(value, index, length() - index);}
220 [[nodiscard]] auto index_of(const value_type& value, size_type index, size_type count) const -> size_type {
222 for (auto increment = index; increment < (index + count); ++increment)
223 if (xtd::collections::generic::helpers::equator<type_t> {}(items_[increment], value)) return increment;
224 return xtd::npos;
225 }
226
237
243 template<typename comparison_t>
244 auto sort(comparison_t&& comparison) -> fixed_array& {
245 std::sort(items_, items_ + length(), [&](const type_t& x, const type_t& y) {return comparison(x, y) < 0;});
246 return self_;
247 }
248
256 return sort(0, count(), comparer);
257 }
258
272
275 [[nodiscard]] auto to_array() const noexcept -> xtd::array<value_type> requires std::copy_constructible<value_type> {return size() ? xtd::array<value_type>(items_, items_ + len) : xtd::array<value_type> {};}
276
279 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", *this));}
281
283
285
290 auto operator [](size_type index) const -> const_reference {
292 return items_[index == epos ? size() - 1 : index];
293 }
294
299 return items_[index == epos ? size() - 1 : index];
300 }
301
302
303 private:
304 type_t items_[len];
305 };
306
308 // Deduction guides for xtd::fixed_array
309 // {
310 template<typename type_t, typename... args_t>
311 fixed_array(type_t, args_t...) -> fixed_array <type_t, 1 + sizeof...(args_t)>;
312 // }
314}
Contains xtd::array class.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
xtd::usize rank() const noexcept override
Gets the rank (number of dimensions) of the array.
Definition array.hpp:158
virtual auto length() const noexcept -> size_type
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:122
static auto join(const basic_string &separator, const collection_t &values) noexcept -> basic_string
Definition basic_string.hpp:1258
static const comparer< xtd::any_object > default_comparer
Definition comparer.hpp:50
Exposes a method that compares two objects.
Definition icomparer.hpp:30
Exposes the enumerator, which supports a simple iteration over a collection of a specified type.
Definition ienumerable.hpp:40
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Represents a fixed array class.
Definition fixed_array.hpp:27
virtual auto items() noexcept -> base_type
Returns the underlying base type items.
Definition fixed_array.hpp:100
auto sort(xtd::usize index, xtd::usize count, const xtd::collections::generic::icomparer< type_t > &comparer) -> fixed_array &
Sorts the elements in a range of elements in xtd::fixed_array <type_t> using the specified comparer.
Definition fixed_array.hpp:267
auto data() const noexcept -> const_pointer
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:85
xtd::usize size_type
Represents the array size type (usually xtd::usize).
Definition fixed_array.hpp:47
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition fixed_array.hpp:186
auto data() noexcept -> pointer
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:89
auto fill(const value_type &value) -> void
Fills the elements of this span with a specified value.
Definition fixed_array.hpp:179
auto to_array() const noexcept -> xtd::array< value_type >
Copies the elements of the xtd::fixed_array <type_t> to a new array.
Definition fixed_array.hpp:275
auto equals(const fixed_array &rhs) const noexcept -> bool override
Determines whether this instance and another specified xtd::fixed_array object have the same value.
Definition fixed_array.hpp:170
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition fixed_array.hpp:279
auto index_of(const value_type &value, size_type index) const -> size_type
Determines the index of a specific item in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:213
auto clear() noexcept -> void
Clears the contents of this xtd::span <type> object.
Definition fixed_array.hpp:117
const value_type * const_pointer
Represents the const pointer of array value type.
Definition fixed_array.hpp:45
auto count() const noexcept -> size_type
Gets the number of elements contained in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:80
auto equals(const object &obj) const noexcept -> bool override
Determines whether this instance and a specified object, which must also be a xtd::fixed_array object...
Definition fixed_array.hpp:165
value_type * pointer
Represents the pointer of array value type.
Definition fixed_array.hpp:43
auto copy_to(const size_type index, xtd::array< value_type > &array, size_type array_index, size_type count) const -> void
Copies the elements of the xtd::array <type_t> from a specified index to an xtd::array,...
Definition fixed_array.hpp:155
auto operator[](size_type index) const -> const_reference
Returns a reference to the element at specified location index.
Definition fixed_array.hpp:290
auto copy_to(xtd::array< value_type > &array, size_type array_index) const -> void
Copies the elements of the xtd::array <type_t> to an xtd::array, starting at a particular xtd::array ...
Definition fixed_array.hpp:138
virtual auto items() const noexcept -> const_base_type
Returns the underlying base type items.
Definition fixed_array.hpp:97
auto size() const noexcept -> size_type
Returns the number of elements in the container, i.e. std::distance(xtd::array::begin(),...
Definition fixed_array.hpp:109
type_t value_type
Represents the array value type.
Definition fixed_array.hpp:33
fixed_array(std::initializer_list< type_t > items)
Initializes a new instance of the fixed_array class with specified initializer list.
Definition fixed_array.hpp:61
virtual auto empty() const noexcept -> bool
Checks if the container has no elements, i.e. whether xtd::array::begin() == xtd::array::end().
Definition fixed_array.hpp:93
auto sort() -> fixed_array &
Determines the index of a specific item in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:236
auto copy_to(const size_type index, xtd::array< value_type > &array, size_type array_index) const -> void
Copies the elements of the xtd::array <type_t> from a specified index to an xtd::array,...
Definition fixed_array.hpp:146
const value_type * const_base_type
Represents the const array base type.
Definition fixed_array.hpp:37
xtd::ptrdiff difference_type
Represents the array difference type (usually xtd::ptrdiff).
Definition fixed_array.hpp:49
const value_type & const_reference
Represents the const reference of array value type.
Definition fixed_array.hpp:41
auto sort(comparison_t &&comparison) -> fixed_array &
Sorts the elements in the entire xtd::fixed_array <type_t> using the specified xtd::comparison <type_...
Definition fixed_array.hpp:244
auto index_of(const value_type &value) const noexcept -> size_type
Determines the index of a specific item in the xtd::array <type_t>.
Definition fixed_array.hpp:207
value_type & reference
Represents the reference of array value type.
Definition fixed_array.hpp:39
fixed_array()=default
Initializes a new instance of the fixed_array class that is empty.
value_type * base_type
Represents the array base type.
Definition fixed_array.hpp:35
virtual auto length() const noexcept -> size_type
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition fixed_array.hpp:105
auto copy_to(xtd::array< value_type > &array) const -> void
Copies the entire xtd::array <type_t> to a compatible one-dimensional array.
Definition fixed_array.hpp:130
auto sort(const xtd::collections::generic::icomparer< type_t > &comparer) -> fixed_array &
Sorts the elements in the entire xtd::fixed_array <type_t> using the specified comparer.
Definition fixed_array.hpp:255
auto contains(const value_type &value) const noexcept -> bool
Determines whether an element is in the array.
Definition fixed_array.hpp:121
static auto throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current()) -> void
Throws an exption with specified exception case.
Defines a generalized method that a value type or class implements to create a type-specific method f...
Definition iequatable.hpp:23
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:45
Contains xtd::collections::generic::ienumerable <type_t> interface.
xtd::delegate< int32(type_t x, type_t y)> comparison
Represents the method that compares two objects of the same type.
Definition comparison.hpp:33
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ argument
The argument is not valid.
Definition exception_case.hpp:31
@ index_out_of_range
The index is out of range.
Definition exception_case.hpp:61
@ argument_out_of_range
The argument is out of range.
Definition exception_case.hpp:35
@ invalid_operation
The operation is not valid.
Definition exception_case.hpp:65
#define self_
The self_ expression is a reference value expression whose value is the reference of the implicit obj...
Definition self.hpp:20
constexpr auto npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
constexpr xtd::usize epos
Represents the index of the last valid element in a collection.
Definition epos.hpp:33
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
auto new_ptr(args_t &&... args) -> xtd::ptr< type_t >
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ y
The Y key.
Definition console_key.hpp:136
@ i
The I key.
Definition console_key.hpp:104
@ x
The X key.
Definition console_key.hpp:134
Contains xtd::iequatable interface.
The xtd::collections namespace contains interfaces and classes that define various collections of obj...
Definition any_pair.hpp:10
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:59
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:217
Contains xtd::npos constant.
Contains xtd::optional type.
Contains xtd::string alias.
Implements a function object for performing comparisons. Unless specialised, invokes operator== on ty...
Definition equator.hpp:39
Implements a function object for compare data.
Definition lesser.hpp:39
Represents a value_type struct.
Definition value_type.hpp:34
Contains xtd::helpers::throw_helper class.