github.com/shogo82148/std@v1.22.1-0.20240327122250-4e474527810c/cmd/go/internal/mvs/graph.go (about)

     1  // Copyright 2020 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 mvs
     6  
     7  import (
     8  	"golang.org/x/mod/module"
     9  )
    10  
    11  // Graph implements an incremental version of the MVS algorithm, with the
    12  // requirements pushed by the caller instead of pulled by the MVS traversal.
    13  type Graph struct {
    14  	cmp   func(p, v1, v2 string) int
    15  	roots []module.Version
    16  
    17  	required map[module.Version][]module.Version
    18  
    19  	isRoot   map[module.Version]bool
    20  	selected map[string]string
    21  }
    22  
    23  // NewGraph returns an incremental MVS graph containing only a set of root
    24  // dependencies and using the given max function for version strings.
    25  //
    26  // The caller must ensure that the root slice is not modified while the Graph
    27  // may be in use.
    28  func NewGraph(cmp func(p, v1, v2 string) int, roots []module.Version) *Graph
    29  
    30  // Require adds the information that module m requires all modules in reqs.
    31  // The reqs slice must not be modified after it is passed to Require.
    32  //
    33  // m must be reachable by some existing chain of requirements from g's target,
    34  // and Require must not have been called for it already.
    35  //
    36  // If any of the modules in reqs has the same path as g's target,
    37  // the target must have higher precedence than the version in req.
    38  func (g *Graph) Require(m module.Version, reqs []module.Version)
    39  
    40  // RequiredBy returns the slice of requirements passed to Require for m, if any,
    41  // with its capacity reduced to its length.
    42  // If Require has not been called for m, RequiredBy(m) returns ok=false.
    43  //
    44  // The caller must not modify the returned slice, but may safely append to it
    45  // and may rely on it not to be modified.
    46  func (g *Graph) RequiredBy(m module.Version) (reqs []module.Version, ok bool)
    47  
    48  // Selected returns the selected version of the given module path.
    49  //
    50  // If no version is selected, Selected returns version "none".
    51  func (g *Graph) Selected(path string) (version string)
    52  
    53  // BuildList returns the selected versions of all modules present in the Graph,
    54  // beginning with the selected versions of each module path in the roots of g.
    55  //
    56  // The order of the remaining elements in the list is deterministic
    57  // but arbitrary.
    58  func (g *Graph) BuildList() []module.Version
    59  
    60  // WalkBreadthFirst invokes f once, in breadth-first order, for each module
    61  // version other than "none" that appears in the graph, regardless of whether
    62  // that version is selected.
    63  func (g *Graph) WalkBreadthFirst(f func(m module.Version))
    64  
    65  // FindPath reports a shortest requirement path starting at one of the roots of
    66  // the graph and ending at a module version m for which f(m) returns true, or
    67  // nil if no such path exists.
    68  func (g *Graph) FindPath(f func(module.Version) bool) []module.Version