graph

package
v0.1.0 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Aug 15, 2026 License: MIT Imports: 8 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

Documentation

Overview

Package graph は ID 間の依存グラフを表す。

グラフアルゴリズム(循環検出・トポロジカルソート・幅優先探索)は gonum.org/v1/gonum/graph に委譲し、本パッケージは gonum の int64 ノード ID と 本ツールの文字列 ID("R-1" など)+メタ情報を対応付ける薄い層に徹する。

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Edge

type Edge struct {
	From, To string
	Kind     EdgeKind
}

Edge は辺 1 件。

type EdgeKind

type EdgeKind int

EdgeKind は辺の由来。含有と参照を混ぜると網羅判定と循環検出が狂うため区別する。

const (
	// EdgeReference は本文中の ID 言及による辺(下流が上流に触れている)。
	EdgeReference EdgeKind = iota
	// EdgeContainment は ID 付き見出しの入れ子による辺(親 → 子)。
	EdgeContainment
)

type Graph

type Graph struct {
	// contains filtered or unexported fields
}

Graph は有向グラフ。エッジの向きは「上流 → 下流」(R-1 → FR-2 → IMP-3)。 ゼロ値は使えないので New で生成する。

内部表現は gonum の multi.DirectedGraph。gonum/graph/simple は行列表現の ために gonum/mat(BLAS・LAPACK)へ依存し、バイナリを 1MB 以上増やす。 多重辺は AddEdge が弾くため、多重グラフでも単純グラフとして扱える。

func New

func New() *Graph

New は空のグラフを返す。

func (*Graph) AddEdge

func (g *Graph) AddEdge(from, to string, kind EdgeKind)

AddEdge は from → to のエッジを張る。未知の頂点は情報なしで自動生成する。 自己ループは循環として扱わないため無視する。

同じ組に参照と含有の両方が該当する場合は参照を優先する。 子が親を明示的に参照している場合で、その言及こそが依存の根拠になる。

func (*Graph) AddNode

func (g *Graph) AddNode(n Node)

AddNode は頂点を追加する。既存の ID は(メタ情報を保つため)無視する。

func (*Graph) Cycles

func (g *Graph) Cycles() [][]string

Cycles は循環を検出し、各循環を「始点と終点が同じ ID 列」として返す。

func (*Graph) Edges

func (g *Graph) Edges(kinds ...EdgeKind) []Edge

Edges は辺を安定した順序で返す。kinds を渡すとその種別だけに絞る。

func (*Graph) Has

func (g *Graph) Has(id string) bool

Has は頂点の存在を返す。

func (*Graph) Mermaid

func (g *Graph) Mermaid() string

Mermaid は Mermaid(flowchart LR)形式で出力する。

頂点の識別子には連番を使い、元の ID は表示名に載せる。 Mermaid の識別子に使えない文字を落とすと、日本語の ID が同じ名前へ潰れるため。

func (*Graph) Node

func (g *Graph) Node(id string) *Node

Node は頂点のメタ情報を返す。未登録なら nil。

func (*Graph) Nodes

func (g *Graph) Nodes() []*Node

Nodes は追加順の頂点一覧を返す。

func (*Graph) NodesOfType

func (g *Graph) NodesOfType(typ string) []*Node

NodesOfType は指定した文書タイプの頂点を追加順に返す。

func (*Graph) Reachable

func (g *Graph) Reachable(from string, maxDepth int) map[string]int

Reachable は from から到達できる頂点を距離付きで返す(from 自身は含まない)。 maxDepth が正のときはその距離までに限定する。辺の種別は問わない。

func (*Graph) Reverse

func (g *Graph) Reverse() *Graph

Reverse は全エッジの向きを反転したグラフを返す。 「依存元 → 依存先」と「上流 → 下流」を相互に読み替えるために使う。

func (*Graph) Subgraph

func (g *Graph) Subgraph(ids []string) *Graph

Subgraph は指定した ID だけを残した部分グラフを返す。辺の種別は引き継ぐ。

func (*Graph) Successors

func (g *Graph) Successors(id string, kinds ...EdgeKind) []string

Successors は下流の頂点 ID を辞書順で返す。kinds を渡すとその種別の辺だけを辿る。

type Node

type Node struct {
	ID    string
	Type  string // 文書タイプ(設定の files[].type)
	File  string
	Line  int
	Level int // 定義見出しのレベル(1..6)。0 は自動生成した頂点
	Title string
}

Node は頂点 1 件。ID は文書中の識別子("R-1")で、それ以外は表示用のメタ情報。

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL