Skip to content

kiwi.util.Queue

Nikos Siatras edited this page Sep 22, 2022 · 4 revisions

Queue is a collection designed for holding elements prior to processing. Besides basic Collection operations, queues provide additional insertion, extraction, and inspection operations.

The Queue type exists in the kiwi/util/Queue.bi file.

Queue can hold elements of FreeBasic standard types (Byte, UByte, Short, UShort, Long, Ulong, Integer, UInteger, LongInt, ULongInt, Single, Double, Boolean, String) and Object.

To use User Defined Types (UDT), you must call the MACRO_DefineQueue macro before you initialize a Queue of that type. Read the second Queue example on this page to learn more.

Queue Class Methods

Method Description
Queue.add(byref e as list_type) Inserts the specified element into this queue if it is possible to do so immediately without violating capacity restrictions, returning True upon success.
Queue.addAll(byref c as list_type) Adds all of the elements of the give collection to this collection. The behavior of this operation is undefined if the specified collection is modified while the operation is in progress.
Queue.poll() Retrieves and removes the head of this queue.
Queue.peekFirst() Retrieves, but does not remove, the head of this queue
Queue.peekLast() Retrieves, but does not remove, the last element of this queue
Queue.clean() Removes all of the elements from this Queue. The Queue will be empty after this call returns.
Queue.size() Returns the number of elements in this Queue.
Queue.isEmpty() Returns true if this Queue contains no elements.

Example - Queue with FreeBasic Standard Type

The following example will add 10 random double values to a Queue and then will use a while loop to poll each value and print it on screen.

#include once "kiwi\kiwi.bi"

' Initialize a new Queue to hold Double variables
Dim myQueue as Queue(Double)

' Add 10 Random double values to myQueue
for i as Integer = 0 to 9
	myQueue.add(Math.random())
next i 

' De-Queue each element from the queue, until the queue is empty,
' and print
while myQueue.isEmpty() = false
	print myQueue.poll()
wend

Example - Queue with User Defined Types (UDT)

The following example will add "Student", a user defined type, to a Queue and then will use a while loop to poll each element and print it on screen.

#include once "kiwi\kiwi.bi"

' In this example we will create an ArrayList that holds Students
Type Student extends KObject
	firstName as String
	lastName as String
End Type

' Tells FreeBasic, that you want to use a Queue with "Student" variables
MACRO_DefineQueue(Student)

' Initialize a new Queue to hold Students
Dim studentsQueue as Queue(student)

Dim student1 as student
student1.firstName = "Nikos"
student1.lastName = "Siatras"
studentsQueue.Add(student1) ' Add student1 to studentsQueue

Dim student2 As student
student2.firstName = "Elon"
student2.lastName = "Musk"
studentsQueue.Add(student2) ' Add student2 to studentsQueue

while studentsQueue.isEmpty() = false
	Dim tmp as Student = studentsQueue.poll()
	print "Student " & tmp.firstName &" " & tmp.lastName &" was in the Queue"
wend

Home

Kiwi.DB

  • MySQL/MariaDB - Coming to v1.0.2

Kiwi.IO

Kiwi.lang

Kiwi.locale

Kiwi.time

Kiwi.util

Clone this wiki locally