4#include <system/details/collections_helper.h>
5#include <system/details/comparer_type.h>
6#include <system/object.h>
7#include <system/collections/ilist.h>
8#include <system/array.h>
9#include <system/exceptions.h>
10#include <system/collections/base_enumerator.h>
11#include <system/collections/icomparer.h>
12#include <system/collections/algorithms.h>
13#include <system/collections/read_only_collection.h>
14#include <system/predicate.h>
15#include <system/comparison.h>
16#include <system/action.h>
17#include <system/converter.h>
18#include <system/cycles_detection.h>
19#include <system/multicast_delegate.h>
28namespace System {
namespace Collections {
namespace Generic {
31template<
typename T>
class List;
49 typename std::vector<T>::reference
operator[](
int idx) {
return (*this)->operator[](idx); }
53 typename std::vector<T>::const_reference
operator[](
int idx)
const {
return (*this)->operator[](idx); }
124class ASPOSECPP_SHARED_CLASS
List
128 ASPOSE_COLLECTION_POINTER_MODE_CONTROL(T)
142 typedef std::vector<T, ASPOSE_COLLECTION_ALLOCATOR_TYPE>
vector_t;
166 List() : ASPOSE_COLLECTION_INIT_ALLOCATOR() {}
170 List(
int capacity) : ASPOSE_COLLECTION_INIT_ALLOCATOR()
174 throw ArgumentOutOfRangeException(u
"capacity");
176 m_data.reserve(capacity);
183 AddRange(collection);
192 for (
int i = 0; i < size; ++i)
193 m_data.push_back(inits[i]);
256 return MakeObject<Enumerator>(std::move(
MakeSharedPtr(
this)));
266 return (
int)m_data.size();
270 void Add(
const T& item)
override
272 m_data.push_back(item);
284 return FindInData(m_data.begin(), m_data.end(), item) != m_data.end();
291 iterator it = FindInData(m_data.begin(), m_data.end(), item);
292 if (it != m_data.end())
306 this->CopyTo(0, array, arrayIndex, ASPOSECPP_CHECKED_CAST(
int, m_data.size()));
317 const_iterator it = FindInData(m_data.begin(), m_data.end(), item);
318 if (it != m_data.end())
320 return ASPOSECPP_CHECKED_CAST(
int, it - m_data.begin());
328 void Insert(
int index,
const T& item)
override
330 if (index < 0 ||
static_cast<size_t>(index) > m_data.size()) {
331 throw ArgumentOutOfRangeException(u
"index");
334 m_data.insert(m_data.begin() + index, item);
340 if (Details::IsOutOfBounds(index, m_data)) {
341 throw ArgumentOutOfRangeException(u
"index");
343 m_data.erase(m_data.begin() + index);
351 return operator[](index);
358 operator[](index) = value;
366 return (
int)m_data.capacity();
372 if (capacity < (
int)m_data.size()) {
373 throw ArgumentOutOfRangeException(u
"capacity");
375 m_data.reserve(capacity);
383 InsertRange(
static_cast<int>(m_data.size()), collection);
390 return MakeObject<System::Collections::ObjectModel::ReadOnlyCollection<T>>(
this);
406 return _net_binnary_search(m_data, 0, ASPOSECPP_CHECKED_CAST(
int, m_data.size()), item, comparer);
416 if (index < 0 || count < 0) {
417 throw ArgumentOutOfRangeException(u
"index or count");
420 if (Details::IsOutOfSize(index + count, m_data)) {
421 throw ArgumentException(u
"index or count");
431 template <
typename OutputType>
434 if (converter.IsNull()) {
435 throw System::ArgumentNullException(u
"converter");
438 for (
size_t i = 0; i < m_data.size(); i++) {
439 res->Add(converter(m_data[i]));
448 this->CopyTo(0, array, 0, ASPOSECPP_CHECKED_CAST(
int, m_data.size()));
459 throw ArgumentNullException(u
"array");
462 if (index < 0 || arrayIndex < 0 || count < 0) {
463 throw ArgumentOutOfRangeException(u
"index, arrayIndex or count");
466 if (Details::IsOutOfSize(index + count, m_data)
467 || Details::IsOutOfSize(arrayIndex + count, array->data()) ) {
468 throw ArgumentException(u
"index, arrayIndex or count");
471 std::copy(m_data.begin() + index, m_data.begin() + index + count, array->data().
begin() + arrayIndex);
479 if (match.IsNull()) {
480 throw ArgumentNullException(u
"match");
482 return FindIndex(match) != -1;
489 if (predicate.IsNull()) {
490 throw ArgumentNullException(u
"predicate");
492 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
497 return System::Default<T>();
504 if (match.IsNull()) {
505 throw ArgumentNullException(u
"match");
508 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
520 return FindIndex(0, ASPOSECPP_CHECKED_CAST(
int, m_data.size()), match);
528 return FindIndex(startIndex, ASPOSECPP_CHECKED_CAST(
int, m_data.size()) - startIndex, match);
537 if (match.IsNull()) {
538 throw ArgumentNullException(u
"match");
541 if (count < 0 || startIndex < 0
542 || Details::IsOutOfSize(startIndex + count, m_data))
544 throw ArgumentOutOfRangeException(u
"startIndex or count");
547 if (m_data.empty()) {
551 int const num = startIndex + count;
552 for (
int index = startIndex; index < num; ++index)
554 auto& item = m_data[index];
566 if (match.IsNull()) {
567 throw ArgumentNullException(u
"match");
569 T res = System::Default<T>();
570 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
582 if (action.IsNull()) {
583 throw ArgumentNullException(u
"action");
585 auto data_size = m_data.size();
586 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
589 if (m_data.size() != data_size)
591 throw InvalidOperationException();
602 if (index < 0 || count < 0) {
603 throw ArgumentOutOfRangeException(u
"index or count");
606 if (Details::IsOutOfSize(index + count, m_data)) {
607 throw ArgumentException(u
"index or count");
610 ThisPtr rv = MakeObject<List<T> >(count);
611 for (
auto i = index; i < (index + count); ++i)
613 rv->
m_data.push_back(m_data[i]);
625 if (Details::IsOutOfBounds(index, m_data)) {
626 throw ArgumentOutOfRangeException(u
"index");
628 auto it = FindInData(m_data.begin() + index, m_data.end(), item);
629 if (it != m_data.end())
631 return ASPOSECPP_CHECKED_CAST(
int, it - m_data.begin());
645 throw ArgumentNullException(u
"collection");
648 if (index < 0 ||
static_cast<size_t>(index) > m_data.size()) {
649 throw ArgumentOutOfRangeException(u
"index");
653 m_data.reserve(m_data.size() * 2);
654 m_data.insert(m_data.begin() + index, m_data.begin(), m_data.end());
659 while (it->MoveNext())
661 m_data.insert(m_data.begin() + index++, it->Current());
671 auto rit = FindInData(m_data.rbegin(), m_data.rend(), item);
672 if (rit != m_data.rend())
674 return ASPOSECPP_CHECKED_CAST(
int, rit.base() - m_data.begin() - 1);
688 using namespace Collections::Generic::Details;
689 if (index < 0 || IsOutOfBounds(index, m_data))
691 throw ArgumentOutOfRangeException(u
"startIndex or count");
694 auto rbegin = m_data.rbegin() + m_data.size() - index - 1;
695 auto rend = m_data.rend();
696 auto rit = FindInData(rbegin, rend, item);
699 return ASPOSECPP_CHECKED_CAST(
int, rit.base() - m_data.begin()) - 1;
711 int32_t
LastIndexOf(
const T& item, int32_t index, int32_t count)
const
713 using namespace Collections::Generic::Details;
714 if (index < 0 || count < 0
715 || IsOutOfBounds(index, m_data) || IsOutOfSize(count, m_data)
716 || (index - count + 1) < 0)
718 throw ArgumentOutOfRangeException(u
"index or count");
721 auto rbegin = m_data.rbegin() + m_data.size() - index - 1;
722 auto rend = rbegin + count;
723 auto rit = FindInData(rbegin, rend, item);
727 return ASPOSECPP_CHECKED_CAST(
int, rit.base() - m_data.begin()) - 1;
737 if (match.IsNull()) {
738 throw ArgumentNullException(u
"match");
741 typename vector_t::size_type removed = 0;
743 for (
typename vector_t::size_type index = 0; index < m_data.size(); )
745 if (match(m_data[index]))
747 m_data.erase(m_data.begin() + index);
756 return ASPOSECPP_CHECKED_CAST(
int, removed);
763 if (index < 0 || count < 0) {
764 throw ArgumentOutOfRangeException(u
"index or count");
767 if (Details::IsOutOfSize(index + count, m_data)) {
768 throw ArgumentException(u
"index or count");
771 m_data.erase(m_data.begin() + index, m_data.begin() + index + count);
776 std::reverse(m_data.begin(), m_data.end());
783 if (index < 0 || count < 0) {
784 throw ArgumentOutOfRangeException(u
"index or count");
787 if (Details::IsOutOfSize(index + count, m_data)) {
788 throw ArgumentException(u
"index or count");
791 std::reverse(m_data.begin() + index, m_data.begin() + index + count);
798 std::sort(m_data.begin(), m_data.end(), adapter);
803 std::sort(m_data.begin(), m_data.end(), Details::ComparerType<ValueType>());
811 if (index < 0 || count < 0) {
812 throw ArgumentOutOfRangeException(u
"index or count");
815 if (Details::IsOutOfSize(index + count, m_data)) {
816 throw ArgumentException(u
"index or count");
820 std::sort(m_data.begin() + index, m_data.begin() + index + count, adapter);
826 if (comparison.IsNull()) {
827 throw ArgumentNullException(u
"comparison");
829 std::sort(m_data.begin(), m_data.end(), comparison);
836 typename vector_t::size_type size = m_data.size();
837 auto result = MakeObject<Array<T> >(ASPOSECPP_CHECKED_CAST(
int, size));
838 std::copy(m_data.begin(), m_data.end(), result->data().begin());
845 m_data.shrink_to_fit();
855 throw ArgumentNullException(u
"match");
857 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
870 if (Details::IsOutOfBounds(index, m_data)) {
871 throw ArgumentOutOfRangeException(u
"index");
873 return m_data[
static_cast<typename vector_t::size_type
>(index)];
878 typename vector_t::const_reference
operator[](
int index)
const
880 if (Details::IsOutOfBounds(index, m_data)) {
881 throw ArgumentOutOfRangeException(u
"index");
883 return m_data[
static_cast<typename vector_t::size_type
>(index)];
896 std::for_each(list.begin(), list.end(), [
this](
const T &v) { m_data.push_back(v); });
902 return new System::Details::NativeIteratorWrapper<T, iterator>(begin(), end());
907 return new System::Details::NativeIteratorWrapper<T, iterator>(end(), end());
912 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(begin(), end());
917 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(end(), end());
928 template <
typename Q,
typename I>
929 typename std::enable_if<Details::IsEqualExist<Q>::value, I>::type FindInData(I begin, I end,
const Q &item)
const
931 return std::find(begin, end, item);
940 template <
typename Q,
typename I>
941 typename std::enable_if<!Details::IsEqualExist<Q>::value, I>::type FindInData(I begin, I end,
const Q &item)
const
943 throw System::NotImplementedException(ASPOSE_CURRENT_FUNCTION);
952 template <
typename Q,
typename I>
965#ifdef __DBG_FOR_EACH_MEMBER
969 void DBG_for_each_member(DBG::for_each_member_visitor &visitor)
const override
971 visitor.add_self(
this);
972 DBG::for_each_of_Object(
this, m_data, visitor);
977 const char* DBG_class_name()
const override {
return "List<T>"; }
980#ifdef ASPOSE_GET_SHARED_MEMBERS
981 DEFINE_GET_SHARED_MEMBERS(m_data)
Interface that compares two objects in greater-equal-less sense. Objects of this class should only be...
Definition: icomparer.h:20
System::Details::VirtualizedConstIterator< T > const_iterator
Const iterator type.
Definition: ienumerable.h:216
System::Details::VirtualizedIterator< T > iterator
Iterator type.
Definition: ienumerable.h:214
Interface of indexed container of elements. Objects of this class should only be allocated using Syst...
Definition: ilist.h:19
Enumerator to iterate through list elements. Objects of this class should only be allocated using Sys...
Definition: list.h:244
RTTI_INFO_TEMPLATE_CLASS(List< T >::Enumerator, System::BaseTypesInfo< System::Object >)
RTTI information.
Enumerator(const ThisPtr &data)
Creates enumerator iterating through specific list.
Definition: list.h:248
List forward declaration.
Definition: list.h:127
List()
Creates empty list.
Definition: list.h:166
vector_t::const_reference operator[](int index) const
Accessor function.
Definition: list.h:878
vector_t::reference operator[](int index)
Accessor function.
Definition: list.h:868
int32_t LastIndexOf(const T &item, int32_t index) const
Searches for the specified object and returns the zero-based index of the last occurrence within the ...
Definition: list.h:686
void Sort(const SharedPtr< System::Collections::Generic::IComparer< T > > &comparator)
Sorts elements in the list.
Definition: list.h:795
vector_t::const_iterator const_iterator
Const iterator type.
Definition: list.h:147
vector_t::const_reverse_iterator const_reverse_iterator
Const reverse iterator type.
Definition: list.h:151
bool Exists(System::Predicate< T > match)
Checks if element adhering to specific predicate exists in list.
Definition: list.h:477
const_reverse_iterator rend() const noexcept
Gets a reverse iterator for a non-existent element before the start of the const-qualified collection...
Definition: list.h:229
void CopyTo(System::ArrayPtr< T > array, int arrayIndex) override
Copies list elements into existing array elements.
Definition: list.h:304
System::Details::VirtualizedIteratorBase< T > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: list.h:915
void Sort(int index, int count, SharedPtr< System::Collections::Generic::IComparer< T > > comparator)
Sorts elements in the list slice.
Definition: list.h:809
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: list.h:910
int32_t LastIndexOf(const T &item, int32_t index, int32_t count) const
Searches for the specified object and returns the zero-based index of the last occurrence within the ...
Definition: list.h:711
SharedPtr< IEnumerable< T > > IEnumerablePtr
Container holding elements of same type we hold.
Definition: list.h:161
std::vector< T, ASPOSE_COLLECTION_ALLOCATOR_TYPE > vector_t
Underlying data type.
Definition: list.h:142
iterator begin() noexcept
Gets iterator to the first element of collection.
Definition: list.h:198
void Reverse()
Reverses elements order of the whole list.
Definition: list.h:774
void InsertRange(int index, IEnumerablePtr collection)
Inserts data range at specific position.
Definition: list.h:642
SharedPtr< List< T > > ThisPtr
Pointer type.
Definition: list.h:155
int IndexOf(const T &item) const override
Gets first index of specific item.
Definition: list.h:315
const_reverse_iterator crend() const noexcept
Gets a reverse iterator for a non-existent const-qualified element before the start of the collection...
Definition: list.h:236
SharedPtr< System::Collections::ObjectModel::ReadOnlyCollection< T > > AsReadOnly()
Gets read-only reference to this collection.
Definition: list.h:388
int get_Capacity() const
Gets current list capacity.
Definition: list.h:364
void set_Capacity(int capacity)
Sets list capacity.
Definition: list.h:370
int32_t LastIndexOf(const T &item) const
Searches for the specified object and returns the zero-based index of the last occurrence within the ...
Definition: list.h:669
vector_t::reverse_iterator reverse_iterator
Reverse iterator type.
Definition: list.h:149
void _add_range(std::initializer_list< T > list)
C++ specific.
Definition: list.h:894
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: list.h:900
void Add(const T &item) override
Adds element to the end of list.
Definition: list.h:270
bool TrueForAll(System::Predicate< T > match)
Determines whether every element in the collection matches the conditions defined by the specified pr...
Definition: list.h:851
const_iterator cend() const noexcept
Gets iterator for a non-existent const-qualified element behind the end of the collection.
Definition: list.h:215
int RemoveAll(Predicate< T > match)
Removes all elements matching specific predicate.
Definition: list.h:735
void ForEach(System::Action< T > action)
Applies action to all elements in list.
Definition: list.h:580
iterator end() noexcept
Gets iterator for a non-existent element behind the end of the collection.
Definition: list.h:201
IEnumeratorPtr GetEnumerator() override
Gets enumerator to iterate through list elements.
Definition: list.h:254
void Sort(Comparison< T > comparison, bool)
Sorts elements in the list.
Definition: list.h:824
void Reverse(int index, int count)
Reverses elements order of the list slice.
Definition: list.h:781
int FindIndex(int startIndex, int count, System::Predicate< T > match)
Looks for element adhering to specific predicate.
Definition: list.h:535
vector_t & data()
Underlying data structure access function.
Definition: list.h:888
List(int capacity)
Creates list with pre-defined capacity.
Definition: list.h:170
const_reverse_iterator crbegin() const noexcept
Gets a reverse iterator to the last const-qualified element of collection (first in reverse).
Definition: list.h:233
void idx_set(int index, T value) override
Sets element at specific position.
Definition: list.h:356
System::Details::VirtualizedIteratorBase< T > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: list.h:905
T Find(System::Predicate< T > predicate)
Looks for element adhering to specific predicate.
Definition: list.h:487
int BinarySearch(int index, int count, const T &item, const SharedPtr< System::Collections::Generic::IComparer< T > > &comparer) const
Looks for item in a sorted list.
Definition: list.h:414
int IndexOf(const T &item, int index) const
Looks for specific item in list.
Definition: list.h:623
T FindLast(System::Predicate< T > match)
Looks for last element adhering to specific predicate.
Definition: list.h:564
ThisPtr GetRange(int index, int count)
Creates slice of list.
Definition: list.h:600
void CopyTo(int index, const System::ArrayPtr< T > &array, int arrayIndex, int count)
Copies elements starting from the specified index into existing array elements.
Definition: list.h:456
int BinarySearch(const T &item) const
Looks for item in a sorted list.
Definition: list.h:396
vector_t::iterator iterator
Iterator type.
Definition: list.h:145
void Sort()
Sorts elements in the list using default comparator.
Definition: list.h:801
void Insert(int index, const T &item) override
Inserts item at specified position.
Definition: list.h:328
const_iterator cbegin() const noexcept
Gets iterator to the first const-qualified element of collection.
Definition: list.h:212
int FindIndex(System::Predicate< T > match)
Looks for element adhering to specific predicate.
Definition: list.h:518
T idx_get(int index) const override
Gets element at specific position.
Definition: list.h:349
ArrayPtr< T > ToArray() const
Converst list to array.
Definition: list.h:834
void CopyTo(const System::ArrayPtr< T > &array)
Copies all elements into existing array elements.
Definition: list.h:446
const_iterator begin() const noexcept
Gets iterator to the first element of the const-qualified collection.
Definition: list.h:205
void RemoveAt(int index) override
Removes item at specified position.
Definition: list.h:338
ListPtr< T > FindAll(System::Predicate< T > match)
Looks for elements adhering to specific predicate.
Definition: list.h:502
int get_Count() const override
Gets number of elements in current list.
Definition: list.h:264
bool Remove(const T &item) override
Removes first instance of specific item from list.
Definition: list.h:289
int BinarySearch(const T &item, const SharedPtr< System::Collections::Generic::IComparer< T > > &comparer) const
Looks for item in a sorted list.
Definition: list.h:404
vector_t m_data
Underlting data structure.
Definition: list.h:157
void Clear() override
Deletes all elements.
Definition: list.h:275
void AddRange(IEnumerablePtr collection)
Adds all elements from collection (or itself) to the end of current list.
Definition: list.h:381
IList< T > BaseType
Interface type.
Definition: list.h:134
void TrimExcess()
Makes list capacity to fit its size.
Definition: list.h:843
const_iterator end() const noexcept
Gets iterator for a non-existent element behind the end of the const-qualified collection.
Definition: list.h:208
List(IEnumerablePtr collection)
Copy constructor.
Definition: list.h:181
SharedPtr< List< OutputType > > ConvertAll(Converter< T, OutputType > converter)
Creates a list of elements converted to different type.
Definition: list.h:432
~List() override
Destructor.
Definition: list.h:963
int FindIndex(int startIndex, System::Predicate< T > match)
Looks for element adhering to specific predicate.
Definition: list.h:526
const vector_t & data() const
Underlying data structure access function.
Definition: list.h:891
void RemoveRange(int index, int count)
Removes slice of list.
Definition: list.h:761
bool Contains(const T &item) const override
Checks if item is present in list.
Definition: list.h:282
reverse_iterator rbegin() noexcept
Gets a reverse iterator to the last element of collection (first in reverse).
Definition: list.h:219
void AddInitializer(int size, const T inits[])
Adds elements to list; used when translating initializers.
Definition: list.h:190
reverse_iterator rend() noexcept
Gets a reverse iterator for a non-existent element before the start of the collection.
Definition: list.h:222
const_reverse_iterator rbegin() const noexcept
Gets a reverse iterator to the last element of the const-qualified collection (first in reverse).
Definition: list.h:226
T ValueType
This type.
Definition: list.h:132
SharedPtr< IEnumerator< T > > IEnumeratorPtr
Enumerator type.
Definition: list.h:163
List pointer with access operators. This type is a pointer to manage other object's deletion....
Definition: list.h:38
std::vector< T >::const_reference operator[](int idx) const
Accessor.
Definition: list.h:53
std::vector< T >::reference operator[](int idx)
Accessor.
Definition: list.h:49
bool operator==(std::nullptr_t) const
Checks if List pointer is null.
Definition: list.h:57
ListPtr(std::nullptr_t=nullptr)
Initializes null-pointer.
Definition: list.h:41
ListPtr(const SharedPtr< List< T > > &obj)
Initializes pointer to specified list.
Definition: list.h:44
Iterator class for simple containers holding elements directly using rbegin() and rend() functions....
Definition: base_enumerator.h:81
Represents a pointer to the method that compares two objects of the same type. This type should be al...
Definition: comparison.h:93
Base class that enables using methods available for System.Object class in C#. All non-trivial classe...
Definition: object.h:62
Pointer class to wrap types being allocated on heap. Use it to manage memory for classes inheriting O...
Definition: smart_ptr.h:180
SmartPtr< Object > ToObjectPtr() const
Converts any pointer type to pointer to Object. Doesn't require Pointee_ type to be complete.
Definition: object.h:832
auto begin() -> decltype(std::declval< Q >().begin())
Accessor for begin() method of an underling collection. Only compiles if SmartPtr_ is specialization ...
Definition: smart_ptr.h:830
Object * GetObjectOrNull() const
Gets pointed object (if any) or nullptr. Same as get().
Definition: smart_ptr.h:783
class System::SmartPtr::Data m_data
An instance of Data class.
class ASPOSECPP_SHARED_CLASS List
Definition: ienumerable.h:18
int _net_binnary_search(const containterT< T, Allocator > &container, int index, int count, T value, const SharedPtr< System::Collections::Generic::IComparer< T > > &comparer)
Implements binary search in random access container.
Definition: algorithms.h:23
Definition: db_command.h:9
bool Equals(const TA &a, const TB &b)
Determines the equality of two values applying operator==() to them.
Definition: primitive_types.h:77
MulticastDelegate< void(Args...)> Action
Delegate type that references methods that have no return value.
Definition: action.h:40
SmartPtr< X > MakeSharedPtr(X *p)
Converts raw pointer to smart pointer.
Definition: smart_ptr.h:1650
MulticastDelegate< TOutput(TInput)> Converter
Represents a pointer to the invokable entity that accepts a single argument of the TInput type and re...
Definition: converter.h:14
MulticastDelegate< bool(T)> Predicate
Represents a pointer to a predicate - an invokable entity that accepts a single argument and returns ...
Definition: predicate.h:41
Adapter to use IComparer within STL environment. Uses IComparer if set; otherwise,...
Definition: icomparer.h:44