From 523131123057ba8ad4024c94d13ffa070419e800 Mon Sep 17 00:00:00 2001 From: mesmo Date: Thu, 12 Jul 2018 17:28:24 +0100 Subject: [PATCH] Remove nested function in topological sort --- lib/node/index.d.ts | 2 +- lib/node/index.js | 2 +- lib/node/sort.d.ts | 2 +- lib/node/sort.js | 32 +++++++++++++++++++------------- lib/node/tree.d.ts | 2 +- lib/node/tree.js | 2 +- src/index.ts | 2 +- src/sort.ts | 34 +++++++++++++++++++--------------- src/tree.ts | 2 +- 9 files changed, 45 insertions(+), 35 deletions(-) diff --git a/lib/node/index.d.ts b/lib/node/index.d.ts index cb7e2bb..5060d64 100644 --- a/lib/node/index.d.ts +++ b/lib/node/index.d.ts @@ -5,7 +5,7 @@ * * @copyright (c) 2017-8 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ export { Tree } from "./tree"; export { Sort } from "./sort"; diff --git a/lib/node/index.js b/lib/node/index.js index 13c3076..74841c8 100644 --- a/lib/node/index.js +++ b/lib/node/index.js @@ -6,7 +6,7 @@ * * @copyright (c) 2017-8 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ Object.defineProperty(exports, "__esModule", { value: true }); // pull in the tree algorithms diff --git a/lib/node/sort.d.ts b/lib/node/sort.d.ts index d3236b8..f996aae 100644 --- a/lib/node/sort.d.ts +++ b/lib/node/sort.d.ts @@ -5,7 +5,7 @@ * * @copyright (c) 2018 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ export declare namespace Sort { /** diff --git a/lib/node/sort.js b/lib/node/sort.js index 1ee63e8..37e02f0 100644 --- a/lib/node/sort.js +++ b/lib/node/sort.js @@ -6,9 +6,24 @@ * * @copyright (c) 2018 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ Object.defineProperty(exports, "__esModule", { value: true }); +function visit(item, visited, sorted, dependencies, throwOnCircularDependency) { + if (visited.indexOf(item) === -1) { + visited.push(item); + for (var _i = 0, _a = dependencies(item); _i < _a.length; _i++) { + var dependency = _a[_i]; + visit(dependency, visited, sorted, dependencies, throwOnCircularDependency); + } + sorted.push(item); + } + else { + if (throwOnCircularDependency && sorted.indexOf(item) === -1) { + throw new Error('Topologicalt sort: circular dependency detected'); + } + } +} var Sort; (function (Sort) { /** @@ -22,19 +37,10 @@ var Sort; if (throwOnCircularDependency === void 0) { throwOnCircularDependency = false; } var sorted = new Array(); var visited = new Array(); - function visit(item) { - if (visited.indexOf(item) === -1) { - visited.push(item); - dependencies(item).forEach(visit); - sorted.push(item); - } - else { - if (throwOnCircularDependency && sorted.indexOf(item) === -1) { - throw new Error('Topologicalt sort: circular dependency detected'); - } - } + for (var _i = 0, source_1 = source; _i < source_1.length; _i++) { + var item = source_1[_i]; + visit(item, visited, sorted, dependencies, throwOnCircularDependency); } - source.forEach(visit); return sorted; } Sort.topological = topological; diff --git a/lib/node/tree.d.ts b/lib/node/tree.d.ts index af235ee..85c5573 100644 --- a/lib/node/tree.d.ts +++ b/lib/node/tree.d.ts @@ -5,7 +5,7 @@ * * @copyright (c) 2017 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ export declare namespace Tree { /** diff --git a/lib/node/tree.js b/lib/node/tree.js index ef6fccc..29b8a98 100644 --- a/lib/node/tree.js +++ b/lib/node/tree.js @@ -6,7 +6,7 @@ * * @copyright (c) 2017 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ Object.defineProperty(exports, "__esModule", { value: true }); var Tree; diff --git a/src/index.ts b/src/index.ts index 1defcf2..344f437 100644 --- a/src/index.ts +++ b/src/index.ts @@ -5,7 +5,7 @@ * * @copyright (c) 2017-8 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ // pull in the tree algorithms diff --git a/src/sort.ts b/src/sort.ts index 0f797f2..dbe0080 100644 --- a/src/sort.ts +++ b/src/sort.ts @@ -5,9 +5,25 @@ * * @copyright (c) 2018 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ +function visit(item: T, visited: Array, sorted: Array, dependencies: (item: T) => Array, throwOnCircularDependency: boolean): void { + if (visited.indexOf(item) === -1) { + visited.push(item); + + for (const dependency of dependencies(item)) { + visit(dependency, visited, sorted, dependencies, throwOnCircularDependency); + } + + sorted.push(item); + } else { + if (throwOnCircularDependency && sorted.indexOf(item) === -1) { + throw new Error('Topologicalt sort: circular dependency detected'); + } + } +} + export namespace Sort { /** * Sorts an array of items based on a function that derives dependencies such that each item in the sorted array precedes its dependencies. @@ -20,22 +36,10 @@ export namespace Sort { var sorted = new Array(); var visited = new Array(); - function visit(item: T): void { - if (visited.indexOf(item) === -1) { - visited.push(item); - - dependencies(item).forEach(visit); - - sorted.push(item); - } else { - if(throwOnCircularDependency && sorted.indexOf(item) === -1) { - throw new Error('Topologicalt sort: circular dependency detected'); - } - } + for (const item of source) { + visit(item, visited, sorted, dependencies, throwOnCircularDependency); } - source.forEach(visit); - return sorted; } } \ No newline at end of file diff --git a/src/tree.ts b/src/tree.ts index 5f9ce2a..fbb32c0 100644 --- a/src/tree.ts +++ b/src/tree.ts @@ -5,7 +5,7 @@ * * @copyright (c) 2017 David Mesquita-Morris * - * Licensed under the MIT and GPL v3 licences + * Licensed under the MIT licence */ export namespace Tree {