瀏覽代碼

Added Stack.Swap for swapping 2 stacks.

Mark Sibly 7 年之前
父節點
當前提交
f061890a8e
共有 1 個文件被更改,包括 15 次插入1 次删除
  1. 15 1
      modules/std/collections/stack.monkey2

+ 15 - 1
modules/std/collections/stack.monkey2

@@ -663,14 +663,20 @@ Class Stack<T> Implements IContainer<T>
 		Return New Stack( _data.Slice( index1,index2 ) )
 		Return New Stack( _data.Slice( index1,index2 ) )
 	End
 	End
 	
 	
-	#rem monkeydoc Swaps 2 elements in the stack.
+	#rem monkeydoc Swaps 2 elements in the stack, or 2 stacks.
+	
+	This method can be used to either swap 2 elements in the stack, or 2 entire stacks.
 	
 	
 	In debug builds, a runtime error will occur if `index1` or `index2` is out of range.
 	In debug builds, a runtime error will occur if `index1` or `index2` is out of range.
 	
 	
+	Swapping entire stacks simply swaps the storage arrays and lengths of the 2 stacks, and is therefore very fast.
+	
 	@param index1 The index of the first element.
 	@param index1 The index of the first element.
 	
 	
 	@param index2 The index of the second element.
 	@param index2 The index of the second element.
 	
 	
+	@param stack The stack to swap with.
+	
 	#end
 	#end
 	Method Swap( index1:Int,index2:Int )
 	Method Swap( index1:Int,index2:Int )
 		DebugAssert( index1>=0 And index1<_length And index2>=0 And index2<_length,"Stack index out of range" )
 		DebugAssert( index1>=0 And index1<_length And index2>=0 And index2<_length,"Stack index out of range" )
@@ -680,6 +686,14 @@ Class Stack<T> Implements IContainer<T>
 		_data[index2]=t
 		_data[index2]=t
 	End
 	End
 	
 	
+	Method Swap( stack:Stack )
+		Local data:=_data,length:=_length
+		_data=stack._data
+		_length=stack._length
+		stack._data=data
+		stack._length=length
+	End
+	
 	#rem monkeydoc Sorts the stack.
 	#rem monkeydoc Sorts the stack.
 
 
 	@param ascending True to sort the stack in ascending order, false to sort in descending order.
 	@param ascending True to sort the stack in ascending order, false to sort in descending order.