Libraries.Containers.Array Documentation
The Array class is a data structure that stores items in contiguous memory. An item is typically stored and accessed through an index or location. This location always starts at 0, this means the first item in the array is at location 0, the second is at location 1, etc. The default maximum size is set to 10, but can be changed by using the SetSize(value) method or the array will automatically make itself large when the space is needed (note: it is possible to turn the resizing off with the SetAutoResize(false) method).
Example Code
use Libraries.Containers.Array
class Main
action Main
//make the array
Array<integer> myArray
//add a value
myArray:Add(12)
//get it back
integer value = myArray:Get(0)
end
end
Inherits from: Libraries.Language.Object
Summary
Actions Summary Table
Actions | Description |
---|---|
Add(Type value) | This action adds a value to the end of the array. |
Add(integer location, Type value) | This action adds a value at a location in the indexed object. |
AddToEnd(Type value) | This action adds an item to the end of the array. |
AddToFront(Type value) | This action adds an item to the front of the array at index 0. |
ClearContents(integer start, integer stop) | This action clears out any remnant objects. |
Compare(Libraries.Language.Object object) | This action compares two object hash codes and returns an integer. |
Copy() | This action returns a deep copy of the array in question. |
CopyToArray() | This action copies the list to an array data structure. |
Empty() | This action empty's the list, clearing out all of the items contained within it. |
Empty(boolean value) | This action empty's the list. |
Equals(Libraries.Language.Object object) | This action determines if two objects are equal based on their hash code values. |
Get(integer location) | This action gets the item at a given location in an array. |
GetAutoResize() | This action returns true if the array is dynamic(resizable) or false if the array does not automatically resize. |
GetFirstLocation(Type value) | This action gets the first occurrence of the item and returns its location. |
GetFromEnd() | This action gets the item at the end of the array(the item will remain in the array). |
GetFromFront() | This action gets the item at the front of the array(the item will remain in the array). |
GetHashCode() | This action gets the hash code for an object. |
GetIterator() | This action gets an iterator for the object and returns that iterator. |
GetLastLocation(Type value) | This action gets the last occurrence of the item and returns its location. |
GetMaxSize() | This action gets the number of items that can be stored in the array(max size). |
GetSize() | This action gets the size of the array. |
Has(Type value) | This action determines if an addable object contains a certain item. |
IsEmpty() | This action returns a boolean value, true if the container is empty and false if it contains any items. |
Remove(Type value) | This action removes the first occurrence of an item that is found in the Addable object. |
RemoveAll(Type value) | This action removes all occurrences of an item from the Addable object. |
RemoveAt(integer location) | This action removes an item from an indexed object and returns that item. |
RemoveFromEnd() | This action removes the item at the end of the array. |
RemoveFromFront() | This action removes the item at the front of the list. |
Set(integer location, Type value) | This action sets the item at a given location in the indexed object to a new item. |
SetAutoResize(boolean resizable) | This action changes the flag that tells the structure if it is a dynamic array or not. |
SetMaxSize(integer size) | This action sets the number of items that can be stored in the array(max size). |
SetSize(integer size) | This action sets the size of the array and fills it with undefined items. |
Shuffle() | This action shuffles the values in the array randomly. |
Shuffle(integer minimum, integer maximum) | This action shuffles the values in the array randomly. |
Shuffle(integer minimum, integer maximum, number seed) | This action shuffles the values in the array randomly. |
Shuffle(number seed) | This action shuffles the values in the array randomly. |
Sort() | This action sorts the values of the array using a merge sort algorithm. |
Sort(Libraries.Containers.Support.Comparison comparison) | This action sorts the values of the array using a merge sort algorithm. |
Sort(Libraries.Containers.Support.Comparison comparison, Libraries.Containers.Array<Type> temp) | This action sorts the values of the array using a merge sort algorithm. |
Actions Documentation
Add(Type value)
This action adds a value to the end of the array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(22)
Parameters
- Libraries.Language.Object: The value to be inserted.
Add(integer location, Type value)
This action adds a value at a location in the indexed object.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(0, 22)
Parameters
- integer location: The index or location the value will be stored at.
- Libraries.Language.Object: The item to be added to the indexed object.
AddToEnd(Type value)
This action adds an item to the end of the array. If the max size has been reached an the array is not re-sizable an InvalidLocationError will be alerted.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:AddToEnd(12)
Parameters
- Libraries.Language.Object: The item to be added to the array.
AddToFront(Type value)
This action adds an item to the front of the array at index 0. Then moves all other items down one index. If the max size is already reached and the array is not re-sizable an InvalidLocationError will be alerted.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:AddToFront(12)
Parameters
- Libraries.Language.Object: The item to be added to the array.
ClearContents(integer start, integer stop)
This action clears out any remnant objects. This should only be used if Empty(false) was used.
Parameters
Compare(Libraries.Language.Object object)
This action compares two object hash codes and returns an integer. The result is larger if this hash code is larger than the object passed as a parameter, smaller, or equal. In this case, -1 means smaller, 0 means equal, and 1 means larger. This action was changed in Quorum 7 to return an integer, instead of a CompareResult object, because the previous implementation was causing efficiency issues.
Example Code
Object o
Object t
integer result = o:Compare(t) //1 (larger), 0 (equal), or -1 (smaller)
Parameters
- Libraries.Language.Object: The object to compare to.
Return
integer: The Compare result, Smaller, Equal, or Larger.
Copy()
This action returns a deep copy of the array in question. As such, all elements are copied from one array to the other. While the array is a deep copy, the elements inside of the array are not copied.
Example Code
//the array class is Copyable
use Libraries.Containers.Array
Array<integer> array
Object o = array:Copy()
Array<integer> copy = cast(Array<integer>, o)
Return
Libraries.Language.Object: Returns a deep copy of the array.
CopyToArray()
This action copies the list to an array data structure.
Return
Libraries.Containers.Array: This returns an array of the list.
Empty()
This action empty's the list, clearing out all of the items contained within it.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
myArray:Empty()
Empty(boolean value)
This action empty's the list. While this does set the size to 0, this version of empty does not clear out the contents. The reason for this is that some algorithms need to manage the memory manually for efficiency.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
myArray:Empty()
Parameters
Equals(Libraries.Language.Object object)
This action determines if two objects are equal based on their hash code values.
Example Code
use Libraries.Language.Object
use Libraries.Language.Types.Text
Object o
Text t
boolean result = o:Equals(t)
Parameters
- Libraries.Language.Object: The to be compared.
Return
boolean: True if the hash codes are equal and false if they are not equal.
Get(integer location)
This action gets the item at a given location in an array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
integer result = myArray:Get(0)
Parameters
- integer location: The index or location the value is located at.
Return
Libraries.Language.Object: The item at the given location.
GetAutoResize()
This action returns true if the array is dynamic(resizable) or false if the array does not automatically resize.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
boolean result = myArray:GetAutoResize()
Return
boolean: True if the array is resizable and false if it is not.
GetFirstLocation(Type value)
This action gets the first occurrence of the item and returns its location. If the item was not found -1 is returned.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
integer location = myArray:GetFirstLocation(22)
Parameters
- Libraries.Language.Object: The item being searched for.
Return
integer: The location of the first occurrence of the item.
GetFromEnd()
This action gets the item at the end of the array(the item will remain in the array).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(4)
myArray:Add(13)
myArray:Add(12)
integer value = myArray:GetFromEnd()
Return
Libraries.Language.Object: The item at the end of the array.
GetFromFront()
This action gets the item at the front of the array(the item will remain in the array).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(4)
myArray:Add(13)
myArray:Add(12)
integer value = myArray:GetFromFront()
Return
Libraries.Language.Object: The item at the front of the array.
GetHashCode()
This action gets the hash code for an object.
Example Code
Object o
integer hash = o:GetHashCode()
Return
integer: The integer hash code of the object.
GetIterator()
This action gets an iterator for the object and returns that iterator.
Example Code
use Libraries.Containers.Array
use Libraries.Containers.Iterator
Array<integer> myArray
myArray:SetSize(4)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
Iterator<integer> it = myArray:GetIterator()
Return
Libraries.Containers.Iterator: Returns the iterator for an object.
GetLastLocation(Type value)
This action gets the last occurrence of the item and returns its location. If the item was not found -1 is returned.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
integer location = myArray:GetLastLocation(22)
Parameters
- Libraries.Language.Object: The item being searched for.
Return
integer: The location of the last occurrence of the item.
GetMaxSize()
This action gets the number of items that can be stored in the array(max size).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
integer maxSize = myArray:GetMaxSize()
Return
integer:
GetSize()
This action gets the size of the array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
integer size = myArray:GetSize()
Return
integer:
Has(Type value)
This action determines if an addable object contains a certain item.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:Has(33)
Parameters
- Libraries.Language.Object: The item to find in the Addable object.
Return
boolean: Returns true if the item was in the Addable object and false if it was not.
IsEmpty()
This action returns a boolean value, true if the container is empty and false if it contains any items.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:IsEmpty()
Return
boolean: Returns true when the container is empty and false when it is not.
Remove(Type value)
This action removes the first occurrence of an item that is found in the Addable object.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:Remove(22)
Parameters
- Libraries.Language.Object: The item to find in the Addable object.
Return
boolean: Returns true if the item was removed and false if it was not removed.
RemoveAll(Type value)
This action removes all occurrences of an item from the Addable object.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
boolean result = myArray:RemoveAll(22)
Parameters
- Libraries.Language.Object: The item to find in the Addable object.
Return
boolean: Returns true if the item was removed and false if it was not removed.
RemoveAt(integer location)
This action removes an item from an indexed object and returns that item.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
myArray:Set(1, 33)
myArray:Set(2, 45)
myArray:Set(3, 22)
integer item = myArray:RemoveAt(2)
Parameters
- integer location: The index or location of the item to remove.
Return
Libraries.Language.Object: The item that was removed from the indexed object.
RemoveFromEnd()
This action removes the item at the end of the array.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
integer removed = myArray:RemoveFromEnd()
Return
Libraries.Language.Object: The item at the end of the array.
RemoveFromFront()
This action removes the item at the front of the list.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
integer removed = myArray:RemoveFromFront()
Return
Libraries.Language.Object: The item at the front of the list.
Set(integer location, Type value)
This action sets the item at a given location in the indexed object to a new item.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
myArray:Set(0, 22)
Parameters
- integer location: The index or location the value will be stored at.
- Libraries.Language.Object: The item to be added to the indexed object.
SetAutoResize(boolean resizable)
This action changes the flag that tells the structure if it is a dynamic array or not. If it is dynamic(an array list) then resizable is true and if it is a standard array(not dynamic) then resizable is false.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetAutoResize(false)
myArray:SetSize(10)
Parameters
- boolean resizable: The value to set the resizable flag to.
SetMaxSize(integer size)
This action sets the number of items that can be stored in the array(max size). The max size can only be increased, any value that is lower will leave the array with the same max size it had.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetMaxSize(20)
Parameters
- integer size: The max size to set for the array.
SetSize(integer size)
This action sets the size of the array and fills it with undefined items. Changing the size of the array means any items already in the array will be copied over.
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:SetSize(10)
Parameters
- integer size: The size of the array.
Shuffle()
This action shuffles the values in the array randomly.
Example Code
use Libraries.Containers.Array
constant integer max = 20
Array<integer> values
i = 0
repeat while i < max
values:Add(i)
i = i + 1
end
values:Shuffle()
i = 0
repeat while i < max
output values:Get(i)
i = i + 1
end
Shuffle(integer minimum, integer maximum)
This action shuffles the values in the array randomly. In this version, a minimum and maximum part of the array is provided. If those values are outside the bounds of the array, they are clamped to correct values.
Example Code
use Libraries.Containers.Array
constant integer max = 20
Array<integer> values
i = 0
repeat while i < max
values:Add(i)
i = i + 1
end
values:Shuffle(3, 7) //Only shuffle part of the array
i = 0
repeat while i < max
output values:Get(i)
i = i + 1
end
Parameters
- integer minimum: The smallest index to shuffle
- integer maximum: The largest index to shuffle
Shuffle(integer minimum, integer maximum, number seed)
This action shuffles the values in the array randomly. In this version, a minimum and maximum part of the array is provided. If those values are outside the bounds of the array, they are clamped to correct values. This version also uses a random number seed.
Example Code
use Libraries.Containers.Array
constant integer max = 20
Array<integer> values
i = 0
repeat while i < max
values:Add(i)
i = i + 1
end
values:Shuffle(3, 7, 0) //Only shuffle part of the array and pass a random seed
i = 0
repeat while i < max
output values:Get(i)
i = i + 1
end
Parameters
- integer minimum: The smallest index to shuffle
- integer maximum: The largest index to shuffle
- number seed: The seed for the random number generator
Shuffle(number seed)
This action shuffles the values in the array randomly. In this version, a seed for a random number generator is used.
Example Code
use Libraries.Containers.Array
constant integer max = 20
Array<integer> values
i = 0
repeat while i < max
values:Add(i)
i = i + 1
end
values:Shuffle(0) //a random seed of 0
i = 0
repeat while i < max
output values:Get(i)
i = i + 1
end
Parameters
- number seed: The seed for the random number generator
Sort()
This action sorts the values of the array using a merge sort algorithm. It is guaranteed to execute in O(n log n).
Example Code
use Libraries.Containers.Array
Array<integer> myArray
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
myArray:Sort()
Sort(Libraries.Containers.Support.Comparison comparison)
This action sorts the values of the array using a merge sort algorithm. It is guaranteed to execute in O(n log n). The example uses a support class called IntegerComparison, which duplicates the default sorting for integers.
Example Code
use Libraries.Containers.Support.Comparison
class IntegerComparison is Comparison
action Compare(Object left, Object right) returns integer
Integer l = cast(Integer, left)
Integer r = cast(Integer, right)
if l:GetValue() < r:GetValue()
return parent:Comparison:SMALLER
elseif l:GetValue() > r:GetValue()
return parent:Comparison:LARGER
else
return parent:Comparison:EQUAL
end
end
end
use Libraries.Containers.Array
Array<integer> myArray
IntegerComparison compare
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
myArray:Sort(compare)
Parameters
Sort(Libraries.Containers.Support.Comparison comparison, Libraries.Containers.Array<Type> temp)
This action sorts the values of the array using a merge sort algorithm. It is guaranteed to execute in O(n log n). The example uses a support class called IntegerComparison, which duplicates the default sorting for integers. This version requires passing a comparison object and an equally sized array to use as temporary storage.
Example Code
use Libraries.Containers.Support.Comparison
class IntegerComparison is Comparison
action Compare(Object left, Object right) returns integer
Integer l = cast(Integer, left)
Integer r = cast(Integer, right)
if l:GetValue() < r:GetValue()
return parent:Comparison:SMALLER
elseif l:GetValue() > r:GetValue()
return parent:Comparison:LARGER
else
return parent:Comparison:EQUAL
end
end
end
use Libraries.Containers.Array
Array<integer> myArray
Array<integer> temp
IntegerComparison compare
myArray:Add(33)
myArray:Add(13)
myArray:Add(43)
myArray:Sort(compare, temp)