-
-
Notifications
You must be signed in to change notification settings - Fork 101
/
Dict.mqh
430 lines (359 loc) · 12.4 KB
/
Dict.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
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2023, EA31337 Ltd |
//| https://github.com/EA31337 |
//+------------------------------------------------------------------+
/*
* This file is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
*/
// Prevents processing this includes file for the second time.
#ifndef DICT_MQH
#define DICT_MQH
#include "Convert.mqh"
#include "DictBase.mqh"
#include "Matrix.mqh"
#include "Serializer.mqh"
#include "SerializerNodeIterator.mqh"
template <typename K, typename V>
class DictIterator : public DictIteratorBase<K, V> {
public:
/**
* Constructor.
*/
DictIterator() {}
/**
* Constructor.
*/
DictIterator(DictBase<K, V>& dict, unsigned int slotIdx) : DictIteratorBase(dict, slotIdx) {}
/**
* Copy constructor.
*/
DictIterator(const DictIterator& right) : DictIteratorBase(right) {}
};
/**
* Hash-table based dictionary.
*/
template <typename K, typename V>
class Dict : public DictBase<K, V> {
protected:
public:
/**
* Constructor.
*/
Dict() {}
Dict(string _data, string _dlm = "\n") {}
/**
* Copy constructor.
*/
Dict(const Dict<K, V>& right) {
Clear();
Resize(right.GetSlotCount());
for (unsigned int i = 0; i < (unsigned int)ArraySize(right._DictSlots_ref.DictSlots); ++i) {
_DictSlots_ref.DictSlots[i] = right._DictSlots_ref.DictSlots[i];
}
_DictSlots_ref._num_used = right._DictSlots_ref._num_used;
_current_id = right._current_id;
_mode = right._mode;
}
void operator=(const Dict<K, V>& right) {
Clear();
Resize(right.GetSlotCount());
for (unsigned int i = 0; i < (unsigned int)ArraySize(right._DictSlots_ref.DictSlots); ++i) {
_DictSlots_ref.DictSlots[i] = right._DictSlots_ref.DictSlots[i];
}
_DictSlots_ref._num_used = right._DictSlots_ref._num_used;
_current_id = right._current_id;
_mode = right._mode;
}
void Clear() {
for (unsigned int i = 0; i < (unsigned int)ArraySize(_DictSlots_ref.DictSlots); ++i) {
if (_DictSlots_ref.DictSlots[i].IsUsed()) _DictSlots_ref.DictSlots[i].SetFlags(0);
}
_DictSlots_ref._num_used = 0;
}
/**
* Inserts value using hashless key.
*/
bool Push(V value) {
if (!InsertInto(_DictSlots_ref, value)) return false;
return true;
}
/**
* Inserts value using hashless key.
*/
bool operator+=(V value) { return Push(value); }
/**
* Inserts or replaces value for a given key.
*/
bool Set(K key, V value) {
if (!InsertInto(_DictSlots_ref, key, value, true)) return false;
return true;
}
V operator[](K key) {
if (_mode == DictModeList) return GetSlot((unsigned int)key).value;
int position;
DictSlot<K, V>* slot = GetSlotByKey(_DictSlots_ref, key, position);
if (!slot) return (V)NULL;
return slot.value;
}
/**
* Returns value for a given key.
*
* @return
* Returns value for a given key, otherwise the default value.
*/
V GetByKey(const K _key, V _default = NULL) {
unsigned int position;
DictSlot<K, V>* slot = GetSlotByKey(_DictSlots_ref, _key, position);
if (!slot) {
return _default;
}
return slot.value;
}
/**
* Returns value for a given position.
*/
V GetByPos(unsigned int _position) {
DictSlot<K, V>* slot = GetSlotByPos(_DictSlots_ref, _position);
if (!slot) {
Alert("Invalid DictStruct position \"", _position, "\" (called by GetByPos()). Returning empty structure.");
DebugBreak();
static V _empty;
return _empty;
}
return slot.value;
}
/**
* Checks whether dictionary contains given key => value pair.
*/
template <>
bool Contains(const K key, const V value) {
unsigned int position;
DictSlot<K, V>* slot = GetSlotByKey(_DictSlots_ref, key, position);
if (!slot) return false;
return slot.value == value;
}
/**
* Returns index of dictionary's value or -1 if value doesn't exist.
*/
template <>
int IndexOf(V& value) {
for (DictIteratorBase<K, V> i(Begin()); i.IsValid(); ++i) {
if (i.Value() == value) {
return (int)i.Index();
}
}
return -1;
}
/**
* Checks whether dictionary contains given value.
*/
bool Contains(const V value) {
for (DictIterator<K, V> i = Begin(); i.IsValid(); ++i) {
if (i.Value() == value) {
return true;
}
}
return false;
}
protected:
/**
* Inserts value into given array of DictSlots.
*/
bool InsertInto(DictSlotsRef<K, V>& dictSlotsRef, const K key, V value, bool allow_resize) {
if (_mode == DictModeUnknown)
_mode = DictModeDict;
else if (_mode != DictModeDict) {
Alert("Warning: Dict already operates as a list, not a dictionary!");
return false;
}
unsigned int position;
DictSlot<K, V>* keySlot = GetSlotByKey(dictSlotsRef, key, position);
if (keySlot == NULL && !IsGrowUpAllowed()) {
// Resize is prohibited.
return false;
}
// Will resize dict if there were performance problems before.
if (allow_resize && IsGrowUpAllowed() && !dictSlotsRef.IsPerformant()) {
if (!GrowUp()) {
return false;
}
// We now have new positions of slots, so we have to take the corrent slot again.
keySlot = GetSlotByKey(dictSlotsRef, key, position);
}
if (keySlot == NULL && dictSlotsRef._num_used == ArraySize(dictSlotsRef.DictSlots)) {
// No DictSlotsRef.DictSlots available.
if (overflow_listener != NULL) {
if (!overflow_listener(DICT_OVERFLOW_REASON_FULL, dictSlotsRef._num_used, 0)) {
// Overwriting slot pointed exactly by key's position in the hash table (we don't check for possible
// conflicts).
keySlot = &dictSlotsRef.DictSlots[Hash(key) % ArraySize(dictSlotsRef.DictSlots)];
}
}
if (keySlot == NULL) {
// We need to expand array of DictSlotsRef.DictSlots (by 25% by default).
if (!GrowUp()) return false;
}
}
if (keySlot == NULL) {
position = Hash(key) % ArraySize(dictSlotsRef.DictSlots);
unsigned int _starting_position = position;
int _num_conflicts = 0;
bool _overwrite_slot = false;
// Searching for empty DictSlot<K, V> or used one with the matching key. It skips used, hashless DictSlots.
while (dictSlotsRef.DictSlots[position].IsUsed() &&
(!dictSlotsRef.DictSlots[position].HasKey() || dictSlotsRef.DictSlots[position].key != key)) {
if (overflow_listener_max_conflicts != 0 && ++_num_conflicts == overflow_listener_max_conflicts) {
if (overflow_listener != NULL) {
if (!overflow_listener(DICT_OVERFLOW_REASON_TOO_MANY_CONFLICTS, dictSlotsRef._num_used, _num_conflicts)) {
// Overflow listener returned false so we won't search for further empty slot.
_overwrite_slot = true;
break;
}
} else {
// Even if there is no overflow listener function, we stop searching for further empty slot as maximum
// number of conflicts has been reached.
_overwrite_slot = true;
break;
}
}
// Position may overflow, so we will start from the beginning.
position = (position + 1) % ArraySize(dictSlotsRef.DictSlots);
}
if (_overwrite_slot) {
// Overwriting starting position for faster further lookup.
position = _starting_position;
} else {
// Slot overwrite is not needed. Using empty slot.
++dictSlotsRef._num_used;
}
dictSlotsRef.AddConflicts(_num_conflicts);
}
dictSlotsRef.DictSlots[position].key = key;
dictSlotsRef.DictSlots[position].value = value;
dictSlotsRef.DictSlots[position].SetFlags(DICT_SLOT_HAS_KEY | DICT_SLOT_IS_USED | DICT_SLOT_WAS_USED);
return true;
}
/**
* Inserts hashless value into given array of DictSlots.
*/
bool InsertInto(DictSlotsRef<K, V>& dictSlotsRef, V value) {
if (_mode == DictModeUnknown)
_mode = DictModeList;
else if (_mode != DictModeList) {
Alert("Warning: Dict already operates as a dictionary, not a list!");
DebugBreak();
return false;
}
if (dictSlotsRef._num_used == ArraySize(dictSlotsRef.DictSlots)) {
// No DictSlotsRef.DictSlots available, we need to expand array of DictSlotsRef.DictSlots.
if (!GrowUp()) return false;
}
unsigned int position = Hash((unsigned int)dictSlotsRef._list_index) % ArraySize(dictSlotsRef.DictSlots);
// Searching for empty DictSlot<K, V>.
while (dictSlotsRef.DictSlots[position].IsUsed()) {
// Position may overflow, so we will start from the beginning.
position = (position + 1) % ArraySize(dictSlotsRef.DictSlots);
}
dictSlotsRef.DictSlots[position].value = value;
dictSlotsRef.DictSlots[position].SetFlags(DICT_SLOT_IS_USED | DICT_SLOT_WAS_USED);
++dictSlotsRef._list_index;
++dictSlotsRef._num_used;
return true;
}
/**
* Expands array of DictSlots by given percentage value.
*/
bool GrowUp(int percent = DICT_GROW_UP_PERCENT_DEFAULT) {
return Resize(MathMax(10, (int)((float)ArraySize(_DictSlots_ref.DictSlots) * ((float)(percent + 100) / 100.0f))));
}
/**
* Shrinks or expands array of DictSlots.
*/
bool Resize(int new_size) {
if (new_size <= MathMin(_DictSlots_ref._num_used, ArraySize(_DictSlots_ref.DictSlots))) {
// We already use minimum number of slots possible.
return true;
}
DictSlotsRef<K, V> new_DictSlots;
if (ArrayResize(new_DictSlots.DictSlots, new_size) == -1) return false;
int i;
for (i = 0; i < new_size; ++i) {
new_DictSlots.DictSlots[i].SetFlags(0);
}
new_DictSlots._num_used = 0;
// Copies entire array of DictSlots into new array of DictSlots. Hashes will be rehashed.
for (i = 0; i < ArraySize(_DictSlots_ref.DictSlots); ++i) {
if (!_DictSlots_ref.DictSlots[i].IsUsed()) continue;
if (_DictSlots_ref.DictSlots[i].HasKey()) {
if (!InsertInto(new_DictSlots, _DictSlots_ref.DictSlots[i].key, _DictSlots_ref.DictSlots[i].value, false))
return false;
} else {
if (!InsertInto(new_DictSlots, _DictSlots_ref.DictSlots[i].value)) return false;
}
}
// Freeing old DictSlots array.
ArrayFree(_DictSlots_ref.DictSlots);
_DictSlots_ref = new_DictSlots;
return true;
}
public:
#ifdef __cplusplus
template <>
#endif
SerializerNodeType Serialize(Serializer& s) {
if (s.IsWriting()) {
for (DictIteratorBase<K, V> i(Begin()); i.IsValid(); ++i) {
V value = i.Value();
s.Pass(THIS_REF, GetMode() == DictModeDict ? i.KeyAsString() : "", value);
}
return (GetMode() == DictModeDict) ? SerializerNodeObject : SerializerNodeArray;
} else {
SerializerIterator<V> i;
for (i = s.Begin<V>(); i.IsValid(); ++i) {
if (i.HasKey()) {
// Converting key to a string.
K key;
Convert::StringToType(i.Key(), key);
// Note that we're retrieving value by a key (as we are in an
// object!).
Set(key, s.Value<V>(i.Key()));
} else {
Push(s.Value<V>());
}
}
return i.ParentNodeType();
}
}
/**
* Initializes object with given number of elements. Could be skipped for non-containers.
*/
void SerializeStub(int _n1 = 1, int _n2 = 1, int _n3 = 1, int _n4 = 1, int _n5 = 1) {
V _child = (V)NULL;
while (_n1-- > 0) {
Push(_child);
}
}
/**
* Converts values into 1D matrix.
*/
template <typename X>
Matrix<X>* ToMatrix() {
Matrix<X>* result = new Matrix<X>(Size());
for (DictIterator<K, V> i = Begin(); i.IsValid(); ++i) result[i.Index()] = (X)i.Value();
return result;
}
};
#endif