Skip to content

Commit

Permalink
feat(adjacency) hasVertex
Browse files Browse the repository at this point in the history
  • Loading branch information
dearlordylord committed May 7, 2023
1 parent d39337d commit 2fd123d
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 3 deletions.
6 changes: 6 additions & 0 deletions packages/adjacency/src/api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,12 @@ export interface IGraph<T = number> {
* @param to -
*/
hasEdge(from: T, to: T): boolean;
/**
* Returns true if a vertex exists for the given id.
*
* @param id -
*/
hasVertex(id: T): boolean;
/**
* Returns number of edges for given vertex. By default only outgoing edges
* are counted, but can be customized via given {@link DegreeType}. Note: In
Expand Down
4 changes: 4 additions & 0 deletions packages/adjacency/src/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ export class AdjacencyBitMatrix implements IGraph<number> {
return this.mat.at(from, to) !== 0;
}

hasVertex(id: number): boolean {
return this.mat.popCountRow(id) !== 0 || this.mat.popCountColumn(id) !== 0;
}

degree(id: number, type: DegreeType = "out") {
let degree = 0;
if (this.undirected || type !== "in")
Expand Down
4 changes: 4 additions & 0 deletions packages/adjacency/src/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,10 @@ export class AdjacencyList implements IGraph<number> {
return true;
}

hasVertex(id: number) {
return !!this.adjacency[id];
}

addEdge(from: number, to: number) {
const vertex = this.ensureVertexData(from);
this.ensureVertexData(to);
Expand Down
4 changes: 4 additions & 0 deletions packages/adjacency/src/sparse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,10 @@ export class AdjacencyMatrix extends CSR implements IGraph<number> {
return this.at(from, to) !== 0;
}

hasVertex(id: number): boolean {
return this.degree(id, "inout") > 0;
}

numEdges() {
const n = this.data.length;
return this.undirected ? n / 2 : n;
Expand Down
12 changes: 12 additions & 0 deletions packages/adjacency/test/binary.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,4 +68,16 @@ group("adjacency (bitmatrix)", {
"edges"
);
},
hasVertex: () => {
const m = defAdjBitMatrix(6, [
[0, 1],
[5, 4],
] satisfies typeof edges, true);
assert.ok(m.hasVertex(0));
assert.ok(m.hasVertex(1));
assert.ok(!m.hasVertex(2));
assert.ok(!m.hasVertex(3));
assert.ok(m.hasVertex(4));
assert.ok(m.hasVertex(5));
}
});
10 changes: 10 additions & 0 deletions packages/adjacency/test/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,14 @@ group("adjacency (list)", {
);
// console.log(m.toString());
},
hasVertex: () => {
const m = defAdjList([
[1, 2],
[2, 0],
]);
assert.ok(m.hasVertex(0));
assert.ok(m.hasVertex(1));
assert.ok(m.hasVertex(2));
assert.ok(!m.hasVertex(3));
}
});
12 changes: 12 additions & 0 deletions packages/adjacency/test/sparse.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,4 +37,16 @@ group("adjacency (sparse)", {
"edges"
);
},
hasVertex: () => {
const m = defAdjMatrix(6, [
[0, 1],
[5, 4],
] satisfies typeof edges, true);
assert.ok(m.hasVertex(0));
assert.ok(m.hasVertex(5));
assert.ok(m.hasVertex(1));
assert.ok(m.hasVertex(4));
assert.ok(!m.hasVertex(2));
assert.ok(!m.hasVertex(3));
}
});
19 changes: 16 additions & 3 deletions packages/bitfield/test/index.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,18 @@
import { group } from "@thi.ng/testament";
// import * as assert from "assert";
// import * as b from "../src/index.js"
import * as assert from "assert";
import { BitField } from "../src/index.js"

group("bitfield", {});
group("bitfield", {
'setAt (boolean)': () => {
const bf = new BitField(8);
assert.ok(!bf.at(1));
bf.setAt(1, true);
assert.ok(!!bf.at(1));
},
'setAt (number)': () => {
const bf = new BitField(8);
assert.ok(!bf.at(1));
bf.setAt(1, 4);
assert.ok(!!bf.at(1));
}
});

0 comments on commit 2fd123d

Please sign in to comment.