Skip to content

Commit

Permalink
Remove nested function in topological sort
Browse files Browse the repository at this point in the history
  • Loading branch information
mesmo committed Jul 12, 2018
1 parent 6cfee75 commit 5231311
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 35 deletions.
2 changes: 1 addition & 1 deletion lib/node/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
2 changes: 1 addition & 1 deletion lib/node/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion lib/node/sort.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
32 changes: 19 additions & 13 deletions lib/node/sort.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
/**
Expand All @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion lib/node/tree.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
/**
Expand Down
2 changes: 1 addition & 1 deletion lib/node/tree.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
34 changes: 19 additions & 15 deletions src/sort.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<T>(item: T, visited: Array<T>, sorted: Array<T>, dependencies: (item: T) => Array<T>, 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.
Expand All @@ -20,22 +36,10 @@ export namespace Sort {
var sorted = new Array<T>();
var visited = new Array<T>();

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;
}
}
2 changes: 1 addition & 1 deletion src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 5231311

Please sign in to comment.