Skip to content

Core 模块接口

模块路径: lib/core/ · 文件: types.mbt, traits.mbt, error.mbt


节点唯一标识符(整数索引包装类型),所有图操作的基础。

pub(all) struct NodeId(Int) derive(Debug, Eq)
方法描述
NodeId(id: Int)用整数创建节点 ID
.0解包获取底层 Int 值

带数据的节点(MVP 阶段固定为 Double,后续可泛型化)。

pub(all) struct Node {
id : NodeId
data : Double
} derive(Debug)

带数据的边。

pub(all) struct Edge {
from : NodeId
to : NodeId
data : Double
} derive(Debug)

所有存储实现的最低要求接口

pub(open) trait GraphReadable {
fn node_count(Self) -> Int
fn edge_count(Self) -> Int
fn contains_node(Self, NodeId) -> Bool
fn contains_edge(Self, NodeId, NodeId) -> Bool
fn get_node(Self, NodeId) -> Double?
fn get_edge(Self, NodeId, NodeId) -> Double?
fn neighbors(Self, NodeId) -> Iter[NodeId]
fn neighbors_with_weight(Self, NodeId) -> Iter[(NodeId, Double)]
fn degree(Self, NodeId) -> Int
fn is_directed(Self) -> Bool
fn is_empty(Self) -> Bool
fn node_ids(Self) -> Iter[NodeId]
fn edges(Self) -> Iter[(NodeId, NodeId, Double)]
}
方法返回说明
node_countInt总节点数
edge_countInt总边数
contains_nodeBool节点是否存在
contains_edgeBool边是否存在
get_nodeDouble?获取节点数据
get_edgeDouble?获取边权重
neighborsIter[NodeId]邻居节点迭代器
neighbors_with_weightIter[(NodeId, Double)]邻居+权重迭代器
degreeInt节点度(或出度)
is_directedBool是否为有向图
is_emptyBool是否为空图
node_idsIter[NodeId]所有节点 ID 迭代器
edgesIter[(NodeId, NodeId, Double)]所有边迭代器

GraphWritable — +5 方法(继承 GraphReadable)

Section titled “GraphWritable — +5 方法(继承 GraphReadable)”

动态修改接口(邻接表、邻接矩阵实现,CSR/CSC 不实现)。

pub(open) trait GraphWritable: GraphReadable {
fn add_node(Self, Double) -> NodeId
fn remove_node(Self, NodeId) -> Bool
fn add_edge(Self, NodeId, NodeId, Double) -> Result[Unit, GraphError]
fn remove_edge(Self, NodeId, NodeId) -> Bool
fn clear(Self) -> Unit
}
方法返回错误
add_nodeNodeId
remove_nodeBoolfalse=节点不存在
add_edgeResult[Unit, GraphError]NodeNotFound / EdgeAlreadyExists
remove_edgeBoolfalse=边不存在
clearUnit

GraphDirected — +6 方法(继承 GraphReadable)

Section titled “GraphDirected — +6 方法(继承 GraphReadable)”

有向图入边/出边查询。

pub(open) trait GraphDirected: GraphReadable {
fn in_neighbors(Self, NodeId) -> Iter[NodeId]
fn out_neighbors(Self, NodeId) -> Iter[NodeId]
fn in_degree(Self, NodeId) -> Int
fn out_degree(Self, NodeId) -> Int
fn predecessors(Self, NodeId) -> Iter[(NodeId, Double)]
fn successors(Self, NodeId) -> Iter[(NodeId, Double)]
}

GraphFull — 便捷别名(继承 GraphWritable + GraphDirected)

Section titled “GraphFull — 便捷别名(继承 GraphWritable + GraphDirected)”
pub(open) trait GraphFull: GraphWritable + GraphDirected {}

GraphBatchReadable — +2 方法(继承 GraphReadable)

Section titled “GraphBatchReadable — +2 方法(继承 GraphReadable)”

CSR/CSC 批量读取优化。

pub(open) trait GraphBatchReadable: GraphReadable {
fn batch_neighbors(Self, Array[NodeId]) -> Array[Array[NodeId]]
fn batch_edges(Self, Array[(NodeId, NodeId)]) -> Array[Double?]
}

pub(all) enum GraphError {
NodeNotFound(NodeId)
EdgeAlreadyExists(NodeId, NodeId)
InvalidNodeId
} derive(Debug, Eq)
变体含义
NodeNotFound(n)节点 n 不存在
EdgeAlreadyExists(u,v)(u,v) 已存在
InvalidNodeId节点 ID 无效(如负数)

GraphReadable (12 方法, 所有存储)
├── GraphWritable (+5, 邻接表/矩阵)
│ └── GraphFull = Writable + Directed (便捷别名)
├── GraphDirected (+6, 有向图)
│ └── GraphFull (同上)
└── GraphBatchReadable (+2, CSR/CSC)

相关文档: