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 "index.hpp"
8#include "optional.hpp"
9#include "npos.hpp"
10#include "range.hpp"
11#include "read_only_span.hpp"
12#include "span.hpp"
13#include "string.hpp"
16#include <concepts>
17
19namespace xtd {
30 template<typename type_t, xtd::usize len>
31 class fixed_array : public xtd::object, public xtd::collections::generic::ienumerable<type_t>, public xtd::iequatable<fixed_array<type_t, len>> {
32 public:
34
37 using value_type = type_t;
49 using const_pointer = const value_type*;
55
57
61 fixed_array() = default;
65 fixed_array(std::initializer_list<type_t> items) {
66 auto index = xtd::usize {0};
67 for (const auto& item : items)
68 items_[index++] = item;
69 }
70
71
73 fixed_array(fixed_array&&) = default;
74 fixed_array& operator =(fixed_array&&) = default;
75 fixed_array(const fixed_array&) = default;
76 fixed_array& operator =(const fixed_array&) = default;
78
80
84 [[nodiscard]] auto count() const noexcept -> size_type {return size();}
85
89 [[nodiscard]] auto data() const noexcept -> const_pointer {return items_;}
93 [[nodiscard]] auto data() noexcept -> pointer {return items_;}
94
97 [[nodiscard]] virtual auto empty() const noexcept -> bool {return len == xtd::usize {0};}
98
101 [[nodiscard]] virtual auto items() const noexcept -> const_base_type {return items_;}
104 [[nodiscard]] virtual auto items() noexcept -> base_type {return items_;}
105
109 [[nodiscard]] virtual auto length() const noexcept -> size_type {return len;}
110
113 [[nodiscard]] auto size() const noexcept -> size_type {return length();}
115
117
121 auto clear() noexcept -> void {fill(value_type {});}
122
125 [[nodiscard]] auto contains(const value_type& value) const noexcept -> bool {
126 for (const auto& item : items_)
127 if (xtd::collections::generic::helpers::equator<type_t> {}(reinterpret_cast<const value_type&>(item), value)) return true;
128 return false;
129 }
130
134 auto copy_to(xtd::array<value_type>& array) const -> void {
135 copy_to(0, array, 0);
136 }
137
142 auto copy_to(xtd::array<value_type>& array, size_type array_index) const -> void {
143 return copy_to(0, array, array_index);
144 }
145
150 auto copy_to(const size_type index, xtd::array<value_type>& array, size_type array_index) const -> void {
151 return copy_to(index, array, array_index, length() - index);
152 }
153
165
169 [[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));}
174 [[nodiscard]] auto equals(const fixed_array & rhs) const noexcept -> bool override {
175 if (count() != rhs.count()) return false;
176 for (size_type i = 0; i < count(); i++)
177 if (!xtd::collections::generic::helpers::equator<type_t> {}(items_[i], rhs.items_[i])) return false;
178 return true;
179 }
180
183 auto fill(const value_type& value) -> void {
184 for (auto& item : *this)
185 item = value;
186 }
187
190 [[nodiscard]] auto get_enumerator() const -> xtd::collections::generic::enumerator<value_type> override {
191 struct fixed_array_enumerator : public xtd::collections::generic::ienumerator < value_type > {
192 explicit fixed_array_enumerator(const fixed_array & items) : items_(items) {}
193
194 [[nodiscard]] const value_type& current() const override {
196 return items_[index_];
197 }
198 bool move_next() override {return ++index_ < items_.count();}
199 void reset() override {index_ = xtd::npos;}
200
201 private:
202 size_type index_ = xtd::npos;
203 const fixed_array& items_;
204 };
205 return {new_ptr<fixed_array_enumerator>(*this)};
206 }
207
211 [[nodiscard]] auto index_of(const value_type& value) const noexcept -> size_type {return index_of(value, 0, length());}
217 [[nodiscard]] auto index_of(const value_type& value, size_type index) const -> size_type {return index_of(value, index, length() - index);}
224 [[nodiscard]] auto index_of(const value_type& value, size_type index, size_type count) const -> size_type {
226 for (auto increment = index; increment < (index + count); ++increment)
227 if (xtd::collections::generic::helpers::equator<type_t> {}(items_[increment], value)) return increment;
228 return xtd::npos;
229 }
230
241
247 template<typename comparison_t>
248 auto sort(comparison_t&& comparison) -> fixed_array& {
249 std::sort(items_, items_ + length(), [&](const type_t& x, const type_t& y) {return comparison(x, y) < 0;});
250 return self_;
251 }
252
260 return sort(0, count(), comparer);
261 }
262
276
279 [[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> {};}
280
283 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", *this));}
285
287
297
304
310 }
311
316 }
317
323
327 return xtd::span<value_type> {*this, range};
328 }
329
335 return operator[](index);
336 }
337
341 return operator[](index);
342 }
343
348 return operator[](index);
349 }
350
354 return operator[](index);
355 }
356
362
366 return operator[](range);
367 }
368
369
370 private:
371 type_t items_[len];
372 };
373
375 // Deduction guides for xtd::fixed_array
376 // {
377 template<typename type_t, typename... args_t>
378 fixed_array(type_t, args_t...) -> fixed_array <type_t, 1 + sizeof...(args_t)>;
379 // }
381}
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:1240
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:31
virtual auto items() noexcept -> base_type
Returns the underlying base type items.
Definition fixed_array.hpp:104
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:271
auto data() const noexcept -> const_pointer
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:89
xtd::usize size_type
Represents the array size type (usually xtd::usize).
Definition fixed_array.hpp:51
auto get_enumerator() const -> xtd::collections::generic::enumerator< value_type > override
Returns an enumerator that iterates through a collection.
Definition fixed_array.hpp:190
auto data() noexcept -> pointer
Returns pointer to the underlying array serving as element storage.
Definition fixed_array.hpp:93
auto fill(const value_type &value) -> void
Fills the elements of this span with a specified value.
Definition fixed_array.hpp:183
auto operator()(size_type index) const -> const_reference
Returns a reference to the element at specified location index.
Definition fixed_array.hpp:334
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:279
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:174
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition fixed_array.hpp:283
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:217
auto clear() noexcept -> void
Clears the contents of this xtd::span <type> object.
Definition fixed_array.hpp:121
const value_type * const_pointer
Represents the const pointer of array value type.
Definition fixed_array.hpp:49
auto count() const noexcept -> size_type
Gets the number of elements contained in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:84
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:169
value_type * pointer
Represents the pointer of array value type.
Definition fixed_array.hpp:47
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:159
auto operator[](size_type index) const -> const_reference
Returns a reference to the element at specified location index.
Definition fixed_array.hpp:293
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:142
virtual auto items() const noexcept -> const_base_type
Returns the underlying base type items.
Definition fixed_array.hpp:101
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:113
type_t value_type
Represents the array value type.
Definition fixed_array.hpp:37
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:65
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:97
auto sort() -> fixed_array &
Determines the index of a specific item in the xtd::fixed_array <type_t>.
Definition fixed_array.hpp:240
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:150
const value_type * const_base_type
Represents the const array base type.
Definition fixed_array.hpp:41
xtd::ptrdiff difference_type
Represents the array difference type (usually xtd::ptrdiff).
Definition fixed_array.hpp:53
const value_type & const_reference
Represents the const reference of array value type.
Definition fixed_array.hpp:45
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:248
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:211
value_type & reference
Represents the reference of array value type.
Definition fixed_array.hpp:43
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:39
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:109
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:134
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:259
auto contains(const value_type &value) const noexcept -> bool
Determines whether an element is in the array.
Definition fixed_array.hpp:125
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:40
Represents a range that has start and end indexes.
Definition range.hpp:38
Definition __span_definitions.hpp:16
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:62
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
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.
Contains xtd::index struct.
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:65
constexpr auto size() const noexcept -> size_type
Returns the number of elements.
Definition read_only_span.hpp:213
Contains xtd::npos constant.
Contains xtd::optional type.
Contains xtd::range class.
Contains xtd::read_only_span class.
Contains xtd::span class.
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 type that can be used to index a collection either from the beginning or the end.
Definition index.hpp:38
auto get_offset(value_type length) const noexcept -> xtd::usize
Calculates the offset from the start of the collection using the specified collection length.
Represents a value_type struct.
Definition value_type.hpp:34
Contains xtd::helpers::throw_helper class.