@slithy/prim-lib 0.3.1 → 0.3.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +2 -0
- package/dist/index.js +20 -1
- package/package.json +1 -1
package/dist/index.d.ts
CHANGED
|
@@ -167,6 +167,7 @@ declare class Step {
|
|
|
167
167
|
mutate(): Step;
|
|
168
168
|
}
|
|
169
169
|
|
|
170
|
+
type ShapeCtor = new (w: number, h: number) => ShapeInterface;
|
|
170
171
|
declare class Optimizer {
|
|
171
172
|
cfg: Cfg;
|
|
172
173
|
state: State;
|
|
@@ -174,6 +175,7 @@ declare class Optimizer {
|
|
|
174
175
|
_steps: number;
|
|
175
176
|
_stopped: boolean;
|
|
176
177
|
_paused: boolean;
|
|
178
|
+
_stepPlan: ShapeCtor[];
|
|
177
179
|
constructor(original: Canvas, cfg: Cfg);
|
|
178
180
|
start(): void;
|
|
179
181
|
stop(): void;
|
package/dist/index.js
CHANGED
|
@@ -854,6 +854,22 @@ var Debug = class extends Shape {
|
|
|
854
854
|
};
|
|
855
855
|
|
|
856
856
|
// src/optimizer.ts
|
|
857
|
+
function buildStepPlan(cfg) {
|
|
858
|
+
const { shapeTypes, shapeWeights, steps } = cfg;
|
|
859
|
+
if (!shapeWeights || shapeWeights.length !== shapeTypes.length) return [];
|
|
860
|
+
const total = shapeWeights.reduce((sum, w) => sum + w, 0);
|
|
861
|
+
const floats = shapeWeights.map((w) => w / total * steps);
|
|
862
|
+
const floors = floats.map(Math.floor);
|
|
863
|
+
const remainder = steps - floors.reduce((s, n) => s + n, 0);
|
|
864
|
+
const indices = floats.map((f, i) => ({ i, frac: f - floors[i] })).sort((a, b) => b.frac - a.frac).slice(0, remainder).map((x) => x.i);
|
|
865
|
+
const counts = floors.map((n, i) => indices.includes(i) ? n + 1 : n);
|
|
866
|
+
const plan = shapeTypes.flatMap((ctor, i) => Array(counts[i]).fill(ctor));
|
|
867
|
+
for (let i = plan.length - 1; i > 0; i--) {
|
|
868
|
+
const j = Math.floor(Math.random() * (i + 1));
|
|
869
|
+
[plan[i], plan[j]] = [plan[j], plan[i]];
|
|
870
|
+
}
|
|
871
|
+
return plan;
|
|
872
|
+
}
|
|
857
873
|
var Optimizer = class {
|
|
858
874
|
cfg;
|
|
859
875
|
state;
|
|
@@ -861,6 +877,7 @@ var Optimizer = class {
|
|
|
861
877
|
_steps;
|
|
862
878
|
_stopped;
|
|
863
879
|
_paused;
|
|
880
|
+
_stepPlan;
|
|
864
881
|
constructor(original, cfg) {
|
|
865
882
|
this.cfg = cfg;
|
|
866
883
|
this.state = new State(original, Canvas.empty(cfg));
|
|
@@ -869,6 +886,7 @@ var Optimizer = class {
|
|
|
869
886
|
this._paused = false;
|
|
870
887
|
this.onStep = () => {
|
|
871
888
|
};
|
|
889
|
+
this._stepPlan = buildStepPlan(cfg);
|
|
872
890
|
}
|
|
873
891
|
start() {
|
|
874
892
|
this._stopped = false;
|
|
@@ -909,10 +927,11 @@ var Optimizer = class {
|
|
|
909
927
|
}
|
|
910
928
|
_findBestStep() {
|
|
911
929
|
const LIMIT = this.cfg.shapes;
|
|
930
|
+
const ctor = this._stepPlan[this._steps];
|
|
912
931
|
let bestStep = null;
|
|
913
932
|
const promises = [];
|
|
914
933
|
for (let i = 0; i < LIMIT; i++) {
|
|
915
|
-
const shape = Shape.create(this.cfg);
|
|
934
|
+
const shape = ctor ? new ctor(this.cfg.width, this.cfg.height) : Shape.create(this.cfg);
|
|
916
935
|
const promise = new Step(shape, this.cfg).compute(this.state).then((step) => {
|
|
917
936
|
if (!bestStep || step.distance < bestStep.distance) {
|
|
918
937
|
bestStep = step;
|