Files
go-ringdeque/options.go

46 lines
1.2 KiB
Go

package ringdeque
import "fmt"
// DefaultGrowthFactor is the default multiplier a [Ring] uses to increase its
// capacity. Most implementations use 2.
const DefaultGrowthFactor = 2
// DefaultCapacity is the default starting capacity of a new [Ring]. Sixteen was
// chosen as a reasonable default.
const DefaultCapacity = 16
// A config holds all values that modify the behavior of a [Ring].
type config struct {
capacity uint64
growthFactor uint64
}
// An Option can be used with [New] to modify the behavior of a [Ring].
type Option func(*config)
// GrowthFactor modifies the multiplier used to increase (and decrease) the
// capacity of a [Ring].
// Its value must be greater than 1.
func GrowthFactor(value int) Option {
if value <= 1 {
panic(fmt.Errorf("go-ring: growth factor must be greater than 1, got %d", value))
}
return func(c *config) {
c.growthFactor = uint64(value)
}
}
// Capacity modifies the starting capacity of a [Ring].
// Its value must be non-negative.
func Capacity(value int) Option {
if value < 0 {
panic(fmt.Errorf("go-ring: starting capacity must be non-negative, got %d", value))
}
return func(c *config) {
c.capacity = uint64(value)
}
}