package fstree

import (
	"math"
	"sync/atomic"
)

// FileCounter used to count files in FSTree. The implementation must be thread-safe.
type FileCounter interface {
	Set(v uint64)
	Inc()
	Dec()
}

type noopCounter struct{}

func (c *noopCounter) Set(uint64) {}
func (c *noopCounter) Inc()       {}
func (c *noopCounter) Dec()       {}

type SimpleCounter struct {
	v atomic.Uint64
}

func NewSimpleCounter() *SimpleCounter {
	return &SimpleCounter{}
}

func (c *SimpleCounter) Set(v uint64)  { c.v.Store(v) }
func (c *SimpleCounter) Inc()          { c.v.Add(1) }
func (c *SimpleCounter) Dec()          { c.v.Add(math.MaxUint64) }
func (c *SimpleCounter) Value() uint64 { return c.v.Load() }