Debian Codegraph @

internal/graph/compact.go

raw symbols SCIP
1 // Copyright 2026 The Go Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 package graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/
6
7 import "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/"
8
9 // A CompactGraph is a Graph with nodes that are compactly numbered from [0,
10 // NumNodes()).
11 //
12 // Compactly numbered graphs are useful for many graph algorithms, and many
13 // graph representations are naturally compact.
14 //
15 // To compact an arbitrary graph, use [Compact].
16 type CompactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/CompactGraph# interface {
17 Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[int]
18 IsCompactscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/CompactGraph#IsCompact.()
19 }
20
21 // A nodePreserving graph is a transformation of another graph that preserves
22 // node IDs.
23 type nodePreservingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/nodePreserving# interface {
24 Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[int]
25 unwrapPreservingNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/nodePreserving#unwrapPreservingNodes.() Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[int]
26 }
27
28 // Compact takes a Graph with arbitrary NodeIDs and returns a compact graph.
29 //
30 // If g implements [CompactGraph], it assumes g is already compact and simply
31 // returns g and an identity mapping.
32 func Compactscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Compact().[NodeID comparable](g Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[NodeID]) (CompactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/CompactGraph#, *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[NodeID]) {
33 // If it's already compact, simply return it.
34 if gc, ok := g.(CompactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/CompactGraph#); ok {
35 // The above assertion ensures NodeID is int, so we know this type
36 // assertion will always succeed.
37 return gc, any(NewIdentityIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIdentityIndex().(gc.NumNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#NumNodes.())).(*Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[NodeID])
38 }
39
40 // If it's a transformation and the underlying graph is compact, we can use
41 // an identity index. Though we still need to build a compactGraph to
42 // satisfy the CompactGraph interface.
43 g2, _ := g.(nodePreservingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/nodePreserving#)
44 for g2 != nil {
45 unwrapped := g2.unwrapPreservingNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/nodePreserving#unwrapPreservingNodes.()
46 if gc, ok := unwrapped.(CompactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/CompactGraph#); ok {
47 index := any(NewIdentityIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIdentityIndex().(gc.NumNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#NumNodes.())).(*Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[NodeID])
48 cg := compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID]{g, index}
49 return &cg, index
50 }
51 g2, _ = unwrapped.(nodePreservingscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/nodePreserving#)
52 }
53
54 // Nope, just build an index.
55 cg := compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID]{g, NewIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIndex().(g.Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#Nodes.())}
56 return &cg, cg.mscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#m.
57 }
58
59 type compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID comparable] struct {
60 gscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#g. Graphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#[NodeID]
61 mscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#m. *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[NodeID]
62 }
63
64 func (g *compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID]) Nodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#Nodes().() iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[int] {
65 return func(yield func(int) bool) {
66 for i := range g.gscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#g..NumNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#NumNodes.() {
67 if !yield(i) {
68 break
69 }
70 }
71 }
72 }
73
74 func (g *compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID]) NumNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#NumNodes().() int {
75 return g.gscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#g..NumNodesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#NumNodes.()
76 }
77
78 func (g *compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID]) Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#Out().(node int) iterscip-go gomod github.com/golang/go/src go1.25.0 iter/.Seqscip-go gomod github.com/golang/go/src go1.25.0 iter/Seq#[int] {
79 id := g.mscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#m..Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(node)
80 return func(yield func(int) bool) {
81 for nid := range g.gscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#g..Outscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Graph#Out.(id) {
82 if !yield(g.mscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#m..Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(nid)) {
83 break
84 }
85 }
86 }
87 }
88
89 func (g *compactGraphscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#[NodeID]) IsCompactscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/compactGraph#IsCompact().() {}