-
-
Notifications
You must be signed in to change notification settings - Fork 100
/
DictIteratorBase.mqh
140 lines (116 loc) · 3.79 KB
/
DictIteratorBase.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
//+------------------------------------------------------------------+
//| 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/>.
*
*/
#ifndef __MQL__
// Allows the preprocessor to include a header file when it is needed.
#pragma once
#endif
#include "DictBase.mqh"
#include "DictSlotsRef.h"
#include "SerializerConversions.h"
template <typename K, typename V>
class DictBase;
template <typename K, typename V>
class DictIteratorBase {
protected:
DictBase<K, V>* _dict;
int _hash;
int _slotIdx;
int _index;
bool _invalid_until_incremented;
public:
/**
* Constructor.
*/
DictIteratorBase() : _dict(NULL) { _invalid_until_incremented = false; }
/**
* Constructor.
*/
DictIteratorBase(DictBase<K, V>& dict, int slotIdx)
: _dict(&dict), _hash(dict.GetHash()), _slotIdx(slotIdx), _index(0) {
_invalid_until_incremented = false;
}
/**
* Copy constructor.
*/
DictIteratorBase(const DictIteratorBase& right)
: _dict(right._dict),
_hash(right._dict ? right._dict PTR_DEREF GetHash() : 0),
_slotIdx(right._slotIdx),
_index(right._index) {
_invalid_until_incremented = false;
}
/**
* Iterator incrementation operator.
*/
void operator++(void) {
// Going to the next slot.
++_slotIdx;
++_index;
_invalid_until_incremented = false;
DictSlot<K, V>* slot = _dict PTR_DEREF GetSlot(_slotIdx);
// Iterating until we find valid, used slot.
while (slot != NULL && !slot PTR_DEREF IsUsed()) {
slot = _dict PTR_DEREF GetSlot(++_slotIdx);
}
if (!slot || !slot PTR_DEREF IsValid()) {
// Invalidating iterator.
_dict = NULL;
}
}
bool HasKey() { return _dict PTR_DEREF GetSlot(_slotIdx) PTR_DEREF HasKey(); }
K Key() {
CheckValidity();
return PTR_ATTRIB(_dict, GetMode()) == DictModeList ? (K)_slotIdx : _dict PTR_DEREF GetSlot(_slotIdx) PTR_DEREF key;
}
string KeyAsString(bool includeQuotes = false) {
return HasKey() ? SerializerConversions::ValueToString(Key(), includeQuotes) : "";
}
int Index() {
CheckValidity();
return _index;
}
V Value() {
CheckValidity();
return _dict PTR_DEREF GetSlot(_slotIdx) PTR_DEREF value;
}
void CheckValidity() {
if (_invalid_until_incremented) {
Alert("Iterator must be incremented before using it again!");
DebugBreak();
}
}
bool IsValid() { return _dict != NULL; }
bool IsLast() {
if (!IsValid()) return true;
if (_dict PTR_DEREF GetMode() == DictModeUnknown || _dict PTR_DEREF Size() == 0) {
return false;
}
if (_dict PTR_DEREF GetMode() != DictModeList) {
Alert("Dict iterator's IsLast() method may be used only when elements are added via Push() method.");
}
return _index == _dict PTR_DEREF Size() - 1;
}
void ShiftPosition(int shift, bool invalid_until_incremented = false) {
_slotIdx += shift;
_index += shift;
_invalid_until_incremented |= invalid_until_incremented;
}
};