1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-10 07:42:27 +08:00

list: add DeleteIf method (#50)

DeleteIf delete all satisfying f(data[i]), returns count of removed elements
This commit is contained in:
donutloop
2022-07-23 12:52:29 +02:00
committed by GitHub
parent 0299c454ab
commit 3d7600a9e4
3 changed files with 70 additions and 1 deletions

View File

@@ -163,6 +163,31 @@ func (l *List[T]) DeleteAt(index int) {
l.data = data
}
// DeleteIf delete all satisfying f(data[i]), returns count of removed elements
func (l *List[T]) DeleteIf(f func(T) bool) int {
data := l.data
size := len(data)
var c int
for index := 0; index < len(data); index++ {
if !f(data[index]) {
continue
}
if index == size-1 {
data = append(data[:index])
} else {
data = append(data[:index], data[index+1:]...)
index--
}
c++
}
if c > 0 {
l.data = data
}
return c
}
// UpdateAt update value of list at index, index shoud between 0 and list size -1
func (l *List[T]) UpdateAt(index int, value T) {
data := l.data