-
Notifications
You must be signed in to change notification settings - Fork 0
/
pool.mqh
188 lines (149 loc) · 4.32 KB
/
pool.mqh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <Arrays/Array.mqh>
template <typename T>
class CPool : public CArray {
protected:
T data_[];
private:
public:
CPool() { ArrayResize(data_, 0); }
CPool(int size) { ArrayResize(data_, size); }
~CPool() { Clear(); }
int Size() const { return ArraySize(data_); }
T Item(int index) const { return data_[index]; }
virtual int Create(T &elements[]);
virtual void Append(T &element);
virtual bool Search(T &element);
virtual int Extract(T &data[]);
virtual string ArrayAsString();
void Clear();
int Pop(int index);
int Dequeue();
};
template <typename T>
void CPool::Clear(void) {
ArrayFree(data_);
ArrayResize(data_, 0);
ZeroMemory(data_);
}
template <typename T>
void CPool::Append(T &element) {
int size = Size();
ArrayResize(data_, size+1);
data_[size] = element;
}
template <typename T>
int CPool::Pop(int index) {
T dummy[];
int size = Size();
for (int i = 0; i < size; i++) {
if (i == index) continue;
int dummy_size = ArraySize(dummy);
ArrayResize(dummy, dummy_size+1);
dummy[dummy_size] = data_[i];
}
Clear();
Create(dummy);
return Size();
}
template <typename T>
int CPool::Create(T &elements[]) {
int size = ArraySize(elements);
ArrayResize(data_, size);
for (int i = 0; i < size; i++) data_[i] = elements[i];
return ArraySize(data_);
}
template <typename T>
int CPool::Dequeue(void) { return Pop(0); }
template <typename T>
class CPoolGeneric : public CPool<T> {
private:
protected:
public:
CPoolGeneric(){};
~CPoolGeneric() {};
virtual bool Search(T &element);
virtual string ArrayAsString();
virtual int Create(T &elements[]);
virtual int Extract(T &data[]);
virtual int Remove(T element);
virtual bool Sort();
virtual int Add(T &data[]);
virtual T First();
virtual T Last();
};
template <typename T>
bool CPoolGeneric::Search(T &element) {
int size = Size();
for (int i = 0; i < size; i++) if (element == data_[i]) return true;
return false;
}
template <typename T>
string CPoolGeneric::ArrayAsString(void) {
int size = Size();
string array_string = "";
for (int i = 0; i < size; i++) {
T item = data_[i];
if (i == 0) array_string = (string)item;
else array_string = StringConcatenate(array_string, ", ", (string)item);
}
return array_string;
}
template <typename T>
int CPoolGeneric::Create(T &elements[]) {
int size = ArraySize(elements);
ArrayResize(data_, size);
ArrayCopy(data_, elements, 0, 0);
return Size();
}
template <typename T>
int CPoolGeneric::Extract(T &data[]) {
int size = Size();
ArrayResize(data, size);
ArrayCopy(data, data_);
return ArraySize(data);
}
template <typename T>
int CPoolGeneric::Add(T &data[]) {
for (int i = 0; i < ArraySize(data); i++) Append(data[i]);
return ArraySize(data);
}
template <typename T>
int CPoolGeneric::Remove(T element) {
int size = Size();
CPoolGeneric<T> *dummy = new CPoolGeneric<T>();
for (int i = 0; i < size; i++) {
T item = Item(i);
if (item == element) continue;
dummy.Append(item);
}
Clear();
T extracted[];
int num_extracted = dummy.Extract(extracted);
Create(extracted);
delete dummy;
return Size();
}
template <typename T>
bool CPoolGeneric::Sort(void) {
if (Size() == 0) return false;
return ArraySort(data_);
}
template <typename T>
T CPoolGeneric::First(void) {
Sort();
return Item(0);
}
template <typename T>
T CPoolGeneric::Last(void) {
Sort();
if (Size() == 0) return NULL;
int last = Size() - 1;
return Item(last);
}
// ===== POOL OBJECT
template <typename T>
class CPoolObject : public CPool<T> {
public:
CPoolObject() {};
~CPoolObject(){};
};