1// Copyright 2022 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 5package ssa 6 7import "cmd/internal/src" 8 9// from https://research.swtch.com/sparse 10// in turn, from Briggs and Torczon 11 12type sparseEntryPos struct { 13 key ID 14 val int32 15 pos src.XPos 16} 17 18type sparseMapPos struct { 19 dense []sparseEntryPos 20 sparse []int32 21} 22 23// newSparseMapPos returns a sparseMapPos that can map 24// integers between 0 and n-1 to the pair <int32,src.XPos>. 25func newSparseMapPos(n int) *sparseMapPos { 26 return &sparseMapPos{dense: nil, sparse: make([]int32, n)} 27} 28 29func (s *sparseMapPos) cap() int { 30 return len(s.sparse) 31} 32 33func (s *sparseMapPos) size() int { 34 return len(s.dense) 35} 36 37func (s *sparseMapPos) contains(k ID) bool { 38 i := s.sparse[k] 39 return i < int32(len(s.dense)) && s.dense[i].key == k 40} 41 42// get returns the value for key k, or -1 if k does 43// not appear in the map. 44func (s *sparseMapPos) get(k ID) int32 { 45 i := s.sparse[k] 46 if i < int32(len(s.dense)) && s.dense[i].key == k { 47 return s.dense[i].val 48 } 49 return -1 50} 51 52func (s *sparseMapPos) set(k ID, v int32, a src.XPos) { 53 i := s.sparse[k] 54 if i < int32(len(s.dense)) && s.dense[i].key == k { 55 s.dense[i].val = v 56 s.dense[i].pos = a 57 return 58 } 59 s.dense = append(s.dense, sparseEntryPos{k, v, a}) 60 s.sparse[k] = int32(len(s.dense)) - 1 61} 62 63func (s *sparseMapPos) remove(k ID) { 64 i := s.sparse[k] 65 if i < int32(len(s.dense)) && s.dense[i].key == k { 66 y := s.dense[len(s.dense)-1] 67 s.dense[i] = y 68 s.sparse[y.key] = i 69 s.dense = s.dense[:len(s.dense)-1] 70 } 71} 72 73func (s *sparseMapPos) clear() { 74 s.dense = s.dense[:0] 75} 76 77func (s *sparseMapPos) contents() []sparseEntryPos { 78 return s.dense 79} 80