xtd 0.2.0
Loading...
Searching...
No Matches
basic_array.hpp
Go to the documentation of this file.
1
4
5#if !defined(__XTD_ARRAY_INTERNAL__)
6#error "Do not include this file: Internal use only. Include <xtd/array> or <xtd/array.hpp> instead."
7#endif
8
10namespace xtd {
26 template<class type_t, class allocator_t = xtd::collections::generic::helpers::allocator<type_t>>
27 class basic_array : public xtd::array_abstract_object, public xtd::collections::generic::ilist<type_t>, public xtd::iequatable<basic_array<type_t, allocator_t >> {
28 public:
30
33 using value_type = type_t;
49 using const_pointer = const value_type*;
59
61
63 basic_array(basic_array&& array) = default;
64 basic_array(const basic_array & array) { if (array.data_) *data_ = *array.data_;}
66
68
74 size_type count() const noexcept override {return data_->items.size();}
75
79 virtual pointer data() noexcept {return (pointer)data_->items.data();}
83 virtual const_pointer data() const noexcept {return (pointer)data_->items.data();}
84
88 bool is_fixed_size() const noexcept override {return true;}
89
93 bool is_read_only() const noexcept override {return false;}
94
109 bool is_synchronized() const noexcept override {return false;}
110
113 virtual const base_type& items() const noexcept {return data_->items;}
116 virtual base_type& items() noexcept {return data_->items;}
117
124 virtual size_type length() const noexcept {return data_->items.size();}
128 virtual xtd::int64 long_length() {return static_cast<xtd::int64>(length());}
129
132 virtual size_type max_length() const noexcept {return data_->items.max_size();}
133
139 virtual size_type rank() const noexcept {return 1;}
140
162 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
164
166
170 bool contains(const type_t& value) const noexcept override {
171 for (const auto& item : data_->items)
172 if (xtd::collections::generic::helpers::equator<type_t> {}(reinterpret_cast<const type_t&>(item), value)) return true;
173 return false;
174 }
175
179 void copy_to(xtd::array<type_t>& array) const {
180 copy_to(0, array, 0);
181 }
182
187 void copy_to(xtd::array<type_t>& array, size_type array_index) const override {
188 return copy_to(0, array, array_index);
189 }
190
191 void copy_to(const xtd::array<size_type>& indexes, xtd::array<type_t>& array, size_type array_index) const {
192 return copy_to(compute_index(self_, indexes), array, array_index);
193 }
194
195 void copy_to(const xtd::array<size_type>& indexes, xtd::array<type_t>& array, size_type array_index, size_type count) const {
196 return copy_to(compute_index(self_, indexes), array, array_index, count);
197 }
198
199 void copy_to(const size_type index, xtd::array<type_t>& array, size_type array_index) const {
200 return copy_to(index, array, array_index, length() - index);
201 }
202
203 void copy_to(const size_type index, xtd::array<type_t>& array, size_type array_index, size_type count) const {
205 if (index + count > self_.length() || array_index + count > array.length()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::argument_out_of_range);
206 for (auto i = index; i < (index + count); ++i)
207 array[array_index++] = self_[i];
208 }
209
210 bool equals(const object & obj) const noexcept override {return dynamic_cast<const basic_array<value_type>*>(&obj) && equals(static_cast<const basic_array<value_type>&>(obj));}
211 bool equals(const basic_array & rhs) const noexcept override {
212 if (count() != rhs.count()) return false;
213 for (size_type i = 0; i < count(); i++)
214 if (!xtd::collections::generic::helpers::equator<type_t> {}(data_->items.at(i), rhs.data_->items.at(i))) return false;
215 return data_->lower_bound == rhs.data_->lower_bound && data_->upper_bound == rhs.data_->upper_bound;
216 }
217
220 virtual void fill(const value_type & value) noexcept {
221 for (auto& item : data_->items)
222 item = value;
223 }
224
225 xtd::collections::generic::enumerator<value_type> get_enumerator() const noexcept override {
226 struct basic_array_enumerator : public xtd::collections::generic::ienumerator < value_type > {
227 explicit basic_array_enumerator(const basic_array & items, size_type version) : items_(items), version_(version) {}
228
229 const value_type& current() const override {
231 if (version_ != items_.data_->items.version()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
232 return items_[index_];
233 }
234
235 bool move_next() override {
236 if (version_ != items_.data_->items.version()) xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "Collection was modified; enumeration operation may not execute.");
237 return ++index_ < items_.count();
238 }
239
240 void reset() override {
241 version_ = items_.data_->items.version();
242 index_ = basic_array::npos;
243 }
244
245 private:
246 size_type index_ = basic_array::npos;
247 const basic_array& items_;
248 size_type version_ = 0;
249 };
250 return {new_ptr < basic_array_enumerator > (*this, data_->items.version())};
251 }
252
260 constexpr size_type get_length(size_type dimension) const {return get_upper_bound(dimension) - get_lower_bound(dimension) + 1;}
261
265
273 constexpr xtd::int64 get_long_length(size_type dimension) const {return static_cast < xtd::int64 > (get_upper_bound(dimension) + 1);}
274
282 constexpr size_type get_lower_bound(size_type dimension) const {
284 return data_->lower_bound[dimension];
285 }
286
294 constexpr size_type get_upper_bound(size_type dimension) const {
296 return data_->upper_bound[dimension];
297 }
298
303 const value_type& get_value(const xtd::array < size_type >& indexes) const;
304
308 size_type index_of(const type_t& value) const noexcept override {return index_of(value, 0, length());}
309 size_type index_of(const type_t& value, size_type index) const {return index_of(value, index, length() - index);}
310 size_type index_of(const type_t& value, size_type index, size_type count) const {
312 for (auto increment = index; increment < (index + count); ++increment)
313 if (xtd::collections::generic::helpers::equator<type_t> {}(data_->items[increment], value)) return increment;
314 return xtd::npos;
315 }
316
322 void resize(size_type new_size) {resize(new_size, value_type {});}
323
330 void resize(size_type new_size, value_type value) {
331 if (new_size == length()) return;
334 data_->items.resize(new_size, value);
335 data_->upper_bound[0] = new_size - 1;
336 }
337
342 void set_value(const type_t& value, const xtd::array < size_type >& indexes) {operator()(indexes) = value;}
343
354
360 template<class comparison_t>
361 basic_array < type_t >& sort(comparison_t&& comparison) {
362 data_->items.increment_version();
363 std::sort(data_->items.begin(), data_->items.end(), [&](const type_t& x, const type_t& y) {return comparison(x, y) < 0;});
364 return self_;
365 }
366
373 basic_array < type_t >& sort(const xtd::collections::generic::icomparer < type_t >& comparer) {
374 return sort(0, count(), comparer);
375 }
376
385 basic_array < type_t >& sort(xtd::size index, xtd::size count, const xtd::collections::generic::icomparer < type_t >& comparer) {
387 data_->items.increment_version();
388 std::sort(data_->items.begin(), data_->items.end(), xtd::collections::generic::helpers::lesser<type_t> {comparer});
389 return self_;
390 }
391
394 xtd::string to_string() const noexcept override;
396
398
403 basic_array& operator =(const basic_array & other) {
404 *data_ = *other.data_;
405 return *this;
406 }
407
410 basic_array& operator =(basic_array&& other) noexcept = default;
414 basic_array& operator =(std::initializer_list < type_t >& items) {
415 data_->items = items;
416 data_->upper_bound[0] = data_->items.size() - 1;
417 return *this;
418 }
419
424 const_reference operator [](size_type index) const override {
426 return data_->items.at(index);
427 }
428
434 return data_->items.at(index);
435 }
436
439 operator const base_type& () const noexcept {return data_->items;}
442 operator base_type& () noexcept {return data_->items;}
443
451 type_t& operator()(const xtd::array < size_type >& indexes);
452
460 const type_t& operator()(const xtd::array < size_type >& indexes) const;
462
463 private:
464 template < class type_array_t, size_type rank_array_t, class allocator_array_t >
465 friend class array;
466
467 basic_array() = default;
468 explicit basic_array(const array < size_type, 1 >& lengths);
469 basic_array(const array < size_type, 1 >& lengths, const value_type & value);
470 basic_array(const_pointer array, size_type length) {
472 data_->items = base_type {array, array + length};
473 data_->upper_bound[0] = data_->items.size() - 1;
474 }
475 explicit basic_array(const xtd::collections::generic::ienumerable < type_t >& enumerable) {
476 for (const auto& value : enumerable)
477 data_->items.push_back(value);
478 data_->upper_bound[0] = data_->items.size() - 1;
479 }
480 explicit basic_array(const xtd::collections::generic::ilist < type_t >& items) {
481 data_->items.reserve(items.count());
482 for (const auto& value : items)
483 data_->items.push_back(value);
484 data_->upper_bound[0] = data_->items.size() - 1;
485 }
486
487 template < class input_iterator_t >
488 basic_array(input_iterator_t first, input_iterator_t last) {
489 data_->items.assign(first, last);
490 data_->upper_bound.push_back(data_->items.size() - 1);
491 }
492
493 basic_array(const std::vector < type_t >& items) {
494 data_->items.assign(items.begin(), items.end());
495 data_->upper_bound[0] = data_->items.size() - 1;
496 }
497
498 basic_array(const std::vector < std::vector < type_t>>& items) {
499 for (const std::vector < type_t >& items1 : items)
500 data_->items.insert(data_->items.end(), items1.begin(), items1.end());
501 data_->upper_bound[0] = items.size() - 1;
502 data_->lower_bound.push_back(0);
503 data_->upper_bound.push_back((*items.begin()).size() - 1);
504 }
505
506 basic_array(const std::vector < std::vector < std::vector<type_t>>>& items) {
507 for (const std::vector < std::vector < type_t>>& items1 : items)
508 for (const std::vector < type_t >& items2 : items1)
509 data_->items.insert(data_->items.end(), items2.begin(), items2.end());
510 data_->upper_bound[0] = items.size() - 1;
511 data_->lower_bound.push_back(0);
512 data_->upper_bound.push_back((*items.begin()).size() - 1);
513 data_->lower_bound.push_back(0);
514 data_->upper_bound.push_back((*(*items.begin()).begin()).size() - 1);
515 }
516
517 basic_array(std::initializer_list < type_t > il) {
518 data_->items.assign(il.begin(), il.end());
519 data_->upper_bound[0] = data_->items.size() - 1;
520 }
521
522 basic_array(std::initializer_list < std::initializer_list < type_t>> il) {
523 for (const std::initializer_list < type_t >& il1 : il)
524 data_->items.insert(data_->items.end(), il1.begin(), il1.end());
525 data_->upper_bound[0] = il.size() - 1;
526 data_->lower_bound.push_back(0);
527 data_->upper_bound.push_back((*il.begin()).size() - 1);
528 }
529
530 basic_array(std::initializer_list < std::initializer_list < std::initializer_list<type_t>>> il) {
531 for (const std::initializer_list < std::initializer_list < type_t>>& il1 : il)
532 for (const std::initializer_list < type_t >& il2 : il1)
533 data_->items.insert(data_->items.end(), il2.begin(), il2.end());
534 data_->upper_bound[0] = il.size() - 1;
535 data_->lower_bound.push_back(0);
536 data_->upper_bound.push_back((*il.begin()).size() - 1);
537 data_->lower_bound.push_back(0);
538 data_->upper_bound.push_back((*(*il.begin()).begin()).size() - 1);
539 }
540
541 void add(const type_t& item) override {}
542
543 void clear() override {fill(value_type {});}
544
545 template<class value_t>
546 static xtd::size compute_index(const xtd::basic_array<value_t>& items, const xtd::array<size_type>& indexes);
547 template<class value_t>
548 static xtd::size compute_index(const xtd::basic_array<value_t>& items, xtd::size rank, xtd::size index);
549
550 void insert(size_type index, const type_t& value) override {}
551
552 bool remove(const type_t& item) override {return false;}
553
554 void remove_at(size_type index) override {}
555
556 void reverse(size_type index, size_type count) {
558 if (count == 0) return;
559 data_->items.increment_version();
560 std::reverse(data_->items.begin() + index, data_->items.begin() + index + count);
561 }
562
563 template<class value_t>
564 static xtd::string to_string(const xtd::basic_array<value_t>& items, xtd::size rank, xtd::size base_index);
565
566 struct array_data {
567 xtd::collections::generic::helpers::raw_array < value_type > items;
568 std::vector < size_type > lower_bound {0};
569 std::vector < size_type > upper_bound {std::numeric_limits < size_type >::max()};
570 object sync_root;
571 };
572
574 };
575}
Abstract object that represent array.
Definition array_abstract_object.hpp:25
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
Base object that represent array.
Definition basic_array.hpp:27
const value_type & const_reference
Represents the const reference of array value type.
Definition basic_array.hpp:45
constexpr xtd::int64 get_long_length(size_type dimension) const
Gets a 64-bit integer that represents the total number of elements in all the dimensions of the array...
Definition basic_array.hpp:273
xtd::collections::generic::helpers::allocator< value_type > allocator_type
Represents the array allocator type.
Definition basic_array.hpp:35
virtual size_type rank() const noexcept
Gets the rank (number of dimensions) of the array.
Definition basic_array.hpp:139
virtual const_pointer data() const noexcept
Returns pointer to the underlying array serving as element storage.
Definition basic_array.hpp:83
void set_value(const type_t &value, const xtd::array< size_type > &indexes)
Sets a value to the element at the specified position in the multidimensional array.
Definition basic_array.hpp:342
const value_type & get_value(const xtd::array< size_type > &indexes) const
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
typename xtd::collections::generic::ienumerable< type_t >::const_iterator const_iterator
Represents the const iterator of array value type.
Definition basic_array.hpp:53
xtd::ptrdiff difference_type
Represents the array difference type (usually xtd::ptrdiff).
Definition basic_array.hpp:41
size_type count() const noexcept override
Gets the number of elements contained in the xtd::array <type_t>.
Definition basic_array.hpp:74
bool equals(const object &obj) const noexcept override
Determines whether the specified object is equal to the current object.
Definition basic_array.hpp:210
basic_array< type_t > & sort(const xtd::collections::generic::icomparer< type_t > &comparer)
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified compare...
Definition basic_array.hpp:373
constexpr size_type get_length(size_type dimension) const
Gets the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:260
void copy_to(xtd::array< type_t > &array) const
Copies the entire xtd::array <type_t> to a compatible one-dimensional array.
Definition basic_array.hpp:179
virtual xtd::int64 long_length()
Gets a 64-bit integer that represents the total number of elements in all the dimensions of the array...
Definition basic_array.hpp:128
const value_type * const_pointer
Represents the const pointer of array value type.
Definition basic_array.hpp:49
typename xtd::collections::generic::helpers::raw_array< value_type >::reverse_iterator reverse_iterator
Represents the reverse iterator of array value type.
Definition basic_array.hpp:55
bool contains(const type_t &value) const noexcept override
Determines whether an element is in the array.
Definition basic_array.hpp:170
value_type * pointer
Represents the pointer of array value type.
Definition basic_array.hpp:47
virtual pointer data() noexcept
Returns pointer to the underlying array serving as element storage.
Definition basic_array.hpp:79
bool is_synchronized() const noexcept override
Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is sync...
Definition basic_array.hpp:109
value_type & reference
Represents the reference of array value type.
Definition basic_array.hpp:43
size_type index_of(const type_t &value) const noexcept override
Determines the index of a specific item in the xtd::array <type_t>.
Definition basic_array.hpp:308
constexpr size_type get_lower_bound(size_type dimension) const
Gets the lower bound of the specified dimension in the array.
Definition basic_array.hpp:282
void resize(size_type new_size, value_type value)
Resizes the container to contain count elements, does nothing if count == length()....
Definition basic_array.hpp:330
type_t value_type
Represents the array value type.
Definition basic_array.hpp:33
virtual void fill(const value_type &value) noexcept
Assigns the value to all elements in the container.
Definition basic_array.hpp:220
basic_array & operator=(const basic_array &other)
Copy assignment operator. Replaces the contents with a copy of the contents of other.
Definition basic_array.hpp:403
typename xtd::collections::generic::helpers::raw_array< value_type, allocator_type >::base_type base_type
Represents the array base type.
Definition basic_array.hpp:37
virtual size_type length() const noexcept
Gets a size that represents the total number of elements in all the dimensions of the array.
Definition basic_array.hpp:124
const type_t & operator()(const xtd::array< size_type > &indexes) const
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
virtual const base_type & items() const noexcept
Returns the underlying base type items.
Definition basic_array.hpp:113
constexpr size_type get_upper_bound(size_type dimension) const
Gets the upper bound of the specified dimension in the array.
Definition basic_array.hpp:294
basic_array< type_t > & sort()
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the default comparer.
Definition basic_array.hpp:353
typename xtd::collections::generic::helpers::raw_array< value_type >::const_reverse_iterator const_reverse_iterator
Represents the const reverse iterator of array value type.
Definition basic_array.hpp:57
basic_array< type_t > & sort(xtd::size index, xtd::size count, const xtd::collections::generic::icomparer< type_t > &comparer)
Sorts the elements in a range of elements in xtd::collections::generic::list <type_t> using the speci...
Definition basic_array.hpp:385
basic_array< type_t > & sort(comparison_t &&comparison)
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified xtd::co...
Definition basic_array.hpp:361
xtd::string to_string() const noexcept override
Returns a xtd::string that represents the current object.
typename xtd::collections::generic::ienumerable< type_t >::iterator iterator
Represents the iterator of array value type.
Definition basic_array.hpp:51
const_reference operator[](size_type index) const override
Returns a reference to the element at specified location index.
Definition basic_array.hpp:424
bool is_fixed_size() const noexcept override
Gets a value indicating whether the xtd::collections::generic::ilist <type_t> has a fixed size.
Definition basic_array.hpp:88
bool is_read_only() const noexcept override
Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
Definition basic_array.hpp:93
xtd::array< size_type, 1 > get_lengths() const
Gets an array of the number of elements of all the dimensions of the array.
virtual base_type & items() noexcept
Returns the underlying base type items.
Definition basic_array.hpp:116
void copy_to(xtd::array< type_t > &array, size_type array_index) const override
Copies the elements of the xtd::array <type_t> to an xtd::array, starting at a particular xtd::array ...
Definition basic_array.hpp:187
const xtd::object & sync_root() const noexcept override
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
Definition basic_array.hpp:162
type_t & operator()(const xtd::array< size_type > &indexes)
Gets the value at the specified position in the multidimensional array. The indexes are specified as ...
virtual size_type max_length() const noexcept
Returns the maximum number of elements the container is able to hold due to system or library impleme...
Definition basic_array.hpp:132
xtd::size size_type
Represents the array size type (usually xtd::size).
Definition basic_array.hpp:39
static const comparer< xtd::any_object > default_comparer
Definition comparer.hpp:50
Provides a set of static methods for querying objects that implement ienumerable <type_t>.
Definition enumerable.hpp:32
internal_base_type base_type
Underlying vector type.
Definition raw_array.hpp:123
std::reverse_iterator< const_iterator > const_reverse_iterator
Const reverse iterator.
Definition raw_array.hpp:135
std::reverse_iterator< iterator > reverse_iterator
Reverse iterator.
Definition raw_array.hpp:134
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
typename xtd::collections::generic::extensions::enumerable_iterators< xtd::any_object, xtd::collections::generic::ienumerable< xtd::any_object > >::const_iterator const_iterator
Definition ienumerable.hpp:50
typename xtd::collections::generic::extensions::enumerable_iterators< xtd::any_object, xtd::collections::generic::ienumerable< xtd::any_object > >::iterator iterator
Definition ienumerable.hpp:48
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
static void throws(xtd::helpers::exception_case exception_case, const source_location &location=source_location::current())
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:22
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:44
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.hpp:115
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
std::allocator< type_t > allocator
Represent an allocator alias.
Definition allocator.hpp:38
@ 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
@ out_of_memory
Out of memory.
Definition exception_case.hpp:87
@ argument_null
The argument is null.
Definition exception_case.hpp:33
@ 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
xtd::basic_string< char > string
Represents text as a sequence of UTF-8 code units.
Definition __string_definitions.hpp:43
constexpr xtd::size npos
Represents a value that is not a valid position in a collection.
Definition npos.hpp:26
std::int64_t int64
Represents a 64-bit signed integer.
Definition int64.hpp:23
size_t size
Represents a size of any object in bytes.
Definition size.hpp:23
null_ptr null
Represents a null pointer value.
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::ptrdiff_t ptrdiff
Represent the signed integer type of the result of subtracting two pointers.
Definition ptrdiff.hpp:23
ptr< type_t > new_ptr(args_t &&... args)
The xtd::new_ptr operator creates a xtd::ptr object.
Definition new_ptr.hpp:24
@ other
The operating system is other.
Definition platform_id.hpp:60
@ add
The Add key.
Definition console_key.hpp:170
@ 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
@ insert
The INS (INSERT) key.
Definition console_key.hpp:62
size_type
Specifies how rows or columns of user interface (UI) elements should be sized relative to their conta...
Definition size_type.hpp:22
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
read_only_span< type_t, count > first() const
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:282
read_only_span< type_t, count > last() const
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:307
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:38
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