Documentation
¶
Index ¶
- func CalculateExponentialDecayTo(score, decayTo float64, halfLife time.Duration) time.Duration
- func CalculateExponentialHalfLife(score, decayTo float64, duration time.Duration) time.Duration
- type DecayFunc
- type Scoring
- func (s *Scoring[T]) Add(id T, score float64)
- func (s *Scoring[T]) AddAt(id T, score float64, at time.Time)
- func (s *Scoring[T]) Decrease(id T, score float64)
- func (s *Scoring[T]) DecreaseAt(id T, score float64, at time.Time)
- func (s *Scoring[T]) Increase(id T, score float64)
- func (s *Scoring[T]) IncreaseAt(id T, score float64, at time.Time)
- func (s *Scoring[T]) Map() map[T]float64
- func (s *Scoring[T]) MapAt(at time.Time) map[T]float64
- func (s *Scoring[T]) Ranked(granularity float64) []T
- func (s *Scoring[T]) RankedAt(at time.Time, granularity float64) []T
- func (s *Scoring[T]) Remove(id T)
- func (s *Scoring[T]) Score(id T) (float64, bool)
- func (s *Scoring[T]) ScoreAt(id T, at time.Time) (float64, bool)
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CalculateExponentialDecayTo ¶
CalculateExponentialDecayTo calculates the time it takes for the score to decay to value equal to or less than decayTo. If score and decayTo have different signs, it returns 0. If decayTo is 0, it returns 0. If halfLife is 0 or negative, it returns 0.
func CalculateExponentialHalfLife ¶
CalculateExponentialHalfLife calculates the half-life for an ExponentialDecay function to decay from score to decayTo in duration. If decayTo is 0, it returns 0. If duration is 0 or negative, it returns 0.
Types ¶
type DecayFunc ¶
func ExponentialDecay ¶
ExponentialDecay returns a DecayFunc that decays to zero exponentially over time. Returns nil if halfLife is negative.
func LinearDecay ¶
LinearDecay returns a DecayFunc that decays to zero linearly over time. Returns nil if decayPerSecond is negative.
type Scoring ¶
type Scoring[T comparable] struct { // contains filtered or unexported fields }
Scoring is a simple scoring system that allows to track scores for a set of items.
Scoring is thread-safe.
func New ¶
func New[T comparable](decayFunc DecayFunc, minScore, maxScore float64) *Scoring[T]
New creates a new Scoring instance.
func (*Scoring[T]) Add ¶
Add adds score to the given ID. All consecutive score calculations will be based the time of the call. If the ID already exists, it does nothing.
func (*Scoring[T]) AddAt ¶
AddAt adds score to the given ID at the given time. All consecutive score calculations will be based on the given time. If the ID already exists, it does nothing.
func (*Scoring[T]) Decrease ¶
Decrease decreases the score for the given ID by the given amount. All consecutive score calculations will be based on the time of the call. If the ID does not exist, it does nothing.
func (*Scoring[T]) DecreaseAt ¶
DecreaseAt decreases the score for the given ID by the given amount at the given time. All consecutive score calculations will be based on the given time. If the ID does not exist, it does nothing.
func (*Scoring[T]) Increase ¶
Increase increases the score for the given ID by the given amount. All consecutive score calculations will be based on the time of the call. If the ID does not exist, it does nothing.
func (*Scoring[T]) IncreaseAt ¶
IncreaseAt increases the score for the given ID by the given amount at the given time. All consecutive score calculations will be based on the given time. If the ID does not exist, it does nothing.
func (*Scoring[T]) Ranked ¶
Ranked returns the IDs sorted by their scores in descending order.
All scores are rounded to the nearest multiple of the granularity, and the ranking is based on the rounded scores. This guarantee that items with similar scores will be returned in random order.
func (*Scoring[T]) RankedAt ¶
RankedAt returns the IDs sorted by their scores at the given time in descending order.
All scores are rounded to the nearest multiple of the granularity, and the ranking is based on the rounded scores. This guarantee that items with similar scores will be returned in random order.
func (*Scoring[T]) Remove ¶
func (s *Scoring[T]) Remove(id T)
Remove removes the given ID. If the ID does not exist, it does nothing.