Jelajahi Sumber

Added RemoveIf to List and Stack.

Mark Sibly 9 tahun lalu
induk
melakukan
7d5b2404c2
2 mengubah file dengan 43 tambahan dan 0 penghapusan
  1. 22 0
      modules/std/collections/list.monkey2
  2. 21 0
      modules/std/collections/stack.monkey2

+ 22 - 0
modules/std/collections/list.monkey2

@@ -516,6 +516,28 @@ Class List<T> Implements IContainer<T>
 		Return n
 		Return n
 	End
 	End
 	
 	
+	#rem monkeydoc Removes all values in the list that fulfill a condition.
+	
+	@param cond Condition to test.
+	
+	@return The number of values removed.
+	
+	#end
+	Method RemoveIf:Int( condition:Bool( value:T ) )
+		Local node:=_head._succ,n:=0
+		While node<>_head
+			If condition( node._value )
+				node=node._succ
+				node._pred.Remove()
+				n+=1
+			Else
+				node=node._succ
+			Endif
+		Wend
+		If n _seq+=1
+		Return n
+	End
+	
 	#rem monkeydoc Removes and returns the first value in the list.
 	#rem monkeydoc Removes and returns the first value in the list.
 	
 	
 	In debug builds, a runtime error will occur if the list is empty.
 	In debug builds, a runtime error will occur if the list is empty.

+ 21 - 0
modules/std/collections/stack.monkey2

@@ -615,6 +615,27 @@ Class Stack<T> Implements IContainer<T>
 		Return n
 		Return n
 	End
 	End
 	
 	
+	#rem monkeydoc Removes all values int the stack that fulfill a condition.
+	
+	@param condition The condition to test.
+	
+	@return The number of values removed.
+	
+	#end	
+	Method RemoveIf:Int( condition:Bool( value:T ) )
+		Local put:=0,n:=0
+		For Local get:=0 Until _length
+			If condition( _data[get] )
+				n+=1
+				Continue
+			Endif
+			_data[put]=_data[get]
+			put+=1
+		Next
+		Resize( put )
+		Return n
+	End
+	
 	#rem monkeydoc Returns a range of elements from the stack.
 	#rem monkeydoc Returns a range of elements from the stack.
 	
 	
 	Returns a slice of the stack consisting of all elements from `index1` until `index2` or the end of the stack.
 	Returns a slice of the stack consisting of all elements from `index1` until `index2` or the end of the stack.