Class ScyllaCSRGraph.Builder
Fluent builder for constructing ScyllaCSRGraph instances incrementally, without requiring an intermediate mutable graph.
Inherited Members
Namespace: Scylla.Core.Structures
Assembly: ScyllaCore.dll
Syntax
public sealed class ScyllaCSRGraph.Builder
Remarks
Nodes must be registered with AddNode(int, ScyllaGraphNodeFlags) before edges referencing them can be added via AddEdge(int, int, float). Attempting to add an edge whose source or target node has not been added will throw InvalidOperationException.
When Undirected() is configured, each call to AddEdge(int, int, float)
automatically stores both the forward and reverse edge entries so that the
resulting graph correctly exposes neighbors from either endpoint. Self-loop edges
(fromID == toID) are only stored once even in undirected mode.
The builder is not thread-safe. All calls must originate from a single thread.
Calling Build() does not reset the builder; however the builder state should be considered consumed after Build() is called because subsequent mutations would not affect the already-constructed graph.
var graph = ScyllaCSRGraph.CreateBuilder()
.Undirected()
.Weighted()
.AddNode(0)
.AddNode(1)
.AddNode(2)
.AddEdge(0, 1, 1.5f)
.AddEdge(1, 2, 2.0f)
.Build();
Methods
AddEdge(int, int, float)
Adds an edge between two previously registered nodes.
For undirected graphs, a reverse edge entry is also stored automatically
so that neighbor queries from either endpoint return the connection.
Self-loop edges (fromID == toID) are
permitted and stored once even in undirected mode.
Duplicate edges between the same pair of nodes are allowed and stored as-is.
Declaration
public ScyllaCSRGraph.Builder AddEdge(int fromID, int toID, float weight = 1)
Parameters
| Type | Name | Description |
|---|---|---|
| int | fromID | ID of the source node. Must have been previously registered via AddNode(int, ScyllaGraphNodeFlags). |
| int | toID | ID of the target node. Must have been previously registered via AddNode(int, ScyllaGraphNodeFlags). |
| float | weight | Edge weight. Defaults to |
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph.Builder | This builder instance for method chaining. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when |
AddNode(int, ScyllaGraphNodeFlags)
Registers a new node with the specified ID and optional flags.
Declaration
public ScyllaCSRGraph.Builder AddNode(int nodeID, ScyllaGraphNodeFlags flags = ScyllaGraphNodeFlags.Walkable)
Parameters
| Type | Name | Description |
|---|---|---|
| int | nodeID | A unique integer identifier for the node. Node IDs do not need to be contiguous or start at zero, but must be unique within the builder. |
| ScyllaGraphNodeFlags | flags | Bitpacked property flags for this node. Defaults to Walkable. |
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph.Builder | This builder instance for method chaining. |
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown when a node with |
Build()
Compiles all registered nodes and edges into an immutable ScyllaCSRGraph in CSR format.
Declaration
public ScyllaCSRGraph Build()
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph | A new, fully-initialized ScyllaCSRGraph containing all nodes and edges registered with this builder. |
Remarks
During the build process:
- All edge endpoints are validated; any edge referencing an unregistered node throws InvalidOperationException.
- Nodes are sorted by ID on a working copy of the builder list, leaving the builder's own node order unchanged.
- Edges are sorted first by source node index (ascending), then by target node ID (ascending) within each source group.
- Prefix-sum offsets are computed from the sorted edge list to produce the final CSR layout.
-
For undirected graphs, the logical EdgeCount
is calculated as
(storedEdgeCount - selfLoopCount) / 2 + selfLoopCountbecause non-self-loop edges are stored twice.
Exceptions
| Type | Condition |
|---|---|
| InvalidOperationException | Thrown if any edge in the builder references a node ID that was not registered via AddNode(int, ScyllaGraphNodeFlags). Under normal usage this cannot occur because AddEdge(int, int, float) already validates endpoints; this second validation is a defensive guard against direct manipulation of builder state. |
Directed()
Configures the graph as directed, meaning edges have a specific source->target direction and only the direction they were added is stored. This is the default.
Declaration
public ScyllaCSRGraph.Builder Directed()
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph.Builder | This builder instance for method chaining. |
Undirected()
Configures the graph as undirected, meaning each edge added via AddEdge(int, int, float) is automatically stored in both directions so that neighbor queries from either endpoint return the connection. Self-loop edges are only stored once.
Declaration
public ScyllaCSRGraph.Builder Undirected()
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph.Builder | This builder instance for method chaining. |
Unweighted()
Configures the graph as unweighted (the default). All edge weights are
normalized to 1.0f regardless of the value passed to
AddEdge(int, int, float). The Weighted flag
will not be set on the resulting graph.
Declaration
public ScyllaCSRGraph.Builder Unweighted()
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph.Builder | This builder instance for method chaining. |
Weighted()
Configures the graph as weighted, preserving the weight parameter
passed to AddEdge(int, int, float).
Declaration
public ScyllaCSRGraph.Builder Weighted()
Returns
| Type | Description |
|---|---|
| ScyllaCSRGraph.Builder | This builder instance for method chaining. |