-
Notifications
You must be signed in to change notification settings - Fork 0
/
api.cpp
401 lines (343 loc) · 12.9 KB
/
api.cpp
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
//
// Created by Yegor Jbanov on 9/4/16.
//
#include "api.h"
#include "sync.h"
#include <algorithm>
#include <cassert>
#include <map>
#include <memory>
#include <string>
#include <typeindex>
#include <typeinfo>
#include <iostream>
using namespace std;
namespace barista {
bool _sameType(void *a, void *b) {
return typeid(a) == typeid(b);
}
bool _canUpdate(shared_ptr<RenderNode> node, shared_ptr<Node> configuration) {
if (!_sameType(node->GetConfiguration().get(), configuration.get())) {
return false;
}
return node->GetConfiguration()->GetKey() == configuration->GetKey();
}
RenderNode::RenderNode(shared_ptr<Tree> tree) : _tree(tree) { }
void RenderNode::Update(shared_ptr<Node> newConfiguration, ElementUpdate& update) {
assert(newConfiguration != nullptr);
_configuration = newConfiguration;
}
RenderParent::RenderParent(shared_ptr<Tree> tree) : RenderNode(tree) { }
void RenderParent::ScheduleUpdate() {
_hasDescendantsNeedingUpdate = true;
shared_ptr<RenderParent> parent = GetParent();
while (parent != nullptr) {
parent->_hasDescendantsNeedingUpdate = true;
parent = parent->GetParent();
}
}
void RenderParent::Update(shared_ptr<Node> newConfiguration, ElementUpdate& update) {
_hasDescendantsNeedingUpdate = false;
RenderNode::Update(newConfiguration, update);
}
string Tree::RenderFrame(int indent) {
auto treeUpdate = TreeUpdate();
RenderFrameIntoUpdate(treeUpdate);
return treeUpdate.Render(indent);
}
void Tree::RenderFrameIntoUpdate(TreeUpdate & treeUpdate) {
if (_topLevelNode == nullptr) {
_topLevelNode = _topLevelWidget->Instantiate(shared_from_this());
auto& rootInsertion = treeUpdate.CreateRootElement();
_topLevelNode->Update(_topLevelWidget, rootInsertion);
} else {
auto& rootUpdate = treeUpdate.UpdateRootElement();
_topLevelNode->Update(_topLevelWidget, rootUpdate);
}
}
void Tree::VisitChildren(RenderNodeVisitor visitor) {
visitor(_topLevelNode);
}
void Tree::DispatchEvent(const Event& event) {
_topLevelNode->DispatchEvent(event);
}
shared_ptr<RenderNode> StatelessWidget::Instantiate(shared_ptr<Tree> tree) {
return make_shared<RenderStatelessWidget>(tree);
}
shared_ptr<RenderNode> StatefulWidget::Instantiate(shared_ptr<Tree> t) {
return make_shared<RenderStatefulWidget>(t);
}
void State::ScheduleUpdate() { _node->ScheduleUpdate(); }
void internalSetStateNode(shared_ptr<State> state, shared_ptr<RenderStatefulWidget> node) {
state->_node = node;
}
void RenderStatelessWidget::DispatchEvent(const Event& event) {
if (_child == nullptr) return;
_child->DispatchEvent(event);
}
bool RenderStatelessWidget::CanUpdateUsing(shared_ptr<Node> newConfiguration) {
assert(newConfiguration != nullptr);
auto oldConfiguration = GetConfiguration();
assert(oldConfiguration != nullptr);
return typeid(oldConfiguration) == typeid(newConfiguration);
}
void RenderStatelessWidget::Update(shared_ptr<Node> configPtr, ElementUpdate& update) {
assert(dynamic_cast<StatelessWidget*>(configPtr.get()));
auto newConfiguration = static_pointer_cast<StatelessWidget>(configPtr);
if (GetConfiguration() != newConfiguration) {
// Build the new configuration and decide whether to reuse the child node
// or replace with a new one.
shared_ptr<Node> newChildConfiguration = newConfiguration->Build();
if (_child != nullptr && _canUpdate(_child, newChildConfiguration)) {
_child->Update(newChildConfiguration, update);
} else {
// Replace child
if (_child != nullptr) {
_child->Detach();
}
_child = newChildConfiguration->Instantiate(GetTree());
_child->Update(newChildConfiguration, update);
_child->Attach(shared_from_this());
}
} else if (GetHasDescendantsNeedingUpdate()) {
assert(_child != nullptr);
// Own configuration is the same, but some children are scheduled to be
// updated.
_child->Update(_child->GetConfiguration(), update);
}
RenderParent::Update(newConfiguration, update);
}
void RenderStatefulWidget::VisitChildren(RenderNodeVisitor visitor) {
visitor(_child);
}
void RenderStatefulWidget::ScheduleUpdate() {
_isDirty = true;
RenderParent::ScheduleUpdate();
}
void RenderStatefulWidget::DispatchEvent(const Event& event) {
if (_child == nullptr) return;
_child->DispatchEvent(event);
}
bool RenderStatefulWidget::CanUpdateUsing(shared_ptr<Node> newConfiguration) {
assert(newConfiguration != nullptr);
auto oldConfiguration = GetConfiguration();
assert(oldConfiguration != nullptr);
return typeid(oldConfiguration) == typeid(newConfiguration);
}
void RenderStatefulWidget::Update(shared_ptr<Node> configPtr, ElementUpdate& update) {
assert(dynamic_cast<StatefulWidget*>(configPtr.get()));
auto newConfiguration = static_pointer_cast<StatefulWidget>(configPtr);
if (GetConfiguration() != newConfiguration) {
// Build the new configuration and decide whether to reuse the child node
// or replace with a new one.
_state = newConfiguration->CreateState();
_state->_config = newConfiguration;
internalSetStateNode(_state, shared_from_this());
shared_ptr<Node> newChildConfiguration = _state->Build();
if (_child != nullptr && _sameType(newChildConfiguration.get(), _child->GetConfiguration().get())) {
_child->Update(newChildConfiguration, update);
} else {
if (_child != nullptr) {
_child->Detach();
}
_child = newChildConfiguration->Instantiate(GetTree());
_child->Update(newChildConfiguration, update);
_child->Attach(shared_from_this());
}
} else if (_isDirty) {
_child->Update(_state->Build(), update);
} else if (GetHasDescendantsNeedingUpdate()) {
// Own configuration is the same, but some children are scheduled to be
// updated.
_child->Update(_child->GetConfiguration(), update);
}
_isDirty = false;
RenderParent::Update(newConfiguration, update);
}
void RenderMultiChildParent::VisitChildren(RenderNodeVisitor visitor) {
for (auto child : _currentChildren) {
visitor(child);
}
}
vector<int> ComputeLongestIncreasingSubsequence(vector<int> & sequence) {
auto len = sequence.size();
vector<int> predecessors;
vector<int> mins = {0};
int longest = 0;
for (int i = 0; i < len; i++) {
// Binary search for the largest positive `j ≤ longest`
// such that `list[mins[j]] < list[i]`
int elem = sequence[i];
int lo = 1;
int hi = longest;
while (lo <= hi) {
int mid = (lo + hi) / 2;
if (sequence[mins[mid]] < elem) {
lo = mid + 1;
} else {
hi = mid - 1;
}
}
// After searching, `lo` is 1 greater than the
// length of the longest prefix of `list[i]`
int expansionIndex = lo;
// The predecessor of `list[i]` is the last index of
// the subsequence of length `newLongest - 1`
predecessors.push_back(mins[expansionIndex - 1]);
if (expansionIndex >= mins.size()) {
mins.push_back(i);
} else {
mins[expansionIndex] = i;
}
if (expansionIndex > longest) {
// If we found a subsequence longer than any we've
// found yet, update `longest`
longest = expansionIndex;
}
}
// Reconstruct the longest subsequence
vector<int> lis((unsigned long) longest);
int k = mins[longest];
for (int i = longest - 1; i >= 0; i--) {
lis[i] = sequence[k];
k = predecessors[k];
}
return lis;
}
void RenderMultiChildParent::Update(shared_ptr<Node> configPtr, ElementUpdate& update) {
assert(dynamic_cast<MultiChildNode*>(configPtr.get()));
auto newConfiguration = static_pointer_cast<MultiChildNode>(configPtr);
if (GetConfiguration() != nullptr) {
assert(dynamic_cast<MultiChildNode*>(GetConfiguration().get()));
}
auto oldConfiguration = static_pointer_cast<MultiChildNode>(GetConfiguration());
if (oldConfiguration == newConfiguration) {
// No need to diff child lists.
if (GetHasDescendantsNeedingUpdate()) {
for (auto child = _currentChildren.begin(); child != _currentChildren.end(); child++) {
auto& childUpdate = update.UpdateChildElement(child - _currentChildren.begin());
(*child)->Update((*child)->GetConfiguration(), childUpdate);
}
}
RenderParent::Update(configPtr, update);
return;
}
vector<shared_ptr<Node>> newChildren = newConfiguration->GetChildren();
// A tuple with tracking information about a child node
using TrackedChild = tuple<
vector<shared_ptr<RenderNode>>::iterator, // points to _currentChildren
int, // index of the child in _currentChildren
bool // whether the child pointed to should be retained
>;
auto currentChildren = vector<TrackedChild>(_currentChildren.size());
map<string, int> keyMap;
for (auto iter = _currentChildren.begin(); iter != _currentChildren.end(); iter++) {
auto node = *iter;
int baseIndex = (int) (iter - _currentChildren.begin());
currentChildren[baseIndex] = {
iter,
baseIndex,
false,
};
shared_ptr<Node> config = node->GetConfiguration();
auto key = config->GetKey();
if (key != "") {
keyMap[key] = baseIndex;
}
}
vector<int> sequence;
vector<tuple<shared_ptr<Node>, int>> targetList;
// | |
// node base index (or -1)
vector<TrackedChild>::iterator afterLastUsedUnkeyedChild = currentChildren.begin();
for (auto iter = newChildren.begin(); iter != newChildren.end(); iter++) {
shared_ptr<Node> node = *iter;
auto key = node->GetKey();
vector<TrackedChild>::iterator baseChild = currentChildren.end();
if (key != "") {
auto baseEntry = keyMap.find(key);
if (baseEntry != keyMap.end()) {
baseChild = currentChildren.begin() + baseEntry->second;
shared_ptr<RenderNode> currentChild = *(get<0>(*baseChild));
if (currentChild->CanUpdateUsing(node)) {
auto& childUpdate = update.UpdateChildElement(get<1>(*baseChild));
currentChild->Update(node, childUpdate);
}
}
} else {
// Start with afterLastUsedUnkeyedChild and scan until the first child
// we can update. Use it. This approach is naive. It does not support
// swaps, for example. It does support removes though. For swaps, the
// developer is expected to use keys anyway.
vector<TrackedChild>::iterator scanner = afterLastUsedUnkeyedChild;
while(scanner != currentChildren.end()) {
shared_ptr<RenderNode> currentChild = *(get<0>(*scanner));
if (currentChild->CanUpdateUsing(node)) {
auto& childUpdate = update.UpdateChildElement(get<1>(*scanner));
currentChild->Update(node, childUpdate);
baseChild = scanner;
afterLastUsedUnkeyedChild = scanner + 1;
break;
}
scanner++;
}
}
int baseIndex = -1;
if (baseChild != currentChildren.end()) {
baseIndex = get<1>(*baseChild);
get<2>(*baseChild) = true;
sequence.push_back(baseIndex);
}
targetList.push_back({node, baseIndex});
}
// Compute removes
for (auto i = currentChildren.begin(); i != currentChildren.end(); i++) {
if (!get<2>(*i)) {
update.RemoveChild((int) (i - currentChildren.begin()));
}
}
// Compute inserts and updates
vector<int> lis = ComputeLongestIncreasingSubsequence(sequence);
auto insertionPoint = lis.begin();
vector<shared_ptr<RenderNode>> newChildVector;
int baseCount = (int) _currentChildren.size();
for (auto targetEntry = targetList.begin(); targetEntry != targetList.end(); targetEntry++) {
// Three possibilities:
// - it's a new child => its base index == -1
// - it's a moved child => its base index != -1 && base index != insertion index
// - it's a stationary child => its base index != -1 && base index == insertion index
// Index in the base list of the moved child, or -1
int baseIndex = get<1>(*targetEntry);
// Index in the base list before which target child must be inserted.
int insertionIndex = baseCount;
if (insertionPoint != lis.end()) {
insertionIndex = *insertionPoint;
if (baseIndex == insertionIndex) {
// We've moved past the element in the target list that
// corresponds to the insertion point. Advance to the next one.
insertionPoint++;
}
}
if (baseIndex == -1) {
// New child
shared_ptr<Node> childNode = get<0>(*targetEntry);
// Lock the diff object so child nodes do not push diffs.
auto& childInsertion = update.InsertChildElement(insertionIndex);
auto childRenderNode = childNode->Instantiate(GetTree());
newChildVector.push_back(childRenderNode);
childRenderNode->Update(childNode, childInsertion);
childRenderNode->Attach(shared_from_this());
} else {
if (baseIndex != insertionIndex) {
// Moved child
update.MoveChild(insertionIndex, baseIndex);
newChildVector.push_back(_currentChildren[baseIndex]);
} else {
newChildVector.push_back(_currentChildren[baseIndex]);
}
}
}
_currentChildren = newChildVector;
RenderParent::Update(configPtr, update);
}
}