Skip to main content

interface Graph.Builder<N>

A mutable Graph builder used to efficiently create new immutable instances. See the Graph documentation and the Graph.Builder API documentation

Extends: GraphBase.Builder<N,Tp>

Type parameters

NameDescription
Nthe node type

Properties

connectionSize

Returns the amount of connections in the graph.

Definition

readonly connectionSize: number;

example
ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
.connectionsSize
// => 2

Overrides

Builder.connectionSize

context

Returns the context associated to this collection instance.

Definition

readonly context: WithGraphValues<Tp, N, unknown>['context'];

Overrides

Builder.context

isEmpty

Returns true if there are no entries in the builder.

Definition

readonly isEmpty: boolean;

example
ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
.isEmpty
// => false

Overrides

Builder.isEmpty

nodeSize

Returns the amount of nodes in the graph.

Definition

readonly nodeSize: number;

example
ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
.nodeSize
// => 3

Overrides

Builder.nodeSize

Methods

addGraphElement

Adds the given element graph element to the builder, where a graph element is either a one-element tuple containing a node, or a two-element tuple containing two nodes indicating a connection.

Definition

addGraphElement(element: GraphElement<N>): boolean;

Parameters

NameTypeDescription
elementGraphElement<N>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addGraphElement([1]) // => false
b.addGraphElement([4]) // => true
b.addGraphElement([2, 3]) // => false
b.addGraphElement([4, 1]) // => true

Overrides

Builder.addGraphElement

addGraphElements

Adds the graph elements in the given elements StreamSource to the graph.

Definition

addGraphElements(elements: StreamSource<GraphElement<N>>): boolean;

Parameters

NameTypeDescription
elementsStreamSource<GraphElement<N>>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addGraphElements([[4], [5]]) // => true
b.addGraphElements([[3, 1], [1]]) // => true
b.addGraphElements([[1, 2], [1]]) // => false

Overrides

Builder.addGraphElements

addNode

Adds the given node to the graph.

Definition

addNode(node: N): boolean;

Parameters

NameTypeDescription
nodeN
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addNode(6) // => true
b.addNode(1) // => false

Overrides

Builder.addNode

addNodes

Adds the given nodes to the builder.

Definition

addNodes(nodes: StreamSource<N>): boolean;

Parameters

NameTypeDescription
nodesStreamSource<N>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.addNodes([3, 4, 5]) // => true
b.addNodes([1, 2]) // => false

Overrides

Builder.addNodes

build

Returns an immutable Graph containing the links in this Builder instance.

Definition

build(): WithGraphValues<Tp, N, unknown>['normal'];

example
const b = ArrowGraphHashed.builder<number, number>()
b.connect(1, 2)
b.addNode(3)
const g = b.build()
console.log(g.toArray())
// => [[1, 2], [3]]

Overrides

Builder.build

connect

Adds a connection between node1 and node2 to the graph.

Definition

connect(node1: N, node2: N): boolean;

Parameters

NameTypeDescription
node1Nthe first connection node
node2N
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.connect(3, 1) // => true
b.connect(1, 2) // => false

Overrides

Builder.connect

connectAll

Adds the connections in given connections StreamSource to the graph.

Definition

connectAll(connections: StreamSource<WithGraphValues<Tp, N, unknown>['link']>): boolean;

Parameters

NameTypeDescription
connectionsStreamSource<WithGraphValues<Tp, N, unknown>['link']>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.connectAll([[1, 2], [3, 1]]) // => true
b.connectAll([[1, 2]]) // => false

Overrides

Builder.connectAll

connectIfNodesExist

Returns true if the graph has changed

Definition

connectIfNodesExist(node1: N, node2: N): boolean;

Parameters

NameTypeDescription
node1N
node2N
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.connectIfNodesExist(3, 1) // => true
b.connectIfNodesExist(3, 4) // => false

Overrides

Builder.connectIfNodesExist

disconnect

Removes the connection between given node1 and node2 if the connection was present.

Definition

disconnect<UN = N>(node1: RelatedTo<N, UN>, node2: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
node1RelatedTo<N, UN>the first connection node
node2RelatedTo<N, UN>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.disconnect(1, 2) // => true
b.disconnect(3, 4) // => false

Overrides

Builder.disconnect

disconnectAll

Removes all connections from the given connections StreamSource from the graph.

Definition

disconnectAll<UN = N>(connections: StreamSource<Link<RelatedTo<N, UN>>>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
connectionsStreamSource<Link<RelatedTo<N, UN>>>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.disconnectAll([[1, 2], [3, 4]]) // => true
b.disconnectAll([[3, 4], [5, 6]]) // => false

Overrides

Builder.disconnectAll

forEach

Performs given function f for each entry of the collection, using given state as initial traversal state.

Definition

forEach(f: (entry: [N] | WithGraphValues<Tp, N, unknown>['link'], index: number, halt: () => void) => void, options?: {
      state?: TraverseState;
    }): void;

Parameters

NameTypeDescription
f(entry: [N] | WithGraphValues<Tp, N, unknown>['link'], index: number, halt: () => void) => voidthe function to perform for each entry, receiving:
- entry: the next graph element
- index: the index of the element
- halt: a function that, if called, ensures that no new elements are passed
options{
      state?: TraverseState;
    }
object containing the following
- state: (optional) the traverse state
example
const b = ArrowGraphHashed.of([1], [2, 3], [4]).toBuilder();
b.forEach((entry, i, halt) => {
console.log([entry]);
if (i >= 1) halt();
})
// => logs [1] [2, 3]
note

O(N)

Overrides

Builder.forEach

hasConnection

Returns true if the graph has a connection between given nodes node1 and node2.

Definition

hasConnection<UN = N>(node1: RelatedTo<N, UN>, node2: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
node1RelatedTo<N, UN>the first connection node
node2RelatedTo<N, UN>the second connection node
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.hasConnection(1, 2) // => true
b.hasConnection(6, 7) // => false

Overrides

Builder.hasConnection

hasNode

Returns true if the graph contains the given node.

Definition

hasNode<UN = N>(node: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
nodeRelatedTo<N, UN>the node to search
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.hasNode(1) // => true
b.hasNode(6) // => false

Overrides

Builder.hasNode

removeNode

Removes the given node, and any of its connections, from the graph.

Definition

removeNode<UN = N>(node: RelatedTo<N, UN>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
nodeRelatedTo<N, UN>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.removeNode(1) // => true
b.removeNode(6) // => false

Overrides

Builder.removeNode

removeNodes

Removes the given nodes, and any of their connections, from the graph.

Definition

removeNodes<UN = N>(nodes: StreamSource<RelatedTo<N, UN>>): boolean;

Type parameters

NameDefaultDescription
UNN

Parameters

NameTypeDescription
nodesStreamSource<RelatedTo<N, UN>>
example
const b = ArrowGraphHashed
.of([[1, 2], [2, 3]])
.toBuilder()
b.removeNodes([1, 6, 7]) // => true
b.removeNodes([6, 7]) // => false

Overrides

Builder.removeNodes