diff --git a/src/hybridSimulation.js b/src/hybridSimulation.js index d620e25..f5a5c18 100644 --- a/src/hybridSimulation.js +++ b/src/hybridSimulation.js @@ -9,17 +9,17 @@ export default function (nodes, config) { var hybrid, fullSimulation, - SAMPLE_ITERATIONS = config.hasOwnProperty("iteration") ? config.iteration : 300, - neighbourSize = config.hasOwnProperty("neighbourSize") ? config.neighbourSize : 6, - sampleSize = config.hasOwnProperty("sampleSize") ? config.sampleSize : 3, - distanceRange = config.hasOwnProperty("distanceRange") ? config.distanceRange : 10, - FULL_ITERATIONS = config.hasOwnProperty("fullIterations") ? config.fullIterations : 20, - FullneighbourSize = config.hasOwnProperty("fullNeighbourSize") ? config.fullNeighbourSize : 6, - FullsampleSize = config.hasOwnProperty("fullSampleSize") ? config.fullSampleSize : 3, - FulldistanceRange = config.hasOwnProperty("fullDistanceRange") ? config.fullDistanceRange : 10, - distanceFn = config.hasOwnProperty("distanceFn") ? config.distanceFn : constant(300), - PIVOTS = config.hasOwnProperty("pivots") ? config.pivots : false, - NUMPIVOTS = config.hasOwnProperty("numPivots") ? config.numPivots : 3, + SAMPLE_ITERATIONS = readConf(config, "iteration", 300), + neighbourSize = readConf(config, "neighbourSize", 6), + sampleSize = readConf(config, "sampleSize", 3), + distanceRange = readConf(config, "distanceRange", 10), + FULL_ITERATIONS = readConf(config, "fullIterations", 20), + FullneighbourSize = readConf(config, "fullNeighbourSize", neighbourSize), + FullsampleSize = readConf(config, "fullSampleSize", sampleSize), + FulldistanceRange = readConf(config, "fullDistanceRange", distanceRange), + distanceFn = readConf(config, "distanceFn", constant(300)), + PIVOTS = readConf(config, "pivots", false), + NUMPIVOTS = readConf(config, "numPivots", 3), event = d3.dispatch("sampleTick", "fullTick", "startFull", "end"); var sets = takeSampleFrom(nodes, Math.sqrt(nodes.length)); @@ -150,30 +150,8 @@ export default function (nodes, config) { return fullSimulation.force("neighbourSampling").stress(); } }; - } - -function sampleFromNodes(nodes, size) { - let randElements = [], - max = nodes.length; - - for (var i = 0; i < size; ++i) { - var rand = nodes[Math.floor((Math.random() * max))]; - // If the rand is already in random list or in exclude list - // ignore it and get a new value. - while (randElements.includes(rand)) { - rand = nodes[Math.floor((Math.random() * max))]; - } - randElements.push(rand); - } - - var remainder = nodes.filter(function (node) { - return !randElements.includes(node); - }); - - return { - sample: randElements, - remainder: remainder - }; +function readConf(config, prop, default){ + return config.hasOwnProperty(prop) ? config.prop : default, }