//+------------------------------------------------------------------+ //| List.mqh | //| Copyright 2000-2025, MetaQuotes Ltd. | //| https://www.mql5.com | //+------------------------------------------------------------------+ #include //+------------------------------------------------------------------+ //| Class CList. | //| Purpose: Provides the possibility of working with the list of | //| CObject instances and its dervivatives | //| Derives from class CObject. | //+------------------------------------------------------------------+ class CList : public CObject { protected: CObject *m_first_node; // pointer to the first element of the list CObject *m_last_node; // pointer to the last element of the list CObject *m_curr_node; // pointer to the current element of the list int m_curr_idx; // index of the current list item int m_data_total; // number of elements bool m_free_mode; // flag of the necessity of "physical" deletion of object bool m_data_sort; // flag if the list is sorted or not int m_sort_mode; // mode of sorting of array public: CList(void); ~CList(void); //--- methods of access to protected data bool FreeMode(void) const { return(m_free_mode); } void FreeMode(bool mode) { m_free_mode=mode; } int Total(void) const { return(m_data_total); } bool IsSorted(void) const { return(m_data_sort); } int SortMode(void) const { return(m_sort_mode); } //--- method of identifying the object virtual int Type(void) const { return(0x7779); } //--- methods for working with files virtual bool Save(const int file_handle); virtual bool Load(const int file_handle); //--- method of creating an element of the list virtual CObject *CreateElement(void) { return(NULL); } //--- methods of filling the list int Add(CObject *new_node); int Insert(CObject *new_node,int index); //--- methods for navigating int IndexOf(CObject *node); CObject *GetNodeAtIndex(int index); CObject *GetFirstNode(void); CObject *GetPrevNode(void); CObject *GetCurrentNode(void); CObject *GetNextNode(void); CObject *GetLastNode(void); //--- methods for deleting CObject *DetachCurrent(void); bool DeleteCurrent(void); bool Delete(int index); void Clear(void); //--- method for comparing lists bool CompareList(CList *List); //--- methods for changing void Sort(int mode); bool MoveToIndex(int index); bool Exchange(CObject *node1,CObject *node2); //--- method for searching CObject *Search(CObject *element); protected: void QuickSort(int beg,int end,int mode); CObject *QuickSearch(CObject *element); }; //+------------------------------------------------------------------+ //| Constructor | //+------------------------------------------------------------------+ CList::CList(void) : m_first_node(NULL), m_last_node(NULL), m_curr_node(NULL), m_curr_idx(-1), m_data_total(0), m_free_mode(true), m_data_sort(false), m_sort_mode(0) { } //+------------------------------------------------------------------+ //| Destructor | //+------------------------------------------------------------------+ CList::~CList(void) { Clear(); } //+------------------------------------------------------------------+ //| Method QuickSort | //+------------------------------------------------------------------+ void CList::QuickSort(int beg,int end,int mode) { int i,j,k; CObject *i_ptr,*j_ptr,*k_ptr; //--- i_ptr=GetNodeAtIndex(i=beg); j_ptr=GetNodeAtIndex(j=end); while(i>1" is quick division by 2 k_ptr=GetNodeAtIndex(k=(beg+end)>>1); while(i0) { //--- control the output of the array bounds if(j==0) break; j--; j_ptr=j_ptr.Prev(); } if(i<=j) { Exchange(i_ptr,j_ptr); i++; i_ptr=GetNodeAtIndex(i); //--- control the output of the array bounds if(j==0) break; else { j--; j_ptr=GetNodeAtIndex(j); } } } if(begm_data_total || index<0) return(-1); //--- adjust if(index==-1) { if(m_curr_node==NULL) return(Add(new_node)); } else { if(GetNodeAtIndex(index)==NULL) return(Add(new_node)); } //--- no need to check m_curr_node tmp_node=m_curr_node.Prev(); new_node.Prev(tmp_node); if(tmp_node!=NULL) tmp_node.Next(new_node); else m_first_node=new_node; new_node.Next(m_curr_node); m_curr_node.Prev(new_node); m_data_total++; m_data_sort=false; m_curr_node=new_node; //--- result return(index); } //+------------------------------------------------------------------+ //| Get a pointer to the position of element in the list | //+------------------------------------------------------------------+ CObject *CList::GetNodeAtIndex(int index) { int i; bool revers; CObject *result; //--- check if(index>=m_data_total) return(NULL); if(index==m_curr_idx) return(m_curr_node); //--- optimize bust list if(indexindex;i--) { result=result.Prev(); if(result==NULL) return(NULL); } } else { //--- search from left to right for(;i=m_data_total || !CheckPointer(m_curr_node)) return(false); //--- tune if(m_curr_idx==index) return(true); if(m_curr_idx=i) { //--- ">>1" is quick division by 2 m=(j+i)>>1; if(m<0 || m>=m_data_total) break; t_node=GetNodeAtIndex(m); if(t_node.Compare(element,m_sort_mode)==0) break; if(t_node.Compare(element,m_sort_mode)>0) j=m-1; else i=m+1; t_node=NULL; } //--- result return(t_node); } //+------------------------------------------------------------------+ //| Search position of an element in a sorted list | //+------------------------------------------------------------------+ CObject *CList::Search(CObject *element) { CObject *result; //--- check if(!CheckPointer(element) || !m_data_sort) return(NULL); //--- search result=QuickSearch(element); //--- result return(result); } //+------------------------------------------------------------------+ //| Writing list to file | //+------------------------------------------------------------------+ bool CList::Save(const int file_handle) { CObject *node; bool result=true; //--- check if(!CheckPointer(m_curr_node) || file_handle==INVALID_HANDLE) return(false); //--- write start marker - 0xFFFFFFFFFFFFFFFF if(FileWriteLong(file_handle,-1)!=sizeof(long)) return(false); //--- write type if(FileWriteInteger(file_handle,Type(),INT_VALUE)!=INT_VALUE) return(false); //--- write list size if(FileWriteInteger(file_handle,m_data_total,INT_VALUE)!=INT_VALUE) return(false); //--- sequential scannning of elements in the list using the call of method Save() node=m_first_node; while(node!=NULL) { result&=node.Save(file_handle); node=node.Next(); } //--- successful return(result); } //+------------------------------------------------------------------+ //| Reading list from file | //+------------------------------------------------------------------+ bool CList::Load(const int file_handle) { uint i,num; CObject *node; bool result=true; //--- check if(file_handle==INVALID_HANDLE) return(false); //--- read and checking begin marker - 0xFFFFFFFFFFFFFFFF if(FileReadLong(file_handle)!=-1) return(false); //--- read and checking type if(FileReadInteger(file_handle,INT_VALUE)!=Type()) return(false); //--- read list size num=FileReadInteger(file_handle,INT_VALUE); //--- sequential creation of list items using the call of method Load() Clear(); for(i=0;i