|
| 1 | +//+------------------------------------------------------------------+ |
| 2 | +//| Array.mqh | |
| 3 | +//| Copyright 2009-2017, MetaQuotes Software Corp. | |
| 4 | +//| http://www.mql5.com | |
| 5 | +//+------------------------------------------------------------------+ |
| 6 | +#include <Object.mqh> |
| 7 | +//+------------------------------------------------------------------+ |
| 8 | +//| Class CArray | |
| 9 | +//| Purpose: Base class of dynamic arrays. | |
| 10 | +//| Derives from class CObject. | |
| 11 | +//+------------------------------------------------------------------+ |
| 12 | +class CArray : public CObject |
| 13 | + { |
| 14 | +protected: |
| 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 | + |
| 20 | +public: |
| 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 | + |
| 39 | +protected: |
| 40 | + virtual void QuickSort(int beg,int end,const int mode=0) { m_sort_mode=-1; } |
| 41 | + //--- templates for methods of searching for minimum and maximum |
| 42 | + template<typename T> |
| 43 | + int Minimum(const T &data[],const int start,const int count) const; |
| 44 | + template<typename T> |
| 45 | + int Maximum(const T &data[],const int start,const int count) const; |
| 46 | + }; |
| 47 | +//+------------------------------------------------------------------+ |
| 48 | +//| Constructor | |
| 49 | +//+------------------------------------------------------------------+ |
| 50 | +CArray::CArray(void) : m_step_resize(16), |
| 51 | + m_data_total(0), |
| 52 | + m_data_max(0), |
| 53 | + m_sort_mode(-1) |
| 54 | + { |
| 55 | + } |
| 56 | +//+------------------------------------------------------------------+ |
| 57 | +//| Destructor | |
| 58 | +//+------------------------------------------------------------------+ |
| 59 | +CArray::~CArray(void) |
| 60 | + { |
| 61 | + } |
| 62 | +//+------------------------------------------------------------------+ |
| 63 | +//| Method Set for variable m_step_resize | |
| 64 | +//+------------------------------------------------------------------+ |
| 65 | +bool CArray::Step(const int step) |
| 66 | + { |
| 67 | +//--- check |
| 68 | + if(step>0) |
| 69 | + { |
| 70 | + m_step_resize=step; |
| 71 | + return(true); |
| 72 | + } |
| 73 | +//--- failure |
| 74 | + return(false); |
| 75 | + } |
| 76 | +//+------------------------------------------------------------------+ |
| 77 | +//| Sorting an array in ascending order | |
| 78 | +//+------------------------------------------------------------------+ |
| 79 | +void CArray::Sort(const int mode) |
| 80 | + { |
| 81 | +//--- check |
| 82 | + if(IsSorted(mode)) |
| 83 | + return; |
| 84 | + m_sort_mode=mode; |
| 85 | + if(m_data_total<=1) |
| 86 | + return; |
| 87 | +//--- sort |
| 88 | + QuickSort(0,m_data_total-1,mode); |
| 89 | + } |
| 90 | +//+------------------------------------------------------------------+ |
| 91 | +//| Writing header of array to file | |
| 92 | +//+------------------------------------------------------------------+ |
| 93 | +bool CArray::Save(const int file_handle) |
| 94 | + { |
| 95 | +//--- check handle |
| 96 | + if(file_handle!=INVALID_HANDLE) |
| 97 | + { |
| 98 | + //--- write start marker - 0xFFFFFFFFFFFFFFFF |
| 99 | + if(FileWriteLong(file_handle,-1)==sizeof(long)) |
| 100 | + { |
| 101 | + //--- write array type |
| 102 | + if(FileWriteInteger(file_handle,Type(),INT_VALUE)==INT_VALUE) |
| 103 | + return(true); |
| 104 | + } |
| 105 | + } |
| 106 | +//--- failure |
| 107 | + return(false); |
| 108 | + } |
| 109 | +//+------------------------------------------------------------------+ |
| 110 | +//| Reading header of array from file | |
| 111 | +//+------------------------------------------------------------------+ |
| 112 | +bool CArray::Load(const int file_handle) |
| 113 | + { |
| 114 | +//--- check handle |
| 115 | + if(file_handle!=INVALID_HANDLE) |
| 116 | + { |
| 117 | + //--- read and check start marker - 0xFFFFFFFFFFFFFFFF |
| 118 | + if(FileReadLong(file_handle)==-1) |
| 119 | + { |
| 120 | + //--- read and check array type |
| 121 | + if(FileReadInteger(file_handle,INT_VALUE)==Type()) |
| 122 | + return(true); |
| 123 | + } |
| 124 | + } |
| 125 | +//--- failure |
| 126 | + return(false); |
| 127 | + } |
| 128 | +//+------------------------------------------------------------------+ |
| 129 | +//| Find minimum of array | |
| 130 | +//+------------------------------------------------------------------+ |
| 131 | +template<typename T> |
| 132 | +int CArray::Minimum(const T &data[],const int start,const int count) const |
| 133 | + { |
| 134 | + int real_count; |
| 135 | +//--- check for empty array |
| 136 | + if(m_data_total<1) |
| 137 | + { |
| 138 | + SetUserError(ERR_USER_ARRAY_IS_EMPTY); |
| 139 | + return(-1); |
| 140 | + } |
| 141 | + //--- check for start is out of range |
| 142 | + if(start<0 || start>=m_data_total) |
| 143 | + { |
| 144 | + SetUserError(ERR_USER_ITEM_NOT_FOUND); |
| 145 | + return(-1); |
| 146 | + } |
| 147 | +//--- compute count of elements |
| 148 | + real_count=(count==WHOLE_ARRAY || start+count>m_data_total) ? m_data_total-start : count; |
| 149 | +#ifdef __MQL5__ |
| 150 | + return(ArrayMinimum(data,start,real_count)); |
| 151 | +#else |
| 152 | + return(ArrayMinimum(data,real_count,start)); |
| 153 | +#endif |
| 154 | + } |
| 155 | +//+------------------------------------------------------------------+ |
| 156 | +//| Find maximum of array | |
| 157 | +//+------------------------------------------------------------------+ |
| 158 | +template<typename T> |
| 159 | +int CArray::Maximum(const T &data[],const int start,const int count) const |
| 160 | + { |
| 161 | + int real_count; |
| 162 | +//--- check for empty array |
| 163 | + if(m_data_total<1) |
| 164 | + { |
| 165 | + SetUserError(ERR_USER_ARRAY_IS_EMPTY); |
| 166 | + return(-1); |
| 167 | + } |
| 168 | + //--- check for start is out of range |
| 169 | + if(start<0 || start>=m_data_total) |
| 170 | + { |
| 171 | + SetUserError(ERR_USER_ITEM_NOT_FOUND); |
| 172 | + return(-1); |
| 173 | + } |
| 174 | +//--- compute count of elements |
| 175 | + real_count=(count==WHOLE_ARRAY || start+count>m_data_total) ? m_data_total-start : count; |
| 176 | +#ifdef __MQL5__ |
| 177 | + return(ArrayMaximum(data,start,real_count)); |
| 178 | +#else |
| 179 | + return(ArrayMaximum(data,real_count,start)); |
| 180 | +#endif |
| 181 | + } |
| 182 | +//+------------------------------------------------------------------+ |
0 commit comments