Files
nats-server/hashmap/rand_evict.go
Colin Sullivan 2baac47820 Address issues found by golint.
* No functional changes
* Did not address the ALL_CAPS issues
* Did not modify public APIs and field names.
2016-03-15 15:21:13 -06:00

44 lines
895 B
Go

// Copyright 2012-2013 Apcera Inc. All rights reserved.
package hashmap
import (
"math/rand"
"time"
)
// We use init to setup the random number generator
func init() {
rand.Seed(time.Now().UnixNano())
}
// RemoveRandom can be used for a random policy eviction.
// This is stochastic but very fast and does not impede
// performance like LRU, LFU or even ARC based implementations.
func (h *HashMap) RemoveRandom() {
if h.used == 0 {
return
}
index := (rand.Int()) & int(h.msk)
// Walk forward til we find an entry
for i := index; i < len(h.bkts); i++ {
e := &h.bkts[i]
if *e != nil {
*e = (*e).next
h.used--
return
}
}
// If we are here we hit end and did not remove anything,
// use the index and walk backwards.
for i := index; i >= 0; i-- {
e := &h.bkts[i]
if *e != nil {
*e = (*e).next
h.used--
return
}
}
panic("Should not reach here..")
}