Debian Codegraph @

internal/graph/index.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 (
8 "fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/"
9 "iterscip-go gomod github.com/golang/go/src go1.25.0 iter/"
10 "slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/"
11 )
12
13 // An Index is an immutable, bijective map between [0, N) and an ordered list of keys.
14 type Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key comparable] struct {
15 // There are three Index representations:
16 //
17 // - If identN > 0, an identity map of [0, identN).
18 // - If index == nil, a sorted integer index in values.
19 // - Otherwise, a full index in values and index.
20
21 identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN. int
22 valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#values. []Key
23 indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index. map[Key]int
24 }
25
26 // NewIndex returns an index for the specified list of values.
27 func NewIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIndex().[Key comparable](values 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#[Key]) *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key] {
28 vs := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.Collectscip-go gomod github.com/golang/go/src go1.25.0 slices/Collect().(values)
29 if len(vs) == 0 {
30 return new(Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key])
31 }
32
33 // Fast path: a naturally sorted list needs no index. (Sadly, there's no way
34 // to ask "is Key ordered?")
35 if vi, ok := any(vs).([]int); ok && slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.IsSortedscip-go gomod github.com/golang/go/src go1.25.0 slices/IsSorted().(vi) {
36 return &Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key]{valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#values.: vs}
37 }
38
39 index := make(map[Key]int, len(vs))
40 for i, v := range vs {
41 index[v] = i
42 }
43 return &Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key]{valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#values.: vs, indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index.: index}
44 }
45
46 // NewIdentityIndex returns an index that maps [0, n) to [0, n).
47 func NewIdentityIndexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/NewIdentityIndex().(n int) *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[int] {
48 if n < 0 {
49 panic("n < 0")
50 }
51 // If n == 0, this is actually a "sorted integer index", but it doesn't
52 // matter because everything is out of bounds either way.
53 return &Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[int]{identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN.: n}
54 }
55
56 // Value maps an index to a key.
57 func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key]) Valuescip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Value().(index int) Key {
58 if ix.identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN. > 0 {
59 if index < 0 || index >= ix.identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN. {
60 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("index %d out of range [0, %d)", index, ix.identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN.))
61 }
62 return any(index).(Key)
63 }
64 if index < 0 || index >= len(ix.valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#values.) {
65 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("index %d out of range [0, %d)", index, ix.identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN.))
66 }
67 return ix.valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#values.[index]
68 }
69
70 // Index maps a key to an index.
71 func (ix *Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#[Key]) Indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#Index().(key Key) int {
72 if key, ok := any(key).(int); ok {
73 // Integer-only optimizations.
74 switch {
75 case ix.identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN. > 0:
76 // Identity.
77 if 0 <= key && key < ix.identNscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#identN. {
78 return key
79 }
80 goto oob
81
82 case ix.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index. == nil:
83 // Sorted integers.
84 if i, ok := slicesscip-go gomod github.com/golang/go/src go1.25.0 slices/.BinarySearchscip-go gomod github.com/golang/go/src go1.25.0 slices/BinarySearch().(any(ix.valuesscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#values.).([]int), key); ok {
85 return i
86 }
87 goto oob
88 }
89 }
90 if i, ok := ix.indexscip-go gomod golang.org/x/tools . `golang.org/x/tools/internal/graph`/Index#index.[key]; ok {
91 return i
92 }
93
94 oob:
95 panic(fmtscip-go gomod github.com/golang/go/src go1.25.0 fmt/.Sprintfscip-go gomod github.com/golang/go/src go1.25.0 fmt/Sprintf().("key %v not in index", key))
96 }