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 m_data.reserve(m_data.size() + size);
193 for (
int i = 0; i < size; ++i)
194 m_data.push_back(inits[i]);
257 return MakeObject<Enumerator>(std::move(
MakeSharedPtr(
this)));
267 return (
int)m_data.size();
271 void Add(
const T& item)
override
273 m_data.push_back(item);
285 return FindInData(m_data.begin(), m_data.end(), item) != m_data.end();
292 iterator it = FindInData(m_data.begin(), m_data.end(), item);
293 if (it != m_data.end())
307 this->CopyTo(0, array, arrayIndex, ASPOSECPP_CHECKED_CAST(
int, m_data.size()));
318 const_iterator it = FindInData(m_data.begin(), m_data.end(), item);
319 if (it != m_data.end())
321 return ASPOSECPP_CHECKED_CAST(
int, it - m_data.begin());
329 void Insert(
int index,
const T& item)
override
331 if (index < 0 ||
static_cast<size_t>(index) > m_data.size()) {
332 throw ArgumentOutOfRangeException(u
"index");
335 m_data.insert(m_data.begin() + index, item);
341 if (Details::IsOutOfBounds(index, m_data)) {
342 throw ArgumentOutOfRangeException(u
"index");
344 m_data.erase(m_data.begin() + index);
352 return operator[](index);
359 operator[](index) = value;
367 return (
int)m_data.capacity();
373 if (capacity < (
int)m_data.size()) {
374 throw ArgumentOutOfRangeException(u
"capacity");
376 m_data.reserve(capacity);
384 InsertRange(
static_cast<int>(m_data.size()), collection);
391 return MakeObject<System::Collections::ObjectModel::ReadOnlyCollection<T>>(
this);
407 return _net_binnary_search(m_data, 0, ASPOSECPP_CHECKED_CAST(
int, m_data.size()), item, comparer);
417 if (index < 0 || count < 0) {
418 throw ArgumentOutOfRangeException(u
"index or count");
421 if (Details::IsOutOfSize(index + count, m_data)) {
422 throw ArgumentException(u
"index or count");
432 template <
typename OutputType>
435 if (converter.IsNull()) {
436 throw System::ArgumentNullException(u
"converter");
439 for (
size_t i = 0; i < m_data.size(); i++) {
440 res->Add(converter(m_data[i]));
449 this->CopyTo(0, array, 0, ASPOSECPP_CHECKED_CAST(
int, m_data.size()));
460 throw ArgumentNullException(u
"array");
463 if (index < 0 || arrayIndex < 0 || count < 0) {
464 throw ArgumentOutOfRangeException(u
"index, arrayIndex or count");
467 if (Details::IsOutOfSize(index + count, m_data)
468 || Details::IsOutOfSize(arrayIndex + count, array->data()) ) {
469 throw ArgumentException(u
"index, arrayIndex or count");
472 std::copy(m_data.begin() + index, m_data.begin() + index + count, array->data().
begin() + arrayIndex);
480 if (match.IsNull()) {
481 throw ArgumentNullException(u
"match");
483 return FindIndex(match) != -1;
490 if (predicate.IsNull()) {
491 throw ArgumentNullException(u
"predicate");
493 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
498 return System::Default<T>();
505 if (match.IsNull()) {
506 throw ArgumentNullException(u
"match");
509 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
521 return FindIndex(0, ASPOSECPP_CHECKED_CAST(
int, m_data.size()), match);
529 return FindIndex(startIndex, ASPOSECPP_CHECKED_CAST(
int, m_data.size()) - startIndex, match);
538 if (match.IsNull()) {
539 throw ArgumentNullException(u
"match");
542 if (count < 0 || startIndex < 0
543 || Details::IsOutOfSize(startIndex + count, m_data))
545 throw ArgumentOutOfRangeException(u
"startIndex or count");
548 if (m_data.empty()) {
552 int const num = startIndex + count;
553 for (
int index = startIndex; index < num; ++index)
555 auto& item = m_data[index];
567 if (match.IsNull()) {
568 throw ArgumentNullException(u
"match");
570 T res = System::Default<T>();
571 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
583 if (action.IsNull()) {
584 throw ArgumentNullException(u
"action");
586 auto data_size = m_data.size();
587 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
590 if (m_data.size() != data_size)
592 throw InvalidOperationException();
603 if (index < 0 || count < 0) {
604 throw ArgumentOutOfRangeException(u
"index or count");
607 if (Details::IsOutOfSize(index + count, m_data)) {
608 throw ArgumentException(u
"index or count");
611 ThisPtr rv = MakeObject<List<T> >(count);
612 for (
auto i = index; i < (index + count); ++i)
614 rv->
m_data.push_back(m_data[i]);
626 if (Details::IsOutOfBounds(index, m_data)) {
627 throw ArgumentOutOfRangeException(u
"index");
629 auto it = FindInData(m_data.begin() + index, m_data.end(), item);
630 if (it != m_data.end())
632 return ASPOSECPP_CHECKED_CAST(
int, it - m_data.begin());
646 throw ArgumentNullException(u
"collection");
649 if (index < 0 ||
static_cast<size_t>(index) > m_data.size()) {
650 throw ArgumentOutOfRangeException(u
"index");
654 m_data.reserve(m_data.size() * 2);
655 m_data.insert(m_data.begin() + index, m_data.begin(), m_data.end());
660 while (it->MoveNext())
662 m_data.insert(m_data.begin() + index++, it->Current());
672 auto rit = FindInData(m_data.rbegin(), m_data.rend(), item);
673 if (rit != m_data.rend())
675 return ASPOSECPP_CHECKED_CAST(
int, rit.base() - m_data.begin() - 1);
689 using namespace Collections::Generic::Details;
690 if (index < 0 || IsOutOfBounds(index, m_data))
692 throw ArgumentOutOfRangeException(u
"startIndex or count");
695 auto rbegin = m_data.rbegin() + m_data.size() - index - 1;
696 auto rend = m_data.rend();
697 auto rit = FindInData(rbegin, rend, item);
700 return ASPOSECPP_CHECKED_CAST(
int, rit.base() - m_data.begin()) - 1;
712 int32_t
LastIndexOf(
const T& item, int32_t index, int32_t count)
const
714 using namespace Collections::Generic::Details;
715 if (index < 0 || count < 0
716 || IsOutOfBounds(index, m_data) || IsOutOfSize(count, m_data)
717 || (index - count + 1) < 0)
719 throw ArgumentOutOfRangeException(u
"index or count");
722 auto rbegin = m_data.rbegin() + m_data.size() - index - 1;
723 auto rend = rbegin + count;
724 auto rit = FindInData(rbegin, rend, item);
728 return ASPOSECPP_CHECKED_CAST(
int, rit.base() - m_data.begin()) - 1;
738 if (match.IsNull()) {
739 throw ArgumentNullException(u
"match");
742 typename vector_t::size_type removed = 0;
744 for (
typename vector_t::size_type index = 0; index < m_data.size(); )
746 if (match(m_data[index]))
748 m_data.erase(m_data.begin() + index);
757 return ASPOSECPP_CHECKED_CAST(
int, removed);
764 if (index < 0 || count < 0) {
765 throw ArgumentOutOfRangeException(u
"index or count");
768 if (Details::IsOutOfSize(index + count, m_data)) {
769 throw ArgumentException(u
"index or count");
772 m_data.erase(m_data.begin() + index, m_data.begin() + index + count);
777 std::reverse(m_data.begin(), m_data.end());
784 if (index < 0 || count < 0) {
785 throw ArgumentOutOfRangeException(u
"index or count");
788 if (Details::IsOutOfSize(index + count, m_data)) {
789 throw ArgumentException(u
"index or count");
792 std::reverse(m_data.begin() + index, m_data.begin() + index + count);
799 std::sort(m_data.begin(), m_data.end(), adapter);
804 std::sort(m_data.begin(), m_data.end(), Details::ComparerType<ValueType>());
812 if (index < 0 || count < 0) {
813 throw ArgumentOutOfRangeException(u
"index or count");
816 if (Details::IsOutOfSize(index + count, m_data)) {
817 throw ArgumentException(u
"index or count");
821 std::sort(m_data.begin() + index, m_data.begin() + index + count, adapter);
827 if (comparison.IsNull()) {
828 throw ArgumentNullException(u
"comparison");
830 std::sort(m_data.begin(), m_data.end(), comparison);
837 typename vector_t::size_type size = m_data.size();
838 auto result = MakeObject<Array<T> >(ASPOSECPP_CHECKED_CAST(
int, size));
839 std::copy(m_data.begin(), m_data.end(), result->data().begin());
846 m_data.shrink_to_fit();
856 throw ArgumentNullException(u
"match");
858 for (
iterator it = m_data.begin(); it != m_data.end(); ++it)
871 if (Details::IsOutOfBounds(index, m_data)) {
872 throw ArgumentOutOfRangeException(u
"index");
874 return m_data[
static_cast<typename vector_t::size_type
>(index)];
879 typename vector_t::const_reference
operator[](
int index)
const
881 if (Details::IsOutOfBounds(index, m_data)) {
882 throw ArgumentOutOfRangeException(u
"index");
884 return m_data[
static_cast<typename vector_t::size_type
>(index)];
897 std::for_each(list.begin(), list.end(), [
this](
const T &v) { m_data.push_back(v); });
903 return new System::Details::NativeIteratorWrapper<T, iterator>(begin(), end());
908 return new System::Details::NativeIteratorWrapper<T, iterator>(end(), end());
913 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(begin(), end());
918 return new System::Details::NativeConstIteratorWrapper<T, const_iterator>(end(), end());
929 template <
typename Q,
typename I>
930 typename std::enable_if<Details::IsEqualExist<Q>::value, I>::type FindInData(I begin, I end,
const Q &item)
const
932 return std::find(begin, end, item);
941 template <
typename Q,
typename I>
942 typename std::enable_if<!Details::IsEqualExist<Q>::value, I>::type FindInData(I begin, I end,
const Q &item)
const
944 throw System::NotImplementedException(ASPOSE_CURRENT_FUNCTION);
953 template <
typename Q,
typename I>
966#ifdef __DBG_FOR_EACH_MEMBER
970 void DBG_for_each_member(DBG::for_each_member_visitor &visitor)
const override
972 visitor.add_self(
this);
973 DBG::for_each_of_Object(
this, m_data, visitor);
978 const char* DBG_class_name()
const override {
return "List<T>"; }
981#ifdef ASPOSE_GET_SHARED_MEMBERS
982 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:245
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:249
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:879
vector_t::reference operator[](int index)
Accessor function.
Definition: list.h:869
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:687
void Sort(const SharedPtr< System::Collections::Generic::IComparer< T > > &comparator)
Sorts elements in the list.
Definition: list.h:796
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:478
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:230
void CopyTo(System::ArrayPtr< T > array, int arrayIndex) override
Copies list elements into existing array elements.
Definition: list.h:305
System::Details::VirtualizedIteratorBase< T > * virtualizeEndConstIterator() const override
Gets the implementation of end const iterator for the current container.
Definition: list.h:916
void Sort(int index, int count, SharedPtr< System::Collections::Generic::IComparer< T > > comparator)
Sorts elements in the list slice.
Definition: list.h:810
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginConstIterator() const override
Gets the implementation of begin const iterator for the current container.
Definition: list.h:911
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:712
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:199
void Reverse()
Reverses elements order of the whole list.
Definition: list.h:775
void InsertRange(int index, IEnumerablePtr collection)
Inserts data range at specific position.
Definition: list.h:643
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:316
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:237
SharedPtr< System::Collections::ObjectModel::ReadOnlyCollection< T > > AsReadOnly()
Gets read-only reference to this collection.
Definition: list.h:389
int get_Capacity() const
Gets current list capacity.
Definition: list.h:365
void set_Capacity(int capacity)
Sets list capacity.
Definition: list.h:371
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:670
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:895
System::Details::VirtualizedIteratorBase< T > * virtualizeBeginIterator() override
Gets the implementation of begin iterator for the current container.
Definition: list.h:901
void Add(const T &item) override
Adds element to the end of list.
Definition: list.h:271
bool TrueForAll(System::Predicate< T > match)
Determines whether every element in the collection matches the conditions defined by the specified pr...
Definition: list.h:852
void AddInitializer(int size, const T *inits)
Adds elements to list; used when translating initializers.
Definition: list.h:190
const_iterator cend() const noexcept
Gets iterator for a non-existent const-qualified element behind the end of the collection.
Definition: list.h:216
int RemoveAll(Predicate< T > match)
Removes all elements matching specific predicate.
Definition: list.h:736
void ForEach(System::Action< T > action)
Applies action to all elements in list.
Definition: list.h:581
iterator end() noexcept
Gets iterator for a non-existent element behind the end of the collection.
Definition: list.h:202
IEnumeratorPtr GetEnumerator() override
Gets enumerator to iterate through list elements.
Definition: list.h:255
void Sort(Comparison< T > comparison, bool)
Sorts elements in the list.
Definition: list.h:825
void Reverse(int index, int count)
Reverses elements order of the list slice.
Definition: list.h:782
int FindIndex(int startIndex, int count, System::Predicate< T > match)
Looks for element adhering to specific predicate.
Definition: list.h:536
vector_t & data()
Underlying data structure access function.
Definition: list.h:889
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:234
void idx_set(int index, T value) override
Sets element at specific position.
Definition: list.h:357
System::Details::VirtualizedIteratorBase< T > * virtualizeEndIterator() override
Gets the implementation of end iterator for the current container.
Definition: list.h:906
T Find(System::Predicate< T > predicate)
Looks for element adhering to specific predicate.
Definition: list.h:488
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:415
int IndexOf(const T &item, int index) const
Looks for specific item in list.
Definition: list.h:624
T FindLast(System::Predicate< T > match)
Looks for last element adhering to specific predicate.
Definition: list.h:565
ThisPtr GetRange(int index, int count)
Creates slice of list.
Definition: list.h:601
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:457
int BinarySearch(const T &item) const
Looks for item in a sorted list.
Definition: list.h:397
vector_t::iterator iterator
Iterator type.
Definition: list.h:145
void Sort()
Sorts elements in the list using default comparator.
Definition: list.h:802
void Insert(int index, const T &item) override
Inserts item at specified position.
Definition: list.h:329
const_iterator cbegin() const noexcept
Gets iterator to the first const-qualified element of collection.
Definition: list.h:213
int FindIndex(System::Predicate< T > match)
Looks for element adhering to specific predicate.
Definition: list.h:519
T idx_get(int index) const override
Gets element at specific position.
Definition: list.h:350
ArrayPtr< T > ToArray() const
Converst list to array.
Definition: list.h:835
void CopyTo(const System::ArrayPtr< T > &array)
Copies all elements into existing array elements.
Definition: list.h:447
const_iterator begin() const noexcept
Gets iterator to the first element of the const-qualified collection.
Definition: list.h:206
void RemoveAt(int index) override
Removes item at specified position.
Definition: list.h:339
ListPtr< T > FindAll(System::Predicate< T > match)
Looks for elements adhering to specific predicate.
Definition: list.h:503
int get_Count() const override
Gets number of elements in current list.
Definition: list.h:265
bool Remove(const T &item) override
Removes first instance of specific item from list.
Definition: list.h:290
int BinarySearch(const T &item, const SharedPtr< System::Collections::Generic::IComparer< T > > &comparer) const
Looks for item in a sorted list.
Definition: list.h:405
vector_t m_data
Underlting data structure.
Definition: list.h:157
void Clear() override
Deletes all elements.
Definition: list.h:276
void AddRange(IEnumerablePtr collection)
Adds all elements from collection (or itself) to the end of current list.
Definition: list.h:382
IList< T > BaseType
Interface type.
Definition: list.h:134
void TrimExcess()
Makes list capacity to fit its size.
Definition: list.h:844
const_iterator end() const noexcept
Gets iterator for a non-existent element behind the end of the const-qualified collection.
Definition: list.h:209
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:433
~List() override
Destructor.
Definition: list.h:964
int FindIndex(int startIndex, System::Predicate< T > match)
Looks for element adhering to specific predicate.
Definition: list.h:527
const vector_t & data() const
Underlying data structure access function.
Definition: list.h:892
void RemoveRange(int index, int count)
Removes slice of list.
Definition: list.h:762
bool Contains(const T &item) const override
Checks if item is present in list.
Definition: list.h:283
reverse_iterator rbegin() noexcept
Gets a reverse iterator to the last element of collection (first in reverse).
Definition: list.h:220
reverse_iterator rend() noexcept
Gets a reverse iterator for a non-existent element before the start of the collection.
Definition: list.h:223
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:227
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:98
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