in cpc/pair_table.go [90:140]
func (p *pairTable) maybeDelete(item int) (bool, error) {
lgSizeInts := p.lgSizeInts
sizeInts := 1 << lgSizeInts
mask := sizeInts - 1
shift := p.validBits - lgSizeInts
probe := item >> shift
arr := p.slotsArr
fetched := arr[probe]
for fetched != item && fetched != -1 {
probe = (probe + 1) & mask
fetched = arr[probe]
}
if fetched == -1 {
return false, nil
}
// Remove the target item.
arr[probe] = -1
p.numPairs--
// Drain the cluster into a temporary slice.
var itemsToReinsert []int
probe = (probe + 1) & mask
fetched = arr[probe]
for fetched != -1 {
itemsToReinsert = append(itemsToReinsert, fetched)
// Mark the slot empty and subtract from count.
arr[probe] = -1
p.numPairs--
probe = (probe + 1) & mask
fetched = arr[probe]
}
// (Optional) Log the cluster before reinsertion:
// fmt.Printf("Reinserting cluster: %v, current p.numPairs=%d\n", itemsToReinsert, p.numPairs)
// Now reinsert all the items from the cluster.
for _, it := range itemsToReinsert {
_, err := p.maybeInsert(it)
if err != nil {
return false, err
}
}
// Attempt to shrink if necessary.
for (downsizeDenom*p.numPairs) < (downsizeNumer*(1<<p.lgSizeInts)) && p.lgSizeInts > 2 {
if err := p.rebuild(p.lgSizeInts - 1); err != nil {
return false, err
}
}
return true, nil
}