Rename WeightedChooser's method Put to Add, a better name

This commit is contained in:
Deluan 2023-01-13 19:43:27 -05:00
parent 291455f0b7
commit 769e8bedba
3 changed files with 6 additions and 6 deletions

View File

@ -182,7 +182,7 @@ func (e *externalMetadata) SimilarSongs(ctx context.Context, id string, count in
weight := topCount * (4 + artistWeight) weight := topCount * (4 + artistWeight)
for _, mf := range topSongs { for _, mf := range topSongs {
weightedSongs.Put(mf, weight) weightedSongs.Add(mf, weight)
weight -= 4 weight -= 4
} }
return nil return nil

View File

@ -16,7 +16,7 @@ func NewWeightedRandomChooser() *WeightedChooser {
return &WeightedChooser{} return &WeightedChooser{}
} }
func (w *WeightedChooser) Put(value interface{}, weight int) { func (w *WeightedChooser) Add(value interface{}, weight int) {
w.entries = append(w.entries, value) w.entries = append(w.entries, value)
w.weights = append(w.weights, weight) w.weights = append(w.weights, weight)
w.totalWeight += weight w.totalWeight += weight

View File

@ -10,7 +10,7 @@ var _ = Describe("WeightedRandomChooser", func() {
BeforeEach(func() { BeforeEach(func() {
w = NewWeightedRandomChooser() w = NewWeightedRandomChooser()
for i := 0; i < 10; i++ { for i := 0; i < 10; i++ {
w.Put(i, i) w.Add(i, i)
} }
}) })
@ -23,14 +23,14 @@ var _ = Describe("WeightedRandomChooser", func() {
It("returns the sole item", func() { It("returns the sole item", func() {
w = NewWeightedRandomChooser() w = NewWeightedRandomChooser()
w.Put("a", 1) w.Add("a", 1)
Expect(w.GetAndRemove()).To(Equal("a")) Expect(w.GetAndRemove()).To(Equal("a"))
}) })
It("fails when trying to choose from empty set", func() { It("fails when trying to choose from empty set", func() {
w = NewWeightedRandomChooser() w = NewWeightedRandomChooser()
w.Put("a", 1) w.Add("a", 1)
w.Put("b", 1) w.Add("b", 1)
Expect(w.GetAndRemove()).To(BeElementOf("a", "b")) Expect(w.GetAndRemove()).To(BeElementOf("a", "b"))
Expect(w.GetAndRemove()).To(BeElementOf("a", "b")) Expect(w.GetAndRemove()).To(BeElementOf("a", "b"))
_, err := w.GetAndRemove() _, err := w.GetAndRemove()