type sparseMap struct {
dense []sparseEntry
- sparse []int
+ sparse []int32
}
// newSparseMap returns a sparseMap that can map
// integers between 0 and n-1 to int32s.
func newSparseMap(n int) *sparseMap {
- return &sparseMap{nil, make([]int, n)}
+ return &sparseMap{dense: nil, sparse: make([]int32, n)}
}
func (s *sparseMap) size() int {
func (s *sparseMap) contains(k ID) bool {
i := s.sparse[k]
- return i < len(s.dense) && s.dense[i].key == k
+ return i < int32(len(s.dense)) && s.dense[i].key == k
}
// get returns the value for key k, or -1 if k does
// not appear in the map.
func (s *sparseMap) get(k ID) int32 {
i := s.sparse[k]
- if i < len(s.dense) && s.dense[i].key == k {
+ if i < int32(len(s.dense)) && s.dense[i].key == k {
return s.dense[i].val
}
return -1
func (s *sparseMap) set(k ID, v int32) {
i := s.sparse[k]
- if i < len(s.dense) && s.dense[i].key == k {
+ if i < int32(len(s.dense)) && s.dense[i].key == k {
s.dense[i].val = v
return
}
s.dense = append(s.dense, sparseEntry{k, v})
- s.sparse[k] = len(s.dense) - 1
+ s.sparse[k] = int32(len(s.dense)) - 1
}
// setBit sets the v'th bit of k's value, where 0 <= v < 32
panic("bit index too large.")
}
i := s.sparse[k]
- if i < len(s.dense) && s.dense[i].key == k {
+ if i < int32(len(s.dense)) && s.dense[i].key == k {
s.dense[i].val |= 1 << v
return
}
s.dense = append(s.dense, sparseEntry{k, 1 << v})
- s.sparse[k] = len(s.dense) - 1
+ s.sparse[k] = int32(len(s.dense)) - 1
}
func (s *sparseMap) remove(k ID) {
i := s.sparse[k]
- if i < len(s.dense) && s.dense[i].key == k {
+ if i < int32(len(s.dense)) && s.dense[i].key == k {
y := s.dense[len(s.dense)-1]
s.dense[i] = y
s.sparse[y.key] = i
type sparseSet struct {
dense []ID
- sparse []int
+ sparse []int32
}
// newSparseSet returns a sparseSet that can represent
// integers between 0 and n-1
func newSparseSet(n int) *sparseSet {
- return &sparseSet{nil, make([]int, n)}
+ return &sparseSet{dense: nil, sparse: make([]int32, n)}
}
func (s *sparseSet) cap() int {
func (s *sparseSet) contains(x ID) bool {
i := s.sparse[x]
- return i < len(s.dense) && s.dense[i] == x
+ return i < int32(len(s.dense)) && s.dense[i] == x
}
func (s *sparseSet) add(x ID) {
i := s.sparse[x]
- if i < len(s.dense) && s.dense[i] == x {
+ if i < int32(len(s.dense)) && s.dense[i] == x {
return
}
s.dense = append(s.dense, x)
- s.sparse[x] = len(s.dense) - 1
+ s.sparse[x] = int32(len(s.dense)) - 1
}
func (s *sparseSet) addAll(a []ID) {
func (s *sparseSet) remove(x ID) {
i := s.sparse[x]
- if i < len(s.dense) && s.dense[i] == x {
+ if i < int32(len(s.dense)) && s.dense[i] == x {
y := s.dense[len(s.dense)-1]
s.dense[i] = y
s.sparse[y] = i