Bläddra i källkod

Added TQueue ToArray(), TryDequeue() and TryPeek() methods.
More examples.

woollybah 6 år sedan
förälder
incheckning
0a75f32c88

+ 30 - 0
collections.mod/doc/tqueue_clear.bmx

@@ -0,0 +1,30 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local queue:TQueue<String> = New TQueue<String>
+
+Print "queue.Count() : " + queue.Count()
+Print ""
+
+queue.Enqueue("one")
+queue.Enqueue("two")
+queue.Enqueue("three")
+queue.Enqueue("four")
+queue.Enqueue("five")
+
+For Local num:String = EachIn queue
+	Print num
+Next
+
+Print "~nqueue.Count() : " + queue.Count()
+
+Print "~nqueue.Clear()"
+queue.Clear()
+
+For Local num:String = EachIn queue
+	Print num
+Next
+
+Print "~nqueue.Count() : " + queue.Count()

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

@@ -0,0 +1,22 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local queue:TQueue<String> = New TQueue<String>
+
+Print "queue.Count() : " + queue.Count()
+Print ""
+
+queue.Enqueue("one")
+queue.Enqueue("two")
+queue.Enqueue("three")
+queue.Enqueue("four")
+queue.Enqueue("five")
+
+For Local num:String = EachIn queue
+	Print num
+Next
+
+Print "~nqueue.Contains(~qtwo~q) : " + queue.Contains("two")
+Print "queue.Contains(~qsix~q) : " + queue.Contains("six")

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

@@ -0,0 +1,24 @@
+SuperStrict
+
+Framework brl.collections
+Import brl.standardio
+
+Local queue:TQueue<String> = New TQueue<String>
+
+queue.Enqueue("one")
+queue.Enqueue("two")
+queue.Enqueue("three")
+queue.Enqueue("four")
+queue.Enqueue("five")
+
+For Local num:String = EachIn queue
+	Print num
+Next
+
+Print "~nqueue.Count() : " + queue.Count()
+
+Print "~nqueue.Peek() : " + queue.Peek()
+
+Print "~nqueue.Deque() : " + queue.Dequeue()
+
+Print "~nqueue.Peek() : " + queue.Peek()

+ 44 - 0
collections.mod/queue.bmx

@@ -74,6 +74,22 @@ Public
 	
 	Method CopyTo(array:T[], index:Int = 0)
 	End Method
+
+	Rem
+	bbdoc: Converts a #TQueue to an array.
+	returns: An array of elements.
+	End Rem
+	Method ToArray:T[]()
+		Local arr:T[size]
+		
+		Local i:Int
+		For Local element:T = EachIn Self
+			arr[i] = element
+			i :+ 1
+		Next
+		
+		Return arr
+	End Method
 	
 	Rem
 	bbdoc: Removes all elements from the #TQueue.
@@ -202,6 +218,34 @@ Public
 		tail = 0
 		full = size > 0
 	End Method
+	
+	Rem
+	bbdoc: Tries to remove and return the element at the beginning of the #TQueue.
+	returns: #True if an element was removed and returned from the beginning of the #TQueue successfully; otherwise, #False.
+	about: When this method returns, if the operation was successful, @vlaue contains the element removed. If no element was available to be removed, the value is unspecified.
+	End Rem
+	Method TryDequeue:Int(value:T Var)
+		If Not size Then
+			Return False
+		End If
+		
+		value = Dequeue()
+		Return True
+	End Method
+	
+	Rem
+	bbdoc: Tries to return an element from the beginning of the #TQueue without removing it.
+	returns: #True if an element was returned successfully; otherwise, #False.
+	about: When this method returns, @value contains an element from the beginning of the #TQueue or an unspecified value if the operation failed.
+	End Rem
+	Method TryPeek:Int(value:T Var)
+		If Not size Then
+			Return False
+		End If
+		
+		value = data[head]
+		Return True
+	End Method
 
 	Private
 	Method Resize()