feature: added RemoveIf and RemoveByIndex functions into array list

master
amorozov 2024-06-20 13:33:05 +03:00
parent a472faec29
commit e89429a93c
1 changed files with 24 additions and 3 deletions

View File

@ -15,6 +15,24 @@ func (self *ArrayList[T]) Add(element T) {
self.size++ self.size++
} }
func (self *ArrayList[T]) RemoveIf(filter func(T) bool) {
index := 0
for {
if index >= self.size {
break
}
element := self.content[index]
shouldRemove := filter(element)
if shouldRemove {
self.RemoveByIndex(index)
} else {
index++
}
}
}
func (self *ArrayList[T]) Remove(element T) { func (self *ArrayList[T]) Remove(element T) {
indexFromMove := -1 indexFromMove := -1
for index, value := range self.content { for index, value := range self.content {
@ -25,11 +43,14 @@ func (self *ArrayList[T]) Remove(element T) {
} }
} }
self.RemoveByIndex(indexFromMove)
}
func (self *ArrayList[T]) RemoveByIndex(index int) {
contentSize := len(self.content) contentSize := len(self.content)
if indexFromMove >= 0 { if index >= 0 {
for i := index; i < contentSize; i++ {
for i := indexFromMove; i < contentSize; i++ {
self.content[i-1] = self.content[i] self.content[i-1] = self.content[i]
} }