plebeiangraphlibrary 2.2.0 → 2.2.1

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.
@@ -13,14 +13,13 @@ class Ln {
13
13
  }
14
14
  class tn {
15
15
  /**
16
- *
17
16
  * Construct a graph object (no initializing)
18
17
  *
19
18
  * @param nodes - Map of all the nodes associated with the graph
20
19
  * @param edges - Map of all the edges associated with the graph
21
20
  */
22
21
  constructor(e, n) {
23
- this.nodes = e, this.edges = n;
22
+ this.nodes = e, this.edges = n, this._nextEdgeId = n.size > 0 ? Math.max(...n.keys()) + 1 : 0;
24
23
  }
25
24
  // test function
26
25
  /**
@@ -30,36 +29,34 @@ class tn {
30
29
  const e = "This is a graph with " + this.nodes.size + " nodes and " + this.edges.size + " edges";
31
30
  console.log(e);
32
31
  }
33
- // initialize
34
32
  /**
35
33
  * Initializes the graph and constructs the node adjacency list.
34
+ * Async to avoid blocking the main thread on large graphs.
36
35
  */
37
36
  async initialize() {
38
37
  await this.constructAdjacencyList();
39
38
  }
40
- // new create method
41
39
  /**
42
- *
43
- * This is the official create method to make a graph based on a set of nodes and edges
44
- * It also auto-initializes the graph and sets all the adjacency lists in memory.
40
+ * Official create method to make a graph based on a set of nodes and edges.
41
+ * Auto-initializes the graph and sets all adjacency lists in memory.
45
42
  *
46
43
  * @param nodes - map of nodes
47
44
  * @param edges - map of edges
48
- * @returns
45
+ * @returns initialized graph
49
46
  */
50
47
  static async create(e, n) {
51
48
  const i = new tn(e, n);
52
49
  return await i.initialize(), i;
53
50
  }
54
- // construct the adjacency list representation
55
51
  /**
56
- * Constructs the adjacency associated with the graph
52
+ * Constructs the adjacency list representation for the graph.
53
+ * Async to allow yielding on large graphs and avoid hanging the browser.
57
54
  */
58
55
  async constructAdjacencyList() {
59
56
  this.edges.forEach((e) => {
60
57
  const n = e.start, i = e.end;
61
58
  this.nodes.get(n) && this.nodes.get(n).neighbours.push(i), this.nodes.get(i) && this.nodes.get(i).neighbours.push(n);
62
- });
59
+ }), await Promise.resolve();
63
60
  for (const e of this.nodes.keys()) {
64
61
  const n = this.nodes.get(e).neighbours, i = [...new Set(n)], r = i.indexOf(e);
65
62
  r > -1 && i.splice(r, 1), this.nodes.get(e).neighbours = i;
@@ -82,10 +79,10 @@ class tn {
82
79
  * @param data - data associated with the edge
83
80
  */
84
81
  add_edge(e, n, i) {
85
- const r = new Ln(e, n, i);
86
- this.edges.set(this.edges.size, r);
87
- const s = this.nodes.get(e), a = this.nodes.get(n);
88
- s && s.neighbours.push(n), a && a.neighbours.push(e);
82
+ const r = new Ln(e, n, i), s = this._nextEdgeId++;
83
+ this.edges.set(s, r);
84
+ const a = this.nodes.get(e), o = this.nodes.get(n);
85
+ a && a.neighbours.push(n), o && o.neighbours.push(e);
89
86
  }
90
87
  // get an adjacency list representation of the graph
91
88
  // this only has the indices and not the actual data
@@ -161,12 +158,17 @@ class tn {
161
158
  }
162
159
  /**
163
160
  * Get the position of the nodes in the graph.
161
+ * Nodes without a defined `data.pos` are skipped.
162
+ *
164
163
  * @returns The position map (node ID to Point)
165
164
  */
166
165
  get_position_map() {
166
+ var n, i;
167
167
  const e = /* @__PURE__ */ new Map();
168
- for (const n of this.nodes.keys())
169
- e.set(n, this.nodes.get(n).data.pos);
168
+ for (const r of this.nodes.keys()) {
169
+ const s = (i = (n = this.nodes.get(r)) == null ? void 0 : n.data) == null ? void 0 : i.pos;
170
+ s != null && e.set(r, s);
171
+ }
170
172
  return e;
171
173
  }
172
174
  /**