forked from EA31337/EA31337-classes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DictRef.mqh
296 lines (245 loc) · 8.98 KB
/
DictRef.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
//+------------------------------------------------------------------+
//| EA31337 framework |
//| Copyright 2016-2019, 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_REF_MQH
#define DICT_REF_MQH
#include "DictObject.mqh"
// DictIterator could be used as DictStruct iterator.
#define DictRefIterator DictObjectIterator
/**
* Hash-table based dictionary.
*/
template <typename K, typename V>
class DictRef : public DictObject<K, V> {
public:
/**
* Constructor. You may specifiy intial number of DictSlots that holds values or just leave it as it is.
*/
DictRef(unsigned int _initial_size = 0) {
if (_initial_size > 0) {
Resize(_initial_size);
}
}
/**
* Copy constructor.
*/
DictRef(const DictRef<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];
}
_DictSlots_ref._num_used = right._DictSlots_ref._num_used;
_current_id = right._current_id;
_mode = right._mode;
}
void operator=(const DictRef<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;
}
DictRefIterator<K, V> Begin() {
// Searching for first item index.
for (unsigned int i = 0; i < (unsigned int)ArraySize(_DictSlots_ref.DictSlots); ++i) {
if (_DictSlots_ref.DictSlots[i].IsValid() && _DictSlots_ref.DictSlots[i].IsUsed()) {
DictRefIterator<K, V> iter(this, i);
return iter;
}
}
// No items found.
static DictRefIterator<K, V> invalid;
return invalid;
}
/**
* 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;
}
/**
* Index operator. Returns value for a given key.
*/
V operator[](K key) {
DictSlot<K, V>* slot;
int position;
if (_mode == DictModeList)
slot = GetSlot((unsigned int)key);
else
slot = GetSlotByKey(_DictSlots_ref, key, position);
if (slot == NULL || !slot.IsUsed()) {
Alert("Invalid DictStruct key \"", key, "\" (called by [] operator). Returning empty structure.");
static V _empty;
return _empty;
}
return slot.value;
}
/**
* Returns value for a given key.
*/
V GetByKey(const K _key) {
int position;
DictSlot<K, V>* slot = GetSlotByKey(_DictSlots_ref, _key, position);
if (!slot) {
Alert("Invalid DictStruct key \"", _key, "\" (called by GetByKey()). Returning empty structure.");
static V _empty;
return _empty;
}
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.");
static V _empty;
return _empty;
}
return slot.value;
}
/**
* Checks whether dictionary contains given key => value pair.
*/
template <>
bool Contains(const K key, 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) s.PassStruct(this, i.KeyAsString(), i.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.Struct(i.Key()));
} else
Push(i.Struct());
return i.ParentNodeType();
}
}
};
#endif