提交 2c0d5897 编写于 作者: J Jason Park 提交者: Jason

Add import statement to algorithm codes

上级 841a0e11
module.exports = {
'extends': 'airbnb'
'extends': 'airbnb',
'rules': {
'no-plusplus': 'off',
'no-mixed-operators': 'off',
}
};
\ No newline at end of file
......@@ -9,7 +9,18 @@ for (const category of categories) {
if (algorithm.startsWith('.')) continue;
const filepath = path.resolve(__dirname, '..', 'algorithm', category, algorithm, 'code.js');
const code = fs.readFileSync(filepath, 'utf-8');
const newCode = code.replace(/([a-z])_([a-z])/g, (m, m1, m2) => m1 + m2.toUpperCase());
const tracers = [
'Array1DTracer',
'Array2DTracer',
'ChartTracer',
'GraphTracer',
'LogTracer',
'Randomize',
'Tracer',
];
const used = tracers.filter(tracer => code.includes(tracer));
const importLine = `import { ${used.join(', ')} } from 'algorithm-visualizer';\n\n`;
const newCode = importLine + code;
fs.writeFileSync(filepath, newCode, 'utf-8');
}
}
\ No newline at end of file
import { Data } from '/core/datas';
import { GraphTracer } from '/core/tracers';
import { distance } from '/common/util';
import { tracerManager } from '/core';
......@@ -25,7 +26,7 @@ class GraphData extends Data {
this.logData = null;
}
set(array2d = [], layout = GraphData.LAYOUT.CIRCLE, root = 0) {
set(array2d = [], layout = GraphTracer.LAYOUT.CIRCLE, root = 0) {
const { directed, weighted } = this.options;
const { baseWidth, baseHeight, padding } = this.dimensions;
this.graph = new Graph([], [], directed);
......@@ -53,16 +54,16 @@ class GraphData extends Data {
const height = bottom - top;
const rect = { left, top, right, bottom, width, height };
switch (layout) {
case GraphData.LAYOUT.CIRCLE:
case GraphTracer.LAYOUT.CIRCLE:
this.graph.layoutCircle(rect);
break;
case GraphData.LAYOUT.TREE:
case GraphTracer.LAYOUT.TREE:
this.graph.layoutTree(rect, root);
break;
case GraphData.LAYOUT.RANDOM:
case GraphTracer.LAYOUT.RANDOM:
this.graph.layoutRandom(rect);
break;
case GraphData.LAYOUT.NONE:
case GraphTracer.LAYOUT.NONE:
default:
break;
}
......@@ -209,12 +210,4 @@ class Graph {
}
}
GraphData.LAYOUT = {
CIRCLE: 'circle',
TREE: 'tree',
RANDOM: 'random',
NONE: 'none',
// FORCE_DIRECTED: 'force_directed',
};
export default GraphData;
\ No newline at end of file
......@@ -2,13 +2,17 @@ import React from 'react';
import { Randomize, Seed } from '/core';
import * as Tracers from '/core/tracers';
import { Tracer } from '/core/tracers';
import * as Datas from '/core/datas';
import { Array1DData, Array2DData, ChartData, Data, GraphData, LogData } from '/core/datas';
import { Array1DRenderer, Array2DRenderer, ChartRenderer, GraphRenderer, LogRenderer, Renderer } from '/core/renderers';
Object.assign(window, Tracers);
Object.assign(window, Datas);
Object.assign(window, { Randomize });
Object.assign(window, {
modules: {
'algorithm-visualizer': {
...Tracers,
Randomize,
},
}
});
class TracerManager {
constructor() {
......@@ -162,12 +166,18 @@ class TracerManager {
}
}
sandboxEval(code){
const require = moduleName => window.modules[moduleName]; // fake require
eval(code);
}
execute(callback) {
try {
const lines = this.code.split('\n').map((line, i) => line.replace(/(.+\. *wait *)(\( *\))/g, `$1(${i})`));
const seed = new Seed();
Tracer.seed = seed;
eval(Babel.transform(lines.join('\n'), { presets: ['es2015'] }).code);
const { code } = Babel.transform(lines.join('\n'), { presets: ['es2015'] });
this.sandboxEval(code);
this.reset(seed);
if (callback) callback();
} catch (error) {
......
......@@ -12,4 +12,12 @@ class GraphTracer extends Tracer {
}
}
GraphTracer.LAYOUT = {
CIRCLE: 'circle',
TREE: 'tree',
RANDOM: 'random',
NONE: 'none',
// FORCE_DIRECTED: 'force_directed',
};
export default GraphTracer;
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册