From 1645757b9e77e28165a4c98d8d5bccc2bff648b2 Mon Sep 17 00:00:00 2001 From: Sebastian Tobie Date: Tue, 28 Mar 2023 23:40:34 +0200 Subject: [PATCH] added an cleanup to remove items --- weightedrandom/weightedrandom.go | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/weightedrandom/weightedrandom.go b/weightedrandom/weightedrandom.go index 81aac63..173c496 100644 --- a/weightedrandom/weightedrandom.go +++ b/weightedrandom/weightedrandom.go @@ -98,3 +98,14 @@ func (wr *WeightRandom[T]) Get() (t T) { } return } + +// Cleanup deletes the items where the the function returns true +func (wr *WeightRandom[T]) Cleanup(f func(T) bool) { + nl := make(wflist[T], 0, wr.data.Len()) + for _, item := range wr.data { + if !f(item.value) { + nl = append(nl, item) + } + } + wr.data = nl +}