forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Dict.mqh
273 lines (224 loc) · 8.07 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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2020, 31337 Investments 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"
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) {
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];
}
_current_id = right._current_id;
_mode = right._mode;
}
void operator=(const Dict<K, V>& right) {
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];
}
_current_id = right._current_id;
_mode = right._mode;
}
/**
* Inserts value using hashless key.
*/
bool Push(V value) {
if (!InsertInto(_DictSlots_ref, value)) return false;
return true;
}
/**
* Inserts or replaces value for a given key.
*/
bool Set(K key, V value) {
if (!InsertInto(_DictSlots_ref, key, value)) 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.
*/
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;
}
/**
* 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;
}
protected:
/**
* Inserts value into given array of DictSlots.
*/
bool InsertInto(DictSlotsRef<K, V>& dictSlotsRef, const K key, V value) {
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 && dictSlotsRef._num_used == ArraySize(dictSlotsRef.DictSlots)) {
// No DictSlotsRef.DictSlots available, we need to expand array of DictSlotsRef.DictSlots (by 25%).
if (!Resize(MathMax(10, (int)((float)ArraySize(dictSlotsRef.DictSlots) * 1.25)))) return false;
}
if (keySlot == NULL) {
position = Hash(key) % ArraySize(dictSlotsRef.DictSlots);
// 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)) {
// Position may overflow, so we will start from the beginning.
position = (position + 1) % ArraySize(dictSlotsRef.DictSlots);
}
++dictSlotsRef._num_used;
}
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!");
return false;
}
if (dictSlotsRef._num_used == ArraySize(dictSlotsRef.DictSlots)) {
// No DictSlotsRef.DictSlots available, we need to expand array of DictSlotsRef.DictSlots (by 25%).
if (!Resize(MathMax(10, (int)((float)ArraySize(dictSlotsRef.DictSlots) * 1.25)))) 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;
}
/**
* Shrinks or expands array of DictSlots.
*/
bool Resize(unsigned int new_size) {
if (new_size < _DictSlots_ref._num_used) {
// We can't shrink to less than number of already used DictSlots.
// It is okay to return true.
return true;
}
DictSlotsRef<K, V> new_DictSlots;
if (ArrayResize(new_DictSlots.DictSlots, new_size) == -1) return false;
// Copies entire array of DictSlots into new array of DictSlots. Hashes will be rehashed.
for (unsigned int i = 0; i < (unsigned int)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))
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:
template <>
JsonNodeType Serialize(JsonSerializer& s) {
if (s.IsWriting()) {
for (DictIteratorBase<K, V> i = Begin(); i.IsValid(); ++i) {
// As we can't retrieve reference to the Dict's value, we need to
// use temporary variable.
V value = i.Value();
s.Pass(this, i.KeyAsString(), value);
}
return (GetMode() == DictModeDict) ? JsonNodeObject : JsonNodeArray;
} else {
JsonIterator<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, i.Value(i.Key()));
} else
Push(i.Value());
return i.ParentNodeType();
}
}
};
#endif