theprogrammablemind 7.5.7 → 7.5.8
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +2 -1
- package/src/digraph_internal.js +131 -0
package/package.json
CHANGED
@@ -44,6 +44,7 @@
|
|
44
44
|
"src/config.js",
|
45
45
|
"src/copy.js",
|
46
46
|
"src/digraph.js",
|
47
|
+
"src/digraph_internal.js",
|
47
48
|
"src/generators.js",
|
48
49
|
"src/semantics.js"
|
49
50
|
],
|
@@ -62,6 +63,6 @@
|
|
62
63
|
"json-stable-stringify": "^1.0.1",
|
63
64
|
"node-fetch": "^2.6.1"
|
64
65
|
},
|
65
|
-
"version": "7.5.
|
66
|
+
"version": "7.5.8",
|
66
67
|
"license": "ISC"
|
67
68
|
}
|
@@ -0,0 +1,131 @@
|
|
1
|
+
const toA = (edge) => {
|
2
|
+
if (Array.isArray(edge)) {
|
3
|
+
return edge
|
4
|
+
} else {
|
5
|
+
return [edge.child, edge.parent]
|
6
|
+
}
|
7
|
+
}
|
8
|
+
|
9
|
+
class DigraphInternal {
|
10
|
+
// edges maybe either [child, parent] or { child, parent }
|
11
|
+
constructor (edges = []) {
|
12
|
+
// dont make a copy of edges. this is shared and that breaks stuff. TODO fix this
|
13
|
+
this._edges = edges
|
14
|
+
}
|
15
|
+
|
16
|
+
addEdges(edges) {
|
17
|
+
for (let edge of edges) {
|
18
|
+
this.addEdge(edge)
|
19
|
+
}
|
20
|
+
}
|
21
|
+
|
22
|
+
addEdge(edge) {
|
23
|
+
edge = toA(edge)
|
24
|
+
this._edges.push(edge)
|
25
|
+
}
|
26
|
+
|
27
|
+
get edges() {
|
28
|
+
return this._edges
|
29
|
+
}
|
30
|
+
|
31
|
+
set edges(edges) {
|
32
|
+
this._edges = edges
|
33
|
+
}
|
34
|
+
|
35
|
+
/*
|
36
|
+
set edges(edges) {
|
37
|
+
this._edges = edges.map( toA )
|
38
|
+
}
|
39
|
+
*/
|
40
|
+
|
41
|
+
acdcs (s, from, to) {
|
42
|
+
const todo = [s]
|
43
|
+
const seen = new Set([s])
|
44
|
+
const acdcs = new Set([])
|
45
|
+
while (todo.length > 0) {
|
46
|
+
const n = todo.pop()
|
47
|
+
this._edges.forEach((e) => {
|
48
|
+
e = toA(e)
|
49
|
+
if (e[from] === n) {
|
50
|
+
acdcs.add(e[to])
|
51
|
+
if (!seen.has(e[to])) {
|
52
|
+
todo.push(e[to])
|
53
|
+
seen.add(e[to])
|
54
|
+
}
|
55
|
+
}
|
56
|
+
})
|
57
|
+
}
|
58
|
+
return acdcs
|
59
|
+
}
|
60
|
+
|
61
|
+
isA (low, high) {
|
62
|
+
if (low === high) {
|
63
|
+
return true
|
64
|
+
}
|
65
|
+
return this.ancestors(low).has(high)
|
66
|
+
}
|
67
|
+
|
68
|
+
lessThan (low, high) {
|
69
|
+
if (low === high) {
|
70
|
+
return false
|
71
|
+
}
|
72
|
+
return this.ancestors(low).has(high)
|
73
|
+
}
|
74
|
+
|
75
|
+
descendants (s) {
|
76
|
+
return this.acdcs(s, 1, 0)
|
77
|
+
}
|
78
|
+
|
79
|
+
ancestors (s) {
|
80
|
+
return this.acdcs(s, 0, 1)
|
81
|
+
}
|
82
|
+
|
83
|
+
minima (nodes) {
|
84
|
+
let minima = new Set(nodes)
|
85
|
+
const ancestors = new Set([])
|
86
|
+
nodes.forEach((node) => {
|
87
|
+
this.ancestors(node).forEach((n) => ancestors.add(n))
|
88
|
+
})
|
89
|
+
ancestors.forEach((n) => minima.delete(n))
|
90
|
+
if (minima.size === 0) {
|
91
|
+
// all unrelated
|
92
|
+
minima = new Set(nodes)
|
93
|
+
}
|
94
|
+
return minima
|
95
|
+
}
|
96
|
+
|
97
|
+
maxima (nodes) {
|
98
|
+
const maxima = new Set(nodes)
|
99
|
+
const descendants = new Set([])
|
100
|
+
nodes.forEach((node) => {
|
101
|
+
this.descendants(node).forEach((n) => descendants.add(n))
|
102
|
+
})
|
103
|
+
descendants.forEach((n) => maxima.delete(n))
|
104
|
+
return maxima
|
105
|
+
}
|
106
|
+
|
107
|
+
add (child, parent) {
|
108
|
+
this._edges.push([child, parent])
|
109
|
+
}
|
110
|
+
|
111
|
+
addList (l) {
|
112
|
+
for (let i = 1; i < l.length; ++i) {
|
113
|
+
this._edges.push([l[i - 1], l[i]])
|
114
|
+
}
|
115
|
+
}
|
116
|
+
|
117
|
+
order (todo) {
|
118
|
+
const ordered = []
|
119
|
+
while (todo.length > 0) {
|
120
|
+
const nodes = this.minima(todo)
|
121
|
+
todo = todo.filter((e) => !nodes.has(e))
|
122
|
+
for (const node of nodes) {
|
123
|
+
ordered.push(node)
|
124
|
+
}
|
125
|
+
// ordered = ordered.concat([...nodes])
|
126
|
+
}
|
127
|
+
return ordered
|
128
|
+
}
|
129
|
+
}
|
130
|
+
|
131
|
+
module.exports = DigraphInternal
|