OLD | NEW |
| (Empty) |
1 dart_library.library('DeltaBlue', null, /* Imports */[ | |
2 'dart_sdk' | |
3 ], function(exports, dart_sdk) { | |
4 'use strict'; | |
5 const core = dart_sdk.core; | |
6 const dart = dart_sdk.dart; | |
7 const dartx = dart_sdk.dartx; | |
8 const DeltaBlue$ = Object.create(null); | |
9 const BenchmarkBase$ = Object.create(null); | |
10 DeltaBlue$.main = function() { | |
11 new DeltaBlue$.DeltaBlue().report(); | |
12 }; | |
13 dart.fn(DeltaBlue$.main); | |
14 BenchmarkBase$.BenchmarkBase = class BenchmarkBase extends core.Object { | |
15 new(name) { | |
16 this.name = name; | |
17 } | |
18 run() {} | |
19 warmup() { | |
20 this.run(); | |
21 } | |
22 exercise() { | |
23 for (let i = 0; i < 10; i++) { | |
24 this.run(); | |
25 } | |
26 } | |
27 setup() {} | |
28 teardown() {} | |
29 static measureFor(f, timeMinimum) { | |
30 let time = 0; | |
31 let iter = 0; | |
32 let watch = new core.Stopwatch(); | |
33 watch.start(); | |
34 let elapsed = 0; | |
35 while (dart.notNull(elapsed) < dart.notNull(timeMinimum)) { | |
36 dart.dcall(f); | |
37 elapsed = watch.elapsedMilliseconds; | |
38 iter++; | |
39 } | |
40 return 1000.0 * dart.notNull(elapsed) / iter; | |
41 } | |
42 measure() { | |
43 this.setup(); | |
44 BenchmarkBase$.BenchmarkBase.measureFor(dart.fn(() => { | |
45 this.warmup(); | |
46 }), 100); | |
47 let result = BenchmarkBase$.BenchmarkBase.measureFor(dart.fn(() => { | |
48 this.exercise(); | |
49 }), 2000); | |
50 this.teardown(); | |
51 return result; | |
52 } | |
53 report() { | |
54 let score = this.measure(); | |
55 core.print(`${this.name}(RunTime): ${score} us.`); | |
56 } | |
57 }; | |
58 dart.setSignature(BenchmarkBase$.BenchmarkBase, { | |
59 constructors: () => ({new: [BenchmarkBase$.BenchmarkBase, [core.String]]}), | |
60 methods: () => ({ | |
61 run: [dart.void, []], | |
62 warmup: [dart.void, []], | |
63 exercise: [dart.void, []], | |
64 setup: [dart.void, []], | |
65 teardown: [dart.void, []], | |
66 measure: [core.double, []], | |
67 report: [dart.void, []] | |
68 }), | |
69 statics: () => ({measureFor: [core.double, [core.Function, core.int]]}), | |
70 names: ['measureFor'] | |
71 }); | |
72 DeltaBlue$.DeltaBlue = class DeltaBlue extends BenchmarkBase$.BenchmarkBase { | |
73 new() { | |
74 super.new("DeltaBlue"); | |
75 } | |
76 run() { | |
77 DeltaBlue$.chainTest(100); | |
78 DeltaBlue$.projectionTest(100); | |
79 } | |
80 }; | |
81 dart.setSignature(DeltaBlue$.DeltaBlue, { | |
82 constructors: () => ({new: [DeltaBlue$.DeltaBlue, []]}) | |
83 }); | |
84 let const$; | |
85 DeltaBlue$.Strength = class Strength extends core.Object { | |
86 new(value, name) { | |
87 this.value = value; | |
88 this.name = name; | |
89 } | |
90 nextWeaker() { | |
91 return (const$ || (const$ = dart.const(dart.list([DeltaBlue$.STRONG_PREFER
RED, DeltaBlue$.PREFERRED, DeltaBlue$.STRONG_DEFAULT, DeltaBlue$.NORMAL, DeltaBl
ue$.WEAK_DEFAULT, DeltaBlue$.WEAKEST], DeltaBlue$.Strength))))[dartx.get](this.v
alue); | |
92 } | |
93 static stronger(s1, s2) { | |
94 return dart.notNull(s1.value) < dart.notNull(s2.value); | |
95 } | |
96 static weaker(s1, s2) { | |
97 return dart.notNull(s1.value) > dart.notNull(s2.value); | |
98 } | |
99 static weakest(s1, s2) { | |
100 return dart.notNull(DeltaBlue$.Strength.weaker(s1, s2)) ? s1 : s2; | |
101 } | |
102 static strongest(s1, s2) { | |
103 return dart.notNull(DeltaBlue$.Strength.stronger(s1, s2)) ? s1 : s2; | |
104 } | |
105 }; | |
106 dart.setSignature(DeltaBlue$.Strength, { | |
107 constructors: () => ({new: [DeltaBlue$.Strength, [core.int, core.String]]}), | |
108 methods: () => ({nextWeaker: [DeltaBlue$.Strength, []]}), | |
109 statics: () => ({ | |
110 stronger: [core.bool, [DeltaBlue$.Strength, DeltaBlue$.Strength]], | |
111 weaker: [core.bool, [DeltaBlue$.Strength, DeltaBlue$.Strength]], | |
112 weakest: [DeltaBlue$.Strength, [DeltaBlue$.Strength, DeltaBlue$.Strength]]
, | |
113 strongest: [DeltaBlue$.Strength, [DeltaBlue$.Strength, DeltaBlue$.Strength
]] | |
114 }), | |
115 names: ['stronger', 'weaker', 'weakest', 'strongest'] | |
116 }); | |
117 DeltaBlue$.REQUIRED = dart.const(new DeltaBlue$.Strength(0, "required")); | |
118 DeltaBlue$.STRONG_PREFERRED = dart.const(new DeltaBlue$.Strength(1, "strongPre
ferred")); | |
119 DeltaBlue$.PREFERRED = dart.const(new DeltaBlue$.Strength(2, "preferred")); | |
120 DeltaBlue$.STRONG_DEFAULT = dart.const(new DeltaBlue$.Strength(3, "strongDefau
lt")); | |
121 DeltaBlue$.NORMAL = dart.const(new DeltaBlue$.Strength(4, "normal")); | |
122 DeltaBlue$.WEAK_DEFAULT = dart.const(new DeltaBlue$.Strength(5, "weakDefault")
); | |
123 DeltaBlue$.WEAKEST = dart.const(new DeltaBlue$.Strength(6, "weakest")); | |
124 DeltaBlue$.Constraint = class Constraint extends core.Object { | |
125 new(strength) { | |
126 this.strength = strength; | |
127 } | |
128 addConstraint() { | |
129 this.addToGraph(); | |
130 DeltaBlue$.planner.incrementalAdd(this); | |
131 } | |
132 satisfy(mark) { | |
133 this.chooseMethod(dart.as(mark, core.int)); | |
134 if (!dart.notNull(this.isSatisfied())) { | |
135 if (dart.equals(this.strength, DeltaBlue$.REQUIRED)) { | |
136 core.print("Could not satisfy a required constraint!"); | |
137 } | |
138 return null; | |
139 } | |
140 this.markInputs(dart.as(mark, core.int)); | |
141 let out = this.output(); | |
142 let overridden = out.determinedBy; | |
143 if (overridden != null) overridden.markUnsatisfied(); | |
144 out.determinedBy = this; | |
145 if (!dart.notNull(DeltaBlue$.planner.addPropagate(this, dart.as(mark, core
.int)))) core.print("Cycle encountered"); | |
146 out.mark = dart.as(mark, core.int); | |
147 return overridden; | |
148 } | |
149 destroyConstraint() { | |
150 if (dart.notNull(this.isSatisfied())) DeltaBlue$.planner.incrementalRemove
(this); | |
151 this.removeFromGraph(); | |
152 } | |
153 isInput() { | |
154 return false; | |
155 } | |
156 }; | |
157 dart.setSignature(DeltaBlue$.Constraint, { | |
158 constructors: () => ({new: [DeltaBlue$.Constraint, [DeltaBlue$.Strength]]}), | |
159 methods: () => ({ | |
160 addConstraint: [dart.void, []], | |
161 satisfy: [DeltaBlue$.Constraint, [dart.dynamic]], | |
162 destroyConstraint: [dart.void, []], | |
163 isInput: [core.bool, []] | |
164 }) | |
165 }); | |
166 DeltaBlue$.UnaryConstraint = class UnaryConstraint extends DeltaBlue$.Constrai
nt { | |
167 new(myOutput, strength) { | |
168 this.myOutput = myOutput; | |
169 this.satisfied = false; | |
170 super.new(strength); | |
171 this.addConstraint(); | |
172 } | |
173 addToGraph() { | |
174 this.myOutput.addConstraint(this); | |
175 this.satisfied = false; | |
176 } | |
177 chooseMethod(mark) { | |
178 this.satisfied = this.myOutput.mark != mark && dart.notNull(DeltaBlue$.Str
ength.stronger(this.strength, this.myOutput.walkStrength)); | |
179 } | |
180 isSatisfied() { | |
181 return this.satisfied; | |
182 } | |
183 markInputs(mark) {} | |
184 output() { | |
185 return this.myOutput; | |
186 } | |
187 recalculate() { | |
188 this.myOutput.walkStrength = this.strength; | |
189 this.myOutput.stay = !dart.notNull(this.isInput()); | |
190 if (dart.notNull(this.myOutput.stay)) this.execute(); | |
191 } | |
192 markUnsatisfied() { | |
193 this.satisfied = false; | |
194 } | |
195 inputsKnown(mark) { | |
196 return true; | |
197 } | |
198 removeFromGraph() { | |
199 if (this.myOutput != null) this.myOutput.removeConstraint(this); | |
200 this.satisfied = false; | |
201 } | |
202 }; | |
203 dart.setSignature(DeltaBlue$.UnaryConstraint, { | |
204 constructors: () => ({new: [DeltaBlue$.UnaryConstraint, [DeltaBlue$.Variable
, DeltaBlue$.Strength]]}), | |
205 methods: () => ({ | |
206 addToGraph: [dart.void, []], | |
207 chooseMethod: [dart.void, [core.int]], | |
208 isSatisfied: [core.bool, []], | |
209 markInputs: [dart.void, [core.int]], | |
210 output: [DeltaBlue$.Variable, []], | |
211 recalculate: [dart.void, []], | |
212 markUnsatisfied: [dart.void, []], | |
213 inputsKnown: [core.bool, [core.int]], | |
214 removeFromGraph: [dart.void, []] | |
215 }) | |
216 }); | |
217 DeltaBlue$.StayConstraint = class StayConstraint extends DeltaBlue$.UnaryConst
raint { | |
218 new(v, str) { | |
219 super.new(v, str); | |
220 } | |
221 execute() {} | |
222 }; | |
223 dart.setSignature(DeltaBlue$.StayConstraint, { | |
224 constructors: () => ({new: [DeltaBlue$.StayConstraint, [DeltaBlue$.Variable,
DeltaBlue$.Strength]]}), | |
225 methods: () => ({execute: [dart.void, []]}) | |
226 }); | |
227 DeltaBlue$.EditConstraint = class EditConstraint extends DeltaBlue$.UnaryConst
raint { | |
228 new(v, str) { | |
229 super.new(v, str); | |
230 } | |
231 isInput() { | |
232 return true; | |
233 } | |
234 execute() {} | |
235 }; | |
236 dart.setSignature(DeltaBlue$.EditConstraint, { | |
237 constructors: () => ({new: [DeltaBlue$.EditConstraint, [DeltaBlue$.Variable,
DeltaBlue$.Strength]]}), | |
238 methods: () => ({execute: [dart.void, []]}) | |
239 }); | |
240 DeltaBlue$.NONE = 1; | |
241 DeltaBlue$.FORWARD = 2; | |
242 DeltaBlue$.BACKWARD = 0; | |
243 DeltaBlue$.BinaryConstraint = class BinaryConstraint extends DeltaBlue$.Constr
aint { | |
244 new(v1, v2, strength) { | |
245 this.v1 = v1; | |
246 this.v2 = v2; | |
247 this.direction = DeltaBlue$.NONE; | |
248 super.new(strength); | |
249 this.addConstraint(); | |
250 } | |
251 chooseMethod(mark) { | |
252 if (this.v1.mark == mark) { | |
253 this.direction = this.v2.mark != mark && dart.notNull(DeltaBlue$.Strengt
h.stronger(this.strength, this.v2.walkStrength)) ? DeltaBlue$.FORWARD : DeltaBlu
e$.NONE; | |
254 } | |
255 if (this.v2.mark == mark) { | |
256 this.direction = this.v1.mark != mark && dart.notNull(DeltaBlue$.Strengt
h.stronger(this.strength, this.v1.walkStrength)) ? DeltaBlue$.BACKWARD : DeltaBl
ue$.NONE; | |
257 } | |
258 if (dart.notNull(DeltaBlue$.Strength.weaker(this.v1.walkStrength, this.v2.
walkStrength))) { | |
259 this.direction = dart.notNull(DeltaBlue$.Strength.stronger(this.strength
, this.v1.walkStrength)) ? DeltaBlue$.BACKWARD : DeltaBlue$.NONE; | |
260 } else { | |
261 this.direction = dart.notNull(DeltaBlue$.Strength.stronger(this.strength
, this.v2.walkStrength)) ? DeltaBlue$.FORWARD : DeltaBlue$.BACKWARD; | |
262 } | |
263 } | |
264 addToGraph() { | |
265 this.v1.addConstraint(this); | |
266 this.v2.addConstraint(this); | |
267 this.direction = DeltaBlue$.NONE; | |
268 } | |
269 isSatisfied() { | |
270 return this.direction != DeltaBlue$.NONE; | |
271 } | |
272 markInputs(mark) { | |
273 this.input().mark = mark; | |
274 } | |
275 input() { | |
276 return this.direction == DeltaBlue$.FORWARD ? this.v1 : this.v2; | |
277 } | |
278 output() { | |
279 return this.direction == DeltaBlue$.FORWARD ? this.v2 : this.v1; | |
280 } | |
281 recalculate() { | |
282 let ihn = this.input(), out = this.output(); | |
283 out.walkStrength = DeltaBlue$.Strength.weakest(this.strength, ihn.walkStre
ngth); | |
284 out.stay = ihn.stay; | |
285 if (dart.notNull(out.stay)) this.execute(); | |
286 } | |
287 markUnsatisfied() { | |
288 this.direction = DeltaBlue$.NONE; | |
289 } | |
290 inputsKnown(mark) { | |
291 let i = this.input(); | |
292 return i.mark == mark || dart.notNull(i.stay) || i.determinedBy == null; | |
293 } | |
294 removeFromGraph() { | |
295 if (this.v1 != null) this.v1.removeConstraint(this); | |
296 if (this.v2 != null) this.v2.removeConstraint(this); | |
297 this.direction = DeltaBlue$.NONE; | |
298 } | |
299 }; | |
300 dart.setSignature(DeltaBlue$.BinaryConstraint, { | |
301 constructors: () => ({new: [DeltaBlue$.BinaryConstraint, [DeltaBlue$.Variabl
e, DeltaBlue$.Variable, DeltaBlue$.Strength]]}), | |
302 methods: () => ({ | |
303 chooseMethod: [dart.void, [core.int]], | |
304 addToGraph: [dart.void, []], | |
305 isSatisfied: [core.bool, []], | |
306 markInputs: [dart.void, [core.int]], | |
307 input: [DeltaBlue$.Variable, []], | |
308 output: [DeltaBlue$.Variable, []], | |
309 recalculate: [dart.void, []], | |
310 markUnsatisfied: [dart.void, []], | |
311 inputsKnown: [core.bool, [core.int]], | |
312 removeFromGraph: [dart.void, []] | |
313 }) | |
314 }); | |
315 DeltaBlue$.ScaleConstraint = class ScaleConstraint extends DeltaBlue$.BinaryCo
nstraint { | |
316 new(src, scale, offset, dest, strength) { | |
317 this.scale = scale; | |
318 this.offset = offset; | |
319 super.new(src, dest, strength); | |
320 } | |
321 addToGraph() { | |
322 super.addToGraph(); | |
323 this.scale.addConstraint(this); | |
324 this.offset.addConstraint(this); | |
325 } | |
326 removeFromGraph() { | |
327 super.removeFromGraph(); | |
328 if (this.scale != null) this.scale.removeConstraint(this); | |
329 if (this.offset != null) this.offset.removeConstraint(this); | |
330 } | |
331 markInputs(mark) { | |
332 super.markInputs(mark); | |
333 this.scale.mark = this.offset.mark = mark; | |
334 } | |
335 execute() { | |
336 if (this.direction == DeltaBlue$.FORWARD) { | |
337 this.v2.value = dart.notNull(this.v1.value) * dart.notNull(this.scale.va
lue) + dart.notNull(this.offset.value); | |
338 } else { | |
339 this.v1.value = ((dart.notNull(this.v2.value) - dart.notNull(this.offset
.value)) / dart.notNull(this.scale.value))[dartx.truncate](); | |
340 } | |
341 } | |
342 recalculate() { | |
343 let ihn = this.input(), out = this.output(); | |
344 out.walkStrength = DeltaBlue$.Strength.weakest(this.strength, ihn.walkStre
ngth); | |
345 out.stay = dart.notNull(ihn.stay) && dart.notNull(this.scale.stay) && dart
.notNull(this.offset.stay); | |
346 if (dart.notNull(out.stay)) this.execute(); | |
347 } | |
348 }; | |
349 dart.setSignature(DeltaBlue$.ScaleConstraint, { | |
350 constructors: () => ({new: [DeltaBlue$.ScaleConstraint, [DeltaBlue$.Variable
, DeltaBlue$.Variable, DeltaBlue$.Variable, DeltaBlue$.Variable, DeltaBlue$.Stre
ngth]]}), | |
351 methods: () => ({execute: [dart.void, []]}) | |
352 }); | |
353 DeltaBlue$.EqualityConstraint = class EqualityConstraint extends DeltaBlue$.Bi
naryConstraint { | |
354 new(v1, v2, strength) { | |
355 super.new(v1, v2, strength); | |
356 } | |
357 execute() { | |
358 this.output().value = this.input().value; | |
359 } | |
360 }; | |
361 dart.setSignature(DeltaBlue$.EqualityConstraint, { | |
362 constructors: () => ({new: [DeltaBlue$.EqualityConstraint, [DeltaBlue$.Varia
ble, DeltaBlue$.Variable, DeltaBlue$.Strength]]}), | |
363 methods: () => ({execute: [dart.void, []]}) | |
364 }); | |
365 DeltaBlue$.Variable = class Variable extends core.Object { | |
366 new(name, value) { | |
367 this.constraints = dart.list([], DeltaBlue$.Constraint); | |
368 this.name = name; | |
369 this.value = value; | |
370 this.determinedBy = null; | |
371 this.mark = 0; | |
372 this.walkStrength = DeltaBlue$.WEAKEST; | |
373 this.stay = true; | |
374 } | |
375 addConstraint(c) { | |
376 this.constraints[dartx.add](c); | |
377 } | |
378 removeConstraint(c) { | |
379 this.constraints[dartx.remove](c); | |
380 if (dart.equals(this.determinedBy, c)) this.determinedBy = null; | |
381 } | |
382 }; | |
383 dart.setSignature(DeltaBlue$.Variable, { | |
384 constructors: () => ({new: [DeltaBlue$.Variable, [core.String, core.int]]}), | |
385 methods: () => ({ | |
386 addConstraint: [dart.void, [DeltaBlue$.Constraint]], | |
387 removeConstraint: [dart.void, [DeltaBlue$.Constraint]] | |
388 }) | |
389 }); | |
390 DeltaBlue$.Planner = class Planner extends core.Object { | |
391 new() { | |
392 this.currentMark = 0; | |
393 } | |
394 incrementalAdd(c) { | |
395 let mark = this.newMark(); | |
396 for (let overridden = c.satisfy(mark); overridden != null; overridden = ov
erridden.satisfy(mark)) | |
397 ; | |
398 } | |
399 incrementalRemove(c) { | |
400 let out = c.output(); | |
401 c.markUnsatisfied(); | |
402 c.removeFromGraph(); | |
403 let unsatisfied = this.removePropagateFrom(out); | |
404 let strength = DeltaBlue$.REQUIRED; | |
405 do { | |
406 for (let i = 0; i < dart.notNull(unsatisfied[dartx.length]); i++) { | |
407 let u = unsatisfied[dartx.get](i); | |
408 if (dart.equals(u.strength, strength)) this.incrementalAdd(u); | |
409 } | |
410 strength = strength.nextWeaker(); | |
411 } while (!dart.equals(strength, DeltaBlue$.WEAKEST)); | |
412 } | |
413 newMark() { | |
414 return this.currentMark = dart.notNull(this.currentMark) + 1; | |
415 } | |
416 makePlan(sources) { | |
417 let mark = this.newMark(); | |
418 let plan = new DeltaBlue$.Plan(); | |
419 let todo = sources; | |
420 while (dart.notNull(todo[dartx.length]) > 0) { | |
421 let c = todo[dartx.removeLast](); | |
422 if (c.output().mark != mark && dart.notNull(c.inputsKnown(mark))) { | |
423 plan.addConstraint(c); | |
424 c.output().mark = mark; | |
425 this.addConstraintsConsumingTo(c.output(), todo); | |
426 } | |
427 } | |
428 return plan; | |
429 } | |
430 extractPlanFromConstraints(constraints) { | |
431 let sources = dart.list([], DeltaBlue$.Constraint); | |
432 for (let i = 0; i < dart.notNull(constraints[dartx.length]); i++) { | |
433 let c = constraints[dartx.get](i); | |
434 if (dart.notNull(c.isInput()) && dart.notNull(c.isSatisfied())) sources[
dartx.add](c); | |
435 } | |
436 return this.makePlan(sources); | |
437 } | |
438 addPropagate(c, mark) { | |
439 let todo = dart.list([c], DeltaBlue$.Constraint); | |
440 while (dart.notNull(todo[dartx.length]) > 0) { | |
441 let d = todo[dartx.removeLast](); | |
442 if (d.output().mark == mark) { | |
443 this.incrementalRemove(c); | |
444 return false; | |
445 } | |
446 d.recalculate(); | |
447 this.addConstraintsConsumingTo(d.output(), todo); | |
448 } | |
449 return true; | |
450 } | |
451 removePropagateFrom(out) { | |
452 out.determinedBy = null; | |
453 out.walkStrength = DeltaBlue$.WEAKEST; | |
454 out.stay = true; | |
455 let unsatisfied = dart.list([], DeltaBlue$.Constraint); | |
456 let todo = dart.list([out], DeltaBlue$.Variable); | |
457 while (dart.notNull(todo[dartx.length]) > 0) { | |
458 let v = todo[dartx.removeLast](); | |
459 for (let i = 0; i < dart.notNull(v.constraints[dartx.length]); i++) { | |
460 let c = v.constraints[dartx.get](i); | |
461 if (!dart.notNull(c.isSatisfied())) unsatisfied[dartx.add](c); | |
462 } | |
463 let determining = v.determinedBy; | |
464 for (let i = 0; i < dart.notNull(v.constraints[dartx.length]); i++) { | |
465 let next = v.constraints[dartx.get](i); | |
466 if (!dart.equals(next, determining) && dart.notNull(next.isSatisfied()
)) { | |
467 next.recalculate(); | |
468 todo[dartx.add](next.output()); | |
469 } | |
470 } | |
471 } | |
472 return unsatisfied; | |
473 } | |
474 addConstraintsConsumingTo(v, coll) { | |
475 let determining = v.determinedBy; | |
476 for (let i = 0; i < dart.notNull(v.constraints[dartx.length]); i++) { | |
477 let c = v.constraints[dartx.get](i); | |
478 if (!dart.equals(c, determining) && dart.notNull(c.isSatisfied())) coll[
dartx.add](c); | |
479 } | |
480 } | |
481 }; | |
482 dart.setSignature(DeltaBlue$.Planner, { | |
483 methods: () => ({ | |
484 incrementalAdd: [dart.void, [DeltaBlue$.Constraint]], | |
485 incrementalRemove: [dart.void, [DeltaBlue$.Constraint]], | |
486 newMark: [core.int, []], | |
487 makePlan: [DeltaBlue$.Plan, [core.List$(DeltaBlue$.Constraint)]], | |
488 extractPlanFromConstraints: [DeltaBlue$.Plan, [core.List$(DeltaBlue$.Const
raint)]], | |
489 addPropagate: [core.bool, [DeltaBlue$.Constraint, core.int]], | |
490 removePropagateFrom: [core.List$(DeltaBlue$.Constraint), [DeltaBlue$.Varia
ble]], | |
491 addConstraintsConsumingTo: [dart.void, [DeltaBlue$.Variable, core.List$(De
ltaBlue$.Constraint)]] | |
492 }) | |
493 }); | |
494 DeltaBlue$.Plan = class Plan extends core.Object { | |
495 new() { | |
496 this.list = dart.list([], DeltaBlue$.Constraint); | |
497 } | |
498 addConstraint(c) { | |
499 this.list[dartx.add](c); | |
500 } | |
501 size() { | |
502 return this.list[dartx.length]; | |
503 } | |
504 execute() { | |
505 for (let i = 0; i < dart.notNull(this.list[dartx.length]); i++) { | |
506 this.list[dartx.get](i).execute(); | |
507 } | |
508 } | |
509 }; | |
510 dart.setSignature(DeltaBlue$.Plan, { | |
511 methods: () => ({ | |
512 addConstraint: [dart.void, [DeltaBlue$.Constraint]], | |
513 size: [core.int, []], | |
514 execute: [dart.void, []] | |
515 }) | |
516 }); | |
517 DeltaBlue$.chainTest = function(n) { | |
518 DeltaBlue$.planner = new DeltaBlue$.Planner(); | |
519 let prev = null, first = null, last = null; | |
520 for (let i = 0; i <= dart.notNull(n); i++) { | |
521 let v = new DeltaBlue$.Variable("v", 0); | |
522 if (prev != null) new DeltaBlue$.EqualityConstraint(prev, v, DeltaBlue$.RE
QUIRED); | |
523 if (i == 0) first = v; | |
524 if (i == n) last = v; | |
525 prev = v; | |
526 } | |
527 new DeltaBlue$.StayConstraint(last, DeltaBlue$.STRONG_DEFAULT); | |
528 let edit = new DeltaBlue$.EditConstraint(first, DeltaBlue$.PREFERRED); | |
529 let plan = DeltaBlue$.planner.extractPlanFromConstraints(dart.list([edit], D
eltaBlue$.Constraint)); | |
530 for (let i = 0; i < 100; i++) { | |
531 first.value = i; | |
532 plan.execute(); | |
533 if (last.value != i) { | |
534 core.print("Chain test failed:"); | |
535 core.print(`Expected last value to be ${i} but it was ${last.value}.`); | |
536 } | |
537 } | |
538 }; | |
539 dart.fn(DeltaBlue$.chainTest, dart.void, [core.int]); | |
540 DeltaBlue$.projectionTest = function(n) { | |
541 DeltaBlue$.planner = new DeltaBlue$.Planner(); | |
542 let scale = new DeltaBlue$.Variable("scale", 10); | |
543 let offset = new DeltaBlue$.Variable("offset", 1000); | |
544 let src = null, dst = null; | |
545 let dests = dart.list([], DeltaBlue$.Variable); | |
546 for (let i = 0; i < dart.notNull(n); i++) { | |
547 src = new DeltaBlue$.Variable("src", i); | |
548 dst = new DeltaBlue$.Variable("dst", i); | |
549 dests[dartx.add](dst); | |
550 new DeltaBlue$.StayConstraint(src, DeltaBlue$.NORMAL); | |
551 new DeltaBlue$.ScaleConstraint(src, scale, offset, dst, DeltaBlue$.REQUIRE
D); | |
552 } | |
553 DeltaBlue$.change(src, 17); | |
554 if (dst.value != 1170) core.print("Projection 1 failed"); | |
555 DeltaBlue$.change(dst, 1050); | |
556 if (src.value != 5) core.print("Projection 2 failed"); | |
557 DeltaBlue$.change(scale, 5); | |
558 for (let i = 0; i < dart.notNull(n) - 1; i++) { | |
559 if (dests[dartx.get](i).value != i * 5 + 1000) core.print("Projection 3 fa
iled"); | |
560 } | |
561 DeltaBlue$.change(offset, 2000); | |
562 for (let i = 0; i < dart.notNull(n) - 1; i++) { | |
563 if (dests[dartx.get](i).value != i * 5 + 2000) core.print("Projection 4 fa
iled"); | |
564 } | |
565 }; | |
566 dart.fn(DeltaBlue$.projectionTest, dart.void, [core.int]); | |
567 DeltaBlue$.change = function(v, newValue) { | |
568 let edit = new DeltaBlue$.EditConstraint(v, DeltaBlue$.PREFERRED); | |
569 let plan = DeltaBlue$.planner.extractPlanFromConstraints(dart.list([edit], D
eltaBlue$.EditConstraint)); | |
570 for (let i = 0; i < 10; i++) { | |
571 v.value = newValue; | |
572 plan.execute(); | |
573 } | |
574 edit.destroyConstraint(); | |
575 }; | |
576 dart.fn(DeltaBlue$.change, dart.void, [DeltaBlue$.Variable, core.int]); | |
577 DeltaBlue$.planner = null; | |
578 BenchmarkBase$.Expect = class Expect extends core.Object { | |
579 static equals(expected, actual) { | |
580 if (!dart.equals(expected, actual)) { | |
581 dart.throw(`Values not equal: ${expected} vs ${actual}`); | |
582 } | |
583 } | |
584 static listEquals(expected, actual) { | |
585 if (expected[dartx.length] != actual[dartx.length]) { | |
586 dart.throw(`Lists have different lengths: ${expected[dartx.length]} vs $
{actual[dartx.length]}`); | |
587 } | |
588 for (let i = 0; i < dart.notNull(actual[dartx.length]); i++) { | |
589 BenchmarkBase$.Expect.equals(expected[dartx.get](i), actual[dartx.get](i
)); | |
590 } | |
591 } | |
592 fail(message) { | |
593 dart.throw(message); | |
594 } | |
595 }; | |
596 dart.setSignature(BenchmarkBase$.Expect, { | |
597 methods: () => ({fail: [dart.dynamic, [dart.dynamic]]}), | |
598 statics: () => ({ | |
599 equals: [dart.void, [dart.dynamic, dart.dynamic]], | |
600 listEquals: [dart.void, [core.List, core.List]] | |
601 }), | |
602 names: ['equals', 'listEquals'] | |
603 }); | |
604 // Exports: | |
605 exports.DeltaBlue = DeltaBlue$; | |
606 exports.BenchmarkBase = BenchmarkBase$; | |
607 }); | |
OLD | NEW |