Browse Source

list:RemoveVal

0x4a52466c696e74 3 months ago
parent
commit
3b7ff89f14
1 changed files with 16 additions and 1 deletions
  1. 16 1
      containers/concurrent/list.go

+ 16 - 1
containers/concurrent/list.go

@@ -154,11 +154,27 @@ func (s *List) pushFront(v interface{}) *Element {
 	return elem
 }
 
+func (s *List) RemoveVal(val any) (ok bool) {
+	s.m.Lock()
+	elem := s.search(val)
+	if elem != nil {
+		s.removeElem(elem)
+		ok = true
+	}
+	s.m.Unlock()
+	return
+}
+
 func (s *List) Remove(elem *Element) {
 	if elem == nil {
 		return
 	}
 	s.m.Lock()
+	s.removeElem(elem)
+	s.m.Unlock()
+}
+
+func (s *List) removeElem(elem *Element) {
 	if elem == s.first {
 		s.first = elem.next
 	}
@@ -167,7 +183,6 @@ func (s *List) Remove(elem *Element) {
 	}
 	elem.destroy()
 	s.size--
-	s.m.Unlock()
 }
 
 func (s *List) Size() (size int) {