1
0
mirror of https://github.com/duke-git/lancet.git synced 2026-02-13 01:02:28 +08:00

feat: add func Iterator, ForEach, RetainAll and DeleteAll for List structure (#71)

This commit is contained in:
燕归来
2023-02-06 09:46:25 +08:00
committed by GitHub
parent bc3c080ac3
commit 325be0d6a1
2 changed files with 112 additions and 0 deletions

View File

@@ -5,6 +5,7 @@
package datastructure
import (
"github.com/duke-git/lancet/v2/iterator"
"reflect"
)
@@ -323,3 +324,46 @@ func (l *List[T]) SubList(fromIndex, toIndex int) *List[T] {
copy(subList, data)
return NewList(subList)
}
// ForEach performs the given action for each element of the list.
func (l *List[T]) ForEach(consumer func(T)) {
for _, it := range l.data {
consumer(it)
}
}
// RetainAll retains only the elements in this list that are contained in the given list.
func (l *List[T]) RetainAll(list *List[T]) bool {
return l.batchRemove(list, true)
}
// DeleteAll removes from this list all of its elements that are contained in the given list.
func (l *List[T]) DeleteAll(list *List[T]) bool {
return l.batchRemove(list, false)
}
func (l *List[T]) batchRemove(list *List[T], complement bool) bool {
var (
w = 0
data = l.data
size = len(data)
)
for i := 0; i < size; i++ {
if list.Contain(data[i]) == complement {
data[w] = data[i]
w++
}
}
if w != size {
l.data = data[:w]
return true
}
return false
}
// Iterator returns an iterator over the elements in this list in proper sequence.
func (l *List[T]) Iterator() iterator.Iterator[T] {
return iterator.FromSlice(l.data)
}