mql5/Include/Generic/ArrayList.mqh

617 lines
49 KiB
MQL5

2025-08-05 01:57:33 -04:00
<EFBFBD><EFBFBD>//+------------------------------------------------------------------+
//| ArrayList.mqh |
//| Copyright 2000-2025, MetaQuotes Ltd. |
//| https://www.mql5.com |
//+------------------------------------------------------------------+
#include <Generic\Interfaces\IList.mqh>
#include <Generic\Interfaces\IComparer.mqh>
#include <Generic\Internal\EqualFunction.mqh>
#include <Generic\Internal\ArrayFunction.mqh>
#include <Generic\Internal\Introsort.mqh>
#include <Generic\Internal\DefaultComparer.mqh>
//+------------------------------------------------------------------+
//| Class CArrayList<T>. |
//| Usage: Represents a strongly typed list of values that can be |
//| accessed by index. Provides methods to search, sort, and |
//| manipulate lists. |
//+------------------------------------------------------------------+
template<typename T>
class CArrayList: public IList<T>
{
protected:
T m_items[];
int m_size;
const int m_default_capacity;
public:
CArrayList(void);
CArrayList(const int capacity);
CArrayList(ICollection<T>*collection);
CArrayList(T &array[]);
~CArrayList(void);
//--- methods of access to protected data
int Capacity(void);
void Capacity(const int capacity);
int Count(void);
bool Contains(T item);
void TrimExcess(void);
//--- method of access to the data
bool TryGetValue(const int index,T &value);
bool TrySetValue(const int index,T value);
//--- methods of filling the array
bool Add(T item);
bool AddRange(T &array[]);
bool AddRange(ICollection<T>*collection);
bool Insert(const int index,T item);
bool InsertRange(const int index,T &array[]);
bool InsertRange(const int index,ICollection<T>*collection);
//--- methods of copy data from collection
int CopyTo(T &dst_array[],const int dst_start=0);
//--- methods for binary searching index
int BinarySearch(T item);
int BinarySearch(T item,IComparer<T>*comparer);
int BinarySearch(const int index,const int count,T item,IComparer<T>*comparer);
//--- methods for searching index
int IndexOf(T item);
int IndexOf(T item,const int start_index);
int IndexOf(T item,const int start_index,const int count);
int LastIndexOf(T item);
int LastIndexOf(T item,const int start_index);
int LastIndexOf(T item,const int start_index,const int count);
//--- methods of cleaning and deleting
void Clear(void);
bool Remove(T item);
bool RemoveAt(const int index);
bool RemoveRange(const int start_index,const int count);
//--- reversing method
bool Reverse(void);
bool Reverse(const int start_index,const int count);
//--- sorting method
bool Sort(void);
bool Sort(IComparer<T>*comparer);
bool Sort(const int start_index,const int count,IComparer<T>*comparer);
private:
void EnsureCapacity(const int min);
};
//+------------------------------------------------------------------+
//| Initializes a new instance of the CArrayList<T> class that is |
//| empty and has the default initial capacity. |
//+------------------------------------------------------------------+
template<typename T>
CArrayList::CArrayList(void): m_default_capacity(4),
m_size(0)
{
ArrayResize(m_items,m_default_capacity);
}
//+------------------------------------------------------------------+
//| Initializes a new instance of the CArrayList<T> class that is |
//| empty and has the specified initial capacity. |
//+------------------------------------------------------------------+
template<typename T>
CArrayList::CArrayList(const int capacity): m_default_capacity(4),
m_size(0)
{
ArrayResize(m_items,capacity);
}
//+------------------------------------------------------------------+
//| Initializes a new instance of the CArrayList<T> class that |
//| contains elements copied from the specified array and has |
//| sufficient capacity to accommodate the number of elements copied.|
//+------------------------------------------------------------------+
template<typename T>
CArrayList::CArrayList(T &array[]): m_default_capacity(4),
m_size(0)
{
m_size=ArrayCopy(m_items,array);
}
//+------------------------------------------------------------------+
//| Initializes a new instance of the CArrayList<T> class that |
//| contains elements copied from the specified collection and has |
//| sufficient capacity to accommodate the number of elements copied.|
//+------------------------------------------------------------------+
template<typename T>
CArrayList::CArrayList(ICollection<T>*collection): m_default_capacity(4),
m_size(0)
{
//--- check collection
if(CheckPointer(collection)!=POINTER_INVALID)
m_size=collection.CopyTo(m_items,0);
}
//+------------------------------------------------------------------+
//| Destructor. |
//+------------------------------------------------------------------+
template<typename T>
CArrayList::~CArrayList(void)
{
}
//+------------------------------------------------------------------+
//| Gets the total number of elements the internal data structure can|
//| hold without resizing. |
//+------------------------------------------------------------------+
template<typename T>
int CArrayList::Capacity(void)
{
return ArraySize(m_items);
}
//+------------------------------------------------------------------+
//| Sets the total number of elements the internal data structure can|
//| hold without resizing. |
//+------------------------------------------------------------------+
template<typename T>
void CArrayList::Capacity(const int capacity)
{
//--- check new capacity should be greater than current size
if(capacity>=m_size && capacity!=ArraySize(m_items))
{
if(capacity>0)
ArrayResize(m_items,capacity);
else
ArrayFree(m_items);
}
}
//+------------------------------------------------------------------+
//| Gets the number of elements. |
//+------------------------------------------------------------------+
template<typename T>
int CArrayList::Count(void)
{
return(m_size);
}
//+------------------------------------------------------------------+
//| Determines whether an element is in the list. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::Contains(T item)
{
//--- try to find item in array
for(int i=0; i<m_size; i++)
{
//--- use default equality function
if(::Equals(m_items[i],item))
return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| Gets the element at the specified index. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::TryGetValue(const int index,T &value)
{
//--- check index
if((uint)index<(uint)m_size)
{
//--- get value by index
value=m_items[index];
return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| Sets the element at the specified index. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::TrySetValue(const int index,T value)
{
//--- check index
if((uint)index<(uint)m_size)
{
//--- set value by index
m_items[index]=value;
return(true);
}
return(false);
}
//+------------------------------------------------------------------+
//| Sets the capacity to the actual number of elements in the list, |
//| if that number is less than a threshold value. |
//+------------------------------------------------------------------+
template<typename T>
void CArrayList::TrimExcess(void)
{
//--- calculate threshold value
int threshold=(int)(((double)ArraySize(m_items))*0.9);
//--- set a Aapacity equal to the size
if(m_size<threshold)
Capacity(m_size);
}
//+------------------------------------------------------------------+
//| Adds an value to the end of the list. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::Add(T item)
{
//--- increse capacity if its necessary
if(m_size==ArraySize(m_items))
EnsureCapacity(m_size+1);
//--- add value to the end
m_items[m_size++]=item;
return(true);
}
//+------------------------------------------------------------------+
//| Adds the elements of the specified array to the end of the list. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::AddRange(T &array[])
{
//--- increse capacity if its necessary
if(m_size==ArraySize(m_items))
EnsureCapacity(m_size+ArraySize(array));
//--- add values to the end
m_size+=ArrayCopy(m_items,array,m_size);
return(true);
}
//+------------------------------------------------------------------+
//| Adds the elements of the specified collection to the end of the |
//| list. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::AddRange(ICollection<T>*collection)
{
//--- check collection
if(CheckPointer(collection)==POINTER_INVALID)
return(false);
//--- increse capacity if its necessary
if(m_size==ArraySize(m_items))
EnsureCapacity(m_size+collection.Count());
//--- add value to the end
m_size+=collection.CopyTo(m_items,m_size);
return(true);
}
//+------------------------------------------------------------------+
//| Inserts an element into the list at the specified index. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::Insert(const int index,T item)
{
//--- check index
if((uint)index>(uint)m_size)
return(false);
//--- increse capacity if its necessary
if(m_size==ArraySize(m_items))
EnsureCapacity(m_size+1);
//--- shift the values to the right
if(index<m_size)
ArrayCopy(m_items,m_items,index+1,index,m_size-index);
//--- insert value by index
m_items[index]=item;
m_size++;
return(true);
}
//+------------------------------------------------------------------+
//| Inserts the elements of a array into the list at the specified |
//| index. |
//+------------------------------------------------------------------+
template<typename T>
bool CArrayList::InsertRange(const int index,T &array[])
{
//--- check index
if((uint)index>(uint)m_size)
return(false);
int size=ArraySize(array);
//--- check size of inserted array
if(size>0)
{
//--- increse capacity if its necessary
EnsureCapacity(m_size+size);
//--- shift the values to the right
if(index<m_size)
ArrayCopy(m_items,m_items,index+size,index,m_size-index);
//--- insert values by index
m_size+=ArrayCopy(m_items,array,index);
}
return(true);
}
//+------------------------------------------------------------------+
//| Inserts the elements of a collection into the list at the |
//| specified index. |
//+------------------------------------------------------------------+