Browse Source

Added TQueue and TStack constructors.

woollybah 6 years ago
parent
commit
2d8568bd1f
2 changed files with 59 additions and 1 deletions
  1. 29 0
      collections.mod/queue.bmx
  2. 30 1
      collections.mod/stack.bmx

+ 29 - 0
collections.mod/queue.bmx

@@ -29,6 +29,35 @@ Public
 		data = New T[initialCapacity]
 	End Method
 
+	Rem
+	bbdoc: Creates a new #TQueue initialised by @array.
+	End Rem
+	Method New(array:T[])
+		initialCapacity = 16
+		If array Then
+			initialCapacity = Max(initialCapacity, array.length)
+		End If
+		data = New T[initialCapacity]
+		
+		If array Then
+			For Local element:T = EachIn array
+				Enqueue(element)
+			Next
+		End If
+	End Method
+
+	Rem
+	bbdoc: Creates a new #TQueue initialised by @iterable.
+	End Rem
+	Method New(iterable:IIterable<T>)
+		New(16)
+		If iterable Then
+			For Local value:T = EachIn iterable
+				Enqueue(value)
+			Next
+		End If
+	End Method
+
 	Rem
 	bbdoc: Returns an iterator that iterates through the #TQueue.
 	End Rem

+ 30 - 1
collections.mod/stack.bmx

@@ -10,7 +10,7 @@ Type TStack<T> Implements ICollection<T>
 Private
 	Field data:T[]
 	Field size:Int
-	Field intialCapacity:Int
+	Field initialCapacity:Int
 Public
 
 	Rem
@@ -21,6 +21,35 @@ Public
 		data = New T[initialCapacity]
 	End Method
 
+	Rem
+	bbdoc: Creates a new #TStack initialised by @array.
+	End Rem
+	Method New(array:T[])
+		initialCapacity = 16
+		If array Then
+			initialCapacity = Max(initialCapacity, array.length)
+		End If
+		data = New T[initialCapacity]
+		
+		If array Then
+			For Local element:T = EachIn array
+				Push(element)
+			Next
+		End If
+	End Method
+
+	Rem
+	bbdoc: Creates a new #TStack initialised by @iterable.
+	End Rem
+	Method New(iterable:IIterable<T>)
+		New(16)
+		If iterable Then
+			For Local value:T = EachIn iterable
+				Push(value)
+			Next
+		End If
+	End Method
+
 	Rem
 	bbdoc: Returns an iterator for the #TStack.
 	End Rem