Răsfoiți Sursa

Added TStack TryPeek() and TryPop() methods.
Fixed ToArray() ordering.
More TStack examples.

woollybah 6 ani în urmă
părinte
comite
3af3869e98

+ 22 - 0
collections.mod/doc/tstack_clear.bmx

@@ -0,0 +1,22 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+Print "stack.Count() : " + stack.Count()
+
+For Local num:String = EachIn stack
+	Print num
+Next
+
+Print "~nstack.Clear()"
+stack.Clear()
+
+Print "stack.Count() : " + stack.Count()

+ 18 - 0
collections.mod/doc/tstack_contains.bmx

@@ -0,0 +1,18 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+For Local num:String = EachIn stack
+	Print num
+Next
+
+Print "~nstack.Contains(~qthree~q) : " + stack.Contains("three")
+Print "stack.Contains(~qsix~q) : " + stack.Contains("six")

+ 16 - 0
collections.mod/doc/tstack_getiterator.bmx

@@ -0,0 +1,16 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+Local iterator:IIterator<String> = stack.GetIterator()
+While iterator.MoveNext()
+	Print iterator.Current()
+Wend

+ 21 - 0
collections.mod/doc/tstack_peek.bmx

@@ -0,0 +1,21 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+For Local num:String = EachIn stack
+	Print num
+Next
+
+Print "~nstack.Peek() : " + stack.Peek()
+
+Print "~nstack.Pop() : " + stack.Pop()
+
+Print "~nstack.Peek() : " + stack.Peek()

+ 24 - 0
collections.mod/doc/tstack_pop.bmx

@@ -0,0 +1,24 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+For Local num:String = EachIn stack
+	Print num
+Next
+
+Print "~nstack.Pop() : " + stack.Pop()
+Print "stack.Pop() : " + stack.Pop()
+Print "stack.Pop() : " + stack.Pop()
+
+Print "~nstack.Count() : " + stack.Count()
+For Local num:String = EachIn stack
+	Print num
+Next

+ 17 - 0
collections.mod/doc/tstack_toarray.bmx

@@ -0,0 +1,17 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+Local arr:String[] = stack.ToArray()
+
+For Local s:String = EachIn arr
+	Print s
+Next

+ 25 - 0
collections.mod/doc/tstack_trypeek.bmx

@@ -0,0 +1,25 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+For Local num:String = EachIn stack
+	Print num
+Next
+
+Print ""
+
+Local v:String
+
+While stack.TryPeek(v)
+
+	Print "The next value is : " + v + " -> " + stack.Pop()
+
+Wend

+ 25 - 0
collections.mod/doc/tstack_trypop.bmx

@@ -0,0 +1,25 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local stack:TStack<String> = New TStack<String>
+stack.Push("one")
+stack.Push("two")
+stack.Push("three")
+stack.Push("four")
+stack.Push("five")
+
+For Local num:String = EachIn stack
+	Print num
+Next
+
+Print ""
+
+Local v:String
+
+While stack.TryPop(v)
+
+	Print "Popped : " + v
+
+Wend

+ 29 - 1
collections.mod/stack.bmx

@@ -146,7 +146,9 @@ Public
 	Method ToArray:T[]()
 		Local arr:T[size]
 			
-		ArrayCopy(data, 0, arr, 0, size)
+		For Local i:Int = 0 Until size
+			arr[i] = data[size - i - 1]
+		Next
 		
 		Return arr
 	End Method
@@ -164,6 +166,32 @@ Public
 		End If
 	End Method
 	
+	Rem
+	bbdoc: Tries to return an element from the top of the #TStack without removing it.
+	returns: #True if an element was returned successfully; otherwise, #False.
+	End Rem
+	Method TryPeek:Int(value:T Var)
+		If Not size Then
+			Return False
+		End If
+		
+		value = Peek()
+		Return True
+	End Method
+	
+	Rem
+	bbdoc: Tries to remove and return an element from the top of the #TStack.
+	returns: #True if an element was removed and returned from the top of the #TStack successfully; otherwise, #False.
+	End Rem
+	Method TryPop:Int(value:T Var)
+		If Not size Then
+			Return False
+		End If
+
+		value = Pop()
+		Return True
+	End Method
+	
 	Private
 	Method ResizeAsNeeded(minCapacity:Int)
 		Local capacity:Int = data.length