| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
package typerefs |
| 6 |
|
| 7 |
import ( |
| 8 |
"fmt" |
| 9 |
"math/bits" |
| 10 |
"strings" |
| 11 |
"sync" |
| 12 |
|
| 13 |
"golang.org/x/tools/gopls/internal/cache/metadata" |
| 14 |
"golang.org/x/tools/gopls/internal/util/moremaps" |
| 15 |
) |
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
type PackageIndex struct { |
| 20 |
|
| 21 |
|
| 22 |
mu sync.Mutex |
| 23 |
ids []metadata.PackageID |
| 24 |
m map[metadata.PackageID]IndexID |
| 25 |
} |
| 26 |
|
| 27 |
|
| 28 |
|
| 29 |
func NewPackageIndex() *PackageIndex { |
| 30 |
return &PackageIndex{ |
| 31 |
m: make(map[metadata.PackageID]IndexID), |
| 32 |
} |
| 33 |
} |
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
func (index *PackageIndex) IndexID(id metadata.PackageID) IndexID { |
| 38 |
index.mu.Lock() |
| 39 |
defer index.mu.Unlock() |
| 40 |
if i, ok := index.m[id]; ok { |
| 41 |
return i |
| 42 |
} |
| 43 |
i := IndexID(len(index.ids)) |
| 44 |
index.m[id] = i |
| 45 |
index.ids = append(index.ids, id) |
| 46 |
return i |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
func (index *PackageIndex) PackageID(idx IndexID) metadata.PackageID { |
| 53 |
index.mu.Lock() |
| 54 |
defer index.mu.Unlock() |
| 55 |
return index.ids[idx] |
| 56 |
} |
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
type PackageSet struct { |
| 61 |
|
| 62 |
parent *PackageIndex |
| 63 |
sparse map[int]blockType |
| 64 |
} |
| 65 |
|
| 66 |
type blockType = uint |
| 67 |
const blockSize = bits.UintSize |
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
func (index *PackageIndex) NewSet() *PackageSet { |
| 74 |
return &PackageSet{ |
| 75 |
parent: index, |
| 76 |
sparse: make(map[int]blockType), |
| 77 |
} |
| 78 |
} |
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
func (index *PackageIndex) DeclaringPackage(sym Symbol) metadata.PackageID { |
| 83 |
return index.PackageID(sym.Package) |
| 84 |
} |
| 85 |
|
| 86 |
|
| 87 |
func (s *PackageSet) AddPackage(id metadata.PackageID) { |
| 88 |
s.Add(s.parent.IndexID(id)) |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
func (s *PackageSet) Add(idx IndexID) { |
| 95 |
i := int(idx) |
| 96 |
s.sparse[i/blockSize] |= 1 << (i % blockSize) |
| 97 |
} |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
func (s *PackageSet) Union(other *PackageSet) { |
| 105 |
if other == nil { |
| 106 |
return |
| 107 |
} |
| 108 |
if other.parent != s.parent { |
| 109 |
panic("other set is from a different PackageIndex instance") |
| 110 |
} |
| 111 |
for k, v := range other.sparse { |
| 112 |
if v0 := s.sparse[k]; v0 != v { |
| 113 |
s.sparse[k] = v0 | v |
| 114 |
} |
| 115 |
} |
| 116 |
} |
| 117 |
|
| 118 |
|
| 119 |
func (s *PackageSet) Contains(id metadata.PackageID) bool { |
| 120 |
i := int(s.parent.IndexID(id)) |
| 121 |
return s.sparse[i/blockSize]&(1<<(i%blockSize)) != 0 |
| 122 |
} |
| 123 |
|
| 124 |
|
| 125 |
func (s *PackageSet) Elems(f func(IndexID)) { |
| 126 |
for i, v := range moremaps.Sorted(s.sparse) { |
| 127 |
for b := range blockSize { |
| 128 |
if (v & (1 << b)) != 0 { |
| 129 |
f(IndexID(i*blockSize + b)) |
| 130 |
} |
| 131 |
} |
| 132 |
} |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
func (s *PackageSet) String() string { |
| 137 |
var ids []string |
| 138 |
s.Elems(func(id IndexID) { |
| 139 |
ids = append(ids, string(s.parent.PackageID(id))) |
| 140 |
}) |
| 141 |
return fmt.Sprintf("{%s}", strings.Join(ids, ", ")) |
| 142 |
} |