From 6ba2fa63d0c53ab56b240c8921a69e72b2d2f23f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Tue, 9 Mar 2021 20:14:39 +0100 Subject: [PATCH 1/6] :books: docs(sorted): Fix docs generation. --- src/sorted.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/sorted.js b/src/sorted.js index 7e21e7c..d0949e7 100644 --- a/src/sorted.js +++ b/src/sorted.js @@ -7,7 +7,7 @@ import kahn from './kahn.js'; * Sort the vertices topologically breaking ties according to a given function. * * @param {Iterable} edges - The input graph as a list of edges. - * @param {(a: any, b: any) => Number} breakTies - The function to break ties. + * @param {Function} breakTies - The function to break ties. * @returns {Iterable} The vertices sorted in topological order. */ export default function sorted(edges, breakTies = undefined) { From 6a2d2c0f9ab942c52a9be7758a2c25836ce1040b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Tue, 9 Mar 2021 20:16:35 +0100 Subject: [PATCH 2/6] :truck: refactor: Rename kahn to kahn1962. --- src/{kahn.js => kahn1962.js} | 2 +- src/sorted.js | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) rename src/{kahn.js => kahn1962.js} (91%) diff --git a/src/kahn.js b/src/kahn1962.js similarity index 91% rename from src/kahn.js rename to src/kahn1962.js index ad2f507..d389b7b 100644 --- a/src/kahn.js +++ b/src/kahn1962.js @@ -7,7 +7,7 @@ * @param {any} graph * @returns {Iterable} The vertices in topological order. */ -export default function* kahn(queue, graph) { +export default function* kahn1962(queue, graph) { while (true) { const u = queue.pop(); if (u === undefined) break; diff --git a/src/sorted.js b/src/sorted.js index d0949e7..75f97a9 100644 --- a/src/sorted.js +++ b/src/sorted.js @@ -1,7 +1,7 @@ import Heap from '@aureooms/js-pairing-heap'; import {EfficientlyInvertiblePairs as Pairs} from '@aureooms/js-pairs'; -import kahn from './kahn.js'; +import kahn1962 from './kahn1962.js'; /** * Sort the vertices topologically breaking ties according to a given function. @@ -19,5 +19,5 @@ export default function sorted(edges, breakTies = undefined) { for (const [, v] of graph) freeVertices.delete(v); for (const u of freeVertices) queue.push(u); - return kahn(queue, graph); + return kahn1962(queue, graph); } From 6e68d65225074d5395fa7cdfa5ae9a0428d16e93 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Tue, 9 Mar 2021 20:22:24 +0100 Subject: [PATCH 3/6] :recycle: refactor: Hoist postcondition check. --- src/kahn1962.js | 4 ---- src/sorted.js | 11 +++++++++-- 2 files changed, 9 insertions(+), 6 deletions(-) diff --git a/src/kahn1962.js b/src/kahn1962.js index d389b7b..54f33e3 100644 --- a/src/kahn1962.js +++ b/src/kahn1962.js @@ -19,8 +19,4 @@ export default function* kahn1962(queue, graph) { } } } - - if (graph.size > 0) { - throw new Error('kahn: graph has at least one cycle'); - } } diff --git a/src/sorted.js b/src/sorted.js index 75f97a9..202a7a9 100644 --- a/src/sorted.js +++ b/src/sorted.js @@ -9,8 +9,9 @@ import kahn1962 from './kahn1962.js'; * @param {Iterable} edges - The input graph as a list of edges. * @param {Function} breakTies - The function to break ties. * @returns {Iterable} The vertices sorted in topological order. + * @throws {Error} If the input graph contains a cycle. */ -export default function sorted(edges, breakTies = undefined) { +export default function* sorted(edges, breakTies = undefined) { const graph = Pairs.from(edges); const queue = breakTies ? new Heap(breakTies) : []; @@ -19,5 +20,11 @@ export default function sorted(edges, breakTies = undefined) { for (const [, v] of graph) freeVertices.delete(v); for (const u of freeVertices) queue.push(u); - return kahn1962(queue, graph); + yield* kahn1962(queue, graph); + + if (graph.size > 0) { + throw new Error( + '@aureooms/js-topological-sorting#sorted: input graph contains a cycle', + ); + } } From fe252c2a7a1645532ae1badd43b218a55b1a58de Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Tue, 9 Mar 2021 20:23:01 +0100 Subject: [PATCH 4/6] :books: docs(kahn1962): Document parameters. --- src/kahn1962.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/kahn1962.js b/src/kahn1962.js index 54f33e3..978068e 100644 --- a/src/kahn1962.js +++ b/src/kahn1962.js @@ -3,8 +3,8 @@ * * See https://en.wikipedia.org/wiki/Topological_sorting#CITEREFKahn1962 * - * @param {any} queue - * @param {any} graph + * @param {any} queue - Free vertices. + * @param {any} graph - Edges of the graph. * @returns {Iterable} The vertices in topological order. */ export default function* kahn1962(queue, graph) { From 2dd0dfade1c9cdd12607aed6d733c0db39445e34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Tue, 9 Mar 2021 20:23:26 +0100 Subject: [PATCH 5/6] :sparkles: feat(api): Export kahn1962. --- src/index.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/index.js b/src/index.js index 15bcfe5..ff71f27 100644 --- a/src/index.js +++ b/src/index.js @@ -1,3 +1,4 @@ +import kahn1962 from './kahn1962.js'; import sorted from './sorted.js'; -export {sorted}; +export {kahn1962, sorted}; From 5764939718fdfa799aaca73101827820910a664d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Ooms?= Date: Tue, 9 Mar 2021 20:25:33 +0100 Subject: [PATCH 6/6] :hatching_chick: release: Bumping to v0.1.0. --- package.json | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/package.json b/package.json index f944aa8..8fc86ee 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "@aureooms/js-topological-sorting", "description": "Topological sorting for JavaScript", - "version": "0.0.1", + "version": "0.1.0", "license": "AGPL-3.0", "author": "aureooms", "homepage": "https://aureooms.github.io/js-topological-sorting",