You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
804 B
Go

package bucket
import "fmt"
// Bucket to store volume
type Bucket struct {
// Capacity amount the bucket can store
Capacity uint64
// Volume is the current volume of the bucket
Volume uint64
}
// Fill sets the volume to the capacity to fill bucket
func (b *Bucket) Fill() {
b.Volume = b.Capacity
}
// Empty sets the volume to 0
func (b *Bucket) Empty() {
b.Volume = 0
}
// Pour fills the target bucket to the top
func (b *Bucket) Pour(target *Bucket) {
availableVolume := target.Capacity - target.Volume
if availableVolume > b.Volume {
target.Volume += b.Volume
b.Volume = 0
} else {
b.Volume -= availableVolume
target.Volume += availableVolume
}
}
// String gets string representation
func (b *Bucket) String() string {
return fmt.Sprintf("%dvol/%dcap", b.Volume, b.Capacity)
}