blob: 2394915e2c95d7a43099262610b5a411e5f8548d (
plain)
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
|
#include "StarImage.hpp"
#include "StarImageProcessing.hpp"
#include "StarDirectives.hpp"
#include "StarXXHash.hpp"
#include "StarHash.hpp"
namespace Star {
NestedDirectives::NestedDirectives() : m_root(nullptr) {}
NestedDirectives::NestedDirectives(String const& directives) {
parseDirectivesIntoLeaf(directives);
}
NestedDirectives::NestedDirectives(String&& directives) {
String mine = move(directives); // most useless move constructor in the world
parseDirectivesIntoLeaf(mine);
}
void NestedDirectives::parseDirectivesIntoLeaf(String const& directives) {
Leaf leaf;
for (String& op : directives.split('?')) {
if (!op.empty())
leaf.entries.emplace_back(imageOperationFromString(op), op);
}
m_root = std::make_shared<Cell>(move(leaf));
}
inline bool NestedDirectives::empty() const {
return (bool)m_root;
}
inline bool NestedDirectives::compare(NestedDirectives const& other) const {
if (m_root == other.m_root)
return true;
return false;
}
void NestedDirectives::append(NestedDirectives const& other) {
convertToBranches().emplace_back(other.branch());
}
NestedDirectives& NestedDirectives::operator+=(NestedDirectives const& other) {
append(other);
return *this;
}
String NestedDirectives::toString() const {
String string;
addToString(string);
return string;
}
void NestedDirectives::addToString(String& string) const {
if (m_root)
m_root->buildString(string);
}
void NestedDirectives::forEach(LeafCallback callback) const {
if (m_root)
m_root->forEach(callback);
}
void NestedDirectives::forEachPair(LeafPairCallback callback) const {
if (m_root) {
LeafCallback pairCallback = [&](Leaf const& leaf) {
for (auto& entry : leaf.entries)
callback(entry.operation, entry.string);
};
m_root->forEach(pairCallback);
}
}
bool NestedDirectives::forEachAbortable(AbortableLeafCallback callback) const {
if (!m_root)
return false;
else
return m_root->forEachAbortable(callback);
}
bool NestedDirectives::forEachPairAbortable(AbortableLeafPairCallback callback) const {
if (!m_root)
return false;
else {
AbortableLeafCallback pairCallback = [&](Leaf const& leaf) -> bool {
for (auto& entry : leaf.entries) {
if (!callback(entry.operation, entry.string))
return false;
}
return true;
};
return m_root->forEachAbortable(pairCallback);
}
}
Image NestedDirectives::apply(Image& image) const {
Image current = image;
forEachPair([&](ImageOperation const& operation, String const& string) {
processImageOperation(operation, current);
});
return current;
}
NestedDirectives::Branches& NestedDirectives::convertToBranches() {
if (!m_root) {
m_root = std::make_shared<Cell>(Branches());
}
else if (m_root->value.is<Branches>())
return;
Leaf& leaf = m_root->value.get<Leaf>();
Branches newBranches;
newBranches.emplace_back(std::make_shared<Cell const>(move(leaf)));
m_root->value = move(newBranches);
return m_root->value.get<Branches>();
}
bool NestedDirectives::Leaf::Entry::operator==(NestedDirectives::Leaf::Entry const& other) const {
return string == other.string;
}
bool NestedDirectives::Leaf::Entry::operator!=(NestedDirectives::Leaf::Entry const& other) const {
return string != other.string;
}
size_t NestedDirectives::Leaf::length() const {
return entries.size();
}
bool NestedDirectives::Leaf::operator==(NestedDirectives::Leaf const& other) const {
size_t len = length();
if (len != other.length())
return false;
for (size_t i = 0; i != len; ++i) {
if (entries[i] != other.entries[i])
return false;
}
return true;
}
bool NestedDirectives::Leaf::operator!=(NestedDirectives::Leaf const& other) const {
return !(*this == other);
}
NestedDirectives::Cell::Cell() : value(Leaf()) {};
NestedDirectives::Cell::Cell(Leaf&& leaf) : value(move(leaf)) {};
NestedDirectives::Cell::Cell(Branches&& branches) : value(move(branches)) {};
NestedDirectives::Cell::Cell(const Leaf& leaf) : value(leaf) {};
NestedDirectives::Cell::Cell(const Branches& branches) : value(branches) {};
/*
bool NestedDirectives::Cell::operator==(NestedDirectives::Cell const& other) const {
if (auto leaf = value.ptr<Leaf>()) {
if (auto otherLeaf = other.value.ptr<Leaf>())
return *leaf == *otherLeaf;
else {
}
}
else {
for (auto& branch : value.get<Branches>()) {
}
}
}
bool NestedDirectives::Cell::operator!=(NestedDirectives::Cell const& other) const {
return !(*this == other);
}
//*/
void NestedDirectives::Cell::buildString(String& string) const {
if (auto leaf = value.ptr<Leaf>())
for (auto& entry : leaf->entries) {
string += "?";
string += entry.string;
}
else {
for (auto& branch : value.get<Branches>())
branch->buildString(string);
}
}
void NestedDirectives::Cell::forEach(LeafCallback& callback) const {
if (auto leaf = value.ptr<Leaf>())
callback(*leaf);
else {
for (auto& branch : value.get<Branches>())
branch->forEach(callback);
}
}
bool NestedDirectives::Cell::forEachAbortable(AbortableLeafCallback& callback) const {
if (auto leaf = value.ptr<Leaf>()) {
if (!callback(*leaf))
return false;
} else {
for (auto& branch : value.get<Branches>())
if (!branch->forEachAbortable(callback))
return false;
}
return true;
}
}
|