xtd 1.0.0
Loading...
Searching...
No Matches
list.hpp
Go to the documentation of this file.
1
4#pragma once
6#include "helpers/equator.hpp"
7#include "helpers/lesser.hpp"
9#include "comparer.hpp"
10#include "enumerable.hpp"
11#include "ilist.hpp"
12#define __XTD_CORE_INTERNAL__
14#undef __XTD_CORE_INTERNAL__
16#include "../../action.hpp"
17#include "../../comparison.hpp"
18#include "../../converter.hpp"
19#include "../../intptr.hpp"
20#include "../../is.hpp"
21#include "../../object.hpp"
22#include "../../optional.hpp"
23#include "../../new_ptr.hpp"
24#include "../../predicate.hpp"
25#include "../../raw_type.hpp"
27#include "../../self.hpp"
28#include "../../span.hpp"
29#include "../../string.hpp"
30
32namespace xtd {
34 namespace collections {
36 namespace generic {
83 template<typename type_t, typename allocator_t>
84 class list : public xtd::object, public xtd::collections::generic::ilist<type_t>, public xtd::iequatable<xtd::collections::generic::list<type_t, allocator_t >> {
85 public:
87
104 using const_pointer = const value_type*;
108
110
120 list() noexcept = default;
127 explicit list(size_type capacity) {self_.capacity(capacity);}
135 list(const xtd::collections::generic::ienumerable<type_t>& collection) requires std::copy_constructible<type_t> {
136 for (const auto& value : collection)
137 data_->items.push_back(value);
138 }
139
141 template<xtd::iterable iterable_t>
142 list(iterable_t&& items) {
143 for (const auto& item : items)
144 data_->items.push_back(item);
145 }
146
148 list(list& list) requires std::copy_constructible<type_t> {*data_ = *list.data_;}
151 list(const list& list) requires std::copy_constructible<type_t> {*data_ = *list.data_;}
155 data_ = std::move(list.data_);
156 list.data_ = new_ptr<list_data>();
157 }
158
160 list(const base_type& list) requires std::copy_constructible<type_t> {data_->items = list;}
163 list(base_type&& list) {data_->items = std::move(list);}
166 list(std::initializer_list<type_t> items) requires std::copy_constructible<type_t> {add_range(items);}
170 template <std::input_iterator input_iterator_t>
171 list(input_iterator_t first, input_iterator_t last) requires std::copy_constructible<type_t> {
172 for (auto iterator = first; iterator != last; ++iterator)
173 add(*iterator);
174 }
175
176
178
202 [[nodiscard]] auto capacity() const noexcept -> size_type {return data_->items.capacity();}
226 auto capacity(size_type value) -> void {
229 if (value == capacity()) return;
230 if (value < capacity()) data_->items.shrink_to_fit();
231 data_->items.reserve(value);
232 }
233
255 [[nodiscard]] auto count() const noexcept -> size_type override {return data_->items.size();}
256
260 [[nodiscard]] auto data() noexcept -> pointer override {return reinterpret_cast<pointer>(data_->items.data());}
264 [[nodiscard]] auto data() const noexcept -> const_pointer override {return reinterpret_cast<const_pointer>(data_->items.data());}
265
268 [[nodiscard]] const auto& items() const noexcept {return data_->items;}
271 [[nodiscard]] auto& items() noexcept {return data_->items;}
273
275
290 auto add(const type_t& item) -> void override {
291 if constexpr (std::copy_constructible<type_t>) data_->items.push_back(item);
292 else xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "value_type is not copy constructible.");
293 }
294
307 auto add(type_t&& item) -> void {data_->items.push_back(std::forward<type_t>(item));}
308
317 auto add_range(const xtd::collections::generic::ienumerable<type_t>& enumerable) -> void requires std::copy_constructible<type_t> {insert_range(count(), enumerable);}
318
327 auto add_range(std::initializer_list<type_t> il) -> void requires std::copy_constructible<type_t> {insert_range(count(), il);}
328
330 template<typename enumerable_t>
331 auto add_range(const enumerable_t& enumerable) -> void requires std::copy_constructible<type_t> {insert_range(count(), enumerable);}
333
338 [[nodiscard]] auto as_read_only() const noexcept -> read_only_collection requires std::copy_constructible<type_t> {return read_only_collection {self_};}
339
353 [[nodiscard]] auto binary_search(const type_t& item) const noexcept -> xtd::usize {return binary_search(0, count(), item, xtd::collections::generic::comparer<type_t>::default_comparer);}
365 [[nodiscard]] auto binary_search(const type_t& item, const xtd::collections::generic::icomparer<type_t>& comparer) const noexcept -> xtd::usize {return binary_search(0, count(), item, comparer);}
382 auto first = data_->items.begin();
383 auto last = data_->items.begin();
384 std::advance(first, index);
385 std::advance(last, index + count);
386 auto position = std::lower_bound(first, last, item, helpers::lesser<type_t> {comparer});
387
388 if (position != data_->items.end() && !comparer.compare(item, *position))
389 return std::distance(data_->items.begin(), position);
390 return ~std::distance(data_->items.begin(), position);
391 }
392
397 auto clear() -> void override {data_->items.clear();}
398
402 [[nodiscard]] auto contains(const type_t& value) const noexcept -> bool override {
403 return index_of(value) != npos;
404 }
405
413 template<typename output_t, typename converter_t>
414 [[nodiscard]] auto convert_all(converter_t converter) const -> list<output_t> {
415 auto result = list<output_t> {};
416 auto apply_converter = xtd::converter<output_t, const type_t&> {converter};
417 for (const auto& item : self_)
418 result.add(apply_converter(item));
419 return result;
420 }
421
431 auto copy_to(xtd::array<type_t>& array) const -> void requires std::copy_constructible<type_t> {copy_to(0, array, 0, count());}
439 auto copy_to(xtd::array<type_t>& array, size_type array_index) const -> void override {
440 if constexpr (std::copy_constructible<type_t>) copy_to(0, array, array_index, count());
441 else xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "value_type is not copy constructible.");
442 }
443
456 auto copy_to(size_type index, xtd::array<type_t>& array, size_type array_index, size_type count) const -> void requires std::copy_constructible<type_t> {
458 for (auto i = index; i < (index + count); ++i)
459 array[array_index++] = self_[i];
460 }
461
467 data_->items.reserve(capacity);
468 return self_.capacity();
469 }
470
477 [[nodiscard]] auto equals(const object& obj) const noexcept -> bool override {return is<list<value_type>>(obj) && equals(static_cast<const list<value_type>& > (obj));}
481 [[nodiscard]] auto equals(const list& obj) const noexcept -> bool override {
482 if (count() != obj.count()) return false;
483 for (size_type i = 0; i < count(); i++)
484 if (!helpers::equator<type_t> {}(self_[i], obj[i])) return false;
485 return true;
486 }
487
499 template<typename predicate_t>
500 [[nodiscard]] auto exists(predicate_t match) const -> bool {
501 return find_index(match) != npos;
502 }
503
515 template<typename predicate_t>
516 [[nodiscard]] auto find(predicate_t match) const -> optional<type_t> {
517 auto index = find_index(match);
518 if (index == npos) return nullopt;
519 return self_[index];
520 }
521
533 template<typename predicate_t>
534 [[nodiscard]] auto find_all(predicate_t match) const -> list {
536 auto result = list {};
537 for (const auto& item : self_)
538 if (predicate(item)) result.add(item);
539 return result;
540 }
541
547 template<typename predicate_t>
548 [[nodiscard]] auto find_index(predicate_t match) const -> xtd::usize {return find_index(0, count(), match);}
556 template<typename predicate_t>
557 [[nodiscard]] auto find_index(xtd::usize start_index, predicate_t match) const -> xtd::usize {return find_index(start_index, count() - start_index, match);}
566 template<typename predicate_t>
567 [[nodiscard]] auto find_index(xtd::usize start_index, xtd::usize count, predicate_t match) const -> xtd::usize {
570 for (auto index = start_index; index < start_index + count; ++index)
571 if (predicate(self_[index])) return index;
572 return npos;
573 }
574
586 template<typename predicate_t>
587 [[nodiscard]] auto find_last(predicate_t match) const -> optional<type_t> {
588 auto index = find_last_index(match);
589 if (index == npos) return nullopt;
590 return self_[index];
591 }
592
598 template<typename predicate_t>
599 [[nodiscard]] auto find_last_index(predicate_t match) const -> xtd::usize {return find_last_index(count() - 1, count(), match);}
607 template<typename predicate_t>
608 [[nodiscard]] auto find_last_index(xtd::usize start_index, predicate_t match) const -> xtd::usize {return find_last_index(start_index, start_index + 1, match);}
617 template<typename predicate_t>
618 [[nodiscard]] auto find_last_index(xtd::usize start_index, xtd::usize count, predicate_t match) const -> xtd::usize {
621 auto end_index = start_index + 1 - count;
622 for (auto index = start_index; ; --index) {
623 if (predicate(self_[index])) return index;
624 if (index == end_index) break;
625 }
626 return npos;
627 }
628
637 template<typename action_t>
638 auto for_each(action_t action) -> void {
639 auto apply_action = xtd::action<const type_t&> {action};
640 for (const auto& item : self_)
641 apply_action(item);
642 }
643
646 [[nodiscard]] enumerator<value_type> get_enumerator() const noexcept override {
647 struct list_enumerator : public ienumerator<value_type> {
648 explicit list_enumerator(const list& items, xtd::usize version) : items_(items), version_(version) {}
649
650 [[nodiscard]] const value_type& current() const override {
652 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.");
653 return items_[index_];
654 }
655
656 [[nodiscard]] bool move_next() override {
657 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.");
658 return ++index_ < items_.count();
659 }
660
661 void reset() override {
662 version_ = items_.data_->items.version();
663 index_ = list::npos;
664 }
665
666 private:
667 size_type index_ = list::npos;
668 const list& items_;
669 size_type version_ = 0;
670 };
671
672 return {new_ptr<list_enumerator>(self_, data_->items.version())};
673 }
674
687 [[nodiscard]] auto get_range(size_type index, size_type count) -> list {
689 return list<type_t> {data_->items.begin() + index, data_->items.begin() + index + count};
690 }
691
695 [[nodiscard]] auto index_of(const type_t& value) const noexcept -> size_type override {
696 if (count() == 0) return npos;
697 return index_of(value, 0, count());
698 }
699
704 [[nodiscard]] auto index_of(const type_t& value, size_type index) const -> size_type {return index_of(value, index, count() - index);}
711 [[nodiscard]] auto index_of(const type_t& value, size_type index, size_type count) const -> size_type {
712 return find_index(index, count, delegate_(const auto& n) {return helpers::equator<type_t> {}(n, value);});
713 }
714
720 auto insert(size_type index, const type_t& value) -> void override {
721 if constexpr (std::copy_constructible<type_t>) {
723 data_->items.insert(data_->items.begin() + index, value);
724 } else xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "value_type is not copy constructible.");
725 }
726
731 auto insert(size_type index, type_t&& value) -> void {
733 data_->items.insert(data_->items.begin() + index, std::forward<type_t>(value));
734 }
735
747
748 // If the collection is this instance, it must be copied to avoid an infinite loop.
749 if (static_cast<const void*>(&enumerable) == static_cast<const void*>(this)) {
751 return;
752 }
753
754 data_->items.insert(data_->items.begin() + index, enumerable.begin(), enumerable.end());
755 }
756
765 auto insert_range(size_type index, const std::initializer_list<type_t>& items) -> void {
767 data_->items.insert(data_->items.begin() + index, items.begin(), items.end());
768 }
769
771 template<typename collection_t>
772 auto insert_range(size_type index, const collection_t& items) -> void {
774
775 // If the collection is this instance, it must be copied to avoid an infinite loop.
776 if (static_cast<const void*>(&items) == static_cast<const void*>(this)) {
778 return;
779 }
780
781 data_->items.insert(data_->items.begin() + index, items.begin(), items.end());
782 }
784
788 [[nodiscard]] auto last_index_of(const type_t& value) const noexcept -> size_type {
789 if (count() == 0) return npos;
790 return last_index_of(value, count() - 1, count());
791 }
792
797 [[nodiscard]] auto last_index_of(const type_t& value, size_type index) const -> size_type {
798 return last_index_of(value, index, index + 1);
799 }
806 [[nodiscard]] auto last_index_of(const type_t& value, size_type index, size_type count) const -> size_type {
807 return find_last_index(index, count, delegate_(const auto& n) {return helpers::equator<type_t> {}(n, value);});
808 }
809
815 auto remove(const type_t& item) noexcept -> bool override {
816 auto index = index_of(item);
817 if (index == npos) return false;
819 return true;
820 }
821
833 template<typename predicate_t>
834 auto remove_all(predicate_t match) -> xtd::usize {
836 auto count = xtd::usize {0};
837 auto iterator = data_->items.begin();
838 while (iterator != data_->items.end())
839 if (!predicate(*iterator)) iterator++;
840 else {
841 iterator = data_->items.erase(iterator);
842 ++count;
843 }
844 return count;
845 }
846
850 auto remove_at(size_type index) -> void override {
852 data_->items.erase(data_->items.begin() + index);
853 }
854
865 data_->items.erase(data_->items.begin() + index, data_->items.begin() + index + count);
866 }
867
873 virtual auto resize(size_type count) -> void {
874 if constexpr (std::copy_constructible<type_t>) {
875 resize(count, value_type {});
876 } else xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "value_type is not copy constructible.");
877 }
884 virtual auto resize(size_type count, const value_type& value) -> void {
885 if constexpr (std::copy_constructible<type_t>) {
887 if (count == self_.count()) return;
888 data_->items.resize(count, value);
889 } else xtd::helpers::throw_helper::throws(xtd::helpers::exception_case::invalid_operation, "value_type is not copy constructible.");
890 }
891
898 auto reverse() -> list<type_t>& {return reverse(0, count());}
910 data_->items.increment_version();
911 std::reverse(data_->items.begin() + index, data_->items.begin() + index + count);
912 return self_;
913 }
914
920 [[nodiscard]] auto slice(size_type start, size_type length) const -> xtd::read_only_span<type_t> {
922 return xtd::read_only_span<type_t> {data_->items.begin() + start, data_->items.begin() + start + length};
923 }
924
929 [[nodiscard]] auto slice(size_type start, size_type length) -> xtd::span<type_t> {
931 return xtd::span<type_t> {data_->items.begin() + start, data_->items.begin() + start + length};
932 }
933
952 auto sort(xtd::callable<bool, const type_t&, const type_t&> auto&& less_comparison) -> list<type_t>& {return sort(0, count(), less_comparison);}
961 auto sort(xtd::callable<std::strong_ordering, const type_t&, const type_t&> auto&& ordering_comparison) -> list<type_t>& {return sort(0, count(), ordering_comparison);}
981 data_->items.increment_version();
982 std::sort(data_->items.begin() + index, data_->items.begin() + index + count, less_comparison);
983 return self_;
984 }
985 auto sort(xtd::usize index, xtd::usize count, xtd::callable<std::strong_ordering, const type_t&, const type_t&> auto&& ordering_comparison) -> list<type_t>& {return sort(index, count, [ordering_comparison](const type_t& x, const type_t& y) {return ordering_comparison(x, y) == std::strong_ordering::less;});}
986 auto sort(xtd::usize index, xtd::usize count, xtd::callable<xtd::int32, const type_t&, const type_t&> auto&& comparison) -> list<type_t>& {return sort(index, count, [comparison](const type_t& x, const type_t& y) {return comparison(x, y) < 0;});}
1002
1010 [[nodiscard]] auto to_array() const noexcept -> xtd::array<value_type> requires std::copy_constructible<type_t> {return count() ? xtd::array<value_type>(data_->items.begin(), data_->items.end()) : xtd::array<value_type> {};}
1011
1014 [[nodiscard]] auto to_string() const noexcept -> xtd::string override {return xtd::string::format("[{}]", xtd::string::join(", ", self_));}
1015
1036 auto trim_excess() -> void {data_->items.shrink_to_fit();}
1037
1049 template<typename prediacate_t>
1050 [[nodiscard]] auto true_for_all(prediacate_t match) const -> bool {
1052 for (const auto& item : self_)
1053 if (!predicate(item)) return false;
1054 return true;
1055 }
1056
1057
1059
1064 auto operator =(const list& other) -> list& requires std::copy_constructible<type_t> = default;
1068 auto operator =(list&& other) noexcept -> list& {
1069 data_->items = std::move(other.data_->items);
1070 return self_;
1071 }
1072
1075 auto operator =(const std::initializer_list<type_t>& items) -> list& requires std::copy_constructible<type_t> {
1076 data_->items = items;
1077 return self_;
1078 }
1079
1080 using xtd::collections::generic::ilist<type_t>::operator [];
1081 using xtd::collections::generic::ilist<type_t>::operator ();
1086 auto operator [](size_type index) const -> const_reference override {
1088 return data_->items[index];
1089 }
1090
1096 return data_->items[index];
1097 }
1098
1101 operator const_base_type& () const noexcept {return data_->items;}
1104 operator base_type& () noexcept {return data_->items;}
1106
1107 private:
1108 auto is_fixed_size() const noexcept -> bool override {return false;}
1109 auto is_read_only() const noexcept -> bool override {return false;}
1110 auto is_synchronized() const noexcept -> bool override {return false;}
1111 const xtd::object& sync_root() const noexcept override {return data_->sync_root;}
1112
1113 struct list_data {
1114 xtd::collections::generic::helpers::raw_array<value_type, allocator_t> items;
1115 xtd::object sync_root;
1116 };
1117
1119 };
1120
1122 // Deduction guides for xtd::collections::generic::list
1123 // {
1124 template<typename type_t>
1125 list(std::initializer_list<type_t>) -> list<type_t>;
1126
1127 template<typename type_t>
1129
1130 template<xtd::iterable iterable_t>
1132
1133 template<typename type_t>
1134 list(const ilist<type_t>&) -> list<type_t>;
1135
1136 template<typename type_t>
1137 list(const std::vector<type_t>&) -> list<type_t>;
1138
1139 template<typename type_t, typename allocator_t = helpers::allocator<type_t>>
1141
1142 template<typename type_t, typename allocator_t = helpers::allocator<type_t>>
1144
1145 template<typename type_t>
1146 list(std::vector<type_t>&&) -> list<type_t>;
1147
1148 template<typename type_t, typename allocator_t = helpers::allocator<type_t>>
1150
1151 template<typename input_iterator_t>
1152 list(input_iterator_t, input_iterator_t) -> list<std::iter_value_t<input_iterator_t>>;
1153 // }
1155 }
1156 }
1157}
1158
1161 template<typename source_t, typename enumerable_t>
1162 inline auto enumerable<source_t, enumerable_t>::to_list() const -> xtd::collections::generic::list<source_t> {
1163 return xtd::linq::enumerable::to_list(self());
1164 }
1165}
1166
1167template<xtd::iterable source_t>
1168auto xtd::linq::enumerable::as_enumerable(source_t&& source) noexcept {
1169 if constexpr(xtd::collections::generic::enumerable<source_t>) return std::move(source);
1171}
1172
1173template<typename key_t, xtd::iterable source_t, xtd::callable<key_t, xtd::iterable_value_type<source_t>> key_selector_t, xtd::callable<bool, key_t, key_t> key_equater_t>
1174auto xtd::linq::enumerable::count_by(source_t&& source, key_selector_t&& key_selector, key_equater_t&& key_equater) noexcept -> xtd::collections::generic::enumerable_generator<xtd::collections::generic::key_value_pair<key_t, xtd::usize>> {
1175 auto result = list<key_value_pair<key_t, xtd::usize>> {};
1176 auto keys = list<key_t> {};
1177 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
1178 //auto enumerator = source_holder.get().get_enumerator();
1179 auto enumerator = source.get_enumerator();
1180 while (enumerator.move_next()) {
1181 auto key = key_selector(enumerator.current());
1182 auto index = xtd::usize {0};
1183 for (; index < keys.count(); ++index)
1184 if (key_equater(keys[index], key)) break;
1185 if (index < keys.count()) result[index] = {key, result[index].value() + 1};
1186 else {
1187 keys.add(key);
1188 result.add({key, 1});
1189 }
1190 }
1191
1192 for (const auto& item : result)
1193 co_yield item;
1194}
1195
1196template<xtd::iterable source_t>
1198 auto result = list<xtd::iterable_value_type<source_t>> {};
1199 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
1200 //for (const auto& item : source_holder.get())
1201 for (const auto& item : source)
1202 if (!contains(result, item))
1203 result.add(item);
1204
1205 for (const auto& item : result)
1206 co_yield item;
1207}
1208
1209template<xtd::iterable source_t>
1210auto xtd::linq::enumerable::distinct(source_t&& source, const iequality_comparer<source_t>& comparer) noexcept -> xtd::collections::generic::enumerable_generator<xtd::iterable_value_type<source_t>> {
1211 auto result = list<xtd::iterable_value_type<source_t>> {};
1212 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
1213 //for (const auto& item : source_holder.get())
1214 for (const auto& item : source)
1215 if (!contains(result, item, comparer))
1216 result.add(item);
1217
1218 for (const auto& item : result)
1219 co_yield item;
1220}
1221
1222template<xtd::iterable source_t, xtd::func_callable<bool, xtd::iterable_value_type<source_t>, xtd::iterable_value_type<source_t>> equater_t>
1224 auto result = list<xtd::iterable_value_type<source_t>> {};
1225 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
1226 //for (const auto& item : source_holder.get())
1227 for (const auto& item : source)
1228 if (!contains(result, item, equater))
1229 result.add(item);
1230
1231 for (const auto& item : result)
1232 co_yield item;
1233}
1234
1235template<xtd::iterable source_t>
1236auto xtd::linq::enumerable::from(source_t&& source) noexcept {
1237 return as_enumerable(std::forward<source_t>(source));
1238}
1239
1240
1241template<xtd::iterable source_t, xtd::func_callable<bool, xtd::iterable_value_type<source_t>, xtd::iterable_value_type<source_t>> lesser_t>
1243 auto result = list<xtd::iterable_value_type<source_t>> {};
1244 //auto source_holder = enumerable_holder<source_t> {std::forward<source_t>(source)};
1245 //for (const auto& item : source_holder.get())
1246 for (const auto& item : source)
1247 result.add(item);
1248 std::sort(result.items().begin(), result.items().end(), lesser);
1249 for (const auto& item : result)
1250 co_yield item;
1251}
1252
1253template<typename key_t, xtd::iterable source_t, xtd::callable<key_t, xtd::iterable_value_type<source_t>> key_selector_t>
1255 auto result = list<xtd::iterable_value_type<source_t>> {};
1256 for (const auto& item : source)
1257 result.add(item);
1258 std::sort(result.items().begin(), result.items().end(), [key_selector](auto&& a, auto&& b) {return key_selector(a) < key_selector(b);});
1259 for (const auto& item : result)
1260 co_yield item;
1261}
1262
1263template<xtd::iterable source_t, xtd::callable<xtd::iterable_value_type<source_t>, xtd::iterable_value_type<source_t>> key_selector_t>
1265 auto result = list<xtd::iterable_value_type<source_t>> {};
1266 for (const auto& item : source)
1267 result.add(item);
1268 std::sort(result.items().begin(), result.items().end(), [key_selector](auto&& a, auto&& b) {return key_selector(a) < key_selector(b);});
1269 for (const auto& item : result)
1270 co_yield item;
1271}
1272
1273template<typename key_t, xtd::iterable source_t, xtd::callable<key_t, xtd::iterable_value_type<source_t>> key_selector_t>
1275 auto result = list<xtd::iterable_value_type<source_t>> {};
1276 for (const auto& item : source)
1277 result.add(item);
1278 std::sort(result.items().begin(), result.items().end(), [key_selector](auto&& a, auto&& b) {return key_selector(a) > key_selector(b);});
1279 for (const auto& item : result)
1280 co_yield item;
1281}
1282
1283template<xtd::iterable source_t, xtd::callable<xtd::iterable_value_type<source_t>, xtd::iterable_value_type<source_t>> key_selector_t>
1285 auto result = list<xtd::iterable_value_type<source_t>> {};
1286 for (const auto& item : source)
1287 result.add(item);
1288 std::sort(result.items().begin(), result.items().end(), [key_selector](auto&& a, auto&& b) {return key_selector(a) > key_selector(b);});
1289 for (const auto& item : result)
1290 co_yield item;
1291}
1292
1293template<typename value_t>
1295 return list<value_t>(source);
1296}
Contains xtd::action delegate.
Contains xtd::collections::generic::helpers::allocator alias.
Provides methods for creating, manipulating, searching, and sorting arrays, thereby serving as the ba...
Definition array.hpp:64
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
Provides a base class for implementations of the xtd::collections::generic::icomparer <type_t> generi...
Definition comparer.hpp:33
auto compare(const first_argument_type &x, const second_argument_type &y) const -> result_type override
Compares two entities and returns a value indicating whether one is less than, equal to,...
Definition comparer.hpp:66
static const comparer< xtd::any_object > default_comparer
Definition comparer.hpp:50
Represents an enumerable generator that supports deferred, lazy iteration over a collection of a spec...
Definition enumerable_generator.hpp:44
auto to_list() const -> xtd::collections::generic::list< value_t >
Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <ty...
value_t value_type
Represents the ienumerable value type.
Definition enumerable.hpp:40
internal_base_type base_type
Underlying vector type.
Definition raw_array.hpp:132
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 > >::iterator iterator
Definition ienumerable.hpp:48
Supports a simple iteration over a generic collection.
Definition ienumerator.hpp:58
Represents a collection of objects that can be individually accessed by index.
Definition ilist.hpp:42
static constexpr xtd::usize npos
Definition ilist.hpp:55
virtual auto is_fixed_size() const noexcept -> bool=0
Gets a value indicating whether the xtd::collections::generic::ilist <type_t> has a fixed size.
virtual auto is_synchronized() const noexcept -> bool=0
Gets a value indicating whether access to the xtd::collections::generic::icollection <type_t> is sync...
virtual auto count() const noexcept -> xtd::usize=0
Gets the number of elements contained in the xtd::collections::generic::icollection <type_t>.
virtual auto sync_root() const noexcept -> const xtd::object &=0
Gets an object that can be used to synchronize access to the the xtd::collections::generic::icollecti...
virtual auto is_read_only() const noexcept -> bool=0
Gets a value indicating whether the xtd::collections::generic::icollection <type_t> is read-only.
Represents a strongly typed list of objects that can be accessed by index. Provides methods to search...
Definition list.hpp:84
auto last_index_of(const type_t &value, size_type index, size_type count) const -> size_type
Determines the last index of a specific item in the xtd::collections::generic::list <type_t>.
Definition list.hpp:806
auto to_array() const noexcept -> xtd::array< value_type >
Copies the elements of the xtd::collections::generic::list <type_t> to a new array.
Definition list.hpp:1010
auto count() const noexcept -> size_type override
Gets the number of elements contained in the xtd::collections::generic::list <type_t>.
Definition list.hpp:255
list(std::initializer_list< type_t > items)
Constructs the container with the contents of the specified initializer list, and allocator.
Definition list.hpp:166
auto data() const noexcept -> const_pointer override
Returns pointer to the underlying array serving as element storage.
Definition list.hpp:264
auto operator=(const list &other) -> list &=default
Copy assignment operator. Replaces the contents with a copy of the contents of other.
auto index_of(const type_t &value) const noexcept -> size_type override
Determines the index of a specific item in the xtd::collections::generic::list <type_t>.
Definition list.hpp:695
auto equals(const object &obj) const noexcept -> bool override
Determines whether the specified object is equal to the current object.
Definition list.hpp:477
auto find_index(xtd::usize start_index, xtd::usize count, predicate_t match) const -> xtd::usize
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:567
auto sort(xtd::callable< bool, const type_t &, const type_t & > auto &&less_comparison) -> list< type_t > &
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified xtd::co...
Definition list.hpp:952
auto operator[](size_type index) const -> const_reference override
Returns a reference to the element at specified location index.
Definition list.hpp:1086
auto insert(size_type index, const type_t &value) -> void override
Determines the index of a specific item in the xtd::collections::generic::list <type_t>.
Definition list.hpp:720
list(base_type &&list)
Move constructor with specified base type list.
Definition list.hpp:163
auto sort() -> list< type_t > &
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the default comparer.
Definition list.hpp:943
auto find_last_index(xtd::usize start_index, xtd::usize count, predicate_t match) const -> xtd::usize
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:618
const value_type * const_pointer
Definition list.hpp:104
const base_type const_base_type
Definition list.hpp:94
auto last_index_of(const type_t &value) const noexcept -> size_type
Determines the last index of a specific item in the xtd::collections::generic::list <type_t>.
Definition list.hpp:788
auto sort(xtd::usize index, xtd::usize count, const xtd::collections::generic::icomparer< type_t > &comparer) -> list< type_t > &
Sorts the elements in a range of elements in xtd::collections::generic::list <type_t> using the speci...
Definition list.hpp:1001
auto slice(size_type start, size_type length) -> xtd::span< type_t >
Creates a shallow copy of a range of elements in the source xtd::collections::generic::list <type_t>.
Definition list.hpp:929
auto insert(size_type index, type_t &&value) -> void
Inserts an element into the xtd::collections::generic::list <type_t> at the specified index.
Definition list.hpp:731
auto remove_range(size_type index, size_type count) -> void
Removes a range of elements from the xtd::collections::generic::list <type_t>.
Definition list.hpp:863
auto index_of(const type_t &value, size_type index) const -> size_type
Determines the index of a specific item in the xtd::collections::generic::list <type_t>.
Definition list.hpp:704
auto ensure_capacity(xtd::usize capacity) -> xtd::usize
Ensures that the capacity of this list is at least the specified capacity. If the current capacity is...
Definition list.hpp:466
auto remove(const type_t &item) noexcept -> bool override
Removes the first occurrence of a specific object from the xtd::collections::generic::list <type_t>.
Definition list.hpp:815
auto to_string() const noexcept -> xtd::string override
Returns a xtd::string that represents the current object.
Definition list.hpp:1014
auto add(const type_t &item) -> void override
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.hpp:290
auto reverse() -> list< type_t > &
Resizes the container to contain count elements, does nothing if count == size(). @param count The ne...
Definition list.hpp:898
auto remove_all(predicate_t match) -> xtd::usize
Removes all the elements that match the conditions defined by the specified predicate.
Definition list.hpp:834
auto find_last(predicate_t match) const -> optional< type_t >
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:587
xtd::usize size_type
Definition list.hpp:96
auto find_last_index(xtd::usize start_index, predicate_t match) const -> xtd::usize
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:608
auto slice(size_type start, size_type length) const -> xtd::read_only_span< type_t >
Creates a shallow copy of a range of elements in the source xtd::collections::generic::list <type_t>.
Definition list.hpp:920
auto add_range(const xtd::collections::generic::ienumerable< type_t > &enumerable) -> void
Adds copy of elements from the specified collection to the end of the xtd::collections::generic::list...
Definition list.hpp:317
auto data() noexcept -> pointer override
Returns pointer to the underlying array serving as element storage.
Definition list.hpp:260
list() noexcept=default
Initializes a new instance of the xtd::collections::generic::list class that is empty.
list(const list &list)
Default copy constructor with specified list.
Definition list.hpp:151
auto & items() noexcept
Returns the underlying base type items.
Definition list.hpp:271
auto add_range(std::initializer_list< type_t > il) -> void
Adds copy of elements from the specified collection to the end of the xtd::collections::generic::list...
Definition list.hpp:327
const value_type & const_reference
Definition list.hpp:100
auto capacity(size_type value) -> void
Sets the total number of elements the internal data structure can hold without resizing.
Definition list.hpp:226
auto insert_range(size_type index, const std::initializer_list< type_t > &items) -> void
Inserts copy of elements from a collection into the xtd::collections::generic::list <type_t> at the s...
Definition list.hpp:765
auto for_each(action_t action) -> void
Performs the specified action on each element of the xtd::collections::generic::list <type_t>.
Definition list.hpp:638
auto sort(xtd::usize index, xtd::usize count, xtd::callable< bool, const type_t &, const type_t & > auto &&less_comparison) -> list< type_t > &
Sorts the elements in a range of elements in xtd::collections::generic::list <type_t> using the speci...
Definition list.hpp:979
list(const xtd::collections::generic::ienumerable< type_t > &collection)
Initializes a new instance of the xtd::collections::generic::list <type_t> class that contains elemen...
Definition list.hpp:135
auto contains(const type_t &value) const noexcept -> bool override
Determines whether an element is in the xtd::colllections::generic::list <type_t>.
Definition list.hpp:402
list(list &list)
Default copy constructor with specified list.
Definition list.hpp:148
list(const base_type &list)
Copy constructor with specified base type list.
Definition list.hpp:160
auto sort(xtd::callable< xtd::int32, const type_t &, const type_t & > auto &&comparison) -> list< type_t > &
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified xtd::co...
Definition list.hpp:970
typename ilist< xtd::any_object >::value_type value_type
Definition list.hpp:90
list(list &&list)
Move constructor with specified list.
Definition list.hpp:154
list(iterable_t &&items)
Initializes a new instance of the xtd::iterable that contains elements copied from the specified coll...
Definition list.hpp:142
auto exists(predicate_t match) const -> bool
Determines whether the xtd::collections::generic::list <type_t> contains elements that match the cond...
Definition list.hpp:500
auto true_for_all(prediacate_t match) const -> bool
Determines whether every element in the xtd::collections::generic::list <type_t> matches the conditio...
Definition list.hpp:1050
value_type & reference
Definition list.hpp:98
auto copy_to(xtd::array< type_t > &array, size_type array_index) const -> void override
Copies the entire xtd::colllections::generic::list <type_t> to a compatible one-dimensional array,...
Definition list.hpp:439
enumerator< value_type > get_enumerator() const noexcept override
Returns an enumerator that iterates through the xtd::collections::generic::list <type_t>.
Definition list.hpp:646
auto sort(xtd::callable< std::strong_ordering, const type_t &, const type_t & > auto &&ordering_comparison) -> list< type_t > &
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified xtd::co...
Definition list.hpp:961
auto get_range(size_type index, size_type count) -> list
Creates a shallow copy of a range of elements in the source xtd::collections::generic::list <type_t>.
Definition list.hpp:687
auto binary_search(xtd::usize index, xtd::usize count, const type_t &item, const xtd::collections::generic::icomparer< type_t > &comparer) const -> xtd::usize
Searches a range of elements in the sorted xtd::collections::generic::list <type_t> for an element us...
Definition list.hpp:380
auto copy_to(xtd::array< type_t > &array) const -> void
Copies the entire xtd::collections::generic::list <type_t> to a compatible one-dimensional array.
Definition list.hpp:431
typename xtd::collections::generic::helpers::raw_array< value_type >::base_type base_type
Definition list.hpp:92
auto binary_search(const type_t &item, const xtd::collections::generic::icomparer< type_t > &comparer) const noexcept -> xtd::usize
Searches the entire sorted xtd::collections::generic::list <type_t> for an element using the specifie...
Definition list.hpp:365
auto copy_to(size_type index, xtd::array< type_t > &array, size_type array_index, size_type count) const -> void
Copies the entire xtd::collections::generic::list <type_t> to a compatible one-dimensional array,...
Definition list.hpp:456
auto equals(const list &obj) const noexcept -> bool override
Indicates whether the current object is equal to another object of the same type.
Definition list.hpp:481
auto clear() -> void override
Removes all elements from the xtd::collections::generic::list <type_t>.
Definition list.hpp:397
auto capacity() const noexcept -> size_type
Definition list.hpp:202
auto find_last_index(predicate_t match) const -> xtd::usize
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:599
auto remove_at(size_type index) -> void override
Removes the element at the specified index of the xtd::collections::generic::list <type_t>.
Definition list.hpp:850
auto find_index(xtd::usize start_index, predicate_t match) const -> xtd::usize
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:557
value_type * pointer
Definition list.hpp:102
auto reverse(size_type index, size_type count) -> list< type_t > &
Reverses the order of the elements in the specified range.
Definition list.hpp:908
auto find_index(predicate_t match) const -> xtd::usize
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:548
auto find_all(predicate_t match) const -> list
Retrieves all the elements that match the conditions defined by the specified predicate.
Definition list.hpp:534
auto binary_search(const type_t &item) const noexcept -> xtd::usize
Searches the entire sorted xtd::collections::generic::list <type_t> for an element using the default ...
Definition list.hpp:353
const auto & items() const noexcept
Definition list.hpp:268
xtd::collections::object_model::read_only_collection< value_type > read_only_collection
Definition list.hpp:106
auto find(predicate_t match) const -> optional< type_t >
Searches for an element that matches the conditions defined by the specified predicate,...
Definition list.hpp:516
auto insert_range(size_type index, const xtd::collections::generic::ienumerable< type_t > &enumerable) -> void
Inserts copy of elements from a collection into the xtd::collections::generic::list <type_t> at the s...
Definition list.hpp:745
auto convert_all(converter_t converter) const -> list< output_t >
Converts the elements in the current xtd::colllections::generic::list <type_t> to another type,...
Definition list.hpp:414
auto sort(const xtd::collections::generic::icomparer< type_t > &comparer) -> list< type_t > &
Sorts the elements in the entire xtd::collections::generic::list <type_t> using the specified compare...
Definition list.hpp:992
auto trim_excess() -> void
Sets the capacity to the actual number of elements in the xtd::collections::generic::list <type_t>,...
Definition list.hpp:1036
list(input_iterator_t first, input_iterator_t last)
Constructs the container with the contents of the range [first, last).
Definition list.hpp:171
auto as_read_only() const noexcept -> read_only_collection
Returns a read-only xtd::collections::object_model::read_only_collection <type_t> wrapper for the cur...
Definition list.hpp:338
auto add(type_t &&item) -> void
Adds an object to the end of the xtd::collections::generic::list <type_t>.
Definition list.hpp:307
Provides the base class for a generic read-only collection.
Definition read_only_collection.hpp:39
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
static auto order_by_descending(source_t &&source, key_selector_t &&key_selector) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
Sorts the elements of a sequence in descending order according to a key.
static auto distinct(source_t &&source) noexcept -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
Returns distinct elements from a sequence by using the default equality comparer to compare values.
typename xtd::collections::generic::list< type_t > list
Represents the list value type.
Definition enumerable_.hpp:94
static auto count_by(source_t &&source, key_selector_t &&key_selector) noexcept -> xtd::collections::generic::enumerable_generator< xtd::collections::generic::key_value_pair< key_t, xtd::usize > >
Returns the count of elements in the source sequence grouped by key.
static auto from(source_t &&source) noexcept
Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
static auto order(source_t &&source) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
Sorts the elements of a sequence in ascending order.
static auto as_enumerable(source_t &&source) noexcept
Returns the input typed as xtd::collections::generic::ienumerable <type_t>.
static auto order_by(source_t &&source, key_selector_t &&key_selector) -> xtd::collections::generic::enumerable_generator< xtd::iterable_value_type< source_t > >
Sorts the elements of a sequence in ascending order according to a key.
static auto to_list(const ienumerable< source_t > &source)
Creates a xtd::collections::generic::list <type_t> from an xtd::collections::generic::ienumerable <ty...
Supports all classes in the xtd class hierarchy and provides low-level services to derived classes....
Definition object.hpp:40
Definition __span_definitions.hpp:16
Represents a non-owning view over a contiguous sequence of objects.
Definition span.hpp:62
Represents the version number of an assembly, operating system, or the xtd. This class cannot be inhe...
Definition version.hpp:115
Contains xtd::collections::generic::enumerable concept.
Contains xtd::comparison delegate.
Definition callable.hpp:11
Definition enumerable.hpp:17
Contains xtd::converter alias.
Contains xtd::collections::generic::helpers::equator struct.
Contains xtd::collections::generic::comparer <type_t> class.
Contains xtd::collections::generic::ilist <type_t> interface.
generic::enumerator< xtd::any_object > enumerator
Supports a simple iteration over a non-generic collection.
Definition enumerator.hpp:28
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
xtd::delegate< output_t(input_t input)> converter
Represents a method that converts an object from one type to another type.
Definition converter.hpp:33
static auto format(const basic_string< char > &fmt, args_t &&... args) -> basic_string
@ 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_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
#define delegate_
The declaration of a delegate type is similar to a method signature. It has a return value and any nu...
Definition delegate.hpp:1018
xtd::sptr< type_t > ptr
The xtd::ptr object is a shared pointer.
Definition ptr.hpp:27
std::optional< type_t > optional
Represents the optional alias on std::optional.
Definition optional.hpp:26
std::size_t usize
Represents an unsigned size of any object in bytes.
Definition usize.hpp:22
constexpr null_opt nullopt
Represents a nullopt value. Used to indicate that an std::optional does not contain a value.
Definition nullopt.hpp:26
xtd::func< bool, type_t > predicate
Represents a delegate that defines a set of criteria and determines whether the specified object meet...
Definition predicate.hpp:16
auto is(xtd::any value) -> bool
Checks if the result of an expression is compatible with a given type.
Definition is.hpp:485
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
@ other
The operating system is other.
Definition platform_id.hpp:60
@ y
The Y key.
Definition console_key.hpp:136
@ n
The N key.
Definition console_key.hpp:114
@ i
The I key.
Definition console_key.hpp:104
@ x
The X key.
Definition console_key.hpp:134
keys
Specifies key codes and modifiers.
Definition keys.hpp:77
Contains xtd::intptr type.
Contains xtd::is method.
Contains xtd::collections::generic::helpers::lesser struct.
The xtd::extensions namespace contains interface extensions.
Definition collection_common.hpp:14
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
The xtd namespace contains all fundamental classes to access Hardware, Os, System,...
Definition abstract_object.hpp:8
auto first() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the first count elements of the sequence.
Definition read_only_span.hpp:245
xtd::usize size_type
Represents the read_only_span size type (usually xtd::usize).
Definition read_only_span.hpp:65
auto last() const -> read_only_span< type_t, count >
Obtains a subspan consisting of the last N elements of the sequence.
Definition read_only_span.hpp:286
constexpr auto length() const noexcept -> size_type
Returns the length of the current read_only_span.
Definition read_only_span.hpp:209
Contains xtd::new_ptr method.
Contains xtd::object class.
Contains xtd::optional type.
Contains xtd::predicate delegate.
Contains xtd::collections::generic::helpers::raw_array class.
Contains xtd::raw_type alias.
Contains xtd::collections::object_model::read_only_collection class.
Contains xtd::read_only_span class.
Contains self_ keyword.
Contains xtd::span class.
Contains xtd::string alias.
Supports a simple iteration over a generic collection.
Definition enumerator.hpp:39
const type_t & current() const override
Gets the element in the collection at the current position of the enumerator.
Definition enumerator.hpp:63
bool move_next() override
Advances the enumerator to the next element of the collection.
Definition enumerator.hpp:72
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
static const index end
Represents a value that is not a valid position in a collection.
Definition index.hpp:136
Represents a value_type struct.
Definition value_type.hpp:34