diff options
| author | ewy <ewy0@protonmail.com> | 2026-09-11 15:00:48 +0200 |
|---|---|---|
| committer | ewy <ewy0@protonmail.com> | 2026-09-11 15:00:48 +0200 |
| commit | c69e6c0770359e2febafcf487eb21c4aa608c3e9 (patch) | |
| tree | d1f4ac93b2bca7b4df62d698fde91ed15371c6c7 /engine/pool/pool.go | |
initial commit
Diffstat (limited to 'engine/pool/pool.go')
| -rw-r--r-- | engine/pool/pool.go | 58 |
1 files changed, 58 insertions, 0 deletions
diff --git a/engine/pool/pool.go b/engine/pool/pool.go new file mode 100644 index 0000000..2179a12 --- /dev/null +++ b/engine/pool/pool.go @@ -0,0 +1,58 @@ +package pool + +const Infinite = -1 +const Empty = 0 + +type Pool struct { + OverflowsTo *Pool + SubtractsTo *Pool + Max int + Value int +} + +func (p *Pool) Add(amount int) { + p.Value += amount + if p.Max == Infinite { + return + } + + if p.Value > p.Max { + overflow := p.Value - p.Max + p.Value = p.Max + p.OverflowsTo.Add(overflow) + } +} + +func (p *Pool) Subtract(amount int) { + if p.SubtractsTo != nil { + forward := amount + if forward > p.Value { + forward = p.Value + } + p.SubtractsTo.Add(forward) + } + + if p.Value == Infinite { + return + } + + p.Value -= amount + if p.Value < 0 { + p.Value = 0 + } +} + +func (p *Pool) Fits(amount int) bool { + return p.Value+amount <= p.Max +} + +func (p *Pool) Has(amount int) bool { + return p.Value >= amount +} + +func New(start, max int) *Pool { + return &Pool{ + Max: max, + Value: start, + } +} |
