提交 88191691 编写于 作者: J Jason Park 提交者: Jason

Add randomize module

上级 9871d1c7
const integer = (min = 1, max = 9) => {
return (Math.random() * (max - min + 1) | 0) + min;
};
const array1D = (N, options) => {
return array2D(1, N, options)[0];
};
const array2D = (N = 10, M = 10, options = {}) => {
const { sorted = false, min, max } = options;
const D = [];
for (let i = 0; i < N; i++) {
D.push([]);
for (let j = 0; j < M; j++) {
D[i].push(integer(min, max));
}
if (sorted) D[i].sort((a, b) => a - b);
}
return D;
};
const graph = (N = 5, options = {}) => {
const { directed = true, weighted = false, ratio = .3, min, max } = options;
const G = new Array(N);
for (let i = 0; i < N; i++) G[i] = new Array(N);
for (let i = 0; i < N; i++) {
G[i][i] = 0;
if (directed) {
for (let j = 0; j < N; j++) {
if (i === j) continue;
G[i][j] = Math.random() < ratio ? weighted ? integer(min, max) : 1 : 0;
}
} else {
for (let j = 0; j < i; j++) {
G[i][j] = G[j][i] = Math.random() < ratio ? weighted ? integer(min, max) : 1 : 0;
}
}
}
return G;
};
export default {
integer,
array1D,
array2D,
graph,
};
\ No newline at end of file
......@@ -18,7 +18,7 @@ class Array2DData extends Data {
super.set();
}
notify(x, y, v) {
notify(x, y, v = this.data[x][y].value) {
this.data[x][y].value = v;
this.data[x][y].notified = true;
this.render();
......
......@@ -108,12 +108,12 @@ class Graph {
}
addNode(id, weight, visited, x = 0, y = 0) {
if (this.findNode(id)) throw new Error(`Node '${id}' is already added.`);
if (this.findNode(id)) return;
this.nodes.push({ id, weight, visited, x, y });
}
addEdge(source, target, weight, visited) {
if (this.findEdge(source, target)) throw new Error(`Edge from '${source}' to '${target}' is already added.`);
if (this.findEdge(source, target)) return;
this.edges.push({ source, target, weight, visited });
}
......
export { default as Randomize } from './Randomize';
export { default as Seed } from './Seed';
export { default as tracerManager } from './tracerManager';
\ No newline at end of file
import React from 'react';
import { Seed } from '/core';
import { Randomize, Seed } from '/core';
import * as Tracers from '/core/tracers';
import { Tracer } from '/core/tracers';
import * as Datas from '/core/datas';
......@@ -8,6 +8,7 @@ import { Array1DRenderer, Array2DRenderer, ChartRenderer, GraphRenderer, LogRend
Object.assign(window, Tracers);
Object.assign(window, Datas);
Object.assign(window, { Randomize });
class TracerManager {
constructor() {
......
class Tracer {
constructor(title = this.constructor.name, options = {}) {
if (typeof title === 'object') {
options = title;
title = this.constructor.name;
}
this.key = Tracer.seed.addTracer(this.constructor.name, title, options);
this.register(
'reset',
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册