1//+------------------------------------------------------------------+ 2//| Array.mqh | 3//| Copyright 2009-2013, MetaQuotes Software Corp. | 4//| http://www.mql4.com | 5//+------------------------------------------------------------------+ 6#include <Object.mqh> 7//+------------------------------------------------------------------+ 8//| Class CArray | 9//| Purpose: Base class of dynamic arrays. | 10//| Derives from class CObject. | 11//+------------------------------------------------------------------+ 12class CArray : public CObject 13 { 14protected: 15 int m_step_resize; // increment size of the array 16 int m_data_total; // number of elements 17 int m_data_max; // maximmum size of the array without memory reallocation 18 int m_sort_mode; // mode of array sorting 19 20public: 21 CArray(void); 22 ~CArray(void); 23 //--- methods of access to protected data 24 int Step(void) const { return(m_step_resize); } 25 bool Step(const int step); 26 int Total(void) const { return(m_data_total); } 27 int Available(void) const { return(m_data_max-m_data_total); } 28 int Max(void) const { return(m_data_max); } 29 bool IsSorted(const int mode=0) const { return(m_sort_mode==mode); } 30 int SortMode(void) const { return(m_sort_mode); } 31 //--- cleaning method 32 void Clear(void) { m_data_total=0; } 33 //--- methods for working with files 34 virtual bool Save(const int file_handle); 35 virtual bool Load(const int file_handle); 36 //--- sorting method 37 void Sort(const int mode=0); 38 39protected: 40 virtual void QuickSort(int beg,int end,const int mode=0) { } 41 }; 42//+------------------------------------------------------------------+ 43//| Constructor | 44//+------------------------------------------------------------------+ 45CArray::CArray(void) : m_step_resize(16), 46 m_data_total(0), 47 m_data_max(0), 48 m_sort_mode(-1) 49 { 50 } 51//+------------------------------------------------------------------+ 52//| Destructor | 53//+------------------------------------------------------------------+ 54CArray::~CArray(void) 55 { 56 } 57//+------------------------------------------------------------------+ 58//| Method Set for variable m_step_resize | 59//+------------------------------------------------------------------+ 60bool CArray::Step(const int step) 61 { 62//--- check 63 if(step>0) 64 { 65 m_step_resize=step; 66 return(true); 67 } 68//--- failure 69 return(false); 70 } 71//+------------------------------------------------------------------+ 72//| Sorting an array in ascending order | 73//+------------------------------------------------------------------+ 74void CArray::Sort(const int mode) 75 { 76//--- check 77 if(IsSorted(mode)) 78 return; 79 m_sort_mode=mode; 80 if(m_data_total<=1) 81 return; 82//--- sort 83 QuickSort(0,m_data_total-1,mode); 84 } 85//+------------------------------------------------------------------+ 86//| Writing header of array to file | 87//+------------------------------------------------------------------+ 88bool CArray::Save(const int file_handle) 89 { 90//--- check handle 91 if(file_handle!=INVALID_HANDLE) 92 { 93 //--- write start marker - 0xFFFFFFFFFFFFFFFF 94 if(FileWriteLong(file_handle,-1)==sizeof(long)) 95 { 96 //--- write array type 97 if(FileWriteInteger(file_handle,Type(),INT_VALUE)==INT_VALUE) 98 return(true); 99 } 100 } 101//--- failure 102 return(false); 103 } 104//+------------------------------------------------------------------+ 105//| Reading header of array from file | 106//+------------------------------------------------------------------+ 107bool CArray::Load(const int file_handle) 108 { 109//--- check handle 110 if(file_handle!=INVALID_HANDLE) 111 { 112 //--- read and check start marker - 0xFFFFFFFFFFFFFFFF 113 if(FileReadLong(file_handle)==-1) 114 { 115 //--- read and check array type 116 if(FileReadInteger(file_handle,INT_VALUE)==Type()) 117 return(true); 118 } 119 } 120//--- failure 121 return(false); 122 } 123//+------------------------------------------------------------------+ 124