Name | Type | is_array | initial_value |
//TESH.scrollpos=273
//TESH.alwaysfold=0
Collections
v0.6.0
===========
===========
Collections are...well, theyre what they sound like...collections of objects. They store
groups of struct instances. Each type of collections object provides different
functionality, but all share common features since they all extend the same basic
interface.
Using Collection
================
To implement Collections, you need to first define which data types you want to use
Collections with. To do that, simply use the textmacro named "Collection". The textmacro
takes three arguments:
TYPE : The actual name of the data type; must be a STRUCT type, not a
native type (the only exception is that it can be integer)
TYPE_NAME : The name of the data type as you want it to appear in collection
objects names
(Convention is to have the first letter of each word
capitalized)
TYPE_CONSTANT_NAME : The name of the data type as you want it to appear in the names of
constant variables
(Convention is to have all letters capitalized, with words separated
by underscores)
Why textmacros? Because neither JASS nor vJASS supports generic data typing, so since
Collections objects cannot specify their element types at runtime, it must be done at
compile-time with textmacros.
Once you have implemented a textmacro, the Collection and Iterator interfaces will be defined
for the data type specified in the textmacro. The interfaces will be prefixed with the value of
TYPE_NAME for that data type and all constant variables will be prefixed with the value
of TYPE_CONSTANT_NAME.
The textmacro must be run outside of any library or scope.
For example, to create the collection and iterator interfaces that support the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
Storage Space Constraints:
--------------------------
Maximum of 8190 instantiated Collection objects for a given data type at any one time.
===============================
interface <TYPE_NAME>Collection
===============================
A collections object represents a group of objects of a certain data type. All collections
objects must implement the methods required by this interface. Different types of collections
objects, however, have different implementations. While different implementations all must
conform to the specifications outlined by Collection, beyond these specifications their
behavior may be self-determined.
Instance Methods
================
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to this Collection. Returns true if the element was successfully
added. Returns false if the operation failed for any reason.
Parameters:
-<TYPE> toAdd - the element to add to this Collection.
Returns:
true if the element was successfully added; false otherwise.
------
method addAll takes <TYPE_NAME>Collection collection returns nothing
------
Attempts to add all elements of another Collection to this Collection in the order
returned by the other Collection's Collection.enum() method. This operation takes at least O(n)
time with respect to the size of the other Collection and may take longer depending on the
implementation of the Collection.add() method of this Collection.
Parameters:
-<TYPE_NAME>Collection collection - the other Collection whose elements are to be added to this
Collection.
------
method clear takes nothing returns nothing
------
Removes all elements from this Collection.
------
method contains takes <TYPE> element returns boolean
------
Checks if at least one instance of a certain element is in this Collection.
Parameters:
-<TYPE> element - the element for which to search in this Collection.
Returns:
true if at least one instance of the specified element is in this Collection; false otherwise.
------
method destroy takes nothing returns nothing
------
Destroys this Collection, freeing up allocation space for other collections.
------
method enum takes <TYPE_NAME>_Enum enum returns nothing
------
Enumerates through all elements of this Collection and evaluates the specified enumeration
function. This is a cleaner way than using an iterator to execute code for all elements in a
collection, but it has its disadvantages. Firstly, waits are not supported. Secondly,
there is no way to modify the collection being enumerated without risking indeterministic
behavior.
Parameters:
-<TYPE_NAME>_Enum enum - the enumeration function to be evaluated.
See Also:
-<TYPE_NAME>_Enum
------
method isEmpty takes nothing returns boolean
------
Checks if this Collection is empty (has no elements).
Returns:
true if this Collection has no elements; false otherwise.
------
method iterator takes nothing returns <TYPE_NAME>Iterator
------
Returns an Iterator for this Collection.
Returns:
An Iterator for this Collection.
See Also:
-<TYPE_NAME>Iterator
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove one instance of a certain element from this Collection. Returns true
if the element was succesfully removed. Returns false if the operation failed for any reason.
Parameters:
-<TYPE> toRemove - the element to remove from this Collection.
Returns:
true if the element was successfully removed; false otherwise.
------
method removeAll takes <TYPE_NAME>Collection collection returns nothing
------
Removes all elements contained in another Collection from this Collection. This operation takes
at least O(n) time with respect to the size of this Collection and may take longer depending
on the implementation of the Collection.contains() method of the other Collection.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be removed from this
Collection.
------
method retainAll takes <TYPE_NAME>Collection collection returns nothing
------
Removes all elements from this Collection except for those elements also found in another
Collection. The ordering of elements retained in this Collection are preserved (even
if they were not ordered in any consistent manner to begin with). This operation takes at
least O(n) time with respect to the size of this Collection and may take longer depending on
the implementation of the Collection.contains() method of the other Collection.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be retained in this
Collection.
------
method size takes nothing returns integer
------
Returns the number of elements in this Collection.
Returns:
The number of elements in this Collection.
------
method toArray takes nothing returns <TYPE_NAME>Array
------
Returns an array representation of this Collection. The array is not backed by this Collection,
so any changes made to the array will not be reflected in the collection, and any changes
made to the Collection after this method is called will not be reflected in the array.
Returns:
An array representation of this Collection, not backed by the Collection.
=============================
interface <TYPE_NAME>Iterator
=============================
An Iterator is capable of iterating over every element in a Collection. Calling Iterator.next()
on an iterator until Iterator.hasNext() returns false will return every ocurrence of every
element in the iterator's collection exactly once.
Instance Methods
================
------
method hasNext takes nothing returns boolean
------
Checks if this Iterator has more elements to return (in other words, if Iterator.next() != 0).
Returns:
true if this Iterator has more elements to return; false otherwise.
------
method next takes nothing returns <TYPE>
------
Returns the next element of this Iterator. If this Iterator has reached the end of its
iteration, this method will return 0.
Returns:
The next element of this Iterator. If no such element exists, this method returns 0.
------
method remove takes nothing returns nothing
------
Removes the last element returned by this Iterator. This method does nothing if
Iterator.remove() has already been called since the last call to Iterator.next(). Removing
elements from or otherwise modifying a collection while iterating over it with this iterator
other than using Iterator.remove() may lead to indeterministic behavior.
=======================
struct <TYPE_NAME>Array
=======================
A sized dynamic array. It performs (slightly) slower than vJASS's dynamic arrays but unlike
those arrays you may specify the size freely upon instantiation and there is no inherent size
limit (You can theoretically have 8190 arrays each with a size of 100000). You may have up to
8190 dynamic arrays of a given data type instantiated at any one time.
Constructors
============
------
static method create takes integer size returns <TYPE_NAME>Array
------
Creates a new instance of Array with the given size. The size of the new Array cannot be changed
after instantiation. All indices are empty (hold a value of 0) upon instantiation.
Parameters:
-integer size - the size of the new Array. If a value less than one is passed to this parameter,
the new Array will have a size of 1.
Instance Methods
================
------
method size takes nothing returns integer
------
Returns the size of this Array. The size of this Array is the maximum number of indices it
contains and is always a constant number, even if some or all of those indices are empty.
Returns:
The size of this Array.
===================================================================================
function interface <TYPE_NAME>_Enum takes <TYPE> this, integer data returns nothing
===================================================================================
This function interface is used by the enum() method of collections objects to enumerate
through all of their elements. Functions that wish to be passed to the enum() method must
extend this interface.
See Also:
-<TYPE_NAME>Collection.enum()
==================================================================================
function interface <TYPE_NAME>_Comparator takes <TYPE> x, <TYPE> y returns integer
==================================================================================
This function interface should be extended by all functions that wish to compare two struct
instances to determine their order. The general contract for this interface is that if
struct x is to be considered greater than struct y, the function returns a positive number,
if struct x is to be considered less than struct y, the function returns a negative number,
and if the two structs are to be considered equal, the function returns 0.
//TESH.scrollpos=0
//TESH.alwaysfold=0
Set
v0.6.0
======
======
Using Set
=========
Requirements:
-------------
-Collection
To implement Set, you only need to implement Collection. For instructions on how to do this,
refer to the Collection documentation.
Additional Storage Space Constraints:
-------------------------------------
None
Tips for Use:
-------------
Use a set if you need to store a group of elements and ensure that no element occurs more than
once in the group. Generally, sets are extremely efficient at adding, removing, and searching
for elements, but not all implementations need satisfy this property.
============================================================
abstract struct <TYPE_NAME>Set extends <TYPE_NAME>Collection
============================================================
A set is a collections object that does not allow duplicate entries. In other words, every
element in a set is guaranteed to be unique.
DO NOT ATTEMPT TO INSTANTIATE THIS STRUCT TYPE DIRECTLY!
Instance Methods
================
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to this Set. Returns true if the element was successfully
added. Returns false if the specified element is already contained in this set.
Parameters:
-<TYPE> toAdd - the element to add to this Set.
Returns:
true if the element was successfully added; false otherwise.
------
method addAll takes <TYPE_NAME>Collection collection returns nothing
------
Attempts to add all elements of another Collection to this Set in the order returned by the
other Collection's Collection.enum() method. This operation takes at least O(n) time with
respect to the size of the other Collection and may take longer depending on the implementation
of the Collection.add() method of this Set.
Parameters:
-<TYPE_NAME>Collection collection - the other Collection whose elements are to be added to this
Set.
------
method clear takes nothing returns nothing
------
Removes all elements from this Set.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element is in this Set.
Parameters:
-<TYPE> element - the element for which to search in this Set.
Returns:
true if the specified element is in this Set; false otherwise.
------
method destroy takes nothing returns nothing
------
Destroys this Set.
------
method enum takes <TYPE_NAME>_Enum enum returns nothing
------
Enumerates through all elements of this Set and evaluates an enumeration function.
Parameters:
-<TYPE_NAME>_Enum enum - the enumeration function to be evaluated.
------
method isEmpty takes nothing returns boolean
------
Checks if this Set is empty (has no elements).
Returns:
true if this Set has no elements; false otherwise.
------
method iterator takes nothing returns <TYPE_NAME>Iterator
------
Returns an Iterator for this Set.
Returns:
An Iterator for this Set.
See Also:
-<TYPE_NAME>Iterator
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove a certain element from this Set. Returns true if the element was succesfully
removed. Returns false if this Set does not contain the specified element.
Parameters:
-<TYPE> toRemove - the element to remove from this Set.
Returns:
true if the element was successfully removed; false otherwise.
------
method removeAll takes <TYPE_NAME>Collection collection returns nothing
------
Removes all elements contained in another Collection from this Set. This operation takes
at least O(n) time with respect to the size of this Set and may take longer depending
on the implementation of the Collection.contains() method of the other Collection.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be removed from this
Set.
------
method retainAll takes <TYPE_NAME>Collection collection returns nothing
------
Removes all elements from this Set except for those elements also found in another Collection.
The ordering of elements retained in this Set are preserved (even if they were not ordered in
any consistent manner to begin with). This operation takes at least O(n) time with respect to
the size of this Set and may take longer depending on the implementation of the
Collection.contains() method of the other Collection.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be retained in this
Set.
------
method size takes nothing returns integer
------
Returns the number of elements in this Set.
Returns:
The number of elements in this Set.
------
method toArray takes nothing returns <TYPE_NAME>Array
------
Returns an array representation of this Set. The array is not backed by this Set,
so any changes made to the array will not be reflected in the Set, and any changes
made to the Set after this method is called will not be reflected in the array.
Returns:
An array representation of this Set, not backed by the Set.
//TESH.scrollpos=39
//TESH.alwaysfold=0
LinkedHashSet
v0.6.0
=============
=============
Using LinkedHashSet
===================
Requirements:
-------------
-Set
To implement LinkedHashSet, you first need to implement Set (see the Set documentation). After
you have implemented Set for a data type, you may then implement LinkedHashSet for the same data
type.
The textmacro to implement LinkedHashSet is named: "LinkedHashSet"
The textmacro must be run outside of any library or scope.
For example, to create LinkedHashSet objects that support the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro LinkedHashSet("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
None
Tips for Use:
-------------
Use LinkedHashSet if you require a set with fast insertion and retrieval (you will be regularly
adding, removing, and/or checking if the set contains elements) and you either don't need
elements to be ordered in the set or you wish the set to preserve the order in which elements
were inserted (elements inserted first come first in the set).
======================================================
struct <TYPE_NAME>LinkedHashSet extends <TYPE_NAME>Set
======================================================
LinkedHashSet is an implementation of Set that contains a doubly-linked list running through its
elements. This implementation preserves the order in which the elements were inserted into the
set. LinkedHashSet uses reference equality, meaning struct a is considered to be equal to
struct b if and only if a and b are in fact the same instance.
All single-element insertion and retrieval methods are guaranteed to run in constant time.
Instance Methods
================
Inherited from Set:
-method addAll takes <TYPE_NAME>Collection collection returns nothing
-method clear takes nothing returns nothing
-method isEmpty takes nothing returns boolean
-method removeAll takes <TYPE_NAME>Collection collection returns nothing
-method retainAll takes <TYPE_NAME>Collection collection returns nothing
-method size takes nothing returns integer
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to this Set. Returns true if the element was successfully added.
Returns false if the specified element is already contained in this Set or if this Set has
reached the maximum element limit. The order of elements inserted into the Set by this method
is preserved. This method is guaranteed to run in constant time.
Parameters:
-<TYPE> toAdd - the element to add to this Set.
Returns:
true if the element was successfully added; false otherwise.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element is in this Set. This method is guaranteed to run in constant time.
Parameters:
-<TYPE> element - the element for which to search in this Set.
Returns:
true if the specified element is in this Set; false otherwise.
------
method enum takes <TYPE_NAME>_Enum enum returns nothing
------
Enumerates through all elements of this Set and evaluates an enumeration function. The elements
are enumerated in the same order as they were inserted into the Set.
Parameters:
-<TYPE_NAME>_Enum enum - the enumeration function to be evaluated.
------
method iterator takes nothing returns <TYPE_NAME>Iterator
------
Returns an Iterator for this Set. The Iterator will iterate over the elements in this Set in the
same order as that in which the elements were inserted.
Returns:
An Iterator for this Set.
See Also:
-<TYPE_NAME>Iterator
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove a certain element from this Set. Returns true if the element was succesfully
removed. Returns false if this Set does not contain the specified element. This method is
guaranteed to run in constant time.
Parameters:
-<TYPE> toRemove - the element to remove from this Set.
Returns:
true if the element was successfully removed; false otherwise.
------
method toArray takes nothing returns <TYPE_NAME>Array
------
Returns an array representation of this Set. The array is not backed by this Set,
so any changes made to the array will not be reflected in the Set, and any changes
made to the Set after this method is called will not be reflected in the array. The
array has size equal to <TYPE_CONSTANT_NAME>_MAX_ELEMENTS, but only enough indices required
to hold all elements of this Set will be used. All indices to the right of these
indices will hold values of 0. The ordering of the elements in the array is the same as the
ordering of the elements in this Set.
Returns:
An array representation of this Set, not backed by the Set.
//TESH.scrollpos=30
//TESH.alwaysfold=0
TreeSet
v0.6.0
=======
=======
Using TreeSet
=============
Requirements:
-------------
-Set
To implement TreeSet, you first need to implement Set (see the Set documentation) After you
have implemented Set for a data type, you may then implement TreeSet for the same data type.
An important thing to note is that TreeSet may ONLY be implemented for data types that
support the < and > boolean operators. Data types that support these are limited to integer
and struct types that have overloaded the < operator.
The textmacro to implement TreeSet is named: "TreeSet"
The textmacro must be run outside of any library or scope.
For example, to create TreeSet objects that support the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro TreeSet("Data", "Data", "DATA)
Additional Storage Space Constraints:
-------------------------------------
Maximum of 8189 elements in all existing TreeSet objects for a given data type at any one time.
Tips for Use:
-------------
Use TreeSet if you require a set that orders its elements based on some property of the
elements themselves. If you are going to be adding elements in a more or less random order into
the set based on how you want the set to order them (in other words, you are not pre-ordering
the elements for the set before adding them to the set), then the basic TreeSet implementation
should be fine. However, if you expect to be adding many pre-ordered groups of elements, or if
you just want to ensure that the set operates as quickly as possible, you may wish to use
BalancedTreeSet instead. The tradeoff is that BalancedTreeSet requires many more calculations
when adding and removing elements than TreeSet.
================================================
struct <TYPE_NAME>TreeSet extends <TYPE_NAME>Set
================================================
A TreeSet is an ordered implementation of Set that uses a binary search tree to store its
data. All elements within the set are consistently ordered in one of two ways: by a
comparator or by the natural ordering of the elements (as defined by the < and > boolean
operators) if a comparator was not specified upon instantiation of the set. TreeSet uses either
a comparator or the < and > boolean operators to determine struct equality (struct a is
considered to be equal to struct b if a < b is false and a > b is false).
TreeSet.add(), TreeSet.remove(), and TreeSet.contains() all run in O(log n) time IF the tree is
well-balanced. If the tree is not well-balanced (i.e. depth >> breadth), the tree essentially
degenerates into a linked list and the aforementioned methods will run in worst-case
O(n) time.
Constructors
============
------
static method create takes <TYPE_NAME>_Comparator comparator returns <TYPE_NAME>TreeSet
------
Creates a new instance of TreeSet with the specified comparator. If a comparator is specified,
it will be used to determine the ordering of elements within the set. Otherwise, elements
will be ordered according to their natural ordering defined by the < and > boolean operators.
Parameters:
-<TYPE_NAME>_Comparator comparator - the Comparator that the new TreeSet will use to determine
the ordering of its elements.
Instance Methods
================
Inherited From Set:
-method addAll takes <TYPE_NAME>Collection collection returns nothing
-method isEmpty takes nothing returns boolean
-method removeAll takes <TYPE_NAME>Collection collection returns nothing
-method retainAll takes <TYPE_NAME>Collection collection returns nothing
-method size takes nothing returns integer
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to this Set. Returns true if the element was successfully added.
Returns false if the specified element is already contained in this Set or if this Set has
reached the maximum element limit. The order of elements inserted into the Set by this method
is preserved. This method runs in O(log n) time if this TreeSet is well-balanced and runs in
worst-case O(n) time.
Parameters:
-<TYPE> toAdd - the element to add to this Set.
Returns:
true if the element was successfully added; false otherwise.
------
method clear takes nothing returns nothing
------
Removes all elements from this Set. This method runs in O(n) time.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element is in this Set. This method in O(log n) time if this TreeSet is
well-balanced and runs in worst-case O(n) time.
Parameters:
-<TYPE> element - the element for which to search in this Set.
Returns:
true if the specified element is in this Set; false otherwise.
------
method enum takes <TYPE_NAME>_Enum enum returns nothing
------
Enumerates through all elements of this Set in order and evaluates the specified enumeration
function.
Parameters:
-<TYPE_NAME>_Enum enum - the enumeration function to be evaluated.
------
method iterator takes nothing returns <TYPE_NAME>Iterator
------
Returns an iteration capable of iterating through this Set. The elements are returned in the
order that they appear in this Set.
Returns:
An Iterator for this Set.
See Also:
-<TYPE_NAME>Iterator
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove a certain element from this Set. Returns true if the element was succesfully
removed. Returns false if this Set does not contain the specified element. This method runs in
O(log n) time if this TreeSet is well-balanced and runs in worst-case O(n) time.
Parameters:
-<TYPE> toRemove - the element to remove from this Set.
Returns:
true if the element was successfully removed; false otherwise.
------
method toArray takes nothing returns <TYPE_NAME>Array
------
Returns an array representation of this Set. The array is not backed by this Set,
so any changes made to the array will not be reflected in the Set, and any changes
made to the Set after this method is called will not be reflected in the array. The
array has size equal to <TYPE_CONSTANT_NAME>_MAX_ELEMENTS, but only enough indices required
to hold all elements of this Set will be used. All indices to the right of these
indices will hold values of 0. The ordering of the elements in the array is the same as the
ordering of the elements in this Set.
Returns:
An array representation of this Set, not backed by the Set.
============================================================
struct <TYPE_NAME>BalancedTreeSet extends <TYPE_NAME>TreeSet
============================================================
BalancedTreeSet is a self-balancing tree set. More specifically, it is an implementation of a
red-black tree. Unlike TreeSet, this implementation guarantees O(log n) performance for single-
element insertion and retrieval, even if pre-ordered elements are inserted into the set.
Constructors
============
------
static method create takes <TYPE_NAME>_Comparator comparator returns <TYPE_NAME>BalancedTreeSet
------
Creates a new instance of BalancedTreeSet with the specified comparator. If a comparator is
specified, it will be used to determine the ordering of elements within the set. Otherwise,
elements will be ordered according to their natural ordering defined by the < and > boolean
operators.
Instance Methods
================
Inherited From TreeSet:
-method addAll takes <TYPE_NAME>Collection collection returns nothing
-method clear takes nothing returns nothing
-method enum takes <TYPE_NAME>_Enum enum returns nothing
-method isEmpty takes nothing returns boolean
-method iterator takes nothing returns <TYPE_NAME>Iterator
-method removeAll takes <TYPE_NAME>Collection collection returns nothing
-method retainAll takes <TYPE_NAME>Collection collection returns nothing
-method size takes nothing returns integer
-method toArray takes nothing returns <TYPE_NAME>Array
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to this Set. Returns true if the element was successfully added.
Returns false if the specified element is already contained in this Set or if this Set has
reached the maximum element limit. The order of elements inserted into the Set by this method
is preserved. This method is guaranteed to run in O(log n) time.
Parameters:
-<TYPE> toAdd - the element to add to this Set.
Returns:
true if the element was successfully added; false otherwise.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element is in this Set. This method is guaranteed to run in O(log n) time.
Parameters:
-<TYPE> element - the element for which to search in this Set.
Returns:
true if the specified element is in this Set; false otherwise.
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove a certain element from this Set. Returns true if the element was succesfully
removed. Returns false if this Set does not contain the specified element. This method is
guaranteed to run in O(log n) time.
Parameters:
-<TYPE> toRemove - the element to remove from this Set.
Returns:
true if the element was successfully removed; false otherwise.
//TESH.scrollpos=266
//TESH.alwaysfold=0
List
v0.6.0
======
======
Using List
==========
Requirements:
-------------
-Collection
To implement List, you only need to implement Collection. For instructions on how to do this,
refer to the Collection documentation.
Additional Storage Space Constraints:
-------------------------------------
None
Tips for Use:
------------
Use a list if you need to store a group of elements while retaining complete control over the
positions (or indexes) of elements within the group (and by consequence, the order of the
elements). Lists allow the same element to appear in the same list more than once. Generally,
lists are relatively slow when it comes to search operations by element.
=============================================================
abstract struct <TYPE_NAME>List extends <TYPE_NAME>Collection
=============================================================
A list is an ordered collection. The ordering of elements is completely determined by the
user. Duplicate elements are allowed. Users may access elements from the list using integer
indices or search through the list for specific elements.
DO NOT ATTEMPT TO INSTANTIATE THIS STRUCT TYPE DIRECTLY!
Instance Methods
================
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to the end of this List. Returns true if the element was successfully
added.
Parameters:
-<TYPE> toAdd - the element to add to this List.
Returns:
true if the element was successfully added; false otherwise.
------
method addAll takes <TYPE_NAME>Collection collection returns nothing
------
Attempts to add all elements of another Collection to the end of this List in the
order returned by the other collection's Collection.enum() method.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be added to the end of
this List.
------
method addByIndex takes integer index, <TYPE> toAdd returns nothing
------
Attempts to add an element to this List at the specified index. The previous element at that
index, if there is one, and any elements to the right will be shifted to the right (their
indices are incremented up by one). This method does nothing if the index is out of bounds
(index < 0 or index > List.size()).
Parameters:
-integer index - the index at which to add the new element.
-<TYPE> toAdd - the element to add to this List.
------
method clear takes nothing returns nothing
------
Removes all elements from this List.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element occurs at least once in this List.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
true if the specified element is in this List; false otherwise.
------
method destroy takes nothing returns nothing
------
Destroys this List.
------
method enum takes <TYPE_NAME>_Enum enum returns nothing
------
Enumerates through all elements of this List in order and evaluates the specified
enumeration function.
Parameters:
-<TYPE_NAME>_Enum enum - the enumeration function to be evaluated.
------
method getByIndex takes integer index returns <TYPE>
------
Returns the element at the specified index. Returns 0 if there is no element at the index or
if the index is out of bounds (index < 0 or index >= .size()).
Parameters:
-integer index - the index of this List to access.
Returns:
The element at the specified index, or 0 if there is no element at that index or if the index is
out of bounds.
------
method indexOf takes <TYPE> element returns integer
------
Returns the index of the first occurence of a certain element in this List. In other
words, returns the lowest-valued integer i such that List.getByIndex(i) == element would be
true. Returns -1 if there are no occurences of the specified element in this List.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
The index of the first occurence of the specified element in this List, or -1 if there are
no occurences of the specified element.
------
method isEmpty takes nothing returns boolean
------
Checks if this List is empty (has no elements).
Returns:
true if this List has no elements; false otherwise.
------
method iterator takes nothing returns <TYPE_NAME>Iterator
------
Returns an iterator that is capable of iterating through this List in order.
Returns:
An Iterator for this List.
See Also:
-<TYPE_NAME>Iterator
------
method listIterator takes nothing returns <TYPE_NAME>ListTypeIterator
------
Returns a ListTypeIterator for this List initialized to index 0.
Returns:
A ListTypeIterator for this List initialized to index 0.
See Also:
-<TYPE_NAME>List.listIteratorByIndex()
-<TYPE_NAME>ListTypeIterator
------
method listIteratorByIndex takes integer index returns <TYPE_NAME>ListTypeIterator
------
Returns a ListTypeIterator for this list starting at the specified index. An initial call to
ListTypeIterator.next() would return the element at the specified index, while an
initial call to ListTypeIterator.previous() would return the element at the specified index
minus one. This method returns 0 if the provided index is out of bounds (index < 0 or index >
.size()).
Parameters:
-integer index - the index at which the ListTypeIterator is initialized.
Returns:
A ListTypeIterator for this List initialized to the specified index, or 0 if the provided index
is out of bounds.
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove the first occurence (the one with the lowest-valued index) of the specified
element from this List. Returns true if the element was succesfully removed. Returns false if
this list does not contain any occurences of the specified element.
Parameters:
-<TYPE> toRemove - the element to remove from this List.
Returns:
true if the element was successfully removed; false otherwise.
------
method removeAll takes <TYPE_NAME>Collection collection returns nothing
------
Removes all elements of the specified collection from this List. If an element contained in the
other Collection exists more than once in this List, all occurrances of that element will be
removed from this List.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be removed from this
List.
------
method removeByIndex takes integer index returns $TYPE$
------
Attempts to remove and return the element at the specified index from this List. All elements
to the right will be shifted to the left (their indices are decremented by one). This method
returns 0 if there is no element at the specified index or if the index is out of bounds
(index < 0 or index >= .size()).
Parameters:
-integer index - the index of the element to be removed.
Returns:
The element that was removed from this List, or 0 if no element was removed.
------
method retainAll takes <TYPE_NAME>Collection collection returns nothing
------
Removes all elements from this List except for those elements also found in the specified
Collection. If an element contained in the other Collection occurs more than once in this List,
all occurrances of that element will be retained in this List.
Parameters:
-<TYPE_NAME>Collection collection - the Collection whose elements are to be retained in this
List.
------
method setByIndex takes integer index, <TYPE> toSet returns <TYPE>
------
Replaces the element at the specified index in this List to the specified element. Returns the
element previously located at the specified index. This method does nothing and returns 0 if the
index is out of bounds (index < 0 or index >= .size()).
Parameters:
-integer index - the index at which the replacement is to occur.
-<TYPE> toSet - the new element to be placed in the List.
Returns:
The element previously located at the specified index, or 0 if there was no such element or
if the index is out of bounds.
------
method swap takes integer index1, integer index2 returns nothing
------
Swaps the elements located at two given indices. This method does nothing if either index is
out of bounds (index < 0 or index >= .size()).
Parameters:
-integer index1 - the index of the first element to be swapped
-integer index2 - the index of the second element to be swapped
------
method toArray takes nothing returns <TYPE_NAME>Array
------
Returns an array representation of this List. The array is not backed by this List,
so any changes made to the array will not be reflected in the List, and any changes
made to the List after this method is called will not be reflected in the array. The ordering of
the elements in the array is the same as the ordering of the elements in this List.
Returns:
An array representation of this List, not backed by the List.
==============================================================
struct <TYPE_NAME>ListTypeIterator extends <TYPE_NAME>Iterator
==============================================================
A type of iterator that provides greater functionality tailored for lists. List iterators
sit at positions in between two adjacent indices of their lists. ListTypeIterator.next() returns
the element in the list at the index to the right, while ListTypeIterator.previous() returns the
element at the index to the left. Because of the ability of the list iterator to traverse a
list both in the forward and reverse direction, the same occurence of the same element may
be returned more than once (unlike a normal iterator).
------
method add takes <TYPE> toAdd returns nothing
------
Adds the specified element to the index immediately before the index of the element that would
be returned by a call to ListTypeIterator.next().
Parameters:
-<TYPE> toAdd - the element to add to this ListTypeIterator's List.
------
method destroy takes nothing returns nothing
------
Destroys this ListTypeIterator.
------
method hasNext takes nothing returns boolean
------
Checks if this ListTypeIterator has more elements to return when traversing its List in the
forward direction (in other words, if ListTypeIterator.next() != 0).
Returns:
true if this ListTypeIterator has more elements to return when traversing its List in the
forward direction; false otherwise.
------
method hasPrevious takes nothing returns boolean
------
Checks if this ListTypeIterator has more elements to return when traversing its List in the
reverse direction (in other words, if ListTypeIterator.previous() != 0).
Returns:
true if this ListTypeIterator has more elements to return when traversing its List in the
reverse direction; false otherwise.
------
method next takes nothing returns <TYPE>
------
Returns the next element of this ListTypeIterator (the element located at the index to the right
of this ListTypeIterator's current position). This method returns 0 if the iterator has no
more elements to return in the forward direction.
Returns:
The next element of this ListTypeIterator, or 0 if the iterator has no more elements to
return in the forward direction.
------
method nextIndex takes nothing returns integer
------
Returns the index of the element that would be returned by a call to ListTypeIterator.next().
This method returns the size of the List if this iterator is at the end of the List.
Returns:
The index of the element that would be returned by a call to ListTypeIterator.next(), or the
size of this ListTypeIterator's List if this iterator is at the end of the List.
------
method previous takes nothing returns <TYPE>
------
Returns the previous element of this ListTypeIterator (the element located at the index to the
left of this ListTypeIterator's current position). This method returns 0 if the iterator has no
more elements to return in the reverse direction.
Returns:
The next element of this ListTypeIterator, or 0 if the iterator has no more elements to
return in the reverse direction.
------
method previousIndex takes nothing returns integer
------
Returns the index of the element that would be returned by a call to
ListTypeIterator.previous(). This method returns -1 if this iterator is at the beginning of the
List.
Returns:
The index of the element that would be returned by a call to ListTypeIterator.previous(), or -1
if this iterator is at the beginning of its List.
------
method remove takes nothing returns nothing
------
Removes the last element returned by this ListTypeIterator, whether it was by a call to
ListTypeIterator.next() or ListTypeIterator.previous(). This method does nothing if
ListTypeIterator.add() or ListTypeIterator.remove() has been called since the last call to
ListTypeIterator.next() or ListTypeIterator.previous().
------
method setElement takes <TYPE> toSet returns nothing
------
Replaces the last element returned by this ListTypeIterator, whether it was by a call to
ListTypeIterator.next() or ListTypeIterator.previous(), with the specified element. This method
does nothing if ListTypeIterator.add() or ListTypeIterator.remove() has been called since the
last call to ListTypeIterator.next() or ListTypeIterator.previous().
Parameters:
-<TYPE> toSet - the new element to insert into this ListTypeIterator's List.
//TESH.scrollpos=6
//TESH.alwaysfold=0
ArrayList
v0.6.0
=========
=========
Using ArrayList
===============
Requirements:
-------------
-List
To implement ArrayList, you first need to implement List (see the List documentation).
After you have implemented List for a data type, you may then implement ArrayList for the
same data type.
The textmacro to implement ArrayList is named: "ArrayList"
The textmacro must be run outside of any library or scope.
For example, to create list objects that support the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro ArrayList("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
Each ArrayList can have a maximum of 2^24, or 16777215 elements.
Tips for Use:
-------------
Use ArrayList if you want a variable-sized dynamic array that implements the Collection
interface. All the strengths (fast random searches by index) and weaknesses (slow searches
by element, slow addition and removal of elements) of arrays still apply to ArrayList.
===================================================
struct <TYPE_NAME>ArrayList extends <TYPE_NAME>List
===================================================
A list implemented to function efficiently as a re-sizable dynamic array. The maximum capacity
of an ArrayList is equal to 2^24, or 16777215 elements.
Instance Methods
================
Inherited From List:
-method addAll takes <TYPE_NAME>Collection collection returns nothing
-method clear takes nothing returns nothing
-method enum takes <TYPE_NAME>_Enum enum, integer data returns nothing
-method getByIndex takes integer index returns <TYPE>
-method isEmpty takes nothing returns boolean
-method iterator takes nothing returns <TYPE_NAME>Iterator
-method listIterator takes nothing returns <TYPE_NAME>ListTypeIterator
-method listIteratorByIndex takes integer index returns <TYPE_NAME>ListTypeIterator
-method removeAll takes <TYPE_NAME>Collection collection returns nothing
-method retainAll takes <TYPE_NAME>Collection collection returns nothing
-method size takes nothing returns integer
-method toArray takes nothing returns <TYPE_NAME>Array
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to the end of this List. Returns true if the element was successfully
added. Returns false if this List has reached its capacity. This method is guaranteed to run in
constant time.
Parameters:
-<TYPE> toAdd - the element to add to this List.
Returns:
true if the element was successfully added; false otherwise.
------
method addByIndex takes integer index, <TYPE> toAdd returns nothing
------
Attempts to add an element to this List at the specified index. The previous element at that
index, if there is one, and any elements to the right will be shifted to the right (their
indices are incremented up by one). This method does nothing if this List has reached its
capacity or if the index is out of bounds (index < 0 or index > List.size()). This time
complexity of this method depends on the number of elements to the right of the specified index.
Parameters:
-integer index - the index at which to add the new element.
-<TYPE> toAdd - the element to add to this List.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element occurs at least once in this List. This method runs in O(n) time.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
true if the specified element is in this List; false otherwise.
------
method indexOf takes <TYPE> element returns integer
------
Returns the index of the first ocurrence of a certain element in this List. In other
words, returns the lowest-valued integer i such that List.getByIndex(i) == element would be
true. Returns -1 if there are no ocurrences of the specified element in this List. This method
runs in O(n) time.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
The index of the first ocurrence of the specified element in this List, or -1 if there are
no ocurrences of the specified element.
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove the first ocurrence (the one with the lowest-valued index) of the specified
element from this List. Returns true if the element was succesfully removed. Returns false if
this list does not contain any ocurrences of the specified element. This method runs in O(n)
time.
Parameters:
-<TYPE> toRemove - the element to remove from this List.
Returns:
true if the element was successfully removed; false otherwise.
------
method removeByIndex takes integer index returns $TYPE$
------
Attempts to remove and return the element at the specified index from this List. All elements
to the right will be shifted to the left (their indices are decremented by one). This method
returns 0 if there is no element at the specified index or if the index is out of bounds
(index < 0 or index >= .size()). The time complexity of this method depends on the number of
elements to the right of the specified index.
Parameters:
-integer index - the index of the element to be removed.
Returns:
The element that was removed from this List, or 0 if no element was removed.
------
method setByIndex takes integer index, <TYPE> toSet returns <TYPE>
------
Replaces the element at the specified index in this List to the specified element. Returns the
element previously located at the specified index. This method does nothing and returns 0 if the
index is out of bounds (index < 0 or index >= .size()). This method is guaranteed to run in
constant time.
Parameters:
-integer index - the index at which the replacement is to occur.
-<TYPE> toSet - the new element to be placed in the List.
Returns:
The element previously located at the specified index, or 0 if there was no such element or
if the index is out of bounds.
------
method swap takes integer index1, integer index2 returns nothing
------
Swaps the elements located at two given indices. This method does nothing if either index is
out of bounds (index < 0 or index >= .size()). This method is guaranteed to run in constant
time.
Parameters:
-integer index1 - the index of the first element to be swapped
-integer index2 - the index of the second element to be swapped
//TESH.scrollpos=18
//TESH.alwaysfold=0
LinkedList
v0.6.0
==========
==========
Using LinkedList
================
Requirements:
-------------
-List
To implement LinkedList, you first need to implement List (see the List documentation).
After you have implemented List for a data type, you may then implement LinkedList for the
same data type.
The textmacro to implement LinkedList is named: "LinkedList"
The textmacro must be run outside of any library or scope.
For example, to create list objects that support the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro LinkedList("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
Maximum of 8190 - [number of instantiated LinkedList objects for a given data type] * 2 elements
in all LinkedList objects for a given data type at any one time.
Tips for Use:
-------------
Use LinkedList if you wish to use a stack, queue, or double-ended queue, or if you mainly plan
to use the operations associated with these three structures. Namely, LinkedList is specially-
tailored to be efficient at adding and removing elements from the front and back of the list.
Search operations by either index or element, as well as addition and removal by index, are
slow for LinkedList.
====================================================
struct <TYPE_NAME>LinkedList extends <TYPE_NAME>List
====================================================
A doubly-linked list that can function as a queue, stack, or double-ended queue efficiently. Any
operation that traverses into the list by index will operate in O(n/2) time (the implementation
traverses from either the beginning or end depending on which is closer). Search operations
operate in O(n) time.
Instance Methods
================
Inherited From List:
-method addAll takes <TYPE_NAME>Collection collection returns nothing
-method enum takes <TYPE_NAME>_Enum enum, integer data returns nothing
-method isEmpty takes nothing returns boolean
-method iterator takes nothing returns <TYPE_NAME>Iterator
-method listIterator takes nothing returns <TYPE_NAME>ListTypeIterator
-method listIteratorByIndex takes integer index returns <TYPE_NAME>ListTypeIterator
-method removeAll takes <TYPE_NAME>Collection collection returns nothing
-method retainAll takes <TYPE_NAME>Collection collection returns nothing
-method size takes nothing returns integer
-method toArray takes nothing returns <TYPE_NAME>Array
------
method add takes <TYPE> toAdd returns boolean
------
Attempts to add an element to the end of this List. Returns true if the element was successfully
added. This method is guaranteed to run in constant time.
Parameters:
-<TYPE> toAdd - the element to add to this List.
Returns:
true if the element was successfully added; false otherwise.
------
method addByIndex takes integer index, <TYPE> toAdd returns nothing
------
Attempts to add an element to this List at the specified index. The previous element at that
index, if there is one, and any elements to the right will be shifted to the right (their
indices are incremented up by one). This method does nothing if the index is out of bounds
(index < 0 or index > List.size()). This method runs in O(n/2) time.
Parameters:
-integer index - the index at which to add the new element.
-<TYPE> toAdd - the element to add to this List.
------
method addFirst takes <TYPE> toAdd returns nothing
------
Adds the specified element to the beginning of this List. This method is guaranteed to run in
constant time.
Parameters:
-<TYPE> toAdd - the element to add to the beginning of this List.
------
method addLast takes <TYPE> toAdd returns nothing
------
Adds the specified element to the end of this List. This method is guaranteed to run in constant
time.
Parameters:
-<TYPE> toAdd - the element to add to the end of this List.
------
method clear takes nothing returns nothing
------
Removes all elements from this List. This method runs in O(n) time.
------
method contains takes <TYPE> element returns boolean
------
Checks if a certain element occurs at least once in this List. This method runs in O(n) time.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
true if the specified element is in this List; false otherwise.
------
method getByIndex takes integer index returns <TYPE>
------
Returns the element at the specified index. Returns 0 if there is no element at the index or
if the index is out of bounds (index < 0 or index >= .size()). This method runs in O(n/2) time.
Parameters:
-integer index - the index of this List to access.
Returns:
The element at the specified index, or 0 if there is no element at that index or if the index is
out of bounds.
------
method indexOf takes <TYPE> element returns integer
------
Returns the index of the first ocurrence of a certain element in this List. In other
words, returns the lowest-valued integer i such that List.getByIndex(i) == element would be
true. Returns -1 if there are no ocurrences of the specified element in this List. This method
runs in O(n) time.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
The index of the first ocurrence of the specified element in this List, or -1 if there are
no ocurrences of the specified element.
------
method lastIndexOf takes <TYPE> element returns integer
------
Returns the index of the last occurrance of the specified element in this List. In other words,
returns the highest-valued integer i such that List.getByIndex(i) == element would return true.
This method returns -1 if there are no occurrances of the specified element in this List. This
method runs in O(n) time.
Parameters:
-<TYPE> element - the element for which to search in this List.
Returns:
The index of the last ocurrence of the specified element in this List, or -1 if there are
no ocurrences of the specified element in this List.
------
method remove takes <TYPE> toRemove returns boolean
------
Attempts to remove the first ocurrence (the one with the lowest-valued index) of the specified
element from this List. Returns true if the element was succesfully removed. Returns false if
this list does not contain any ocurrences of the specified element. This method runs in O(n)
time.
Parameters:
-<TYPE> toRemove - the element to remove from this List.
Returns:
true if the element was successfully removed; false otherwise.
------
method removeByIndex takes integer index returns $TYPE$
------
Attempts to remove and return the element at the specified index from this List. All elements
to the right will be shifted to the left (their indices are decremented by one). This method
returns 0 if there is no element at the specified index or if the index is out of bounds
(index < 0 or index >= .size()). This method runs in O(n/2) time.
Parameters:
-integer index - the index of the element to be removed.
Returns:
The element that was removed from this List, or 0 if no element was removed.
------
method setByIndex takes integer index, <TYPE> toSet returns <TYPE>
------
Replaces the element at the specified index in this List to the specified element. Returns the
element previously located at the specified index. This method does nothing and returns 0 if the
index is out of bounds (index < 0 or index >= .size()). This method runs in O(n/2) time.
Parameters:
-integer index - the index at which the replacement is to occur.
-<TYPE> toSet - the new element to be placed in the List.
Returns:
The element previously located at the specified index, or 0 if there was no such element or
if the index is out of bounds.
------
method swap takes integer index1, integer index2 returns nothing
------
Swaps the elements located at two given indices. This method does nothing if either index is
out of bounds (index < 0 or index >= .size()). This method runs in O(n) time.
Parameters:
-integer index1 - the index of the first element to be swapped
-integer index2 - the index of the second element to be swapped
//TESH.scrollpos=186
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ Collection +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro Collection takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$Collection
function interface $TYPE_NAME$_Enum takes $TYPE$ this, integer data returns nothing
function interface $TYPE_NAME$_Comparator takes $TYPE$ x, $TYPE$ y returns integer
struct $TYPE_NAME$Array
private static hashtable t
private integer n
static method create takes integer n returns thistype
local thistype this = thistype.allocate()
set this.n = IMaxBJ(0, n)
return this
endmethod
method size takes nothing returns integer
return .n
endmethod
method operator [] takes integer index returns $TYPE$
if (index >= 0 and index < .n) then
return LoadInteger(.t, this, index)
endif
return 0
endmethod
method operator []= takes integer index, $TYPE$ e returns nothing
if (index >= 0 and index < .n) then
call SaveInteger(.t, this, index, e)
endif
endmethod
method destroy takes nothing returns nothing
call FlushChildHashtable(this.t, this)
call this.deallocate()
endmethod
implement optional $TYPE_NAME$ArraySort
implement optional $TYPE_NAME$ArrayStableSort
implement optional $TYPE_NAME$ArraySelection
implement optional $TYPE_NAME$ArrayBinarySearch
private static method onInit takes nothing returns nothing
set .t = InitHashtable()
endmethod
endstruct
private interface CollectionInterface
method add takes $TYPE$ toAdd returns boolean defaults false
method addAll takes $TYPE_NAME$Collection collection returns nothing defaults nothing
method remove takes $TYPE$ toRemove returns boolean defaults false
method removeAll takes $TYPE_NAME$Collection collection returns nothing defaults nothing
method retainAll takes $TYPE_NAME$Collection collection returns nothing defaults nothing
method contains takes $TYPE$ element returns boolean defaults false
method size takes nothing returns integer defaults 0
method isEmpty takes nothing returns boolean defaults false
method clear takes nothing returns nothing defaults nothing
method toArray takes nothing returns $TYPE_NAME$Array defaults 0
method iterator takes nothing returns $TYPE_NAME$Iterator defaults 0
method enum takes $TYPE_NAME$_Enum enum, integer data returns nothing defaults nothing
endinterface
interface $TYPE_NAME$Iterator
method next takes nothing returns $TYPE$
method hasNext takes nothing returns boolean
method remove takes nothing returns nothing
endinterface
struct $TYPE_NAME$Collection extends CollectionInterface
implement optional $TYPE_NAME$CollectionSelection
stub method addAll takes $TYPE_NAME$Collection collection returns nothing
call this.addAllHelper.evaluate(collection)
endmethod
stub method removeAll takes $TYPE_NAME$Collection collection returns nothing
call this.removeAllHelper.evaluate(collection)
endmethod
stub method retainAll takes $TYPE_NAME$Collection collection returns nothing
call this.retainAllHelper.evaluate(collection)
endmethod
private method addAllHelper takes $TYPE_NAME$Collection collection returns nothing
local $TYPE_NAME$Iterator it = collection.iterator()
loop
exitwhen (it.hasNext() == false)
call this.add(it.next())
endloop
call it.destroy()
endmethod
private method removeAllHelper takes $TYPE_NAME$Collection collection returns nothing
local $TYPE_NAME$Iterator it = this.iterator()
loop
exitwhen (it.hasNext() == false)
if (collection.contains(it.next())) then
call it.remove()
endif
endloop
call it.destroy()
endmethod
private method retainAllHelper takes $TYPE_NAME$Collection collection returns nothing
local $TYPE_NAME$Iterator it = this.iterator()
loop
exitwhen (it.hasNext() == false)
if (collection.contains(it.next()) == false) then
call it.remove()
endif
endloop
call it.destroy()
endmethod
endstruct
endlibrary
library $TYPE_NAME$Set requires $TYPE_NAME$Collection
struct $TYPE_NAME$Set extends $TYPE_NAME$Collection
endstruct
endlibrary
library $TYPE_NAME$List requires $TYPE_NAME$Collection
struct $TYPE_NAME$ListTypeIterator extends $TYPE_NAME$Iterator
stub method next takes nothing returns $TYPE$
return 0
endmethod
stub method hasNext takes nothing returns boolean
return false
endmethod
stub method nextIndex takes nothing returns integer
return 0
endmethod
stub method previous takes nothing returns $TYPE$
return 0
endmethod
stub method hasPrevious takes nothing returns boolean
return false
endmethod
stub method previousIndex takes nothing returns integer
return 0
endmethod
stub method add takes $TYPE$ toAdd returns nothing
endmethod
stub method remove takes nothing returns nothing
endmethod
stub method setElement takes $TYPE$ toSet returns nothing
endmethod
endstruct
struct $TYPE_NAME$List extends $TYPE_NAME$Collection
stub method addByIndex takes integer index, $TYPE$ toAdd returns nothing
endmethod
stub method setByIndex takes integer index, $TYPE$ toSet returns $TYPE$
return 0
endmethod
stub method getByIndex takes integer index returns $TYPE$
return 0
endmethod
stub method removeByIndex takes integer index returns $TYPE$
return 0
endmethod
stub method swap takes integer index1, integer index2 returns nothing
endmethod
stub method indexOf takes $TYPE$ element returns integer
return -1
endmethod
stub method listIteratorByIndex takes integer index returns $TYPE_NAME$ListTypeIterator
return 0
endmethod
stub method listIterator takes nothing returns $TYPE_NAME$ListTypeIterator
return .listIteratorByIndex(0)
endmethod
implement optional $TYPE_NAME$ListSort
implement optional $TYPE_NAME$ListStableSort
implement optional $TYPE_NAME$ListSelection
implement optional $TYPE_NAME$ListBinarySearch
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=9
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ LinkedHashSet +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro LinkedHashSet takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$LinkedHashSet requires $TYPE_NAME$Set
private keyword HEAD
private keyword TAIL
private keyword t
private keyword $TYPE_NAME$LinkedHashSetIterator
struct $TYPE_NAME$LinkedHashSet extends $TYPE_NAME$Set
static constant integer HEAD = -0x30f648cd
static constant integer TAIL = -0xb659f432
static hashtable t
private integer n = 0
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
call SaveInteger(thistype.t, this, thistype.HEAD, thistype.TAIL)
call SaveInteger(thistype.t, -this, thistype.TAIL, thistype.HEAD)
return this
endmethod
method size takes nothing returns integer
return this.n
endmethod
method contains takes $TYPE$ element returns boolean
return HaveSavedInteger(thistype.t, this, element)
endmethod
method add takes $TYPE$ toAdd returns boolean
debug call BJDebugMsg("$TYPE_NAME$LinkedHashSet " + I2S(this) + ": Attempting to add " + I2S(toAdd) + ".")
if (this.contains(toAdd)) then
debug call BJDebugMsg("$TYPE_NAME$LinkedHashSet " + I2S(this) + ": Element addition failed: " + I2S(toAdd) + " is already in the set.")
return false
endif
call SaveInteger(thistype.t, this, LoadInteger(thistype.t, -this, thistype.TAIL), toAdd)
call SaveInteger(thistype.t, this, toAdd, thistype.TAIL)
call SaveInteger(thistype.t, -this, toAdd, LoadInteger(thistype.t, -this, thistype.TAIL))
call SaveInteger(thistype.t, -this, thistype.TAIL, toAdd)
set this.n = this.n + 1
debug call BJDebugMsg("$TYPE_NAME$LinkedHashSet " + I2S(this) + ": Element addition succeeded: " + I2S(toAdd) + " is now at index " + I2S(this.n - 1) + ".")
return true
endmethod
method remove takes $TYPE$ toRemove returns boolean
local integer next
local integer prev
debug call BJDebugMsg("$TYPE_NAME$LinkedHashSet " + I2S(this) + ": Attempting to remove " + I2S(toRemove) + ".")
if (this.contains(toRemove) == false) then
debug call BJDebugMsg("$TYPE_NAME$LinkedHashSet " + I2S(this) + ": Element removal failed: " + I2S(toRemove) + " is not in the set.")
return false
endif
set next = LoadInteger(thistype.t, this, toRemove)
set prev = LoadInteger(thistype.t, -this, toRemove)
call RemoveSavedInteger(thistype.t, this, toRemove)
call RemoveSavedInteger(thistype.t, -this, toRemove)
call SaveInteger(thistype.t, -this, next, prev)
call SaveInteger(thistype.t, this, prev, next)
set this.n = this.n - 1
debug call BJDebugMsg("$TYPE_NAME$LinkedHashSet " + I2S(this) + ": Element removal succeeded: " + I2S(toRemove) + " was removed from index " + I2S(this.t[toRemove]) + ".")
return true
endmethod
method isEmpty takes nothing returns boolean
return this.n == 0
endmethod
method clear takes nothing returns nothing
set this.n = 0
call FlushChildHashtable(thistype.t, this)
call FlushChildHashtable(thistype.t, -this)
call SaveInteger(thistype.t, this, thistype.HEAD, thistype.TAIL)
call SaveInteger(thistype.t, -this, thistype.TAIL, thistype.HEAD)
endmethod
method toArray takes nothing returns $TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $TYPE_NAME$Iterator
return $TYPE_NAME$LinkedHashSetIterator.create.evaluate(this)
endmethod
method enum takes $TYPE_NAME$_Enum enum, integer data returns nothing
local integer temp = thistype.HEAD
local $TYPE$ e
loop
set temp = LoadInteger(thistype.t, this, temp)
exitwhen (temp == thistype.TAIL)
set e = temp
call enum.evaluate(e, data)
endloop
endmethod
private method toArrayHelper takes nothing returns $TYPE_NAME$Array
local $TYPE_NAME$Array ar = $TYPE_NAME$Array.create(this.size())
local integer temp = $TYPE_NAME$LinkedHashSet.HEAD
local integer i = 0
loop
set temp = LoadInteger($TYPE_NAME$LinkedHashSet.t, this, temp)
exitwhen (temp == $TYPE_NAME$LinkedHashSet.TAIL)
set ar[i] = temp
set i = i + 1
endloop
return ar
endmethod
method destroy takes nothing returns nothing
call FlushChildHashtable(thistype.t, this)
call FlushChildHashtable(thistype.t, -this)
call this.deallocate()
endmethod
private static method onInit takes nothing returns nothing
set thistype.t = InitHashtable()
endmethod
endstruct
private struct $TYPE_NAME$LinkedHashSetIterator extends $TYPE_NAME$Iterator
private $TYPE_NAME$LinkedHashSet s
private $TYPE$ last = 0
private integer n
static method create takes $TYPE_NAME$LinkedHashSet s returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.n = LoadInteger($TYPE_NAME$LinkedHashSet.t, s, $TYPE_NAME$LinkedHashSet.HEAD)
return this
endmethod
method hasNext takes nothing returns boolean
return this.n != $TYPE_NAME$LinkedHashSet.TAIL
endmethod
method next takes nothing returns $TYPE$
if (this.hasNext()) then
set this.last = this.n
set this.n = LoadInteger($TYPE_NAME$LinkedHashSet.t, this.s, this.last)
return this.last
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != 0) then
call this.s.remove(this.last)
set this.last = 0
endif
endmethod
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=78
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ TreeSet +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro TreeSet takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$TreeSet requires $TYPE_NAME$Set
private keyword $TYPE_NAME$TreeSetIterator
private struct $TYPE_NAME$TreeSetNode
static constant integer BLACK = 0
static constant integer RED = 1
static thistype sentinelNode
$TYPE_NAME$TreeSet s
$TYPE$ e
thistype parent
thistype left
thistype right
integer color
static method create takes $TYPE_NAME$TreeSet s, integer color, $TYPE$ e, thistype parent, boolean sentinel returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.color = color
set this.e = e
set this.parent = parent
if (sentinel == false) then
set this.left = thistype.sentinelNode //thistype.create(s, thistype.BLACK, 0, this, true)
set this.right = thistype.sentinelNode //thistype.create(s, thistype.BLACK, 0, this, true)
endif
return this
endmethod
method operator sentinel takes nothing returns boolean
return this == thistype.sentinelNode
endmethod
method operator sibling takes nothing returns thistype
if (.parent != 0) then
if (this == .parent.left) then
return .parent.right
else
return .parent.left
endif
endif
return 0
endmethod
method operator grandparent takes nothing returns thistype
if (.parent != 0) then
return .parent.parent
endif
return 0
endmethod
method operator uncle takes nothing returns thistype
local thistype grandparent = .grandparent
if (grandparent != 0) then
if (.parent == grandparent.left) then
return grandparent.right
else
return grandparent.left
endif
endif
return 0
endmethod
method rotateLeft takes nothing returns nothing
if (.parent != 0) then
if (this == .parent.left) then
set .parent.left = .right
else
set .parent.right = .right
endif
endif
set .right.parent = .parent
set .parent = .right
set .right = .parent.left
set .parent.left = this
if (.right.sentinel == false) then
set .right.parent = this
endif
if (this == .s.root) then
set .s.root = .parent
endif
endmethod
method rotateRight takes nothing returns nothing
if (.parent != 0) then
if (this == .parent.left) then
set .parent.left = .left
else
set .parent.right = .left
endif
endif
set .left.parent = .parent
set .parent = .left
set .left = .parent.right
set .parent.right = this
if (.left.sentinel == false) then
set .left.parent = this
endif
if (this == .s.root) then
set .s.root = .parent
endif
endmethod
private method inOrderSuccessorHelper takes nothing returns $TYPE_NAME$TreeSetNode
if (this.sentinel) then
return 0
elseif (this.left.sentinel) then
return this
endif
return this.left.inOrderSuccessorHelper()
endmethod
method getInOrderSuccessor takes nothing returns $TYPE_NAME$TreeSetNode
if (this.sentinel) then
return 0
endif
return this.right.inOrderSuccessorHelper.evaluate()
endmethod
private static method onInit takes nothing returns nothing
set thistype.sentinelNode = thistype.create(0, thistype.BLACK, 0, 0, true)
endmethod
endstruct
struct $TYPE_NAME$TreeSet extends $TYPE_NAME$Set
$TYPE_NAME$TreeSetNode root = 0
integer n = 0
$TYPE_NAME$_Comparator comparator = 0
static method create takes $TYPE_NAME$_Comparator comparator returns thistype
local thistype this = thistype.allocate()
set this.comparator = comparator
return this
endmethod
method add takes $TYPE$ toAdd returns boolean
if (this.root == 0) then
set this.root = $TYPE_NAME$TreeSetNode.create(this, $TYPE_NAME$TreeSetNode.BLACK, toAdd, 0, false)
else
if (this.addNode.evaluate(toAdd, this.root) == false) then
return false
endif
endif
set this.n = this.n + 1
return true
endmethod
method remove takes $TYPE$ toRemove returns boolean
local $TYPE_NAME$TreeSetNode node = this.findNode.evaluate(toRemove, this.root)
if (node != 0) then
call this.removeNode.evaluate(node)
set this.n = this.n - 1
return true
endif
return false
endmethod
method contains takes $TYPE$ element returns boolean
return this.findNode.evaluate(element, this.root) != 0
endmethod
method size takes nothing returns integer
return this.n
endmethod
method isEmpty takes nothing returns boolean
return this.n == 0
endmethod
method clear takes nothing returns nothing
call this.clearHelper.evaluate(this.root)
set this.root = 0
set this.n = 0
endmethod
method toArray takes nothing returns $TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $TYPE_NAME$Iterator
return $TYPE_NAME$TreeSetIterator.create.evaluate(this)
endmethod
method enum takes $TYPE_NAME$_Enum enum, integer data returns nothing
if (this.n == 0) then
return
endif
call this.enumHelper.evaluate(this.root, enum, data)
endmethod
method destroy takes nothing returns nothing
call this.clear()
call this.deallocate()
endmethod
private stub method addNode takes $TYPE$ element, $TYPE_NAME$TreeSetNode node returns boolean
local integer order
if (this.comparator != 0) then
set order = this.comparator.evaluate(element, node.e)
else
if (element < node.e) then
set order = -1
elseif (element > node.e) then
set order = 1
else
set order = 0
endif
endif
if (order < 0) then
if (node.left.sentinel) then
set node.left = $TYPE_NAME$TreeSetNode.create(this, $TYPE_NAME$TreeSetNode.RED, element, node, false)
return true
else
return this.addNode(element, node.left)
endif
elseif (order > 0) then
if (node.right.sentinel) then
set node.right = $TYPE_NAME$TreeSetNode.create(this, $TYPE_NAME$TreeSetNode.RED, element, node, false)
return true
else
return this.addNode(element, node.right)
endif
endif
return false
endmethod
private method findNode takes $TYPE$ element, $TYPE_NAME$TreeSetNode node returns $TYPE_NAME$TreeSetNode
local integer order
if (node == 0) then
return 0
elseif (node.sentinel) then
return 0
endif
if (this.comparator != 0) then
set order = this.comparator.evaluate(element, node.e)
else
if (element < node.e) then
set order = -1
elseif (element > node.e) then
set order = 1
else
set order = 0
endif
endif
if (order < 0) then
return this.findNode(element, node.left)
elseif (order > 0) then
return this.findNode(element, node.right)
else
return node
endif
endmethod
private stub method removeNode takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode successor
if (node.right.sentinel == false and node.left.sentinel == false) then
set successor = node.getInOrderSuccessor()
set node.e = successor.e
call this.removeNode.evaluate(successor)
elseif (node.right.sentinel and node.left.sentinel) then
if (node != this.root) then
if (node == node.parent.left) then
set node.parent.left = $TYPE_NAME$TreeSetNode.sentinelNode
else
set node.parent.right = $TYPE_NAME$TreeSetNode.sentinelNode
endif
else
set this.root = 0
endif
call node.destroy()
else
if (node.right.sentinel) then
if (node.parent.left == node) then
set node.parent.left = node.left
else
set node.parent.right = node.left
endif
set node.left.parent = node.parent
if (node == this.root) then
set this.root = node.left
endif
else
if (node.parent.left == node) then
set node.parent.left = node.right
else
set node.parent.right = node.right
endif
set node.right.parent = node.parent
if (node == this.root) then
set this.root = node.right
endif
endif
call node.destroy()
endif
endmethod
private method clearHelper takes $TYPE_NAME$TreeSetNode node returns nothing
if (node == 0) then
return
endif
if (node.left.sentinel == false) then
call this.clearHelper(node.left)
endif
if (node.right.sentinel == false) then
call this.clearHelper(node.right)
endif
call node.destroy()
endmethod
private method enumHelper takes $TYPE_NAME$TreeSetNode node, $TYPE_NAME$_Enum enum, integer data returns nothing
if (node.left.sentinel == false) then
call this.enumHelper(node.left, enum, data)
endif
call enum.evaluate(node.e, data)
if (node.right.sentinel == false) then
call this.enumHelper(node.right, enum, data)
endif
endmethod
private method toArrayHelper takes nothing returns $TYPE_NAME$Array
local $TYPE_NAME$TreeSetNode node = this.root
local $TYPE_NAME$Array ar
local integer n = 0
if (node == 0) then
return 0
endif
set ar = $TYPE_NAME$Array.create(this.size())
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
loop
set ar[n] = node.e
set n = n + 1
if (node.right.sentinel == false) then
set node = node.right
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
else
loop
exitwhen (node.parent == 0 or node != node.parent.right)
set node = node.parent
endloop
set node = node.parent
endif
exitwhen (node.sentinel or node == 0)
endloop
return ar
endmethod
endstruct
struct $TYPE_NAME$BalancedTreeSet extends $TYPE_NAME$TreeSet
private method addNode takes $TYPE$ element, $TYPE_NAME$TreeSetNode node returns boolean
local integer order
if (this.comparator != 0) then
set order = this.comparator.evaluate(element, node.e)
else
if (element < node.e) then
set order = -1
elseif (element > node.e) then
set order = 1
else
set order = 0
endif
endif
if (order < 0) then
if (node.left.sentinel) then
set node.left = $TYPE_NAME$TreeSetNode.create(this, $TYPE_NAME$TreeSetNode.RED, element, node, false)
call this.insertCaseOne.evaluate(node.left)
return true
else
return this.addNode(element, node.left)
endif
elseif (order > 0) then
if (node.right.sentinel) then
set node.right = $TYPE_NAME$TreeSetNode.create(this, $TYPE_NAME$TreeSetNode.RED, element, node, false)
call this.insertCaseOne.evaluate(node.right)
return true
else
return this.addNode(element, node.right)
endif
endif
return false
endmethod
private method removeNode takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode successor
if (node.right.sentinel == false and node.left.sentinel == false) then
set successor = node.getInOrderSuccessor()
set node.e = successor.e
call this.removeNode.evaluate(successor)
elseif (node.right.sentinel and node.left.sentinel) then
if (node != this.root) then
if (node == node.parent.left) then
set node.parent.left = $TYPE_NAME$TreeSetNode.sentinelNode
else
set node.parent.right = $TYPE_NAME$TreeSetNode.sentinelNode
endif
else
set this.root = 0
endif
call node.destroy()
else
if (node.color == $TYPE_NAME$TreeSetNode.RED) then
if (node.right.sentinel) then
if (node.parent.left == node) then
set node.parent.left = node.left
else
set node.parent.right = node.left
endif
set node.left.parent = node.parent
if (node == this.root) then
set this.root = node.left
endif
else
if (node.parent.left == node) then
set node.parent.left = node.right
else
set node.parent.right = node.right
endif
set node.right.parent = node.parent
if (node == this.root) then
set this.root = node.right
endif
endif
else
if (node.right.sentinel) then
if (node.parent.left == node) then
set node.parent.left = node.left
else
set node.parent.right = node.left
endif
set node.left.parent = node.parent
if (node == this.root) then
set this.root = node.left
endif
if (node.left.color == $TYPE_NAME$TreeSetNode.RED) then
set node.left.color = $TYPE_NAME$TreeSetNode.BLACK
else
call this.removeCaseOne.evaluate(node.left)
endif
else
if (node.parent.left == node) then
set node.parent.left = node.right
else
set node.parent.right = node.right
endif
set node.right.parent = node.parent
if (node == this.root) then
set this.root = node.right
endif
if (node.right.color == $TYPE_NAME$TreeSetNode.RED) then
set node.right.color = $TYPE_NAME$TreeSetNode.BLACK
else
call this.removeCaseOne.evaluate(node.right)
endif
endif
endif
call node.destroy()
endif
endmethod
private method insertCaseFive takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode grandparent = node.grandparent
set node.parent.color = $TYPE_NAME$TreeSetNode.BLACK
set grandparent.color = $TYPE_NAME$TreeSetNode.RED
if (node == node.parent.left and node.parent == grandparent.left) then
call grandparent.rotateRight()
else
call grandparent.rotateLeft()
endif
endmethod
private method insertCaseFour takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode grandparent = node.grandparent
if (node == node.parent.right and node.parent == grandparent.left) then
call node.parent.rotateLeft()
set node = node.left
elseif (node == node.parent.left and node.parent == grandparent.right) then
call node.parent.rotateRight()
set node = node.right
endif
call this.insertCaseFive(node)
endmethod
private method insertCaseThree takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode uncle = node.uncle
local $TYPE_NAME$TreeSetNode grandparent = node.grandparent
if (uncle != 0) then
if (uncle.color == $TYPE_NAME$TreeSetNode.RED) then
set node.parent.color = $TYPE_NAME$TreeSetNode.BLACK
set uncle.color = $TYPE_NAME$TreeSetNode.BLACK
set grandparent.color = $TYPE_NAME$TreeSetNode.RED
call this.insertCaseOne.evaluate(grandparent)
else
call this.insertCaseFour(node)
endif
else
call this.insertCaseFour(node)
endif
endmethod
private method insertCaseTwo takes $TYPE_NAME$TreeSetNode node returns nothing
if (node.parent.color != $TYPE_NAME$TreeSetNode.BLACK) then
call this.insertCaseThree(node)
endif
endmethod
private method insertCaseOne takes $TYPE_NAME$TreeSetNode node returns nothing
if (node.parent == 0) then
set node.color = $TYPE_NAME$TreeSetNode.BLACK
else
call this.insertCaseTwo(node)
endif
endmethod
private method removeCaseSix takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode sibling = node.sibling
set sibling.color = node.parent.color
set node.parent.color = $TYPE_NAME$TreeSetNode.BLACK
if (node == node.parent.left) then
set sibling.right.color = $TYPE_NAME$TreeSetNode.BLACK
call node.parent.rotateLeft()
else
set sibling.left.color = $TYPE_NAME$TreeSetNode.BLACK
call node.parent.rotateRight()
endif
endmethod
private method removeCaseFive takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode sibling = node.sibling
if (sibling.color == $TYPE_NAME$TreeSetNode.BLACK) then
if (node == node.parent.left and sibling.right.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.left.color == $TYPE_NAME$TreeSetNode.RED) then
set sibling.color = $TYPE_NAME$TreeSetNode.RED
set sibling.left.color = $TYPE_NAME$TreeSetNode.BLACK
call sibling.rotateRight()
elseif (node == node.parent.right and sibling.left.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.right.color == $TYPE_NAME$TreeSetNode.RED) then
set sibling.color = $TYPE_NAME$TreeSetNode.RED
set sibling.right.color = $TYPE_NAME$TreeSetNode.BLACK
call sibling.rotateLeft()
endif
endif
call this.removeCaseSix(node)
endmethod
private method removeCaseFour takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode sibling = node.sibling
if (node.parent.color == $TYPE_NAME$TreeSetNode.RED and sibling.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.left.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.right.color == $TYPE_NAME$TreeSetNode.BLACK) then
set sibling.color = $TYPE_NAME$TreeSetNode.RED
set node.parent.color = $TYPE_NAME$TreeSetNode.BLACK
else
call this.removeCaseFive(node)
endif
endmethod
private method removeCaseThree takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode sibling = node.sibling
if (node.parent.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.left.color == $TYPE_NAME$TreeSetNode.BLACK and sibling.right.color == $TYPE_NAME$TreeSetNode.BLACK) then
set sibling.color = $TYPE_NAME$TreeSetNode.RED
call this.removeCaseOne.evaluate(node.parent)
else
call this.removeCaseFour(node)
endif
endmethod
private method removeCaseTwo takes $TYPE_NAME$TreeSetNode node returns nothing
local $TYPE_NAME$TreeSetNode sibling = node.sibling
if (sibling.color == $TYPE_NAME$TreeSetNode.RED) then
set node.parent.color = $TYPE_NAME$TreeSetNode.RED
set sibling.color = $TYPE_NAME$TreeSetNode.BLACK
if (node == node.parent.left) then
call node.parent.rotateLeft()
else
call node.parent.rotateRight()
endif
endif
call this.removeCaseThree(node)
endmethod
private method removeCaseOne takes $TYPE_NAME$TreeSetNode node returns nothing
if (node.parent != 0) then
call this.removeCaseTwo(node)
endif
endmethod
endstruct
struct $TYPE_NAME$TreeSetIterator extends $TYPE_NAME$Iterator
private $TYPE_NAME$TreeSet s
private $TYPE_NAME$Array ar
private integer n
private integer last = -1
private integer current = 0
static method create takes $TYPE_NAME$TreeSet s returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.ar = s.toArray()
set this.n = s.n
return this
endmethod
method hasNext takes nothing returns boolean
return this.current < this.n
endmethod
method next takes nothing returns $TYPE$
if (this.hasNext()) then
set this.last = this.current
set this.current = this.current + 1
return this.ar[this.last]
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != -1) then
call this.s.remove(this.ar[this.last])
set this.last = -1
endif
endmethod
method destroy takes nothing returns nothing
call this.ar.destroy()
call this.deallocate()
endmethod
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=12
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ ArrayList +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro ArrayList takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$ArrayList requires $TYPE_NAME$List
private keyword elements
private keyword $TYPE_NAME$ArrayListIterator
private keyword $TYPE_NAME$ArrayListListTypeIterator
struct $TYPE_NAME$ArrayList extends $TYPE_NAME$List
private $TYPE_NAME$Array e
private integer n = 0
implement optional $TYPE_NAME$ArrayListSort
implement optional $TYPE_NAME$ArrayListStableSort
implement optional $TYPE_NAME$ArrayListBinarySearch
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set this.e = $TYPE_NAME$Array.create(0x100000)
return this
endmethod
method operator elements takes nothing returns $TYPE_NAME$Array
return this.e
endmethod
method size takes nothing returns integer
return this.n
endmethod
method indexOf takes $TYPE$ element returns integer
return this.indexOfHelper.evaluate(element)
endmethod
method contains takes $TYPE$ element returns boolean
return this.indexOf(element) != -1
endmethod
method addByIndex takes integer index, $TYPE$ toAdd returns nothing
if (this.n < this.elements.size() and index <= this.n and index >= 0) then
call this.shiftRight.evaluate(index)
set this.elements[index] = toAdd
set this.n = this.n + 1
endif
endmethod
method add takes $TYPE$ toAdd returns boolean
if (this.n < this.elements.size()) then
set this.elements[this.n] = toAdd
set this.n = this.n + 1
return true
endif
return false
endmethod
method setByIndex takes integer index, $TYPE$ toSet returns $TYPE$
local $TYPE$ old = 0
if (index < this.n and index >= 0) then
set old = this.elements[index]
set this.elements[index] = toSet
endif
return old
endmethod
method getByIndex takes integer index returns $TYPE$
if (index < this.n and index >= 0) then
return this.elements[index]
endif
endmethod
method removeByIndex takes integer index returns $TYPE$
local integer i
local $TYPE$ toRemove = 0
if (index < this.n and index >= 0) then
set toRemove = this.elements[index]
call this.shiftLeft.evaluate(index)
set this.n = this.n - 1
endif
return toRemove
endmethod
method remove takes $TYPE$ toRemove returns boolean
local integer index = this.indexOf(toRemove)
if (index == -1) then
return false
endif
call this.removeByIndex(index)
return true
endmethod
method swap takes integer index1, integer index2 returns nothing
local $TYPE$ temp
if (index1 != index2 and index1 >= 0 and index1 < this.n and index2 >= 0 and index2 < this.n) then
set temp = this.elements[index1]
set this.elements[index1] = this.elements[index2]
set this.elements[index2] = temp
endif
endmethod
method isEmpty takes nothing returns boolean
return this.n == 0
endmethod
method clear takes nothing returns nothing
set this.n = 0
endmethod
method toArray takes nothing returns $TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $TYPE_NAME$Iterator
return $TYPE_NAME$ArrayListIterator.create.evaluate(this)
endmethod
method listIteratorByIndex takes integer index returns $TYPE_NAME$ListTypeIterator
if (index >= 0 and index <= this.n) then
return $TYPE_NAME$ArrayListListTypeIterator.create.evaluate(this, index)
endif
return 0
endmethod
method listIterator takes nothing returns $TYPE_NAME$ListTypeIterator
return this.listIteratorByIndex(0)
endmethod
method enum takes $TYPE_NAME$_Enum enum, integer data returns nothing
local integer i = 0
loop
exitwhen (i >= this.n)
call enum.evaluate(this.elements[i], data)
set i = i + 1
endloop
endmethod
method destroy takes nothing returns nothing
call this.elements.destroy()
call this.deallocate()
endmethod
private method shiftRight takes integer index returns nothing
local integer i = this.n - 1
loop
exitwhen (i < index)
set this.elements[i + 1] = this.elements[i]
set i = i - 1
endloop
endmethod
private method shiftLeft takes integer index returns nothing
local integer i = index
loop
exitwhen (index >= this.n - 1)
set this.elements[i] = this.elements[i + 1]
set i = i + 1
endloop
endmethod
private method indexOfHelper takes $TYPE$ element returns integer
local integer i = 0
loop
exitwhen (i >= this.n)
if (this.elements[i] == element) then
return i
endif
set i = i + 1
endloop
return -1
endmethod
private method toArrayHelper takes nothing returns $TYPE_NAME$Array
local $TYPE_NAME$Array ar = $TYPE_NAME$Array.create(this.n)
local integer i = 0
loop
exitwhen (i >= this.n)
set ar[i] = this.elements[i]
set i = i + 1
endloop
return ar
endmethod
endstruct
struct $TYPE_NAME$ArrayListIterator extends $TYPE_NAME$Iterator
private $TYPE_NAME$ArrayList l
private integer lastIndex = -1
private integer n = 0
static method create takes $TYPE_NAME$ArrayList l returns thistype
local thistype this = thistype.allocate()
set this.l = l
return this
endmethod
method hasNext takes nothing returns boolean
return this.n < this.l.size()
endmethod
method next takes nothing returns $TYPE$
if (this.hasNext()) then
set this.lastIndex = this.n
set this.n = this.n + 1
return this.l.elements[this.lastIndex]
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.lastIndex >= 0 and this.lastIndex < this.l.size()) then
call this.l.removeByIndex(this.lastIndex)
set this.lastIndex = -1
set this.n = this.n - 1
endif
endmethod
endstruct
struct $TYPE_NAME$ArrayListListTypeIterator extends $TYPE_NAME$ListTypeIterator
private $TYPE_NAME$ArrayList l
private integer lastN = -1
private integer nextN
private integer prevN
static method create takes $TYPE_NAME$ArrayList l, integer index returns thistype
local thistype this = thistype.allocate()
set this.l = l
set this.nextN = index
set this.prevN = index - 1
return this
endmethod
method hasNext takes nothing returns boolean
return (this.nextN >= 0 and this.nextN < this.l.size())
endmethod
method hasPrevious takes nothing returns boolean
return (this.prevN >= 0 and this.prevN < this.l.size())
endmethod
method next takes nothing returns $TYPE$
if (this.hasNext()) then
set this.lastN = this.nextN
set this.nextN = this.nextN + 1
set this.prevN = this.prevN + 1
return this.l.elements[this.lastN]
endif
return 0
endmethod
method nextIndex takes nothing returns integer
return this.nextN
endmethod
method previous takes nothing returns $TYPE$
if (this.hasPrevious()) then
set this.lastN = this.prevN
set this.prevN = this.prevN - 1
set this.nextN = this.nextN - 1
return this.l.elements[this.lastN]
endif
return 0
endmethod
method previousIndex takes nothing returns integer
return this.prevN
endmethod
method add takes $TYPE$ toAdd returns nothing
if (this.l.size() < this.l.elements.size()) then
call this.l.addByIndex(this.nextN, toAdd)
set this.lastN = -1
set this.nextN = this.nextN + 1
set this.prevN = this.prevN + 1
endif
endmethod
method remove takes nothing returns nothing
if (this.lastN >= 0 and this.lastN < this.l.size()) then
call this.l.removeByIndex(this.lastN)
if (this.lastN == this.prevN) then
set this.nextN = this.nextN - 1
set this.prevN = this.prevN - 1
endif
set this.lastN = -1
endif
endmethod
method setElement takes $TYPE$ toSet returns nothing
if (this.lastN >= 0 and this.lastN < this.l.size()) then
call this.l.setByIndex(this.lastN, toSet)
endif
endmethod
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=6
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ LinkedList +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro LinkedList takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$LinkedList requires $TYPE_NAME$List
private keyword head
private keyword tail
private keyword $TYPE_NAME$LinkedListIterator
private keyword $TYPE_NAME$LinkedListListTypeIterator
private keyword addByItem
private keyword removeByItem
private keyword setByItem
private struct $TYPE_NAME$LinkedListItem
$TYPE$ e
thistype next
thistype prev
static method create takes $TYPE$ e, thistype next, thistype prev returns thistype
local thistype this = thistype.allocate()
set this.e = e
set this.next = next
set this.prev = prev
return this
endmethod
endstruct
struct $TYPE_NAME$LinkedList extends $TYPE_NAME$List
$TYPE_NAME$LinkedListItem head
$TYPE_NAME$LinkedListItem tail
integer n = 0
implement optional $TYPE_NAME$LinkedListSort
implement optional $TYPE_NAME$LinkedListStableSort
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set this.head = $TYPE_NAME$LinkedListItem.create(0, 0, 0)
set this.tail = $TYPE_NAME$LinkedListItem.create(0, 0, 0)
set this.head.next = this.tail
set this.tail.prev = this.head
return this
endmethod
method size takes nothing returns integer
return this.n
endmethod
method addFirst takes $TYPE$ toAdd returns nothing
set this.head.next = $TYPE_NAME$LinkedListItem.create(toAdd, this.head.next, this.head)
set this.head.next.next.prev = this.head.next
set this.n = this.n + 1
endmethod
method addLast takes $TYPE$ toAdd returns nothing
set this.tail.prev = $TYPE_NAME$LinkedListItem.create(toAdd, this.tail, this.tail.prev)
set this.tail.prev.prev.next = this.tail.prev
set this.n = this.n + 1
endmethod
method add takes $TYPE$ toAdd returns boolean
call .addLast(toAdd)
return true
endmethod
method addByIndex takes integer index, $TYPE$ toAdd returns nothing
local integer i
local $TYPE_NAME$LinkedListItem temp
if (index >= 0 and index <= this.n) then
set temp = this.getItemByIndex.evaluate(index)
if (temp != 0) then
call this.addByItem.evaluate(temp, $TYPE_NAME$LinkedListItem.create(toAdd, 0, 0))
endif
endif
endmethod
method setByIndex takes integer index, $TYPE$ toSet returns $TYPE$
local $TYPE_NAME$LinkedListItem temp
local $TYPE$ old = 0
if (index >= 0 and index < .n) then
set temp = this.getItemByIndex.evaluate(index)
if (temp != 0) then
set old = temp.e
call thistype.setByItem.evaluate(temp, toSet)
endif
endif
return old
endmethod
method getFirst takes nothing returns $TYPE$
return this.head.next.e
endmethod
method getLast takes nothing returns $TYPE$
return this.tail.prev.e
endmethod
method getByIndex takes integer index returns $TYPE$
local $TYPE_NAME$LinkedListItem temp
if (index >= 0 and index < .n) then
set temp = this.getItemByIndex.evaluate(index)
if (temp != 0) then
return temp.e
endif
endif
return 0
endmethod
method remove takes $TYPE$ toRemove returns boolean
local $TYPE_NAME$LinkedListItem temp = this.getFirstItemByElement.evaluate(toRemove)
if (temp != 0) then
call this.removeByItem.evaluate(temp)
call temp.destroy()
return true
endif
return false
endmethod
method removeFirst takes nothing returns $TYPE$
local $TYPE_NAME$LinkedListItem temp = this.head.next
local $TYPE$ toRemove = 0
if (.n > 0) then
set toRemove = temp.e
call this.removeByItem.evaluate(this.head.next)
call temp.destroy()
endif
return toRemove
endmethod
method removeLast takes nothing returns $TYPE$
local $TYPE_NAME$LinkedListItem temp = this.tail.prev
local $TYPE$ toRemove = 0
if (.n > 0) then
set toRemove = temp.e
call this.removeByItem.evaluate(this.tail.prev)
call temp.destroy()
endif
return toRemove
endmethod
method removeByIndex takes integer index returns $TYPE$
local $TYPE_NAME$LinkedListItem temp
local $TYPE$ toRemove = 0
if (index >= 0 and index < .n) then
set temp = this.getItemByIndex.evaluate(index)
if (temp != 0) then
call this.removeByItem.evaluate(temp)
set toRemove = temp.e
call temp.destroy()
endif
endif
return toRemove
endmethod
method swap takes integer index1, integer index2 returns nothing
local $TYPE_NAME$LinkedListItem item1
local $TYPE_NAME$LinkedListItem item2
local $TYPE$ temp
if (index1 != index2) then
set item1 = this.getItemByIndex.evaluate(index1)
set item2 = this.getItemByIndex.evaluate(index2)
if (item1 != 0 and item2 != 0) then
set temp = item1.e
set item1.e = item2.e
set item2.e = temp
endif
endif
endmethod
method indexOf takes $TYPE$ element returns integer
return this.getFirstIndexByElement.evaluate(element)
endmethod
method lastIndexOf takes $TYPE$ element returns integer
return this.getLastIndexByElement.evaluate(element)
endmethod
method contains takes $TYPE$ element returns boolean
return this.indexOf(element) != -1
endmethod
method isEmpty takes nothing returns boolean
return this.n == 0
endmethod
method clear takes nothing returns nothing
call this.clearHelper.evaluate()
endmethod
method toArray takes nothing returns $TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $TYPE_NAME$Iterator
return $TYPE_NAME$LinkedListIterator.create.evaluate(this)
endmethod
method listIteratorByIndex takes integer index returns $TYPE_NAME$ListTypeIterator
if (index >= 0 and index <= .n) then
return $TYPE_NAME$LinkedListListTypeIterator.create.evaluate(this, this.getItemByIndex.evaluate(index))
endif
return 0
endmethod
method listIterator takes nothing returns $TYPE_NAME$ListTypeIterator
return .listIteratorByIndex(0)
endmethod
method enum takes $TYPE_NAME$_Enum enum, integer data returns nothing
local $TYPE_NAME$LinkedListItem temp = this.head
loop
set temp = temp.next
exitwhen (temp == this.tail)
call enum.evaluate(temp.e, data)
endloop
endmethod
method destroy takes nothing returns nothing
call this.clear()
call this.head.destroy()
call this.tail.destroy()
call this.deallocate()
endmethod
method addByItem takes $TYPE_NAME$LinkedListItem old, $TYPE_NAME$LinkedListItem toAdd returns nothing
set toAdd.prev = old.prev
set toAdd.next = old
set old.prev = toAdd
set toAdd.prev.next = toAdd
set this.n = this.n + 1
endmethod
method removeByItem takes $TYPE_NAME$LinkedListItem toRemove returns nothing
set toRemove.prev.next = toRemove.next
set toRemove.next.prev = toRemove.prev
set this.n = this.n - 1
endmethod
private method getItemByIndex takes integer index returns $TYPE_NAME$LinkedListItem
local integer i
local $TYPE_NAME$LinkedListItem temp = 0
if (index <= this.n and index >= 0) then
if (this.n - index > index) then
set i = 0
set temp = this.head
loop
set temp = temp.next
exitwhen (i == index)
set i = i + 1
endloop
else
set i = this.n
set temp = this.tail
loop
exitwhen (i == index)
set temp = temp.prev
set i = i - 1
endloop
endif
endif
return temp
endmethod
private method getFirstItemByElement takes $TYPE$ element returns $TYPE_NAME$LinkedListItem
local $TYPE_NAME$LinkedListItem temp = this.head
loop
set temp = temp.next
exitwhen (temp == this.tail)
if (temp.e == element) then
return temp
endif
endloop
return 0
endmethod
private method getLastItemByElement takes $TYPE$ element returns $TYPE_NAME$LinkedListItem
local $TYPE_NAME$LinkedListItem temp = this.tail
loop
set temp = temp.prev
exitwhen (temp == this.head)
if (temp.e == element) then
return temp
endif
endloop
return 0
endmethod
private method getFirstIndexByElement takes $TYPE$ element returns integer
local $TYPE_NAME$LinkedListItem temp = this.head
local integer i = 0
loop
set temp = temp.next
exitwhen (temp == this.tail)
if (temp.e == element) then
return i
endif
set i = i + 1
endloop
return -1
endmethod
private method getLastIndexByElement takes $TYPE$ element returns integer
local $TYPE_NAME$LinkedListItem temp = this.tail
local integer i = this.n - 1
loop
set temp = temp.prev
exitwhen (temp == this.head)
if (temp.e == element) then
return i
endif
set i = i - 1
endloop
return 0
endmethod
private method clearHelper takes nothing returns nothing
local $TYPE_NAME$LinkedListItem temp = this.head
loop
set temp = temp.next
exitwhen (temp == this.tail)
call temp.destroy()
endloop
set this.head.next = this.tail
set this.tail.prev = this.head
set this.n = 0
endmethod
private method toArrayHelper takes nothing returns $TYPE_NAME$Array
local $TYPE_NAME$Array ar = $TYPE_NAME$Array.create(this.size())
local $TYPE_NAME$LinkedListItem temp = this.head
local integer i = 0
loop
set temp = temp.next
exitwhen (temp == this.tail)
set ar[i] = temp.e
set i = i + 1
endloop
return ar
endmethod
static method setByItem takes $TYPE_NAME$LinkedListItem old, $TYPE$ toSet returns nothing
set old.e = toSet
endmethod
endstruct
struct $TYPE_NAME$LinkedListIterator extends $TYPE_NAME$Iterator
private $TYPE_NAME$LinkedList l
private $TYPE_NAME$LinkedListItem last = 0
private $TYPE_NAME$LinkedListItem current
static method create takes $TYPE_NAME$LinkedList l returns thistype
local thistype this = thistype.allocate()
set this.l = l
set this.current = l.head.next
return this
endmethod
method hasNext takes nothing returns boolean
return this.current != this.l.tail
endmethod
method next takes nothing returns $TYPE$
if (this.hasNext()) then
set this.last = .current
set this.current = .current.next
return this.last.e
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != 0) then
call this.l.removeByItem.evaluate(this.last)
call this.last.destroy()
set this.last = 0
endif
endmethod
endstruct
struct $TYPE_NAME$LinkedListListTypeIterator extends $TYPE_NAME$ListTypeIterator
private $TYPE_NAME$LinkedList l
private $TYPE_NAME$LinkedListItem lastI = 0
private $TYPE_NAME$LinkedListItem nextI
private $TYPE_NAME$LinkedListItem prevI
private integer index
static method create takes $TYPE_NAME$LinkedList l, $TYPE_NAME$LinkedListItem start returns thistype
local thistype this = thistype.allocate()
set this.l = l
set this.nextI = start
set this.prevI = start.prev
set this.index = 0
return this
endmethod
method hasNext takes nothing returns boolean
return this.nextI != this.l.tail
endmethod
method hasPrevious takes nothing returns boolean
return this.prevI != this.l.head
endmethod
method next takes nothing returns $TYPE$
if (this.hasNext()) then
set this.lastI = this.nextI
set this.nextI = this.nextI.next
set this.prevI = this.prevI.next
set this.index = this.index + 1
return this.lastI.e
endif
return 0
endmethod
method nextIndex takes nothing returns integer
return this.index
endmethod
method previous takes nothing returns $TYPE$
if (this.hasPrevious()) then
set this.lastI = this.prevI
set this.prevI = this.prevI.prev
set this.nextI = this.nextI.prev
set this.index = this.index - 1
return this.lastI.e
endif
return 0
endmethod
method previousIndex takes nothing returns integer
return this.index - 1
endmethod
method add takes $TYPE$ toAdd returns nothing
call this.l.addByItem.evaluate(this.nextI, $TYPE_NAME$LinkedListItem.create(toAdd, 0, 0))
set this.prevI = this.prevI.next
set this.lastI = 0
endmethod
method remove takes nothing returns nothing
if (this.lastI != 0) then
call this.l.removeByItem.evaluate(this.lastI)
if (this.lastI == this.prevI) then
set this.prevI = this.nextI.prev
set this.index = this.index - 1
else
set this.nextI = this.prevI.next
endif
set this.lastI = 0
endif
endmethod
method setElement takes $TYPE$ toSet returns nothing
if (this.lastI != 0) then
call $TYPE_NAME$LinkedList.setByItem.evaluate(this.lastI, toSet)
endif
endmethod
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=54
//TESH.alwaysfold=0
Map
v0.6.0
======
======
Using Map
=========
Requirements:
-------------
-Collection (for the value data type)
-Set (for the key data type as well as the key-value MapEntry data type)
Map can be considered an add-on to the Collections libraries. While a map is not a type of
collections object, the two are closely related.
Implementing Map is similar to implementing Collection. There is a textmacro named "Map"
that takes six arguments:
KEY_TYPE : The actual name of the data type of the maps keys; must be
a STRUCT type, not a native type
KEY_TYPE_NAME : The name of the key data type as you want it to appear in
map objects names
(Convention is to have the first letter of each word
capitalized)
KEY_TYPE_CONSTANT_NAME : The name of the key data type as you want it to appear in the
names of constant variables
(Convention is to have all letters capitalized, with words
separated by underscores)
VALUE_TYPE : The actual name of the data type of the maps values; must be
a STRUCT type, not a native type
VALUE_TYPE_NAME : The name of the value data type as you want it to appear in
map objects names
(Convention is to have the first letter of each word
capitalized)
VALUE_TYPE_CONSTANT_NAME : The name of the value data type as you want it to appear in the
names of constant variables
(Convention is to have all letters capitalized, with words
separated by underscores)
To implement Map for a certain key data type and a certain value data type, you must also
implement Set for the key data type and Collection for the value data type.
One final requirement is the Set library for the key-value MapEntry data type that is used by
the Map library. This one is a little tricky, but the correct arguments to use for this
"Set" textmacro would be:
TYPE : <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntry
TYPE_NAME : <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntry
TYPE_CONSTANT_NAME : <KEY_TYPE_NAME>_<VALUE_TYPE_NAME>_MAP_ENTRY
where <KEY_TYPE_NAME> and <VALUE_TYPE_NAME> are the arguments that were input into the
"Map" textmacro.
The textmacro must be run outside of any library or scope.
For example, to implement map objects that map data type Key to data type Value:
//! runtextmacro Collection("Key", "Key", "KEY")
//! runtextmacro Collection("Value", "Value", "VALUE")
//! runtextmacro Map("Key", "Key", "KEY", "Value", "Value", "VALUE")
//! runtextmacro Collection("KeyValueMapEntry", "KeyValueMapEntry", "KEY_VALUE_MAP_ENTRY")
Storage Space Constraints:
--------------------------
Maximum of 8190 instantiated Map objects and 8190 total entries in all Map objects for a given
key-value data type pair at any one time.
=============================================
interface <KEY_TYPE_NAME><VALUE_TYPE_NAME>Map
=============================================
A map is an object that maps a collection of data (keys) to another collection of data (values).
Duplicate keys are not allowed. While a map is not itself a collections object, it provides
three views of itself that are collections objects (and therefore extend the Collection
interface).
Instance Methods
================
------
method clear takes nothing returns nothing
------
Removes all entries (mappings) from this Map.
------
method containsKey takes <KEY_TYPE> k returns boolean
------
Checks if this Map contains a certain key.
Parameters:
-<KEY_TYPE> k - the key for which to search.
Returns:
true if this Map contains the specified key; false otherwise.
------
method containsValue takes <VALUE_TYPE> v returns boolean
------
Checks if this Map contains at least one instance of a certain value.
Parameters:
-<VALUE_TYPE> value - the value for which to search.
Returns:
true if this Map contains at least one instance of the specified value; false otherwise.
------
method destroy takes nothing returns nothing
------
Destroys this Map, freeing up allocation space for other instances of maps.
------
method entrySet takes nothing returns <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntrySet
------
Returns a set view of all entries (mappings) in this Map. The set is backed by this Map, so
any changes to this Map are reflected in the set, and vice-versa. The set supports removal
of elements (entries) but not the addition of elements. Destroying the set returned by this
method will not clear the backing Map.
Returns:
A Set backed by this Map containing all entries present in this Map.
------
method get takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Returns the value mapped to the specified key in this map. This method returns 0 if there
is no value mapped to the specified key.
Parameters:
-<KEY_TYPE> k - the key to which the desired value is mapped.
Returns:
The value mapped to the specified key, or 0 if the specified key is not in this Map.
------
method isEmpty takes nothing returns boolean
------
Checks if this Map is empty (has no entries).
Returns:
true if this Map is empty; false otherwise.
------
method keySet takes nothing returns <KEY_TYPE_NAME>Set
------
Returns a set view of all keys present in this Map. The set is backed by this Map, so any
changes to this Map are reflected in the set, and vice-versa. The set supports removal of
elements (keys and their associated entries) but not the addition of elements. Destroying the
set returned by this method will not clear the backing Map.
Returns:
A Set backed by this Map containing all keys present in this Map.
------
method put takes <KEY_TYPE> k, <VALUE_TYPE> v returns <VALUE_TYPE>
------
Maps the specified key to the specified value in this map. Returns the previous value
associated with that key, if any.
Parameters:
-<KEY_TYPE> k - the new key.
-<VALUE_TYPE> v - the value to map to the new key.
Returns:
The value previously associated with the specified key, or 0 if there was no such key in this
Map.
------
method remove takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Attempts to remove the entry associated with the specified key in this Map. Returns the value
associated with that key, if any. This method returns 0 if there was no value mapped to
the specified key.
Parameters:
-<KEY_TYPE> k - the key to be removed from this Map.
Returns:
The value associated with the specified key, or 0 if there is no such key in this Map.
------
method size takes nothing returns integer
------
Returns the number of entries (mappings) in this Map.
Returns:
The number of entries in this Map.
------
method values takes nothing returns <VALUE_TYPE_NAME>Collection
------
Returns a collections object view of all values present in this Map. The collection is backed
by this Map, so any changes to this Map are reflected in the collection, and vice-versa.
The collection supports removal of elements (values and their associated entries) but not the
addition of elements. If Collection.remove() is called on the collection and multiple entries
containing the value to be removed exist in the backing Map, only one such entry will be
removed. Destroying the collection returned by this method will not clear the backing Map.
Returns:
A Collection backed by this Map containing all occurences of all values present in this Map.
==================================================
interface <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntry
==================================================
All map entries returned by the enum() method and iterator of the entry set of a map extend
this interface. NEVER destroy a MapEntry or the map that it was contained in will become
corrupted.
Instance Methods
================
------
method key takes nothing returns <KEY_TYPE>
------
Returns the key associated with this MapEntry.
Returns:
The key associated with this MapEntry.
------
method value takes nothing returns <VALUE_TYPE>
------
Returns the value associated with this MapEntry.
Returns:
The value associated with this MapEntry.
------
method setValue takes <VALUE_TYPE> toSet returns <VALUE_TYPE>
------
Sets the value associated with this MapEntry to the specified value and returns the old
value associated with this MapEntry.
Parameter:
-<VALUE_TYPE> toSet - the new value to associate with this MapEntry.
Returns:
The old value associated with this MapEntry.
//TESH.scrollpos=20
//TESH.alwaysfold=0
LinkedHashMap
v0.6.0
=============
=============
Using LinkedHashMap
===================
Requirements:
-------------
-Map
To implement LinkedHashMap, you first need to implement Map (see the Map documentation) After
you have implemented Map for a pair of key and value data types, you may then implement
LinkedHashMap for the same key-value data type pair.
The textmacro to implement LinkedHashMap is named: "LinkedHashMap"
The textmacro must be run outside of any library or scope.
For example, to create LinkedHashMap objects that that map data type Key to data type Value:
//! runtextmacro Collection("Key", "Key", "KEY")
//! runtextmacro Collection("Value", "Value", "VALUE")
//! runtextmacro Map("Key", "Key", "KEY", "Value", "Value", "VALUE")
//! runtextmacro Collection("KeyValueMapEntry", "KeyValueMapEntry", "KEY_VALUE_MAP_ENTRY")
//! runtextmacro LinkedHashMap("Key", "Key, "KEY", "Value", "Value", "VALUE")
Additional Storage Space Constraints:
-------------------------------------
None
Tips for Use:
-------------
Use LinkedHashSet if you require a map with fast insertion and retrieval (i.e. if you will be
regularly adding, removing, and/or checking if the map contains keys) and you either don't
need entries to be ordered in the map or you wish the map to preserve the order in which entries
were inserted (entries inserted first come first in the map).
================================================================================================
struct <KEY_TYPE_NAME><VALUE_TYPE_NAME>LinkedHashMap extends <KEY_TYPE_NAME><VALUE_TYPE_NAME>Map
================================================================================================
LinkedHashMap is a Map backed by a hashtable. The order in which entries were inserted into the
LinkedHashMap is preserved. All single-entry insertion and retrieval methods by key are
guaranteed to run in constant time. LinkedHashMap uses reference equality.
Instance Methods
================
Inherited From Map:
-method clear takes nothing returns nothing
-method destroy takes nothing returns nothing
-method entrySet takes nothing returns <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntrySet
-method isEmpty takes nothing returns boolean
-method keySet takes nothing returns <KEY_TYPE_NAME>Set
-method size takes nothing returns integer
-method values takes nothing returns <VALUE_TYPE_NAME>Collection
------
method containsKey takes <KEY_TYPE> k returns boolean
------
Checks if this Map contains a certain key. This method is guaranteed to run in constant time.
Parameters:
-<KEY_TYPE> k - the key for which to search.
Returns:
true if this Map contains the specified key; false otherwise.
------
method containsValue takes <VALUE_TYPE> v returns boolean
------
Checks if this Map contains at least one instance of a certain value. This method runs in O(n)
time.
Parameters:
-<VALUE_TYPE> value - the value for which to search.
Returns:
true if this Map contains at least one instance of the specified value; false otherwise.
------
method get takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Returns the value mapped to the specified key in this map. This method returns 0 if there
is no value mapped to the specified key. This method is guaranteed to run in constant time.
Parameters:
-<KEY_TYPE> k - the key to which the desired value is mapped.
Returns:
The value mapped to the specified key, or 0 if the specified key is not in this Map.
------
method put takes <KEY_TYPE> k, <VALUE_TYPE> v returns <VALUE_TYPE>
------
Maps the specified key to the specified value in this map. Returns the previous value
associated with that key, if any. This method is guaranteed to run in constant time.
Parameters:
-<KEY_TYPE> k - the new key.
-<VALUE_TYPE> v - the value to map to the new key.
Returns:
The value previously associated with the specified key, or 0 if there was no such key in this
Map.
------
method remove takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Attempts to remove the entry associated with the specified key in this Map. Returns the value
associated with that key, if any. This method returns 0 if there was no value mapped to
the specified key. This method is guaranteed to run in constant time.
Parameters:
-<KEY_TYPE> k - the key to be removed from this Map.
Returns:
The value associated with the specified key, or 0 if there is no such key in this Map.
//TESH.scrollpos=18
//TESH.alwaysfold=0
TreeMap
v0.6.0
=======
=======
Using TreeMap
=============
Requirements:
-------------
-Map
To implement TreeMap, you first need to implement Map (see the Map documentation) After you
have implemented Map for a pair of key and value data types, you may then implement TreeMap
for the same key-value data type pair.
An important thing to note is that TreeMap may ONLY be implemented for data types that
support the < and > boolean operators. Data types that support these are limited to integer
and struct types that have overloaded the < operator.
The textmacro to implement TreeMap is named: "TreeMap"
The textmacro must be run outside of any library or scope.
For example, to create TreeMap objects that that map data type Key to data type Value:
//! runtextmacro Collection("Key", "Key", "KEY")
//! runtextmacro Collection("Value", "Value", "VALUE")
//! runtextmacro Map("Key", "Key", "KEY", "Value", "Value", "VALUE")
//! runtextmacro Collection("KeyValueMapEntry", "KeyValueMapEntry", "KEY_VALUE_MAP_ENTRY")
//! runtextmacro TreeMap("Key", "Key, "KEY", "Value", "Value", "VALUE")
Additional Storage Space Constraints:
-------------------------------------
None
Tips for Use:
-------------
Use TreeMap if you require a map that orders its keys based on some property of the keys
themselves. If you are going to be adding keys in a more or less random order into
the map based on how you want the map to order them (in other words, you are not pre-ordering
the keys for the map before adding them to the map), then the basic TreeMap implementation
should be fine. However, if you expect to be adding many pre-ordered groups of keys, or if
you just want to ensure that the map operates as quickly as possible, you may wish to use
BalancedTreeMap instead. The tradeoff is that BalancedTreeMap requires many more calculations
when adding and removing keys than TreeMap.
==========================================================================================
struct <KEY_TYPE_NAME><VALUE_TYPE_NAME>TreeMap extends <KEY_TYPE_NAME><VALUE_TYPE_NAME>Map
==========================================================================================
A TreeMap is an ordered implementation of Map that uses a binary search tree to store its
data. All elements within the map are consistently ordered in one of two ways: by a
comparator or by the natural ordering of the elements (as defined by the < and > boolean
operators) if a comparator was not specified upon instantiation of the map. TreeMap uses either
a comparator or the < and > boolean operators to determine struct equality.
The methods TreeMap.put(), TreeMap.get(), TreeMap.remove(), and TreeMap.containsKey() all run in
O(log n) time IF the tree is well-balanced. If the tree is not well-balanced (i.e. depth >>
breadth), the tree essentially degenerates into a linked list and the aforementioned methods
will run in worst-case O(n) time.
Constructors
============
------
static method create takes <KEY_TYPE_NAME>_Comparator comparator returns
<KEY_TYPE_NAME><VALUE_TYPE_NAME>TreeMap
------
Creates a new instance of TreeMap with the specified comparator. If a comparator is specified,
it will be used to determine the ordering of elements within the map. Otherwise, elements
will be ordered according to their natural ordering defined by the < and > boolean operators.
Instance Methods
================
Inherited From TreeMap:
-method clear takes nothing returns nothing
-method destroy takes nothing returns nothing
-method entrySet takes nothing returns <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntrySet
-method isEmpty takes nothing returns boolean
-method keySet takes nothing returns <KEY_TYPE_NAME>Set
-method size takes nothing returns integer
-method values takes nothing returns <VALUE_TYPE_NAME>Collection
------
method containsKey takes <KEY_TYPE> k returns boolean
------
Checks if this Map contains a certain key. This method runs in O(log n) time if the tree is
well-balanced.
Parameters:
-<KEY_TYPE> k - the key for which to search.
Returns:
true if this Map contains the specified key; false otherwise.
------
method containsValue takes <VALUE_TYPE> v returns boolean
------
Checks if this Map contains at least one instance of a certain value. This method runs in O(n)
time.
Parameters:
-<VALUE_TYPE> value - the value for which to search.
Returns:
true if this Map contains at least one instance of the specified value; false otherwise.
------
method get takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Returns the value mapped to the specified key in this map. This method returns 0 if there
is no value mapped to the specified key. This method runs in O(log n) time if the tree is
well-balanced.
Parameters:
-<KEY_TYPE> k - the key to which the desired value is mapped.
Returns:
The value mapped to the specified key, or 0 if the specified key is not in this Map.
------
method put takes <KEY_TYPE> k, <VALUE_TYPE> v returns <VALUE_TYPE>
------
Maps the specified key to the specified value in this map. Returns the previous value
associated with that key, if any. This method runs in O(log n) time if the tree is well-
balanced.
Parameters:
-<KEY_TYPE> k - the new key.
-<VALUE_TYPE> v - the value to map to the new key.
Returns:
The value previously associated with the specified key, or 0 if there was no such key in this
Map.
------
method remove takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Attempts to remove the entry associated with the specified key in this Map. Returns the value
associated with that key, if any. This method returns 0 if there was no value mapped to
the specified key. This method runs in O(log n) time if the tree is well-balanced.
Parameters:
-<KEY_TYPE> k - the key to be removed from this Map.
Returns:
The value associated with the specified key, or 0 if there is no such key in this Map.
==============================================================
struct <KEY_TYPE_NAME><VALUE_TYPE_NAME>BalancedTreeMap extends
<KEY_TYPE_NAME><VALUE_TYPE_NAME>TreeMap
==============================================================
BalancedTreeMap is a self-balancing tree map. More specifically, it is an implementation of a
red-black tree. Unlike TreeMap, this implementation guarantees O(log n) performance for
single-entry insertion and retrieval by key. All other aspects of BalancedTreeMap are identical
to TreeMap.
Constructors
============
------
static method create takes <KEY_TYPE_NAME>_Comparator comparator returns
<KEY_TYPE_NAME><VALUE_TYPE_NAME>BalancedTreeMap
------
Creates a new instance of BalancedTreeMap with the specified comparator. If a comparator is
specified, it will be used to determine the ordering of elements within the map. Otherwise,
elements will be ordered according to their natural ordering defined by the < and > boolean
operators.
Instance Methods
================
Inherited From Map:
-method clear takes nothing returns nothing
-method containsValue takes <VALUE_TYPE> v returns boolean
-method entrySet takes nothing returns <KEY_TYPE_NAME><VALUE_TYPE_NAME>MapEntrySet
-method isEmpty takes nothing returns boolean
-method keySet takes nothing returns <KEY_TYPE_NAME>Set
-method size takes nothing returns integer
-method values takes nothing returns <VALUE_TYPE_NAME>Collection
------
method containsKey takes <KEY_TYPE> k returns boolean
------
Checks if this Map contains a certain key. This method is guaranteed to run in O(log n) time.
Parameters:
-<KEY_TYPE> k - the key for which to search.
Returns:
true if this Map contains the specified key; false otherwise.
------
method get takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Returns the value mapped to the specified key in this map. This method returns 0 if there
is no value mapped to the specified key. This method is guaranteed to run in O(log n) time.
Parameters:
-<KEY_TYPE> k - the key to which the desired value is mapped.
Returns:
The value mapped to the specified key, or 0 if the specified key is not in this Map.
------
method put takes <KEY_TYPE> k, <VALUE_TYPE> v returns <VALUE_TYPE>
------
Maps the specified key to the specified value in this map. Returns the previous value
associated with that key, if any. This method is guaranteed to run in O(log n) time.
Parameters:
-<KEY_TYPE> k - the new key.
-<VALUE_TYPE> v - the value to map to the new key.
Returns:
The value previously associated with the specified key, or 0 if there was no such key in this
Map.
------
method remove takes <KEY_TYPE> k returns <VALUE_TYPE>
------
Attempts to remove the entry associated with the specified key in this Map. Returns the value
associated with that key, if any. This method returns 0 if there was no value mapped to
the specified key. This method is guaranteed to run in O(log n) time.
Parameters:
-<KEY_TYPE> k - the key to be removed from this Map.
Returns:
The value associated with the specified key, or 0 if there is no such key in this Map.
//TESH.scrollpos=19
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ Map +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro Map takes KEY_TYPE, KEY_TYPE_NAME, KEY_TYPE_CONSTANT_NAME, VALUE_TYPE, VALUE_TYPE_NAME, VALUE_TYPE_CONSTANT_NAME
library $KEY_TYPE_NAME$$VALUE_TYPE_NAME$Map requires $KEY_TYPE_NAME$Set, $VALUE_TYPE_NAME$Collection, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntrySet
interface $KEY_TYPE_NAME$$VALUE_TYPE_NAME$Map
method put takes $KEY_TYPE$ k, $VALUE_TYPE$ v returns $VALUE_TYPE$
method get takes $KEY_TYPE$ k returns $VALUE_TYPE$
method remove takes $KEY_TYPE$ k returns $VALUE_TYPE$
method containsKey takes $KEY_TYPE$ k returns boolean
method containsValue takes $VALUE_TYPE$ v returns boolean
method size takes nothing returns integer
method isEmpty takes nothing returns boolean
method clear takes nothing returns nothing
method keySet takes nothing returns $KEY_TYPE_NAME$Set
method values takes nothing returns $VALUE_TYPE_NAME$Collection
method entrySet takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntrySet
endinterface
interface $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry
method key takes nothing returns $KEY_TYPE$
method value takes nothing returns $VALUE_TYPE$
method setValue takes $VALUE_TYPE$ v returns $VALUE_TYPE$
endinterface
endlibrary
//! endtextmacro
//TESH.scrollpos=6
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ LinkedHashMap +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro LinkedHashMap takes KEY_TYPE, KEY_TYPE_NAME, KEY_TYPE_CONSTANT_NAME, VALUE_TYPE, VALUE_TYPE_NAME, VALUE_TYPE_CONSTANT_NAME
library $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap requires $KEY_TYPE_NAME$$VALUE_TYPE_NAME$Map
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySet
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollection
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySet
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySetIterator
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollectionIterator
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySetIterator
private keyword t
private keyword head
private keyword tail
private struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry
private $KEY_TYPE$ keyV
private $VALUE_TYPE$ valueV
thistype next
thistype prev
static method create takes $KEY_TYPE$ k, $VALUE_TYPE$ v returns thistype
local thistype this = thistype.allocate()
set this.keyV = k
set this.valueV = v
return this
endmethod
method key takes nothing returns $KEY_TYPE$
return .keyV
endmethod
method value takes nothing returns $VALUE_TYPE$
return .valueV
endmethod
method setValue takes $VALUE_TYPE$ v returns $VALUE_TYPE$
local $VALUE_TYPE$ old = .valueV
set .valueV = v
return old
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$Map
static hashtable t
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry head
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry tail
private integer n = 0
static method create takes nothing returns thistype
local thistype this = thistype.allocate()
set this.head = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry.create(0, 0)
set this.tail = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry.create(0, 0)
set this.head.next = this.tail
set this.tail.prev = this.head
return this
endmethod
method size takes nothing returns integer
return this.n
endmethod
method put takes $KEY_TYPE$ k, $VALUE_TYPE$ v returns $VALUE_TYPE$
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Attempting to map " + I2S(k) + " to " + I2S(v) + ".")
if (HaveSavedInteger(thistype.t, this, k)) then
set entry = LoadInteger(thistype.t, this, k)
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Mapping succeeded: " + I2S(k) + " is mapped to " + I2S(v) + ", replacing " + I2S(entry.value()) + ".")
return entry.setValue(v)
endif
set entry = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry.create(k, v)
call SaveInteger(thistype.t, this, k, entry)
set entry.next = this.tail
set entry.prev = this.tail.prev
set this.tail.prev.next = entry
set this.tail.prev = entry
set .n = .n + 1
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Mapping succeeded: " + I2S(k) + " is mapped to " + I2S(v) + ".")
return 0
endmethod
method get takes $KEY_TYPE$ k returns $VALUE_TYPE$
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Attempting to retrieve value from " + I2S(k) + ".")
if (HaveSavedInteger(thistype.t, this, k)) then
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Value retrieval succeeded: " + I2S(k) + " is mapped to " + I2S($KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry(LoadInteger(this.t, k, 0)).value()) + ".")
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry(LoadInteger(thistype.t, this, k)).value()
endif
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Value retrieval failed: Key " + I2S(k) + " is not in map.")
return 0
endmethod
method remove takes $KEY_TYPE$ k returns $VALUE_TYPE$
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry
local $VALUE_TYPE$ old = 0
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry next
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry prev
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Attempting to remove key " + I2S(k) + ".")
if (HaveSavedInteger(thistype.t, this, k)) then
set entry = LoadInteger(thistype.t, this, k)
set old = entry.value()
set next = entry.next
set prev = entry.prev
set prev.next = next
set next.prev = prev
call RemoveSavedInteger(thistype.t, this, k)
call entry.destroy()
set .n = .n - 1
debug call BJDebugMsg("$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap " + I2S(this) + ": Key removal succeeded: " + I2S(k) + ", mapped to " + I2S(old) + ", was removed.")
endif
return old
endmethod
method containsKey takes $KEY_TYPE$ k returns boolean
return HaveSavedInteger(thistype.t, this, k)
endmethod
method containsValue takes $VALUE_TYPE$ v returns boolean
return this.containsValueHelper.evaluate(v)
endmethod
method isEmpty takes nothing returns boolean
return this.n == 0
endmethod
method clear takes nothing returns nothing
call this.clearHelper.evaluate()
call FlushChildHashtable(thistype.t, this)
set this.n = 0
endmethod
method keySet takes nothing returns $KEY_TYPE_NAME$Set
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySet.create.evaluate(this)
endmethod
method values takes nothing returns $VALUE_TYPE_NAME$Collection
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollection.create.evaluate(this)
endmethod
method entrySet takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntrySet
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySet.create.evaluate(this)
endmethod
method destroy takes nothing returns nothing
call this.clear()
call this.deallocate()
endmethod
private method containsValueHelper takes $VALUE_TYPE$ v returns boolean
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.head
loop
set entry = entry.next
exitwhen (entry == this.tail)
if (entry.value() == v) then
return true
endif
endloop
return false
endmethod
private method clearHelper takes nothing returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.head
loop
set entry = entry.next
exitwhen (entry == this.tail)
call entry.destroy()
endloop
set this.head.next = this.tail
set this.tail.prev = this.head
endmethod
private static method onInit takes nothing returns nothing
set thistype.t = InitHashtable()
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySet extends $KEY_TYPE_NAME$Set
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap m
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap m returns thistype
local thistype this = thistype.allocate()
set this.m = m
return this
endmethod
method add takes $KEY_TYPE$ toAdd returns boolean
return false
endmethod
method addAll takes $KEY_TYPE_NAME$Collection collection returns nothing
endmethod
method remove takes $KEY_TYPE$ toRemove returns boolean
return this.m.remove(toRemove) != 0
endmethod
method contains takes $KEY_TYPE$ element returns boolean
return this.m.containsKey(element)
endmethod
method size takes nothing returns integer
return this.m.size()
endmethod
method isEmpty takes nothing returns boolean
return this.m.isEmpty()
endmethod
method clear takes nothing returns nothing
call this.m.clear()
endmethod
method toArray takes nothing returns $KEY_TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $KEY_TYPE_NAME$Iterator
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySetIterator.create.evaluate(this)
endmethod
method enum takes $KEY_TYPE_NAME$_Enum enum, integer data returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
call enum.evaluate(entry.key(), data)
endloop
endmethod
private method toArrayHelper takes nothing returns $KEY_TYPE_NAME$Array
local $KEY_TYPE_NAME$Array ar = $KEY_TYPE_NAME$Array.create(this.size())
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
local integer i = 0
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
set ar[i] = entry.key()
set i = i + 1
endloop
return ar
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollection extends $VALUE_TYPE_NAME$Collection
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap m
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap m returns thistype
local thistype this = thistype.allocate()
set this.m = m
return this
endmethod
method add takes $VALUE_TYPE$ toAdd returns boolean
return false
endmethod
method addAll takes $VALUE_TYPE_NAME$Collection collection returns nothing
endmethod
method remove takes $VALUE_TYPE$ toRemove returns boolean
return this.removeValue.evaluate(toRemove)
endmethod
method contains takes $VALUE_TYPE$ element returns boolean
return this.m.containsValue(element)
endmethod
method size takes nothing returns integer
return this.m.size()
endmethod
method isEmpty takes nothing returns boolean
return this.m.isEmpty()
endmethod
method clear takes nothing returns nothing
call this.m.clear()
endmethod
method toArray takes nothing returns $VALUE_TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $VALUE_TYPE_NAME$Iterator
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollectionIterator.create.evaluate(this)
endmethod
method enum takes $VALUE_TYPE_NAME$_Enum enum, integer data returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
call enum.evaluate(entry.value(), data)
endloop
endmethod
private method removeValue takes $VALUE_TYPE$ toRemove returns boolean
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
if (entry.value() == toRemove) then
call this.m.remove(entry.key())
return true
endif
endloop
return false
endmethod
private method toArrayHelper takes nothing returns $VALUE_TYPE_NAME$Array
local $VALUE_TYPE_NAME$Array ar = $VALUE_TYPE_NAME$Array.create(this.size())
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
local integer i = 0
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
set ar[i] = entry.value()
set i = i + 1
endloop
return ar
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySet extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntrySet
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap m
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMap m returns thistype
local thistype this = thistype.allocate()
set this.m = m
return this
endmethod
method add takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry toAdd returns boolean
return false
endmethod
method addAll takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryCollection collection returns nothing
endmethod
method remove takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry toRemove returns boolean
return this.m.remove(toRemove.key()) != 0
endmethod
method contains takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry entry returns boolean
return this.m.containsKey(entry.key())
endmethod
method size takes nothing returns integer
return this.m.size()
endmethod
method isEmpty takes nothing returns boolean
return this.m.isEmpty()
endmethod
method clear takes nothing returns nothing
call this.m.clear()
endmethod
method toArray takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryIterator
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySetIterator.create.evaluate(this)
endmethod
method enum takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry_Enum enum, integer data returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
call enum.evaluate(entry, data)
endloop
endmethod
private method toArrayHelper takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray ar = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray.create(this.size())
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry entry = this.m.head
local integer i = 0
loop
set entry = entry.next
exitwhen (entry == this.m.tail)
set ar[i] = entry
set i = i + 1
endloop
return ar
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySetIterator extends $KEY_TYPE_NAME$Iterator
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySet s
private $KEY_TYPE$ last = 0
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry n = 0
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapKeySet s returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.n = s.m.head.next
return this
endmethod
method hasNext takes nothing returns boolean
return this.n != this.s.m.tail
endmethod
method next takes nothing returns $KEY_TYPE$
if (this.hasNext()) then
set this.last = this.n.key()
set this.n = this.n.next
return this.last
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != 0) then
call this.s.remove(this.last)
set this.last = 0
endif
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollectionIterator extends $VALUE_TYPE_NAME$Iterator
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollection c
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry last = 0
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry n = 0
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapValueCollection c returns thistype
local thistype this = thistype.allocate()
set this.c = c
set this.n = c.m.head.next
return this
endmethod
method hasNext takes nothing returns boolean
return this.n != this.c.m.tail
endmethod
method next takes nothing returns $VALUE_TYPE$
if (this.hasNext()) then
set this.last = this.n
set this.n = this.n.next
return this.last.value()
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != 0) then
call this.c.m.remove(this.last.key())
set this.last = 0
endif
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySetIterator extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryIterator
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySet s
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry last = 0
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntry n = 0
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$LinkedHashMapEntrySet s returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.n = s.m.head.next
return this
endmethod
method hasNext takes nothing returns boolean
return this.n != this.s.m.tail
endmethod
method next takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry
if (this.hasNext()) then
set this.last = this.n
set this.n = this.n.next
return this.last
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != 0) then
call this.s.remove(this.last)
set this.last = 0
endif
endmethod
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=108
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ TreeMap +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro TreeMap takes KEY_TYPE, KEY_TYPE_NAME, KEY_TYPE_CONSTANT_NAME, VALUE_TYPE, VALUE_TYPE_NAME, VALUE_TYPE_CONSTANT_NAME
library $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap requires $KEY_TYPE_NAME$$VALUE_TYPE_NAME$Map
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySet
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollection
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySet
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySetIterator
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollectionIterator
private keyword $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySetIterator
private keyword removeNode
private struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry
static constant integer BLACK = 0
static constant integer RED = 1
static thistype sentinelNode
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m
$KEY_TYPE$ keyV
$VALUE_TYPE$ valueV
thistype parent
thistype left
thistype right
integer color
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m, integer color, $KEY_TYPE$ k, $VALUE_TYPE$ v, thistype parent, boolean sentinel returns thistype
local thistype this = thistype.allocate()
set this.m = m
set this.color = color
set this.keyV = k
set this.valueV = v
set this.parent = parent
if (sentinel == false) then
set this.left = thistype.sentinelNode //thistype.create(s, thistype.BLACK, 0, this, true)
set this.right = thistype.sentinelNode //thistype.create(s, thistype.BLACK, 0, this, true)
endif
return this
endmethod
method operator sentinel takes nothing returns boolean
return this == thistype.sentinelNode
endmethod
method operator sibling takes nothing returns thistype
if (.parent != 0) then
if (this == .parent.left) then
return .parent.right
else
return .parent.left
endif
endif
return 0
endmethod
method operator grandparent takes nothing returns thistype
if (.parent != 0) then
return .parent.parent
endif
return 0
endmethod
method operator uncle takes nothing returns thistype
local thistype grandparent = .grandparent
if (grandparent != 0) then
if (.parent == grandparent.left) then
return grandparent.right
else
return grandparent.left
endif
endif
return 0
endmethod
method rotateLeft takes nothing returns nothing
if (.parent != 0) then
if (this == .parent.left) then
set .parent.left = .right
else
set .parent.right = .right
endif
endif
set .right.parent = .parent
set .parent = .right
set .right = .parent.left
set .parent.left = this
if (.right.sentinel == false) then
set .right.parent = this
endif
if (this == .m.root) then
set .m.root = .parent
endif
endmethod
method rotateRight takes nothing returns nothing
if (.parent != 0) then
if (this == .parent.left) then
set .parent.left = .left
else
set .parent.right = .left
endif
endif
set .left.parent = .parent
set .parent = .left
set .left = .parent.right
set .parent.right = this
if (.left.sentinel == false) then
set .left.parent = this
endif
if (this == .m.root) then
set .m.root = .parent
endif
endmethod
method inOrderSuccessorHelper takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode
if (this.sentinel) then
return 0
elseif (this.left.sentinel) then
return this
endif
return this.left.inOrderSuccessorHelper()
endmethod
method getInOrderSuccessor takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode
if (this.sentinel) then
return 0
endif
return this.right.inOrderSuccessorHelper()
endmethod
method key takes nothing returns $KEY_TYPE$
return .keyV
endmethod
method value takes nothing returns $VALUE_TYPE$
return .valueV
endmethod
method setValue takes $VALUE_TYPE$ v returns $VALUE_TYPE$
local $VALUE_TYPE$ old = .valueV
set .valueV = v
return old
endmethod
private static method onInit takes nothing returns nothing
set thistype.sentinelNode = thistype.create(0, thistype.BLACK, 0, 0, 0, true)
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$Map
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode root = 0
integer n = 0
$KEY_TYPE_NAME$_Comparator comparator = 0
static method create takes $KEY_TYPE_NAME$_Comparator comparator returns thistype
local thistype this = thistype.allocate()
set this.comparator = comparator
return this
endmethod
method put takes $KEY_TYPE$ k, $VALUE_TYPE$ v returns $VALUE_TYPE$
local $VALUE_TYPE$ old = 0
if (this.root == 0) then
set this.root = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.create(this, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK, k, v, 0, false)
set this.n = this.n + 1
else
set old = this.putNode.evaluate(k, v, this.root)
if (old == 0) then
set this.n = this.n + 1
endif
endif
return old
endmethod
method get takes $KEY_TYPE$ k returns $VALUE_TYPE$
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.findNodeByKey.evaluate(k, .root)
if (node != 0) then
return node.value()
endif
return 0
endmethod
method remove takes $KEY_TYPE$ k returns $VALUE_TYPE$
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.findNodeByKey.evaluate(k, .root)
local $VALUE_TYPE$ old = 0
if (node != 0) then
set old = node.value()
call this.removeNode.evaluate(node)
set this.n = this.n - 1
endif
return old
endmethod
method containsKey takes $KEY_TYPE$ k returns boolean
return this.findNodeByKey.evaluate(k, this.root) != 0
endmethod
method containsValue takes $VALUE_TYPE$ v returns boolean
return this.findNodeByValue.evaluate(v) != 0
endmethod
method size takes nothing returns integer
return this.n
endmethod
method isEmpty takes nothing returns boolean
return this.n == 0
endmethod
method clear takes nothing returns nothing
call this.clearHelper.evaluate(this.root)
set this.root = 0
set this.n = 0
endmethod
method keySet takes nothing returns $KEY_TYPE_NAME$Set
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySet.create.evaluate(this)
endmethod
method values takes nothing returns $VALUE_TYPE_NAME$Collection
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollection.create.evaluate(this)
endmethod
method entrySet takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntrySet
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySet.create.evaluate(this)
endmethod
method destroy takes nothing returns nothing
call this.clear()
call this.deallocate()
endmethod
private stub method putNode takes $KEY_TYPE$ k, $VALUE_TYPE$ v, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns $VALUE_TYPE$
local integer order
local $VALUE_TYPE$ old
if (this.comparator != 0) then
set order = this.comparator.evaluate(k, node.keyV)
else
if (k < node.keyV) then
set order = -1
elseif (k > node.keyV) then
set order = 1
else
set order = 0
endif
endif
if (order < 0) then
if (node.left.sentinel) then
set node.left = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.create(this, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED, k, v, node, false)
return 0
else
return this.putNode(k, v, node.left)
endif
elseif (order > 0) then
if (node.right.sentinel) then
set node.right = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.create(this, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED, k, v, node, false)
return 0
else
return this.putNode(k, v, node.right)
endif
endif
set old = node.valueV
call node.setValue(v)
return old
endmethod
stub method removeNode takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode successor
if (node.right.sentinel == false and node.left.sentinel == false) then
set successor = node.getInOrderSuccessor()
set node.keyV = successor.keyV
set node.valueV = successor.valueV
call this.removeNode.evaluate(successor)
elseif (node.right.sentinel and node.left.sentinel) then
if (node != this.root) then
if (node == node.parent.left) then
set node.parent.left = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.sentinelNode
else
set node.parent.right = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.sentinelNode
endif
else
set this.root = 0
endif
call node.destroy()
else
if (node.right.sentinel) then
if (node.parent.left == node) then
set node.parent.left = node.left
else
set node.parent.right = node.left
endif
set node.left.parent = node.parent
if (node == this.root) then
set this.root = node.left
endif
else
if (node.parent.left == node) then
set node.parent.left = node.right
else
set node.parent.right = node.right
endif
set node.right.parent = node.parent
if (node == this.root) then
set this.root = node.right
endif
endif
call node.destroy()
endif
endmethod
private method findNodeByKey takes $KEY_TYPE$ k, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode
local integer order
if (node == 0) then
return 0
elseif (node.sentinel) then
return 0
endif
if (this.comparator != 0) then
set order = this.comparator.evaluate(k, node.key())
else
if (k < node.key()) then
set order = -1
elseif (k > node.key()) then
set order = 1
else
set order = 0
endif
endif
if (order < 0) then
return this.findNodeByKey(k, node.left)
elseif (order > 0) then
return this.findNodeByKey(k, node.right)
else
return node
endif
endmethod
private method findNodeByValue takes $VALUE_TYPE$ v returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.root
if (node == 0) then
return 0
endif
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
loop
if (node.valueV == v) then
return node
endif
if (node.right.sentinel == false) then
set node = node.right
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
else
loop
exitwhen (node.parent == 0 or node != node.parent.right)
set node = node.parent
endloop
set node = node.parent
endif
exitwhen (node == 0 or node.sentinel)
endloop
return 0
endmethod
private method clearHelper takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
if (node == 0) then
return
endif
if (node.left.sentinel == false) then
call this.clearHelper(node.left)
endif
if (node.right.sentinel == false) then
call this.clearHelper(node.right)
endif
call node.destroy()
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$BalancedTreeMap extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap
private method putNode takes $KEY_TYPE$ k, $VALUE_TYPE$ v, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns $VALUE_TYPE$
local integer order
local $VALUE_TYPE$ old
if (this.comparator != 0) then
set order = this.comparator.evaluate(k, node.key())
else
if (k < node.key()) then
set order = -1
elseif (k > node.key()) then
set order = 1
else
set order = 0
endif
endif
if (order < 0) then
if (node.left.sentinel) then
set node.left = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.create(this, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED, k, v, node, false)
call this.insertCaseOne.evaluate(node.left)
return 0
else
return this.putNode(k, v, node.left)
endif
elseif (order > 0) then
if (node.right.sentinel) then
set node.right = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.create(this, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED, k, v, node, false)
call this.insertCaseOne.evaluate(node.right)
return 0
else
return this.putNode(k, v, node.right)
endif
endif
set old = node.valueV
call node.setValue(v)
return old
endmethod
method removeNode takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode successor
if (node.right.sentinel == false and node.left.sentinel == false) then
set successor = node.getInOrderSuccessor()
set node.keyV = successor.keyV
set node.valueV = successor.valueV
call this.removeNode.evaluate(successor)
elseif (node.right.sentinel and node.left.sentinel) then
if (node != this.root) then
if (node == node.parent.left) then
set node.parent.left = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.sentinelNode
else
set node.parent.right = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.sentinelNode
endif
else
set this.root = 0
endif
call node.destroy()
else
if (node.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
if (node.right.sentinel) then
if (node.parent.left == node) then
set node.parent.left = node.left
else
set node.parent.right = node.left
endif
set node.left.parent = node.parent
if (node == this.root) then
set this.root = node.left
endif
else
if (node.parent.left == node) then
set node.parent.left = node.right
else
set node.parent.right = node.right
endif
set node.right.parent = node.parent
if (node == this.root) then
set this.root = node.right
endif
endif
else
if (node.right.sentinel) then
if (node.parent.left == node) then
set node.parent.left = node.left
else
set node.parent.right = node.left
endif
set node.left.parent = node.parent
if (node == this.root) then
set this.root = node.left
endif
if (node.left.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
set node.left.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
else
call this.removeCaseOne.evaluate(node.left)
endif
else
if (node.parent.left == node) then
set node.parent.left = node.right
else
set node.parent.right = node.right
endif
set node.right.parent = node.parent
if (node == this.root) then
set this.root = node.right
endif
if (node.right.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
set node.right.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
else
call this.removeCaseOne.evaluate(node.right)
endif
endif
endif
call node.destroy()
endif
endmethod
private method insertCaseFive takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode grandparent = node.grandparent
set node.parent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
set grandparent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
if (node == node.parent.left and node.parent == grandparent.left) then
call grandparent.rotateRight()
else
call grandparent.rotateLeft()
endif
endmethod
private method insertCaseFour takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode grandparent = node.grandparent
if (node == node.parent.right and node.parent == grandparent.left) then
call node.parent.rotateLeft()
set node = node.left
elseif (node == node.parent.left and node.parent == grandparent.right) then
call node.parent.rotateRight()
set node = node.right
endif
call this.insertCaseFive(node)
endmethod
private method insertCaseThree takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode uncle = node.uncle
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode grandparent = node.grandparent
if (uncle != 0) then
if (uncle.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
set node.parent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
set uncle.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
set grandparent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
call this.insertCaseOne.evaluate(grandparent)
else
call this.insertCaseFour(node)
endif
else
call this.insertCaseFour(node)
endif
endmethod
private method insertCaseTwo takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
if (node.parent.color != $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK) then
call this.insertCaseThree(node)
endif
endmethod
private method insertCaseOne takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
if (node.parent == 0) then
set node.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
else
call this.insertCaseTwo(node)
endif
endmethod
private method removeCaseSix takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode sibling = node.sibling
set sibling.color = node.parent.color
set node.parent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
if (node == node.parent.left) then
set sibling.right.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
call node.parent.rotateLeft()
else
set sibling.left.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
call node.parent.rotateRight()
endif
endmethod
private method removeCaseFive takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode sibling = node.sibling
if (sibling.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK) then
if (node == node.parent.left and sibling.right.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.left.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
set sibling.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
set sibling.left.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
call sibling.rotateRight()
elseif (node == node.parent.right and sibling.left.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.right.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
set sibling.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
set sibling.right.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
call sibling.rotateLeft()
endif
endif
call this.removeCaseSix(node)
endmethod
private method removeCaseFour takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode sibling = node.sibling
if (node.parent.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED and sibling.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.left.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.right.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK) then
set sibling.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
set node.parent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
else
call this.removeCaseFive(node)
endif
endmethod
private method removeCaseThree takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode sibling = node.sibling
if (node.parent.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.left.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK and sibling.right.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK) then
set sibling.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
call this.removeCaseOne.evaluate(node.parent)
else
call this.removeCaseFour(node)
endif
endmethod
private method removeCaseTwo takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode sibling = node.sibling
if (sibling.color == $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED) then
set node.parent.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.RED
set sibling.color = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode.BLACK
if (node == node.parent.left) then
call node.parent.rotateLeft()
else
call node.parent.rotateRight()
endif
endif
call this.removeCaseThree(node)
endmethod
private method removeCaseOne takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node returns nothing
if (node.parent != 0) then
call this.removeCaseTwo(node)
endif
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySet extends $KEY_TYPE_NAME$Set
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m returns thistype
local thistype this = thistype.allocate()
set this.m = m
return this
endmethod
method add takes $KEY_TYPE$ toAdd returns boolean
return false
endmethod
method addAll takes $KEY_TYPE$Collection collection returns nothing
endmethod
method remove takes $KEY_TYPE$ toRemove returns boolean
return this.m.remove(toRemove) != 0
endmethod
method contains takes $KEY_TYPE$ element returns boolean
return this.m.containsKey(element)
endmethod
method size takes nothing returns integer
return this.m.size()
endmethod
method isEmpty takes nothing returns boolean
return this.m.isEmpty()
endmethod
method clear takes nothing returns nothing
call this.m.clear()
endmethod
method toArray takes nothing returns $KEY_TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $KEY_TYPE_NAME$Iterator
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySetIterator.create.evaluate(this)
endmethod
method enum takes $KEY_TYPE_NAME$_Enum enum, integer data returns nothing
if (this.m.n == 0) then
return
endif
call this.enumHelper.evaluate(.m.root, enum, data)
endmethod
private method enumHelper takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node, $KEY_TYPE_NAME$_Enum enum, integer data returns nothing
if (node.left.sentinel == false) then
call this.enumHelper(node.left, enum, data)
endif
call enum.evaluate(node.key(), data)
if (node.right.sentinel == false) then
call this.enumHelper(node.right, enum, data)
endif
endmethod
private method toArrayHelper takes nothing returns $KEY_TYPE_NAME$Array
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.m.root
local $KEY_TYPE_NAME$Array ar
local integer n = 0
if (node == 0) then
return 0
endif
set ar = $KEY_TYPE_NAME$Array.create(this.m.size())
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
loop
set ar[n] = node.key()
set n = n + 1
if (node.right.sentinel == false) then
set node = node.right
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
else
loop
exitwhen (node.parent == 0 or node != node.parent.right)
set node = node.parent
endloop
set node = node.parent
endif
exitwhen (node == 0 or node.sentinel)
endloop
return ar
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollection extends $VALUE_TYPE_NAME$Collection
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m returns thistype
local thistype this = thistype.allocate()
set this.m = m
return this
endmethod
method add takes $VALUE_TYPE$ toAdd returns boolean
return false
endmethod
method addAll takes $VALUE_TYPE_NAME$Collection collection returns nothing
endmethod
method remove takes $VALUE_TYPE$ toRemove returns boolean
return this.removeValue.evaluate(toRemove)
endmethod
method contains takes $VALUE_TYPE$ element returns boolean
return this.m.containsValue(element)
endmethod
method size takes nothing returns integer
return this.m.size()
endmethod
method isEmpty takes nothing returns boolean
return this.m.isEmpty()
endmethod
method clear takes nothing returns nothing
call this.m.clear()
endmethod
method toArray takes nothing returns $VALUE_TYPE_NAME$Array
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $VALUE_TYPE_NAME$Iterator
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollectionIterator.create.evaluate(this)
endmethod
method enum takes $VALUE_TYPE_NAME$_Enum enum, integer data returns nothing
if (this.m.n == 0) then
return
endif
call this.enumHelper.evaluate(this.m.root, enum, data)
endmethod
private method removeValue takes $VALUE_TYPE$ toRemove returns boolean
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.m.root
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
loop
if (node.valueV == toRemove) then
call this.m.removeNode.evaluate(node)
set this.m.n = this.m.n - 1
return true
endif
if (node.right.sentinel == false) then
set node = node.right
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
else
loop
exitwhen (node.parent == 0 or node != node.parent.right)
set node = node.parent
endloop
set node = node.parent
endif
exitwhen (node.sentinel or node == 0)
endloop
return false
endmethod
private method enumHelper takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node, $VALUE_TYPE_NAME$_Enum enum, integer data returns nothing
if (node.left.sentinel == false) then
call this.enumHelper(node.left, enum, data)
endif
call enum.evaluate(node.value(), data)
if (node.right.sentinel == false) then
call this.enumHelper(node.right, enum, data)
endif
endmethod
private method toArrayHelper takes nothing returns $VALUE_TYPE_NAME$Array
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.m.root
local $VALUE_TYPE_NAME$Array ar
local integer n = 0
if (node == 0) then
return 0
endif
set ar = $VALUE_TYPE_NAME$Array.create(this.m.size())
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
loop
set ar[n] = node.value()
set n = n + 1
if (node.right.sentinel == false) then
set node = node.right
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
else
loop
exitwhen (node.parent == 0 or node != node.parent.right)
set node = node.parent
endloop
set node = node.parent
endif
exitwhen (node == 0 or node.sentinel)
endloop
return ar
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySet extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntrySet
$KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMap m returns thistype
local thistype this = thistype.allocate()
set this.m = m
return this
endmethod
method add takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry toAdd returns boolean
return false
endmethod
method remove takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry toRemove returns boolean
return this.m.remove(toRemove.key()) != 0
endmethod
method contains takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry entry returns boolean
return this.m.containsKey(entry.key())
endmethod
method size takes nothing returns integer
return this.m.size()
endmethod
method isEmpty takes nothing returns boolean
return this.m.isEmpty()
endmethod
method clear takes nothing returns nothing
call this.m.clear()
endmethod
method toArray takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray
return this.toArrayHelper.evaluate()
endmethod
method iterator takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryIterator
return $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySetIterator.create.evaluate(this)
endmethod
method enum takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry_Enum enum, integer data returns nothing
if (this.m.n == 0) then
return
endif
call this.enumHelper.evaluate(this.m.root, enum, data)
endmethod
private method enumHelper takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node, $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry_Enum enum, integer data returns nothing
if (node.left.sentinel == false) then
call this.enumHelper(node.left, enum, data)
endif
call enum.evaluate(node, data)
if (node.right.sentinel == false) then
call this.enumHelper(node.right, enum, data)
endif
endmethod
private method toArrayHelper takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapNode node = this.m.root
local $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray ar
local integer n = 0
if (node == 0) then
return 0
endif
set ar = $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray.create(this.m.size())
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
loop
set ar[n] = node
set n = n + 1
if (node.right.sentinel == false) then
set node = node.right
loop
exitwhen (node.left.sentinel)
set node = node.left
endloop
else
loop
exitwhen (node.parent == 0 or node != node.parent.right)
set node = node.parent
endloop
set node = node.parent
endif
exitwhen (node == 0 or node.sentinel)
endloop
return ar
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySetIterator extends $KEY_TYPE_NAME$Iterator
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySet s
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray ar
private integer n
private integer current = 0
private integer last = -1
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapKeySet s returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.ar = s.toArray()
set this.n = s.m.n
return this
endmethod
method hasNext takes nothing returns boolean
return this.current < this.n
endmethod
method next takes nothing returns $KEY_TYPE$
if (this.hasNext()) then
set this.last = this.current
set this.current = this.current + 1
return this.ar[this.last].key()
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != -1) then
call this.s.m.removeNode.evaluate(this.ar[this.last])
set this.s.m.n = this.s.m.n - 1
set this.last = -1
endif
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollectionIterator extends $VALUE_TYPE_NAME$Iterator
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollection c
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray ar
private integer n
private integer current = 0
private integer last = -1
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapValueCollection c returns thistype
local thistype this = thistype.allocate()
set this.c = c
set this.ar = c.toArray()
set this.n = c.m.n
return this
endmethod
method hasNext takes nothing returns boolean
return this.current < this.n
endmethod
method next takes nothing returns $VALUE_TYPE$
if (this.hasNext()) then
set this.last = this.current
set this.current = this.current + 1
return this.ar[this.last].value()
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != -1) then
call this.c.m.removeNode.evaluate(this.ar[this.last])
set this.c.m.n = this.c.m.n - 1
set this.last = -1
endif
endmethod
endstruct
struct $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySetIterator extends $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryIterator
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySet s
private $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntryArray ar
private integer n
private integer current = 0
private integer last = -1
static method create takes $KEY_TYPE_NAME$$VALUE_TYPE_NAME$TreeMapEntrySet s returns thistype
local thistype this = thistype.allocate()
set this.s = s
set this.ar = s.toArray()
set this.n = s.m.n
return this
endmethod
method hasNext takes nothing returns boolean
return this.current < this.n
endmethod
method next takes nothing returns $KEY_TYPE_NAME$$VALUE_TYPE_NAME$MapEntry
if (this.hasNext()) then
set this.last = this.current
set this.current = this.current + 1
return this.ar[this.last]
endif
return 0
endmethod
method remove takes nothing returns nothing
if (this.last != -1) then
call this.s.m.removeNode.evaluate(this.ar[this.last])
set this.s.m.n = this.s.m.n - 1
set this.last = -1
endif
endmethod
endstruct
endlibrary
//! endtextmacro
//TESH.scrollpos=81
//TESH.alwaysfold=0
Sort
v0.6.0
=========
=========
Using Sort
==========
Requirements:
-------------
-Collection
To implement Sort, you first need to implement Collection (see the Collection documentation).
After you have implemented Collection for a data type, you may then implement Sort for the
same data type.
An important thing to note is that Sort may ONLY be implemented for data types that support
the < and > boolean operators. Data types that support these are limited to integer
and struct types that have overloaded the < operator.
The textmacro to implement Sort is named: "Sort"
The textmacro must be run outside of any library or scope.
For example, to implement Sort for the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro Sort("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
N/A.
Tips for Use:
-------------
Sort allows you to sort elements within an Array or List. Sorting algorithms are computation and
time intensive, so use sorting methods with caution. All the sorting methods provided by this
module are not stable, meaning equal-valued elements may or may not retain their original order
after sorting.
======================
module <TYPE_NAME>Sort
======================
This module provides two methods capable of sorting elements with an Array or List. The final
order of elements after sorting may either be determined by a comparator or the natural ordering
of the elements (as defined by the < and > boolean operators). These sorts are not stable,
meaning elements with equal values may or may not retain their original ordering after sorting.
Sort provides methods to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Instance Methods
================
------
method sort takes <TYPE_NAME>_Comparator c returns nothing
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Sorts the elements in this array or list such that lower-valued elements always appear before
higher-valued elements. The final order of the elements is determined by a comparator if one
is specified. Otherwise, the final order is determined by the natural ordering of the elements
defined by the < and > boolean operators. This sort is not stable; elements with equal values
may or may not retain their original ordering after sorting.
This method offers an average-case run-time of O(n * log n) with a worst-case run time of
O(n^2).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the final order of elements.
------
method sortByIndex takes <TYPE_NAME>_Comparator c, integer fromIndex, integer toIndex
returns nothing
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Sorts the elements in this array or list between fromIndex (inclusive) and toIndex (exclusive)
such that lower-valued elements always appear before higher-valued elements. The final order of
the elements is determined by a comparator if one is specified. Otherwise, the final order is
determined by the natural ordering of the elements defined by the < and > boolean operators.
This sort is not stable; elements with equal values may or may not retain their original
ordering after sorting.
This method offers an average-case run-time of O(n * log n) with a worst-case run time of
O(n^2).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the final order of elements.
-integer fromIndex - the index of the first element (inclusive) to be sorted.
-integer toIndex - the index of the last element (exclusive) to be sorted.
//TESH.scrollpos=82
//TESH.alwaysfold=0
StableSort
v0.6.0
==========
==========
Using StableSort
================
Requirements:
-------------
-Collection
To implement StableSort, you first need to implement Collection (see the Collection
documentation). After you have implemented Collection for a data type, you may then implement
StableSort for the same data type.
An important thing to note is that StableSort may ONLY be implemented for data types that support
the < and > boolean operators. Data types that support these are limited to integer
and struct types that have overloaded the < operator.
The textmacro to implement StableSort is named: "StableSort"
The textmacro must be run outside of any library or scope.
For example, to implement StableSort for the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro StableSort("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
N/A.
Tips for Use:
-------------
StableSort allows you to sort elements within an Array or List. Sorting algorithms are
computation and time intensive, so use sorting methods with caution. All the sorting methods
provided by this module are stable, meaning equal-valued elements retain their original order
after sorting. The algorithm used by StableSort is on average 1.5 to 2 times slower than the
algorithm used by Sort. Therefore, if a stable sort is not a priority, it is highly recommended
you use Sort instead. Attempting to sort more than about 1200 elements at once may hit the
op-limit.
============================
module <TYPE_NAME>StableSort
============================
This module provides two methods capable of sorting elements with an array or list. The final
order of elements after sorting may either be determined by a comparator or the natural ordering
of the elements (as defined by the < and > boolean operators). These sorts are stable, meaning
elements with equal values retain their original ordering after sorting.
StableSort provides its sorting methods to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Instance Methods
================
------
method stableSort takes <TYPE_NAME>_Comparator c returns nothing
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Sorts the elements in this array or list such that lower-valued elements always appear before
higher-valued elements. The final order of the elements is determined by a comparator if one
is specified. Otherwise, the final order is determined by the natural ordering of the elements
defined by the < and > boolean operators. This sort is stable, meaning elements with equal
values retain their original ordering after sorting.
This method guarantees average and worst-case run time of O(n * log n).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the final order of elements.
------
method stableSortByIndex takes <TYPE_NAME>_Comparator c, integer fromIndex, integer toIndex
returns nothing
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Sorts the elements in this array or list between fromIndex (inclusive) and toIndex (exclusive)
such that lower-valued elements always appear before higher-valued elements. The final order of
the elements is determined by a comparator if one is specified. Otherwise, the final order is
determined by the natural ordering of the elements defined by the < and > boolean operators.
This sort is stable, meaning elements with equal values retain their original ordering after
sorting.
This method guarantees average and worst-case run time of O(n * log n).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the final order of elements.
-integer fromIndex - the index of the first element (inclusive) to be sorted.
-integer toIndex - the index of the last element (exclusive) to be sorted.
//TESH.scrollpos=189
//TESH.alwaysfold=0
Selection
v0.6.0
=========
=========
Using Selection
===============
Requirements:
-------------
-Collection
To implement Selection, you first need to implement Collection (see the Collection
documentation). After you have implemented Collection for a data type, you may then implement
Selection for the same data type.
An important thing to note is that Selection may ONLY be implemented for data types that support
the < and > boolean operators. Data types that support these are limited to integer
and struct types that have overloaded the < operator.
The textmacro to implement Selection is named: "Selection"
The textmacro must be run outside of any library or scope.
For example, to implement Selection for the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro Selection("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
N/A.
Tips for Use:
-------------
Selection allows you to retrieve the kth lowest element from an Array or Collection, whether the
array or collection is sorted or not. While selection is generally faster than sorting, if you
expect to make multiple selections from the same array or list sequentially, it is a better idea
to simply sort the array or list once and then retrieve elements by index. In general, if you
want to select from a pre-sorted array or collection, it is better to retrieve elements by index
(either directly in the case of dynamic arrays and lists or via Collection.toArray() for other
collection objects) than to retrieve them using the methods provided by this module.
============================
module <TYPE_NAME>Selection
============================
This module provides methods capable of retrieving the kth lowest element from an Array or
Collection, whether the array or collection is sorted or not. The ordering of elements used to
retrieve the kth lowest element may either be determined by a comparator or the natural ordering
of the elements (as defined by the < and > boolean operators).
Selection provides methods to:
-<TYPE_NAME>Array
-<TYPE_NAME>Collection
Instance Methods
================
------
method max takes <TYPE_NAME>_Comparator c returns $TYPE$
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>Collection
Retrieves the maximum element in this array or collection. The ordering of elements used to
determine which element is the maximum is determined by a comparator if one is specified.
Otherwise, the ordering is determined by the natural ordering of the elements defined by the
< and > boolean operators.
This method guarantees worst, average, and best-case run times of O(n).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements.
Returns:
The maximum element in this array or collection, or 0 if this array or collection is empty or
only contains 0 as elements.
------
method maxByIndex takes <TYPE_NAME>_Comparator c, integer fromIndex, integer toIndex
returns $TYPE$
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Retrieves the maximum element in this array or list between fromIndex (inclusive) and toIndex
(exclusive). The ordering of elements used to determine which element is the maximum is
determined by a comparator if one is specified. Otherwise, the ordering is determined by the
natural ordering of the elements defined by the < and > boolean operators.
This method guarantees worst, average, and best-case run times of O(n).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements.
-integer fromIndex - the index of the first element (inclusive) eligible for selection.
-integer toIndex - the index of the last element (exclusive) eligible for selection.
Returns:
The maximum element in this array or list between fromIndex and toIndex, or 0 if this array or
list is empty or only contains 0 as elements, or if either fromIndex or toIndex is out of
bounds.
------
method min takes <TYPE_NAME>_Comparator c returns $TYPE$
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>Collection
Retrieves the minimum element in this array or collection. The ordering of elements used to
determine which element is the minimum is determined by a comparator if one is specified.
Otherwise, the ordering is determined by the natural ordering of the elements defined by the
< and > boolean operators.
This method guarantees worst, average, and best-case run times of O(n).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements.
Returns:
The minimum element in this array or collection, or 0 if this array or collection is empty or
only contains 0 as elements.
------
method minByIndex takes <TYPE_NAME>_Comparator c, integer fromIndex, integer toIndex
returns $TYPE$
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Retrieves the minimum element in this array or list between fromIndex (inclusive) and toIndex
(exclusive). The ordering of elements used to determine which element is the minimum is
determined by a comparator if one is specified. Otherwise, the ordering is determined by the
natural ordering of the elements defined by the < and > boolean operators.
This method guarantees worst, average, and best-case run times of O(n).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements.
-integer fromIndex - the index of the first element (inclusive) eligible for selection.
-integer toIndex - the index of the last element (exclusive) eligible for selection.
Returns:
The minimum element in this array or list between fromIndex and toIndex, or 0 if this array or
list is empty or only contains 0 as elements, or if either fromIndex or toIndex is out of
bounds.
------
method select takes <TYPE_NAME>_Comparator c, integer k returns $TYPE$
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>Collection
Retrieves the kth smallest element in this array or collection. The ordering of elements used to
determine which element is the kth smallest is determined by a comparator if one is specified.
Otherwise, the ordering is determined by the natural ordering of the elements defined by the
< and > boolean operators.
This method offers an average-case run time of O(n) and a worst-case run time of O(n^2).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements.
-integer k - the distance of the desired element from the minimum element in an ordered list
plus one. If all elements in this array or collection were put into an ordered
array in ascending order (the minimum element at index 0), k = i + 1, where i is
the index in the ordered array at which the desired element would be located.
Returns:
The kth smallest element in this array or collection, or 0 if this array or collection is empty
or only contains 0 as elements, or if k is out of bounds (k < 1 or k > this.size()).
------
method selectByIndex takes <TYPE_NAME>_Comparator c, integer k, integer fromIndex,
integer toIndex returns $TYPE$
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Retrieves the kth smallest element in this array or list between fromIndex (inclusive) to
toIndex (exclusive). The ordering of elements used to determine which element is the kth
smallest is determined by a comparator if one is specified. Otherwise, the ordering is
determined by the natural ordering of the elements defined by the < and > boolean operators.
This method offers an average-case run time of O(n) and a worst-case run time of O(n^2).
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements.
-integer k - the distance of the desired element from the minimum element in an ordered list
plus one. If all elements between fromIndex and toIndex in this array or list
were put into an ordered array in ascending order (the minimum element at index 0),
k = i + 1, where i is the index in the ordered array at which the desired element
would be located.
-integer fromIndex - the index of the first element (inclusive) eligible for selection.
-integer toIndex - the index of the last element (exclusive) eligible for selection.
Returns:
The kth smallest element in this array or list between fromIndex and toIndex, or 0 if this array
or list is empty or only contains 0 as elements, if k is out of bounds (k < 1 or
k > toIndex - fromIndex), or if either fromIndex or toIndex is out of bounds.
//TESH.scrollpos=94
//TESH.alwaysfold=0
BinarySearch
v0.6.0
=========
=========
Using BinarySearch
==================
Requirements:
-------------
-Collection
To implement BinarySearch, you first need to implement Collection (see the Collection
documentation). After you have implemented Collection for a data type, you may then implement
BinarySearch for the same data type.
An important thing to note is that BinarySearch may ONLY be implemented for data types that
support the < and > boolean operators. Data types that support these are limited to integer
and struct types that have overloaded the < operator.
The textmacro to implement BinarySearch is named: "BinarySearch"
The textmacro must be run outside of any library or scope.
For example, to implement BinarySearch for the data type Data:
//! runtextmacro Collection("Data", "Data", "DATA")
//! runtextmacro BinarySearch("Data", "Data", "DATA")
Additional Storage Space Constraints:
-------------------------------------
N/A.
Tips for Use:
-------------
BinarySearch provides methods to efficiently search for elements inside sorted arrays and lists.
This methods provided by this module only work on sorted arrays or lists, meaning you need to
either ensure that they stay sorted when adding or removing elements or sort them prior to using
BinarySearch (there are two standard sorting modules available: Sort and StableSort).
==============================
module <TYPE_NAME>BinarySearch
==============================
This module provides methods capable of efficiently searching through sorted arrays and lists
for elements in sublinear time. The ordering of the elements within the sorted array or list may
be determined by a comparator or the natural ordering of the elements (as defined by the
< and > boolean operators).
Selection provides methods to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Instance Methods
================
------
method search takes <TYPE_NAME>_Comparator c, <TYPE> e returns integer
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Searches for the specified element in this Array or List using binary search algorithm. This
Array or List must be sorted prior to calling this method, or else the results will be
undefined. If this Array or List contains multiple elements equal to the one specified, there
is no guarantee which one will be found.
This method guarantees O(log n) runtime.
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements. If no
comparator is provided, then the natural ordering of the elements
will be used.
-<TYPE> e - the element for which to search.
Returns:
The index at which the specified element is found, if it is contained in this Array or List.
Otherwise, -(insertion point) - 1, where insertion point is defined as the index at which the
element would be inserted into this Array or List.
------
method searchByIndex takes <TYPE_NAME>_Comparator c, <TYPE> e, integer fromIndex,
integer toIndex returns integer
------
Provided to:
-<TYPE_NAME>Array
-<TYPE_NAME>List
Searches for the specified element in this Array or List between fromIndex (inclusive) and
toIndex (exclusive) using binary search algorithm. This Array or List must be sorted prior to
calling this method, or else the results will be undefined. If this Array or List contains
multiple elements equal to the one specified, there is no guarantee which one will be found.
This method guarantees O(log n) runtime.
Parameters:
-<TYPE_NAME>_Comparator c - the Comparator to use to determine the ordering of elements. If no
comparator is provided, then the natural ordering of the elements
will be used.
-<TYPE> e - the element for which to search.
-integer fromIndex - the leftmost index to be searched (inclusive).
-integer toIndex - the rightmost index to be searched (exclusive).
Returns:
The index at which the specified element is found, if it is contained in this Array or List
within the specified range, or (-(insertion point) - 1), where insertion point is defined as the
index at which the element would be inserted into this Array or List, if the element is not
contained in this Array or List within the specified range, or -1, if either fromIndex or
toIndex is out of bounds, or if toIndex - fromIndex < 1.
//TESH.scrollpos=27
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ Sort +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro Sort takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$Sort requires $TYPE_NAME$Collection
module $TYPE_NAME$ArraySort
method sortByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns nothing
if (fromIndex >= 0 and toIndex <= this.size() and toIndex - fromIndex > 1) then
if (c == 0) then
call this.quickSort.evaluate(fromIndex, toIndex - 1)
else
call this.comparatorQuickSort.evaluate(c, fromIndex, toIndex - 1)
endif
endif
endmethod
method sort takes $TYPE_NAME$_Comparator c returns nothing
call this.sortByIndex(c, 0, this.size())
endmethod
private method quickSort takes integer leftIndex, integer rightIndex returns nothing
local integer pivotIndex
if (rightIndex - leftIndex > 0) then
set pivotIndex = this.partition.evaluate(leftIndex, rightIndex, (leftIndex + rightIndex) / 2)
call this.quickSort(leftIndex, pivotIndex - 1)
call this.quickSort(pivotIndex + 1, rightIndex)
endif
endmethod
private method partition takes integer leftIndex, integer rightIndex, integer pivotIndex returns integer
local $TYPE$ pivot = this[pivotIndex]
local $TYPE$ temp
local integer i = leftIndex
set this[pivotIndex] = this[rightIndex]
set this[rightIndex] = pivot
loop
exitwhen (i >= rightIndex)
if (this[i] <= pivot) then
set temp = this[i]
set this[i] = this[leftIndex]
set this[leftIndex] = temp
set leftIndex = leftIndex + 1
endif
set i = i + 1
endloop
set temp = this[leftIndex]
set this[leftIndex] = this[rightIndex]
set this[rightIndex] = temp
return leftIndex
endmethod
private method comparatorQuickSort takes $TYPE_NAME$_Comparator c, integer leftIndex, integer rightIndex returns nothing
local integer pivotIndex
if (rightIndex - leftIndex > 0) then
set pivotIndex = this.comparatorPartition.evaluate(c, leftIndex, rightIndex, (leftIndex + rightIndex) / 2)
call this.comparatorQuickSort(c, leftIndex, pivotIndex - 1)
call this.comparatorQuickSort(c, pivotIndex + 1, rightIndex)
endif
endmethod
private method comparatorPartition takes $TYPE_NAME$_Comparator c, integer leftIndex, integer rightIndex, integer pivotIndex returns integer
local $TYPE$ pivot = this[pivotIndex]
local $TYPE$ temp
local integer i = leftIndex
set this[pivotIndex] = this[rightIndex]
set this[rightIndex] = pivot
loop
exitwhen (i >= rightIndex)
if (c.evaluate(this[i], pivot) <= 0) then
set temp = this[i]
set this[i] = this[leftIndex]
set this[leftIndex] = temp
set leftIndex = leftIndex + 1
endif
set i = i + 1
endloop
set temp = this[leftIndex]
set this[leftIndex] = this[rightIndex]
set this[rightIndex] = temp
return leftIndex
endmethod
endmodule
module $TYPE_NAME$ListSort
stub method sortByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns nothing
local $TYPE_NAME$Array ar
local $TYPE_NAME$ListTypeIterator it
local integer i
if (fromIndex >= 0 and toIndex <= .size() and toIndex - fromIndex > 1) then
set ar = .toArray()
set it = .listIteratorByIndex(fromIndex)
call ar.sortByIndex(c, fromIndex, toIndex)
set i = fromIndex
loop
exitwhen (it.hasNext() == false or i >= toIndex)
call it.next()
call it.setElement(ar[i])
set i = i + 1
endloop
call ar.destroy()
call it.destroy()
endif
endmethod
stub method sort takes $TYPE_NAME$_Comparator c returns nothing
call .sortByIndex(c, 0, .size())
endmethod
endmodule
module $TYPE_NAME$ArrayListSort
method sortByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns nothing
call this.e.sortByIndex(c, fromIndex, toIndex)
endmethod
endmodule
endlibrary
//! endtextmacro
//TESH.scrollpos=30
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ StableSort +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro StableSort takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$StableSort requires $TYPE_NAME$Collection
module $TYPE_NAME$ArrayStableSort
method stableSortByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns nothing
if (fromIndex >= 0 and toIndex <= this.size() and toIndex - fromIndex > 1) then
if (c == 0) then
call this.mergeSort.evaluate(fromIndex, toIndex)
else
call this.comparatorMergeSort.evaluate(c, fromIndex, toIndex)
endif
endif
endmethod
method stableSort takes $TYPE_NAME$_Comparator c returns nothing
call this.stableSortByIndex(c, 0, this.size())
endmethod
private method mergeSort takes integer begin, integer end returns nothing
local integer middleIndex
local integer size = end - begin
local integer i = begin
if (size <= 1) then
return
endif
set middleIndex = begin + size / 2
call this.mergeSort(begin, middleIndex)
call this.mergeSort(middleIndex, end)
if (this[middleIndex - 1] > this[middleIndex]) then
call this.merge.evaluate(begin, middleIndex, end)
endif
endmethod
private method merge takes integer begin, integer middle, integer end returns nothing
local integer leftSize = middle - begin
local integer rightSize = end - middle
local integer leftIndex = begin
local integer rightIndex = middle
local integer i = 0
local $TYPE_NAME$Array merged = $TYPE_NAME$Array.create(leftSize + rightSize)
loop
exitwhen (leftSize <= 0 or rightSize <= 0)
if (this[leftIndex] <= this[rightIndex]) then
set merged[i] = this[leftIndex]
set leftIndex = leftIndex + 1
set leftSize = leftSize - 1
else
set merged[i] = this[rightIndex]
set rightIndex = rightIndex + 1
set rightSize = rightSize - 1
endif
set i = i + 1
endloop
loop
exitwhen (leftSize <= 0)
set merged[i] = this[leftIndex]
set leftIndex = leftIndex + 1
set leftSize = leftSize - 1
set i = i + 1
endloop
loop
exitwhen (rightSize <= 0)
set merged[i] = this[rightIndex]
set rightIndex = rightIndex + 1
set rightSize = rightSize - 1
set i = i + 1
endloop
set i = begin
loop
exitwhen (i >= end)
set this[i] = merged[i - begin]
set i = i + 1
endloop
call merged.destroy()
endmethod
private method comparatorMergeSort takes $TYPE_NAME$_Comparator c, integer begin, integer end returns nothing
local integer middleIndex
local integer size = end - begin
local integer i = begin
if (size <= 1) then
return
endif
set middleIndex = begin + size / 2
call this.comparatorMergeSort(c, begin, middleIndex)
call this.comparatorMergeSort(c, middleIndex, end)
if (c.evaluate(this[middleIndex - 1], this[middleIndex]) > 0) then
call this.comparatorMerge.evaluate(c, begin, middleIndex, end)
endif
endmethod
private method comparatorMerge takes $TYPE_NAME$_Comparator c, integer begin, integer middle, integer end returns nothing
local integer leftSize = middle - begin
local integer rightSize = end - middle
local integer leftIndex = begin
local integer rightIndex = middle
local integer i = 0
local $TYPE_NAME$Array merged = $TYPE_NAME$Array.create(leftSize + rightSize)
loop
exitwhen (leftSize <= 0 or rightSize <= 0)
if (c.evaluate(this[leftIndex], this[rightIndex]) <= 0) then
set merged[i] = this[leftIndex]
set leftIndex = leftIndex + 1
set leftSize = leftSize - 1
else
set merged[i] = this[rightIndex]
set rightIndex = rightIndex + 1
set rightSize = rightSize - 1
endif
set i = i + 1
endloop
loop
exitwhen (leftSize <= 0)
set merged[i] = this[leftIndex]
set leftIndex = leftIndex + 1
set leftSize = leftSize - 1
set i = i + 1
endloop
loop
exitwhen (rightSize <= 0)
set merged[i] = this[rightIndex]
set rightIndex = rightIndex + 1
set rightSize = rightSize - 1
set i = i + 1
endloop
set i = begin
loop
exitwhen (i >= end)
set this[i] = merged[i - begin]
set i = i + 1
endloop
call merged.destroy()
endmethod
endmodule
module $TYPE_NAME$ListStableSort
stub method stableSortByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns nothing
local $TYPE_NAME$Array ar
local $TYPE_NAME$ListTypeIterator it
local integer i
if (fromIndex >= 0 and toIndex <= this.size() and toIndex - fromIndex > 1) then
set ar = this.toArray()
set it = this.listIteratorByIndex(fromIndex)
call ar.stableSortByIndex(c, fromIndex, toIndex)
set i = fromIndex
loop
exitwhen (it.hasNext() == false or i >= toIndex)
call it.next()
call it.setElement(ar[i])
set i = i + 1
endloop
call ar.destroy()
call it.destroy()
endif
endmethod
stub method stableSort takes $TYPE_NAME$_Comparator c returns nothing
call this.stableSortByIndex(c, 0, .size())
endmethod
endmodule
module $TYPE_NAME$ArrayListStableSort
method stableSortByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns nothing
call this.e.stableSortByIndex(c, fromIndex, toIndex)
endmethod
endmodule
endlibrary
//! endtextmacro
//TESH.scrollpos=40
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ Selection +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro Selection takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$Selection requires $TYPE_NAME$Collection
module $TYPE_NAME$ArraySelection
method selectByIndex takes $TYPE_NAME$_Comparator c, integer k, integer fromIndex, integer toIndex returns $TYPE$
local $TYPE_NAME$Array ar
local $TYPE$ e = 0
if (fromIndex >= 0 and toIndex <= .size() and k >= 1 and k <= toIndex - fromIndex and toIndex - fromIndex > 1) then
set ar = this.copySelect.evaluate(fromIndex, toIndex)
if (c == 0) then
set e = ar.selectHelper.evaluate(k - 1, fromIndex, toIndex - 1)
else
set e = ar.comparatorSelectHelper.evaluate(c, k - 1, fromIndex, toIndex - 1)
endif
call ar.destroy()
endif
return e
endmethod
method select takes $TYPE_NAME$_Comparator c, integer k returns $TYPE$
return this.selectByIndex(c, k, 0, .size())
endmethod
method minByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns $TYPE$
if (fromIndex >= 0 and toIndex <= .size() and toIndex - fromIndex > 1) then
return this.minHelper.evaluate(c, fromIndex, toIndex)
endif
return 0
endmethod
method min takes $TYPE_NAME$_Comparator c returns $TYPE$
return this.minByIndex(c, 0, this.size())
endmethod
method maxByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns $TYPE$
if (fromIndex >= 0 and toIndex <= .size() and toIndex - fromIndex > 1) then
return this.maxHelper.evaluate(c, fromIndex, toIndex)
endif
return 0
endmethod
method max takes $TYPE_NAME$_Comparator c returns $TYPE$
return this.maxByIndex(c, 0, this.size())
endmethod
private method minHelper takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns $TYPE$
local integer i = fromIndex + 1
local $TYPE$ temp
set temp = this[fromIndex]
loop
exitwhen (i >= toIndex)
if (c != 0) then
if (c.evaluate(this[i], temp) < 0) then
set temp = this[i]
endif
else
if (this[i] < temp) then
set temp = this[i]
endif
endif
set i = i + 1
endloop
return temp
endmethod
private method maxHelper takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns $TYPE$
local integer i = fromIndex + 1
local $TYPE$ temp
set temp = this[fromIndex]
loop
exitwhen (i >= toIndex)
if (c != 0) then
if (c.evaluate(this[i], temp) > 0) then
set temp = this[i]
endif
else
if (this[i] > temp) then
set temp = this[i]
endif
endif
set i = i + 1
endloop
return temp
endmethod
private method selectHelper takes integer k, integer left, integer right returns $TYPE$
local integer pivotIndex = this.partition.evaluate(left, right, (left + right) / 2)
if (k == pivotIndex) then
return this[pivotIndex]
elseif (k < pivotIndex) then
return this.selectHelper(k, left, pivotIndex - 1)
endif
return this.selectHelper(k, pivotIndex + 1, right)
endmethod
private method partition takes integer leftIndex, integer rightIndex, integer pivotIndex returns integer
local $TYPE$ pivot = this[pivotIndex]
local $TYPE$ temp
local integer i = leftIndex
set this[pivotIndex] = this[rightIndex]
set this[rightIndex] = pivot
loop
exitwhen (i >= rightIndex)
if (this[i] <= pivot) then
set temp = this[i]
set this[i] = this[leftIndex]
set this[leftIndex] = temp
set leftIndex = leftIndex + 1
endif
set i = i + 1
endloop
set temp = this[leftIndex]
set this[leftIndex] = this[rightIndex]
set this[rightIndex] = temp
return leftIndex
endmethod
private method comparatorSelectHelper takes $TYPE_NAME$_Comparator c, integer k, integer left, integer right returns $TYPE$
local integer pivotIndex = this.comparatorPartition.evaluate(c, left, right, (left + right) / 2)
if (k == pivotIndex) then
return this[pivotIndex]
elseif (k < pivotIndex) then
return this.comparatorSelectHelper(c, k, left, pivotIndex - 1)
endif
return this.comparatorSelectHelper(c, k, pivotIndex + 1, right)
endmethod
private method comparatorPartition takes $TYPE_NAME$_Comparator c, integer leftIndex, integer rightIndex, integer pivotIndex returns integer
local $TYPE$ pivot = this[pivotIndex]
local $TYPE$ temp
local integer i = leftIndex
set this[pivotIndex] = this[rightIndex]
set this[rightIndex] = pivot
loop
exitwhen (i >= rightIndex)
if (c.evaluate(this[i], pivot) <= 0) then
set temp = this[i]
set this[i] = this[leftIndex]
set this[leftIndex] = temp
set leftIndex = leftIndex + 1
endif
set i = i + 1
endloop
set temp = this[leftIndex]
set this[leftIndex] = this[rightIndex]
set this[rightIndex] = temp
return leftIndex
endmethod
private method copySelect takes integer fromIndex, integer toIndex returns $TYPE_NAME$Array
local $TYPE_NAME$Array copy = $TYPE_NAME$Array.create(toIndex - fromIndex)
local integer i = fromIndex
loop
exitwhen (i >= toIndex)
set copy[i - fromIndex] = this[i]
set i = i + 1
endloop
return copy
endmethod
endmodule
module $TYPE_NAME$CollectionSelection
stub method select takes $TYPE_NAME$_Comparator c, integer k returns $TYPE$
local $TYPE_NAME$Array ar = this.toArray()
local $TYPE$ e
set e = ar.selectByIndex(c, k, 0, ar.size())
call ar.destroy()
return e
endmethod
stub method min takes $TYPE_NAME$_Comparator c returns $TYPE$
return this.select(c, 1)
endmethod
stub method max takes $TYPE_NAME$_Comparator c returns $TYPE$
return this.select(c, this.size())
endmethod
endmodule
module $TYPE_NAME$ListSelection
stub method selectByIndex takes $TYPE_NAME$_Comparator c, integer k, integer fromIndex, integer toIndex returns $TYPE$
local $TYPE_NAME$Array ar
local $TYPE$ e = 0
if (fromIndex >= 0 and toIndex <= .size() and k >= 1 and k <= toIndex - fromIndex and toIndex - fromIndex > 1) then
set ar = this.toArray()
set e = ar.selectByIndex(c, k, fromIndex, toIndex)
call ar.destroy()
endif
return e
endmethod
stub method minByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns $TYPE$
return this.selectByIndex(c, 1, fromIndex, toIndex)
endmethod
stub method maxByIndex takes $TYPE_NAME$_Comparator c, integer fromIndex, integer toIndex returns $TYPE$
return this.selectByIndex(c, toIndex - fromIndex, fromIndex, toIndex)
endmethod
endmodule
endlibrary
//! endtextmacro
//TESH.scrollpos=6
//TESH.alwaysfold=0
/*
++++++++++++++++++++++++
+ Selection +
+ v0.6.0 +
+ Author: aznricepuff +
++++++++++++++++++++++++
*/
//! textmacro BinarySearch takes TYPE, TYPE_NAME, TYPE_CONSTANT_NAME
library $TYPE_NAME$BinarySearch requires $TYPE_NAME$Collection
module $TYPE_NAME$ArrayBinarySearch
method searchByIndex takes $TYPE_NAME$_Comparator c, $TYPE$ e, integer fromIndex, integer toIndex returns integer
if (fromIndex >= 0 and toIndex <= this.size() and toIndex - fromIndex > 0) then
if (c == 0) then
return this.binarySearch.evaluate(e, fromIndex, toIndex)
else
return this.comparatorBinarySearch.evaluate(c, e, fromIndex, toIndex)
endif
endif
return -1
endmethod
method search takes $TYPE_NAME$_Comparator c, $TYPE$ e returns integer
return this.searchByIndex(c, e, 0, this.size())
endmethod
private method binarySearch takes $TYPE$ e, integer left, integer right returns integer
local integer middle
loop
set middle = (right + left) / 2
exitwhen (right - left <= 0)
if (this[middle] < e) then
set left = middle + 1
elseif (this[middle] > e) then
set right = middle
else
return middle
endif
endloop
return -middle - 1
endmethod
private method comparatorBinarySearch takes $TYPE_NAME$_Comparator c, $TYPE$ e, integer left, integer right returns integer
local integer middle
local integer order
loop
set middle = (right + left) / 2
exitwhen (right - left <= 0)
set order = c.evaluate(this[middle], e)
if (order < 0) then
set left = middle + 1
elseif (order > 0) then
set right = middle
else
return middle
endif
endloop
return -middle - 1
endmethod
endmodule
module $TYPE_NAME$ListBinarySearch
stub method searchByIndex takes $TYPE_NAME$_Comparator c, $TYPE$ e, integer fromIndex, integer toIndex returns integer
local $TYPE_NAME$Array ar = this.toArray()
local integer i = ar.searchByIndex(c, e, fromIndex, toIndex)
call ar.destroy()
return i
endmethod
stub method search takes $TYPE_NAME$_Comparator c, $TYPE$ e returns integer
return this.searchByIndex(c, e, 0, this.size())
endmethod
endmodule
module $TYPE_NAME$ArrayListBinarySearch
stub method searchByIndex takes $TYPE_NAME$_Comparator c, $TYPE$ e, integer fromIndex, integer toIndex returns integer
return this.e.searchByIndex(c, e, fromIndex, toIndex)
endmethod
endmodule
endlibrary
//! endtextmacro
//TESH.scrollpos=0
//TESH.alwaysfold=0
v0.6.0
-Removed the Set and List textmacros. Set and List are now automatically implemented when implementing Collection.
-Added the following optional modules:
- Sort
- StableSort
- Selection
- BinarySearch
-Optimized internal code to use as few unnecessary calls to TriggerEvaluate as possible.
-Fixed memory leak in TreeSet's iterator.
-Fixed major bug in BalancedTreeSet where tree rotations were causing tree node references to be lost, the ultimate result of which is that the set stops working.
v0.5.0
-List.listIteratorByIndex() now allows index == List.size() to be specified. In this case the first call to ListTypeIterator.previous() will return the last element in the list.
-TreeSet, BalancedTreeSet, TreeMap, and BalancedTreeMap's addition and removal operations now perform 4 times faster, and search operations (by key for TreeMap) now perform 2 times faster.
v0.4.4
-Fixed bug with LinkedList's list iterator not returning the correct values when calling ListTypeIterator.nextIndex() and ListTypeIterator.previousIndex().
-Fixed bug with LinkedHashSet where it would give a compile error if the textmacro was run for the integer type.
-Fixed documentation error claiming that ListTypeIterator had a .swap() method. It has no such method; this method is in List.
v0.4.3
-Added List.swap() for efficient swapping of elements located at two indices.
-List.listIteratorByIndex() now returns 0 if the index passed to the parameter is out of bounds.
-Documentation now makes it clear that destroying the collections returned by Map.keySet(), Map.values(), and Map.entrySet() will not clear the backing map.
-Removed lingering references in documentation to maximum element limit (not applicable since v0.4.0).
v0.4.2
-Fixed bug with calling TreeSet.clear() and TreeMap.clear() when the set/map is empty.
v0.4.1
-LinkedHashSet no longer uses a hashtable for every instance (now uses one hashtable for all instances).
-LinkedHashMap no longer uses a hashtable for every instance (now uses one hashtable for all instances).
-<TYPE_NAME>Array can now have 0 indices to prevent bugs with Collection.toArray().
-Fixed bug with LinkedHashSet.iterator().remove().
-Fixed bug where calling Collection.iterator() for LinkedHashMap.keySet(), LinkedHashMap.values(), or LinkedHashMap.entrySet() did not work.
v0.4.0
-No longer requires Table.
-Moved each implementation into its own textmacro. (Exceptions are TreeSet/BalancedTreeSet and TreeMap/BalancedTreeMap.)
-Replaced TableSet with LinkedHashSet.
-Replaced TableMap with LinkedHashMap.
-Removed LinkedSet (functionality mirrored by new implementation: LinkedHashSet).
-Removed "Maximum Element/Entry Limit" and "Storage Size" constraints from Collection and Map.
-LinkedHashSet (replacement for TableSet) and LinkedHashMap (replacement for TableMap) no longer use vJASS dynamic arrays.
-ArrayList now uses hashtable indexing (actually a nested <TYPE_NAME>Array) instead of vJASS dynamic array indexing.
-<TYPE_NAME>Array now uses hashtable indexing instead of vJASS dynamic array indexing. Its functionality has also been tweaked (see API).
-Fixed bug where LinkedList.addByIndex() was not working correctly when specifying an index == LinkedList.size().
-Fixed bug in TreeMap where TreeMap.values().enum() would not work correctly if VALUE_TYPE was different from VALUE_TYPE_NAME
v0.3.2
-Changed all "key" identifiers in Map and TreeMap to "k" to avoid any possible conflict with jasshelper's new key type. (Also changed all "value" identifiers to "v" to maintain consistency).
v0.3.1
-Fixed a bug where TableSet's enum() method would enumerate through EVERY index, including the ones that contained no element.
-Added (minimal) debug messages in Set and Map.
v0.3.0
-Added the Comparator function interface to Collection.
-Added five new implementations:
-LinkedSet
-TreeSet
-BalancedTreeSet
-TreeMap
-BalancedTreeMap
-Redid a lot of the documentation.
-Reworked implementation for Collection.removeAll() so that it now takes at least O(n) time with respect to the collection it is called on.
-Fixed struct leaks in LinkedList.
-Fixed bug where values() method of TableMap would always return 0.
v0.2.1
-Expanded the Collection interface to include four new methods:
-addAll takes <TYPE_NAME>Collection collection returns nothing
-removeAll takes <TYPE_NAME>Collection collection returns nothing
-retainAll takes <TYPE_NAME>Collection collection returns nothing
-toArray takes nothing returns nothing
-Fixed documentation errors.
//TESH.scrollpos=45
//TESH.alwaysfold=0
library Implementation
struct A
readonly integer i
static method create takes integer i returns A
local A this = A.allocate()
set this.i = i
return this
endmethod
method operator < takes A a returns boolean
return this.i < a.i
endmethod
method operator == takes A a returns boolean
return this.i == a.i
endmethod
method toString takes nothing returns string
return "Struct A (" + I2S(this) + "):" + I2S(this.i)
endmethod
method print takes nothing returns nothing
call BJDebugMsg(this.toString())
endmethod
endstruct
struct B
readonly string s
static method create takes string s returns B
local B this = B.allocate()
set this.s = s
return this
endmethod
method toString takes nothing returns string
return "Struct B (" + I2S(this) + "):" + this.s
endmethod
method print takes nothing returns nothing
call BJDebugMsg(this.toString())
endmethod
endstruct
endlibrary
//! runtextmacro Collection("A", "A", "A")
//! runtextmacro LinkedHashSet("A", "A", "A")
//! runtextmacro TreeSet("A", "A", "A")
//! runtextmacro ArrayList("A", "A", "A")
//! runtextmacro LinkedList("A", "A", "A")
//! runtextmacro Collection("B", "B", "B")
//! runtextmacro Collection("ABMapEntry", "ABMapEntry", "A_B_MAP_ENTRY")
//! runtextmacro Map("A", "A", "A", "B", "B", "B")
//! runtextmacro LinkedHashMap("A", "A", "A", "B", "B", "B")
//! runtextmacro TreeMap("A", "A", "A", "B", "B", "B")
//! runtextmacro Sort("A", "A", "A")
//! runtextmacro StableSort("A", "A", "A")
//! runtextmacro Selection("A", "A", "A")
//! runtextmacro BinarySearch("A", "A", "A")
//TESH.scrollpos=0
//TESH.alwaysfold=0
library Test initializer init
globals
public boolean array active
public A array a
public B array b
endglobals
private function init takes nothing returns nothing
local integer i = 0
loop
exitwhen (i > 11)
set active[i] = false
set i = i + 1
endloop
set i = 0
loop
exitwhen (i > 100)
set a[i] = A.create(i)
set b[i] = B.create(I2S(i))
set i = i + 1
endloop
call CreateFogModifierRectBJ(true, Player(0), FOG_OF_WAR_VISIBLE, bj_mapInitialPlayableArea)
endfunction
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope LinkedHashSetTest initializer init
globals
private string PREFIX = "LinkedHashSet Test :: "
private real WAIT = 1.0
endglobals
//! textmacro LinkedHashSetTest_End
call s.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local ASet s = ALinkedHashSet.create()
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning LinkedHashSet Test.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedHashSet.add().")
call TriggerSleepAction(WAIT)
if (s.add(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...FAIL")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...FAIL")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro LinkedHashSetTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedHashSet.contains().")
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[0].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[0].toString() + " ...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[8].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[8].toString() + " ...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[1])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[1].toString() + " ...PASS.")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedHashSet.remove().")
call TriggerSleepAction(WAIT)
if (s.remove(Test_a[5]) and s.contains(Test_a[5]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.remove(Test_a[1]) == false and s.contains(Test_a[1]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedHashSet.size().")
call TriggerSleepAction(WAIT)
if (s.size() == 2) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...FAIL.")
//! runtextmacro LinkedHashSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedHashSet.clear().")
call TriggerSleepAction(WAIT)
call s.clear()
if (s.size() == 0 and s.contains(Test_a[0]) == false and s.contains(Test_a[8]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing set...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing set...FAIL")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro TreeSetTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0005, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0005, 75.0)
call SetTextTagText(tag, "Test LinkedHashSet", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=109
//TESH.alwaysfold=0
scope TreeSetTest initializer init
globals
private string PREFIX = "TreeSet Test :: "
private real WAIT = 1.0
endglobals
//! textmacro TreeSetTest_End
call s.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local ASet s = ATreeSet.create(0)
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning TreeSet Test.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of TreeSet.add().")
call TriggerSleepAction(WAIT)
if (s.add(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...FAIL")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...FAIL")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro TreeSetTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of TreeSet.contains().")
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[0].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[0].toString() + " ...FAIL.")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[8].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[8].toString() + " ...FAIL.")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[1])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro TreeSetTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[1].toString() + " ...PASS.")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of TreeSet.remove().")
call TriggerSleepAction(WAIT)
if (s.remove(Test_a[5]) and s.contains(Test_a[5]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.remove(Test_a[1]) == false and s.contains(Test_a[1]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of TreeSet.size().")
call TriggerSleepAction(WAIT)
if (s.size() == 2) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...FAIL.")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of TreeSet.clear().")
call TriggerSleepAction(WAIT)
call s.clear()
if (s.size() == 0 and s.contains(Test_a[0]) == false and s.contains(Test_a[8]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing set...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing set...FAIL")
//! runtextmacro TreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro TreeSetTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0001, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0001, 75.0)
call SetTextTagText(tag, "Test TreeSet", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=43
//TESH.alwaysfold=0
scope BalancedTreeSetTest initializer init
globals
private string PREFIX = "BalancedTreeSet Test :: "
private real WAIT = 1.0
endglobals
//! textmacro BalancedTreeSetTest_End
call s.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local ASet s = ABalancedTreeSet.create(0)
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning BalancedTreeSet Test.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of BalancedTreeSet.add().")
call TriggerSleepAction(WAIT)
if (s.add(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...FAIL")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...FAIL")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro BalancedTreeSetTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of BalancedTreeSet.contains().")
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[0].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[0].toString() + " ...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[8].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[8].toString() + " ...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.contains(Test_a[1])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Set contains: " + Test_a[1].toString() + " ...PASS.")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of BalancedTreeSet.remove().")
call TriggerSleepAction(WAIT)
if (s.remove(Test_a[5]) and s.contains(Test_a[5]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
if (s.remove(Test_a[1]) == false and s.contains(Test_a[1]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of BalancedTreeSet.size().")
call TriggerSleepAction(WAIT)
if (s.size() == 2) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...FAIL.")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of BalancedTreeSet.clear().")
call TriggerSleepAction(WAIT)
call s.clear()
if (s.size() == 0 and s.contains(Test_a[0]) == false and s.contains(Test_a[8]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing set...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing set...FAIL")
//! runtextmacro BalancedTreeSetTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro BalancedTreeSetTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0002, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0002, 75.0)
call SetTextTagText(tag, "Test BalancedTreeSet", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=16
//TESH.alwaysfold=0
scope ArrayListTest initializer init
globals
private string PREFIX = "ArrayList Test :: "
private real WAIT = 1.0
endglobals
//! textmacro ArrayListTest_End
call l.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local AList l = AArrayList.create()
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning ArrayList Test.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.add().")
call TriggerSleepAction(WAIT)
if (l.add(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.add(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.contains().")
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[0].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[0].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[8].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[8].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[1])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[1].toString() + " ...PASS.")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.remove().")
call TriggerSleepAction(WAIT)
if (l.remove(Test_a[5]) and l.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.remove(Test_a[1]) == false and l.contains(Test_a[1]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.size().")
call TriggerSleepAction(WAIT)
if (l.size() == 3) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.getByIndex().")
call TriggerSleepAction(WAIT)
if (l.getByIndex(0) == Test_a[0]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 0...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 0...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.getByIndex(1) == Test_a[8]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 1...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 1...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.getByIndex(2) == Test_a[5]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 2...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 2...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.indexOf().")
call TriggerSleepAction(WAIT)
if (l.indexOf(Test_a[5]) == 2) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.indexOf(Test_a[1]) == -1) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.addByIndex().")
call TriggerSleepAction(WAIT)
call l.addByIndex(1, Test_a[9])
if (l.contains(Test_a[9]) and (l.getByIndex(1) == Test_a[9]) and (l.getByIndex(2) == Test_a[8]) and (l.getByIndex(3) == Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[9].toString() + " to index 1...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[9].toString() + " to index 1...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.addByIndex(4, Test_a[2])
if (l.contains(Test_a[2]) and (l.getByIndex(4) == Test_a[2]) and (l.getByIndex(3) == Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[2].toString() + " to index 4...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[2].toString() + " to index 4...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.addByIndex(10, Test_a[11])
if (l.contains(Test_a[11]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[11].toString() + " to index 10...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[11].toString() + " to index 10...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.removeByIndex().")
call TriggerSleepAction(WAIT)
call l.removeByIndex(2)
if (l.contains(Test_a[8]) == false and (l.getByIndex(2) == Test_a[5]) and (l.getByIndex(3) == Test_a[2])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 2...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 2...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.removeByIndex(10)
if (l.size() == 4) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 10...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 10...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.clear().")
call TriggerSleepAction(WAIT)
call l.clear()
if (l.size() == 0 and l.contains(Test_a[0]) == false and l.contains(Test_a[5]) == false and l.contains(Test_a[2]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing list...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing list...FAIL")
//! runtextmacro ArrayListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro ArrayListTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0003, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0003, 75.0)
call SetTextTagText(tag, "Test ArrayList", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=237
//TESH.alwaysfold=0
scope LinkedListTest initializer init
globals
private string PREFIX = "LinkedList Test :: "
private real WAIT = 1.0
endglobals
//! textmacro LinkedListTest_End
call l.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local ALinkedList l = ALinkedList.create()
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning LinkedList Test.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.add().")
call TriggerSleepAction(WAIT)
if (l.add(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " ...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.add(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[8].toString() + " ...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.add(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " ...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.contains().")
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[0].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[0].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[8])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[8].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[8].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.contains(Test_a[1])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "List contains: " + Test_a[1].toString() + " ...PASS.")
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.remove().")
call TriggerSleepAction(WAIT)
if (l.remove(Test_a[5]) and l.contains(Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.remove(Test_a[1]) == false and l.contains(Test_a[1]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.size().")
call TriggerSleepAction(WAIT)
if (l.size() == 3) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking size...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.getByIndex().")
call TriggerSleepAction(WAIT)
if (l.getByIndex(0) == Test_a[0]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 0...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 0...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.getByIndex(1) == Test_a[8]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 1...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 1...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.getByIndex(2) == Test_a[5]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 2...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index 2...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.indexOf().")
call TriggerSleepAction(WAIT)
if (l.indexOf(Test_a[5]) == 2) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[5].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[5].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if (l.indexOf(Test_a[1]) == -1) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[1].toString() + " ...PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Checking index of: " + Test_a[1].toString() + " ...FAIL.")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.addByIndex().")
call TriggerSleepAction(WAIT)
call l.addByIndex(1, Test_a[9])
if (l.contains(Test_a[9]) and (l.getByIndex(1) == Test_a[9]) and (l.getByIndex(2) == Test_a[8]) and (l.getByIndex(3) == Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[9].toString() + " to index 1...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[9].toString() + " to index 1...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.addByIndex(4, Test_a[2])
if (l.contains(Test_a[2]) and (l.getByIndex(4) == Test_a[2]) and (l.getByIndex(3) == Test_a[5])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[2].toString() + " to index 4...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[2].toString() + " to index 4...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.addByIndex(10, Test_a[11])
if (l.contains(Test_a[11]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[11].toString() + " to index 10...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[11].toString() + " to index 10...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.removeByIndex().")
call TriggerSleepAction(WAIT)
call l.removeByIndex(2)
if (l.contains(Test_a[8]) == false and (l.getByIndex(2) == Test_a[5]) and (l.getByIndex(3) == Test_a[2])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 2...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 2...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.removeByIndex(10)
if (l.size() == 4) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 10...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing: index 10...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.addFirst().")
call TriggerSleepAction(WAIT)
call l.addFirst(Test_a[7])
if (l.contains(Test_a[7]) and (l.getByIndex(0) == Test_a[7]) and (l.getByIndex(1) == Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[7].toString() + " to beginning of list...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[7].toString() + " to beginning of list...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.addFirst(Test_a[0])
if (l.contains(Test_a[0]) and (l.getByIndex(0) == Test_a[0]) and (l.getByIndex(1) == Test_a[7])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " to beginning of list...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[0].toString() + " to beginning of list...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.addLast().")
call TriggerSleepAction(WAIT)
call l.addLast(Test_a[3])
if (l.contains(Test_a[3]) and (l.getByIndex(l.size() - 1) == Test_a[3]) and (l.getByIndex(l.size() - 2) == Test_a[2])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[3].toString() + " to end of list...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[3].toString() + " to end of list...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call l.addLast(Test_a[5])
if (l.contains(Test_a[5]) and (l.getByIndex(l.size() - 1) == Test_a[5]) and (l.getByIndex(l.size() - 2) == Test_a[3])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " to end of list...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Adding: " + Test_a[5].toString() + " to end of list...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.removeFirst().")
call TriggerSleepAction(WAIT)
if ((l.removeFirst() == Test_a[0]) and (l.getByIndex(0) == Test_a[7]) and (l.getByIndex(1) == Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing first element...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing first element...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if ((l.removeFirst() == Test_a[7]) and l.contains(Test_a[7]) == false and (l.getByIndex(0) == Test_a[0])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing first element...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing first element...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.removeLast().")
call TriggerSleepAction(WAIT)
if ((l.removeLast() == Test_a[5]) and (l.getByIndex(l.size() - 1) == Test_a[3]) and (l.getByIndex(l.size() - 2) == Test_a[2])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing last element...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing last element...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
if ((l.removeLast() == Test_a[3]) and l.contains(Test_a[3]) == false and (l.getByIndex(l.size() - 1) == Test_a[2])) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing last element...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Removing last element...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.clear().")
call TriggerSleepAction(WAIT)
call l.clear()
if (l.size() == 0 and l.contains(Test_a[0]) == false and l.contains(Test_a[9]) == false and l.contains(Test_a[5]) == false and l.contains(Test_a[2]) == false) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing list...PASS")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Clearing list...FAIL")
//! runtextmacro LinkedListTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro LinkedListTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0004, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0004, 75.0)
call SetTextTagText(tag, "Test LinkedList", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=0
//TESH.alwaysfold=0
scope SortTest initializer init
globals
private string PREFIX = "Sort Test :: "
private real WAIT = 1.0
private integer ELEMENTS = 100
endglobals
//! textmacro SortTest_End
call ar.destroy()
call al.destroy()
call ll.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local AArray ar = AArray.create(ELEMENTS)
local AArrayList al = AArrayList.create()
local ALinkedList ll = ALinkedList.create()
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
local integer i = 0
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning Sort Test.")
call TriggerSleepAction(WAIT)
loop
exitwhen (i >= ELEMENTS)
set ar[i] = Test_a[GetRandomInt(0, 100)]
call al.add(Test_a[GetRandomInt(0, 100)])
call ll.add(Test_a[GetRandomInt(0, 100)])
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of Array.sort().")
call TriggerSleepAction(WAIT)
call ar.sort(0)
set i = 1
loop
exitwhen (i >= ar.size())
if (ar[i] < ar[i - 1]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SortTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.sort().")
call TriggerSleepAction(WAIT)
call al.sort(0)
set i = 1
loop
exitwhen (i >= al.size())
if (al.getByIndex(i) < al.getByIndex(i - 1)) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SortTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.sort().")
call TriggerSleepAction(WAIT)
call ll.sort(0)
set i = 1
loop
exitwhen (i >= al.size())
if (ll.getByIndex(i) < ll.getByIndex(i - 1)) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SortTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro SortTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0006, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0006, 75.0)
call SetTextTagText(tag, "Test Sort", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=12
//TESH.alwaysfold=0
scope StableSortTest initializer init
globals
private string PREFIX = "StableSort Test :: "
private real WAIT = 1.0
private integer ELEMENTS = 100
endglobals
//! textmacro StableSortTest_End
call ar.destroy()
call al.destroy()
call ll.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function actions takes nothing returns nothing
local AArray ar = AArray.create(ELEMENTS)
local AArrayList al = AArrayList.create()
local ALinkedList ll = ALinkedList.create()
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
local integer i = 0
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning StableSort Test.")
call TriggerSleepAction(WAIT)
loop
exitwhen (i >= ELEMENTS)
set ar[i] = Test_a[GetRandomInt(0, 100)]
call al.add(Test_a[GetRandomInt(0, 100)])
call ll.add(Test_a[GetRandomInt(0, 100)])
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of Array.stableSort().")
call TriggerSleepAction(WAIT)
call ar.stableSort(0)
set i = 1
loop
exitwhen (i >= ar.size())
if (ar[i] < ar[i - 1]) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro StableSortTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.stableSort().")
call TriggerSleepAction(WAIT)
call al.stableSort(0)
set i = 1
loop
exitwhen (i >= al.size())
if (al.getByIndex(i) < al.getByIndex(i - 1)) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro StableSortTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.stableSort().")
call TriggerSleepAction(WAIT)
call ll.stableSort(0)
set i = 1
loop
exitwhen (i >= al.size())
if (ll.getByIndex(i) < ll.getByIndex(i - 1)) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro StableSortTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro StableSortTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0018, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0018, 75.0)
call SetTextTagText(tag, "Test StableSort", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=53
//TESH.alwaysfold=0
scope SelectionTest initializer init
globals
private string PREFIX = "Selection Test :: "
private real WAIT = 1.0
private integer ELEMENTS = 100
private integer REDUNDANCY = 10
endglobals
//! textmacro SelectionTest_End
call standard.destroy()
call ar.destroy()
call al.destroy()
call ll.destroy()
set Test_active[id] = false
return
//! endtextmacro
private function conditions takes nothing returns boolean
return Test_active[GetPlayerId(GetOwningPlayer(GetTriggerUnit()))] == false
endfunction
private function AddStandard takes ALinkedList l, A a returns nothing
local AListTypeIterator it = l.listIterator()
local integer size = l.size()
loop
exitwhen (it.hasNext() == false)
if (it.next() > a) then
call it.previous()
call it.add(a)
exitwhen (true)
endif
endloop
call it.destroy()
if (l.size() == size) then
call l.add(a)
endif
endfunction
private function actions takes nothing returns nothing
local ALinkedList standard = ALinkedList.create()
local AArray ar = AArray.create(ELEMENTS)
//local ALinkedHashSet hs = ALinkedHashSet.create()
//local ATreeSet ts = ATreeSet.create(0)
//local ABalancedTreeSet bts = ABalancedTreeSet.create(0)
local AArrayList al = AArrayList.create()
local ALinkedList ll = ALinkedList.create()
local integer id = GetPlayerId(GetOwningPlayer(GetTriggerUnit()))
local integer temp
local integer i = 0
set Test_active[id] = true
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, "Collections Demo :: Beginning Selection Test.")
call TriggerSleepAction(WAIT)
loop
exitwhen (i >= ELEMENTS)
set temp = GetRandomInt(0, 100)
call AddStandard.evaluate(standard, Test_a[temp])
set ar[i] = Test_a[temp]
call al.add(Test_a[temp])
call ll.add(Test_a[temp])
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of Array.min().")
call TriggerSleepAction(WAIT)
if (ar.min(0).i == standard.getByIndex(0).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of Array.max().")
call TriggerSleepAction(WAIT)
if (ar.max(0).i == standard.getByIndex(standard.size() - 1).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of Array.select().")
call TriggerSleepAction(WAIT)
set i = 0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(1, ar.size())
if (ar.select(0, temp).i != standard.getByIndex(temp - 1).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.min().")
call TriggerSleepAction(WAIT)
if (al.min(0).i == standard.getByIndex(0).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.max().")
call TriggerSleepAction(WAIT)
if (al.max(0).i == standard.getByIndex(standard.size() - 1).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of ArrayList.select().")
call TriggerSleepAction(WAIT)
set i = 0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(1, ar.size())
if (al.select(0, temp).i != standard.getByIndex(temp - 1).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.min().")
call TriggerSleepAction(WAIT)
if (ll.min(0).i == standard.getByIndex(0).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.max().")
call TriggerSleepAction(WAIT)
if (ll.max(0).i == standard.getByIndex(standard.size() - 1).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
else
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "Beginning test of LinkedList.select().")
call TriggerSleepAction(WAIT)
set i = 0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(1, ar.size())
if (ll.select(0, temp).i != standard.getByIndex(temp - 1).i) then
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "FAIL.")
//! runtextmacro SelectionTest_End()
endif
set i = i + 1
endloop
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "PASS.")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(id), 0, 0, 5.00, PREFIX + "End of test.")
//! runtextmacro StableSortTest_End()
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
local texttag tag
call TriggerRegisterUnitInRange(t, gg_unit_ncp2_0019, 150.0, null)
call TriggerAddCondition(t, Condition(function conditions))
call TriggerAddAction(t, function actions)
set tag = CreateTextTag()
call SetTextTagPosUnit(tag, gg_unit_ncp2_0019, 75.0)
call SetTextTagText(tag, "Test Selection", 12 * 0.023 / 10)
call SetTextTagColor(tag, 255, 255, 0, 0)
endfunction
endscope
//TESH.scrollpos=12
//TESH.alwaysfold=0
To use the Benchmark trigger first enable it. Then you need to have stopwatch.dll injected
into your wc3. The instructions on how to do this are on wc3c.net along with StopWatch. If you do
not inject the dll into wc3, then the demo map will be unplayable as long as the Benchmark
trigger is enabled.
Only Player(0) - Red - may activate the benchmarking trigger. To do so, he must type in-game:
-bm [struct name] [number of elements]
-[struct name] is the name of the implementation to benchmark (i.e. LinkedHashSet).
-[number of elements] is the number of elements to load into the collection for the benchmark.
The maximum value allowed is 8000.
The benchmark trigger works for all the Collection implementations, Sort, and StableSort, but
not the Map implementations.
The benchmarking process works like this:
1. Load X random elements into the collection.
2. Call the method to be tested Y times while keeping the size of the collection constant at X.
Any elements being added/removed/searched are random. Any indices used are random (but are
all within bounds).
Special Cases:
- Collection.iterator(): The method is called once and the iterator is allowed to iterate
through the entire collection exactly once.
- Collection.clear(): The method is called once.
- Sort and StableSort: Each sort method is called once.
3. Report the average time taken for the method to complete.
4. Repeat 2 and 3 for every method to be tested.
//TESH.scrollpos=0
//TESH.alwaysfold=0
//! runtextmacro LinkedHashSet("integer", "Integer", "Integer")
//! runtextmacro TreeSet("integer", "Integer", "Integer")
//! runtextmacro ArrayList("integer", "Integer", "Integer")
//! runtextmacro LinkedList("integer", "Integer", "Integer")
//! runtextmacro Sort("integer", "Integer", "Integer")
//! runtextmacro StableSort("integer", "Integer", "Integer")
//! runtextmacro Selection("integer", "Integer", "Integer")
//! runtextmacro BinarySearch("integer", "Integer", "Integer")
native StopWatchCreate takes nothing returns integer
native StopWatchMark takes integer stopwatch returns real
native StopWatchDestroy takes integer stopwatch returns nothing
library Benchmark initializer init requires IntegerLinkedHashSet, IntegerTreeSet, IntegerArrayList, IntegerLinkedList
globals
private constant integer MAX_ELEMENTS = 8000
private constant integer REDUNDANCY = 100
private constant real TEXT_TIME = 20.00
private constant real WAIT = 5.00
private IntegerSet hashSet
private IntegerSet treeSet
private IntegerSet balancedTreeSet
private IntegerList arrayList
private IntegerLinkedList linkedList
private boolean running = false
endglobals
private function BenchmarkAdd takes IntegerCollection c returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
local boolean b
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
set b = c.add(temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
if (b) then
call c.remove(temp)
endif
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkContains takes IntegerCollection c returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
call c.contains(temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkIterator takes IntegerCollection c returns real
local IntegerIterator it
local integer stopWatch
local real time = 0.0
set stopWatch = StopWatchCreate()
set it = c.iterator()
loop
exitwhen (it.hasNext() == false)
call it.next()
endloop
call it.destroy()
set time = StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
return time
endfunction
private function BenchmarkRemove takes IntegerCollection c returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
local boolean b
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
set b = c.remove(temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
if (b) then
call c.add(temp)
endif
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkClear takes IntegerCollection c returns real
local integer stopWatch
local real time = 0.0
set stopWatch = StopWatchCreate()
call c.clear()
set time = StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
return time
endfunction
private function BenchmarkAddByIndex takes IntegerList l returns real
local integer index
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set index = GetRandomInt(0, l.size())
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
call l.addByIndex(index, temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
call l.removeByIndex(l.size() - 1)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkGetByIndex takes IntegerList l returns real
local integer index
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set index = GetRandomInt(0, l.size())
set stopWatch = StopWatchCreate()
call l.getByIndex(index)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkRemoveByIndex takes IntegerList l returns real
local integer index
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set index = GetRandomInt(0, l.size())
set stopWatch = StopWatchCreate()
set temp = l.removeByIndex(index)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
call l.addByIndex(l.size(), temp)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkIndexOf takes IntegerList l returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
call l.indexOf(temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkSwap takes IntegerList l returns real
local integer index1
local integer index2
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set index1 = GetRandomInt(0, l.size())
set index2 = GetRandomInt(0, l.size())
set stopWatch = StopWatchCreate()
call l.swap(index1, index2)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkAddFirst takes IntegerLinkedList l returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
call l.addFirst(temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
call l.removeFirst()
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkAddLast takes IntegerLinkedList l returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set temp = GetRandomInt(-0xfffffff, 0xfffffff)
set stopWatch = StopWatchCreate()
call l.addLast(temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
call l.removeLast()
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkRemoveFirst takes IntegerLinkedList l returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set stopWatch = StopWatchCreate()
set temp = l.removeFirst()
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
call l.addFirst(temp)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkRemoveLast takes IntegerLinkedList l returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY)
set stopWatch = StopWatchCreate()
set temp = l.removeLast()
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
call l.addLast(temp)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function BenchmarkListSort takes IntegerList l returns real
local integer stopWatch
local real time = 0.0
set stopWatch = StopWatchCreate()
call l.sort(0)
set time = StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
return time
endfunction
private function BenchmarkListStableSort takes IntegerList l returns real
local integer stopWatch
local real time = 0.0
set stopWatch = StopWatchCreate()
call l.stableSort(0)
set time = StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
return time
endfunction
private function BenchmarkCollectionSelect takes IntegerCollection c returns real
local integer temp
local integer stopWatch
local integer i = 0
local real time = 0.0
loop
exitwhen (i >= REDUNDANCY / 10)
set temp = GetRandomInt(1, c.size())
set stopWatch = StopWatchCreate()
call c.select(0, temp)
set time = time + StopWatchMark(stopWatch)
call StopWatchDestroy(stopWatch)
set i = i + 1
endloop
return time / REDUNDANCY
endfunction
private function SilentPopulate takes IntegerCollection c, integer iterations returns nothing
loop
exitwhen (c.size() >= iterations)
call c.add(GetRandomInt(-0xfffffff, 0xfffffff))
endloop
endfunction
private function SilentClear takes IntegerCollection c returns nothing
call c.clear()
endfunction
private function StartLinkedHashSetBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning LinkedHashSet Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(hashSet, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedHashSet.add() : " + R2S(BenchmarkAdd.evaluate(hashSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedHashSet.contains() : " + R2S(BenchmarkContains.evaluate(hashSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedHashSet.iterator() : " + R2S(BenchmarkIterator.evaluate(hashSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedHashSet.remove() : " + R2S(BenchmarkRemove.evaluate(hashSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedHashSet.clear() : " + R2S(BenchmarkClear.evaluate(hashSet) * 1000) + " ms")
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of LinkedHashSet Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartTreeSetBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning TreeSet Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(treeSet, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "TreeSet.add() : " + R2S(BenchmarkAdd.evaluate(treeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "TreeSet.contains() : " + R2S(BenchmarkContains.evaluate(treeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "TreeSet.iterator() : " + R2S(BenchmarkIterator.evaluate(treeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "TreeSet.remove() : " + R2S(BenchmarkRemove.evaluate(treeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "TreeSet.clear() : " + R2S(BenchmarkClear.evaluate(treeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of TreeSet Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartBalancedTreeSetBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning BalancedTreeSet Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(balancedTreeSet, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "BalancedTreeSet.add() : " + R2S(BenchmarkAdd.evaluate(balancedTreeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "BalancedTreeSet.contains() : " + R2S(BenchmarkContains.evaluate(balancedTreeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "BalancedTreeSet.iterator() : " + R2S(BenchmarkIterator.evaluate(balancedTreeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "BalancedTreeSet.remove() : " + R2S(BenchmarkRemove.evaluate(balancedTreeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "BalancedTreeSet.clear() : " + R2S(BenchmarkClear.evaluate(balancedTreeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of BalancedTreeSet Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartArrayListBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning ArrayList Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(arrayList, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.add() : " + R2S(BenchmarkAdd.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.contains() : " + R2S(BenchmarkContains.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.addByIndex() : " + R2S(BenchmarkAddByIndex.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.getByIndex() : " + R2S(BenchmarkGetByIndex.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.removeByIndex() : " + R2S(BenchmarkRemoveByIndex.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.indexOf() : " + R2S(BenchmarkIndexOf.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.swap() : " + R2S(BenchmarkSwap.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.iterator() : " + R2S(BenchmarkIterator.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.remove() : " + R2S(BenchmarkRemove.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.clear() : " + R2S(BenchmarkClear.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of ArrayList Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartLinkedListBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning LinkedList Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(linkedList, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.add() : " + R2S(BenchmarkAdd.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.contains() : " + R2S(BenchmarkContains.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.addByIndex() : " + R2S(BenchmarkAddByIndex.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.getByIndex() : " + R2S(BenchmarkGetByIndex.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.removeByIndex() : " + R2S(BenchmarkRemoveByIndex.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.indexOf() : " + R2S(BenchmarkIndexOf.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.swap() : " + R2S(BenchmarkSwap.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.addFirst() : " + R2S(BenchmarkAddFirst.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.addLast() : " + R2S(BenchmarkAddLast.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.removeFirst() : " + R2S(BenchmarkRemoveFirst.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.removeLast() : " + R2S(BenchmarkRemoveLast.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.iterator() : " + R2S(BenchmarkIterator.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.remove() : " + R2S(BenchmarkRemove.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.clear() : " + R2S(BenchmarkClear.evaluate(linkedList) * 1000) + " ms")
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of LinkedList Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartSortBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning Sort Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(arrayList, elements)
call SilentPopulate.evaluate(linkedList, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.sort() : " + R2S(BenchmarkListSort.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.sort() : " + R2S(BenchmarkListSort.evaluate(linkedList) * 1000) + " ms")
call SilentClear.evaluate(arrayList)
call SilentClear.evaluate(linkedList)
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of Sort Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartStableSortBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning StableSort Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(arrayList, elements)
call SilentPopulate.evaluate(linkedList, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.stableSort() : " + R2S(BenchmarkListStableSort.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.stableSort() : " + R2S(BenchmarkListStableSort.evaluate(linkedList) * 1000) + " ms")
call SilentClear.evaluate(arrayList)
call SilentClear.evaluate(linkedList)
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of StableSort Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function StartSelectionBenchmark takes integer elements returns nothing
set running = true
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "Beginning Selection Benchmark ... " + I2S(elements) + " Elements.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call TriggerSleepAction(WAIT / 2)
call SilentPopulate.evaluate(hashSet, elements)
call SilentPopulate.evaluate(treeSet, elements)
call SilentPopulate.evaluate(balancedTreeSet, elements)
call SilentPopulate.evaluate(arrayList, elements)
call SilentPopulate.evaluate(linkedList, elements)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedHashSet.select() : " + R2S(BenchmarkCollectionSelect.evaluate(hashSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "TreeSet.select() : " + R2S(BenchmarkCollectionSelect.evaluate(treeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "BalancedTreeSet.select() : " + R2S(BenchmarkCollectionSelect.evaluate(balancedTreeSet) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "ArrayList.select() : " + R2S(BenchmarkCollectionSelect.evaluate(arrayList) * 1000) + " ms")
call TriggerSleepAction(WAIT)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "LinkedList.select() : " + R2S(BenchmarkCollectionSelect.evaluate(linkedList) * 1000) + " ms")
call SilentClear.evaluate(hashSet)
call SilentClear.evaluate(treeSet)
call SilentClear.evaluate(balancedTreeSet)
call SilentClear.evaluate(arrayList)
call SilentClear.evaluate(linkedList)
call TriggerSleepAction(WAIT / 2)
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, " ")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "End of Selection Benchmark.")
call DisplayTimedTextToPlayer(Player(0), 0, 0, TEXT_TIME, "=============")
set running = false
endfunction
private function actions takes nothing returns nothing
local string s = GetEventPlayerChatString()
local integer length = StringLength(s)
local integer elements
if (running == false) then
if (length > 18 and SubString(s, 4, 18) == "LinkedHashSet ") then
set elements = S2I(SubString(s, 18, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartLinkedHashSetBenchmark.execute(elements)
endif
elseif (length > 12 and SubString(s, 4, 12) == "TreeSet ") then
set elements = S2I(SubString(s, 12, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartTreeSetBenchmark.execute(elements)
endif
elseif (length > 20 and SubString(s, 4, 20) == "BalancedTreeSet ") then
set elements = S2I(SubString(s, 20, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartBalancedTreeSetBenchmark.execute(elements)
endif
elseif (length > 14 and SubString(s, 4, 14) == "ArrayList ") then
set elements = S2I(SubString(s, 14, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartArrayListBenchmark.execute(elements)
endif
elseif (length > 15 and SubString(s, 4, 15) == "LinkedList ") then
set elements = S2I(SubString(s, 15, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartLinkedListBenchmark.execute(elements)
endif
elseif (length > 9 and SubString(s, 4, 9) == "Sort ") then
set elements = S2I(SubString(s, 9, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartSortBenchmark.execute(elements)
endif
elseif (length > 15 and SubString(s, 4, 15) == "StableSort ") then
set elements = S2I(SubString(s, 15, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartStableSortBenchmark.execute(elements)
endif
elseif (length > 14 and SubString(s, 4, 14) == "Selection ") then
set elements = S2I(SubString(s, 14, length))
if (elements > 0 and elements <= MAX_ELEMENTS) then
call StartSelectionBenchmark.execute(elements)
endif
endif
endif
endfunction
private function init takes nothing returns nothing
local trigger t = CreateTrigger()
set hashSet = IntegerLinkedHashSet.create()
set treeSet = IntegerTreeSet.create(0)
set balancedTreeSet = IntegerBalancedTreeSet.create(0)
set arrayList = IntegerArrayList.create()
set linkedList = IntegerLinkedList.create()
call TriggerRegisterPlayerChatEvent(t, Player(0), "-bm ", false)
call TriggerAddAction(t, function actions)
endfunction
endlibrary
//TESH.scrollpos=19
//TESH.alwaysfold=0
//! runtextmacro Collection("integer", "Integer", "INTEGER")
//! runtextmacro Collection("PropertyChangeListener", "Pcl", "PCL")
//! runtextmacro LinkedList("PropertyChangeListener", "Pcl", "PCL")
//! runtextmacro Map("integer", "Integer", "INTEGER", "PclLinkedList", "PclLinkedList", "PCL_LINKED_LIST")
//! runtextmacro Collection("PclLinkedList", "PclLinkedList", "PCL_LINKED_LIST")
//! runtextmacro Collection("IntegerPclLinkedListMapEntry", "IntegerPclLinkedListMapEntry", "INTEGER_PCL_LINKED_LIST_MAP_ENTRY")
//! runtextmacro LinkedHashMap("integer", "Integer", "INTEGER", "PclLinkedList", "PclLinkedList", "PCL_LINKED_LIST")
library PropertyChange requires IntegerPclLinkedListLinkedHashMap
struct PropertyChangeEvent
private PropertyChangeSupport pcs
private string property
private integer old
private integer new
private integer data
static method create takes PropertyChangeSupport pcs, string property, integer old, integer new, integer data returns PropertyChangeEvent
local PropertyChangeEvent this = PropertyChangeEvent.allocate()
set this.pcs = pcs
set this.property = property
set this.old = old
set this.new = new
set this.data = data
return this
endmethod
method getSource takes nothing returns integer
return .pcs.getSource.evaluate()
endmethod
method getProperty takes nothing returns string
return .property
endmethod
method getOldProperty takes nothing returns integer
return .old
endmethod
method getNewProperty takes nothing returns integer
return .new
endmethod
method getData takes nothing returns integer
return .data
endmethod
endstruct
struct PropertyChangeListener
stub method onPropertyChange takes PropertyChangeEvent e returns nothing
endmethod
endstruct
struct PropertyChangeSupport
private integer source
private IntegerPclLinkedListMap properties
static method create takes integer source returns PropertyChangeSupport
local PropertyChangeSupport this = PropertyChangeSupport.allocate()
set this.source = source
set this.properties = IntegerPclLinkedListLinkedHashMap.create()
return this
endmethod
method getSource takes nothing returns integer
return .source
endmethod
method hasListeners takes string property returns boolean
if (property != "") then
if (.properties.containsKey(StringHash(""))) then
return .properties.get(StringHash("")).isEmpty() == false
elseif (.properties.containsKey(StringHash(property))) then
return .properties.get(StringHash(property)).isEmpty() == false
endif
endif
return false
endmethod
method getListeners takes nothing returns PclArray
local PclLinkedListCollection c = .properties.values()
local PclLinkedListIterator it = c.iterator()
local PclLinkedList all = PclLinkedList.create()
local PclArray ar
loop
exitwhen (it.hasNext() == false)
call all.addAll(it.next())
endloop
call it.destroy()
call c.destroy()
set ar = all.toArray()
call all.destroy()
return ar
endmethod
method getListenersByProperty takes string property returns PclArray
if (property != "" and .properties.containsKey(StringHash(property))) then
return .properties.get(StringHash(property)).toArray()
endif
return PclArray.create(0)
endmethod
method addListener takes PropertyChangeListener pcl returns nothing
local PclLinkedList l
if (.properties.containsKey(StringHash("")) == false) then
set l = PclLinkedList.create()
call .properties.put(StringHash(""), l)
else
set l = .properties.get(StringHash(""))
endif
call l.add(pcl)
endmethod
method addListenerByProperty takes string property, PropertyChangeListener pcl returns nothing
local PclLinkedList l
if (property != "") then
if (.properties.containsKey(StringHash(property)) == false) then
set l = PclLinkedList.create()
call .properties.put(StringHash(property), l)
else
set l = .properties.get(StringHash(property))
endif
call l.add(pcl)
endif
endmethod
method removeListener takes PropertyChangeListener pcl returns nothing
local PclLinkedListCollection c = .properties.values()
local PclLinkedListIterator it = c.iterator()
loop
exitwhen (it.hasNext() == false)
call it.next().remove(pcl)
endloop
call it.destroy()
call c.destroy()
endmethod
method removeListenerByProperty takes string property, PropertyChangeListener pcl returns nothing
if (property != "" and .properties.containsKey(StringHash(property))) then
call .properties.get(StringHash(property)).remove(pcl)
endif
endmethod
method firePropertyChange takes string property, integer old, integer new, integer data returns nothing
local PropertyChangeEvent e
local PclIterator it
if (property != "") then
set e = PropertyChangeEvent.create(this, property, old, new, data)
if (.properties.containsKey(StringHash(property))) then
set it = .properties.get(StringHash(property)).iterator()
loop
exitwhen (it.hasNext() == false)
call it.next().onPropertyChange(e)
endloop
call it.destroy()
endif
if (.properties.containsKey(StringHash(""))) then
set it = .properties.get(StringHash("")).iterator()
loop
exitwhen (it.hasNext() == false)
call it.next().onPropertyChange(e)
endloop
call it.destroy()
endif
call e.destroy()
endif
endmethod
method destroy takes nothing returns nothing
local PclLinkedListCollection c = .properties.values()
local PclLinkedListIterator it = c.iterator()
local PclLinkedList l
loop
exitwhen (it.hasNext() == false)
set l = it.next()
call it.remove()
call l.destroy()
endloop
call it.destroy()
call c.destroy()
call .properties.destroy()
call thistype.deallocate(this)
endmethod
endstruct
endlibrary
//TESH.scrollpos=141
//TESH.alwaysfold=0
//! runtextmacro Collection("Card", "Card", "CARD")
//! runtextmacro LinkedList("Card", "Card", "CARD")
library Card requires CardLinkedList
struct Card
static constant integer CLUBS = 0
static constant integer DIAMONDS = 1
static constant integer HEARTS = 2
static constant integer SPADES = 3
private static string array values
private integer suit
private integer value
static method create takes integer suit, integer value returns Card
local Card this = Card.allocate()
set this.suit = suit
set this.value = value
return this
endmethod
method getSuit takes nothing returns integer
return .suit
endmethod
method getValue takes nothing returns integer
return .value
endmethod
method toString takes nothing returns string
local string s = .values[.value]
if (.suit == 0) then
set s = s + " of Clubs"
elseif (.suit == 1) then
set s = s + " of Diamonds"
elseif (.suit == 2) then
set s = s + " of Hearts"
elseif (.suit == 3) then
set s = s + " of Spades"
endif
return s
endmethod
private static method onInit takes nothing returns nothing
set .values[1] = "Ace"
set .values[2] = "Two"
set .values[3] = "Three"
set .values[4] = "Four"
set .values[5] = "Five"
set .values[6] = "Six"
set .values[7] = "Seven"
set .values[8] = "Eight"
set .values[9] = "Nine"
set .values[10] = "Ten"
set .values[11] = "Jack"
set .values[12] = "Queen"
set .values[13] = "King"
endmethod
endstruct
struct Deck
private CardLinkedList deck
static method create takes nothing returns Deck
local Deck this = Deck.allocate()
local integer i = 0
local integer j
set this.deck = CardLinkedList.create()
call this.addStandardDeckToTop.evaluate()
return this
endmethod
method addStandardDeckToTop takes nothing returns nothing
local integer i = 0
local integer j
loop
set j = 1
exitwhen (i > 3)
loop
exitwhen (j > 13)
call .deck.addFirst(Card.create(i, j))
set j = j + 1
endloop
set i = i + 1
endloop
endmethod
method addStandardDeckToBottom takes nothing returns nothing
local integer i = 0
local integer j
loop
set j = 1
exitwhen (i > 3)
loop
exitwhen (j > 13)
call .deck.addLast(Card.create(i, j))
set j = j + 1
endloop
set i = i + 1
endloop
endmethod
method shuffle takes nothing returns nothing
local Card card
local integer i = .deck.size()
local integer j
loop
exitwhen (i <= 1)
set j = GetRandomInt(0, i - 1)
call .deck.swap(i - 1, j)
set i = i - 1
endloop
endmethod
method draw takes nothing returns Card
return .deck.removeFirst()
endmethod
method drawFromBottom takes nothing returns Card
return .deck.removeLast()
endmethod
method addToTop takes Card card returns nothing
call .deck.addFirst(card)
endmethod
method addToBottom takes Card card returns nothing
call .deck.addLast(card)
endmethod
method size takes nothing returns integer
return .deck.size()
endmethod
method empty takes nothing returns nothing
call .deck.clear()
endmethod
method destroy takes nothing returns nothing
call this.deck.destroy()
call this.deallocate()
endmethod
method merge takes Deck other returns nothing
loop
exitwhen (other.size() == 0)
call .addToBottom(other.draw())
endloop
call other.destroy()
endmethod
method throwCards takes nothing returns nothing
loop
exitwhen (.deck.size() == 0)
call .deck.removeFirst().destroy()
endloop
endmethod
endstruct
endlibrary
//TESH.scrollpos=399
//TESH.alwaysfold=0
//! runtextmacro Collection("BlackjackPlayer", "BlackjackPlayer", "BLACKJACK_PLAYER")
//! runtextmacro LinkedHashSet("BlackjackPlayer", "BlackjackPlayer", "BLACKJACK_PLAYER")
library Blackjack requires Card, PropertyChange
struct BlackjackPlayer
static constant integer STATUS_IDLE = 0
static constant integer STATUS_BETTING = 1
static constant integer STATUS_DEALING = 2
private CardLinkedList cards
private Blackjack game = 0
private integer money = 0
private integer score = 0
private boolean softScore = false
private integer bet = 0
private integer status
private PropertyChangeSupport pcs
static method create takes integer money returns BlackjackPlayer
local BlackjackPlayer this = BlackjackPlayer.allocate()
set this.cards = CardLinkedList.create()
set this.money = money
set this.pcs = PropertyChangeSupport.create(this)
set this.status = .STATUS_IDLE
return this
endmethod
method getGame takes nothing returns Blackjack
return .game
endmethod
method getStatus takes nothing returns integer
return .status
endmethod
method getScore takes nothing returns integer
return .score
endmethod
method isScoreSoft takes nothing returns boolean
return .softScore
endmethod
method getMoney takes nothing returns integer
return .money
endmethod
method getBet takes nothing returns integer
return .bet
endmethod
method getCardCount takes nothing returns integer
return .cards.size()
endmethod
method getCards takes nothing returns CardArray
return .cards.toArray()
endmethod
method getPcs takes nothing returns PropertyChangeSupport
return .pcs
endmethod
private method setBet takes integer bet returns nothing
local integer old = .bet
set .bet = bet
call .pcs.firePropertyChange("bet", old, .bet, 0)
endmethod
private method setMoney takes integer money returns nothing
local integer old = .money
set .money = money
call .pcs.firePropertyChange("money", old, .money, 0)
endmethod
method leaveGame takes nothing returns boolean
local Blackjack old = .game
if (.game != 0) then
if (.game.removePlayer.evaluate(this)) then
set .game = 0
call .pcs.firePropertyChange("game", old, .game, 0)
return true
else
return false
endif
endif
return false
endmethod
method joinGame takes Blackjack game returns boolean
if (.game != 0) then
if (.leaveGame() == false) then
return false
endif
endif
if (game.addPlayer.evaluate(this)) then
set .game = game
call .pcs.firePropertyChange("game", 0, .game, 0)
return true
else
return false
endif
return false
endmethod
method enterSpotlight takes nothing returns nothing
local integer old
if (.game != 0) then
set old = .status
if (.game.getStatus.evaluate() == Blackjack.STATUS_BETTING) then
set .status = .STATUS_BETTING
call .pcs.firePropertyChange("status", old, .status, 0)
else
set .status = .STATUS_DEALING
call .pcs.firePropertyChange("status", old, .status, 0)
endif
endif
endmethod
method exitSpotlight takes nothing returns nothing
local integer old = .status
set .status = .STATUS_IDLE
call .pcs.firePropertyChange("status", old, .status, 0)
endmethod
method placeBet takes integer money returns integer
set money = IMaxBJ(0, money)
set money = IMinBJ(money, .money)
call .setBet(money)
return .bet
endmethod
method finalizeBet takes nothing returns nothing
if (.game != 0) then
call .game.finalizeBet.evaluate(this)
endif
endmethod
method winBet takes nothing returns nothing
local integer winnings
if (.score == 21 and .cards.size() == 2) then
set winnings = .bet * 3 / 2
call .pcs.firePropertyChange("outcome", 0, 2, winnings)
else
set winnings = .bet
call .pcs.firePropertyChange("outcome", 0, 1, winnings)
endif
call .setMoney(.money + winnings)
call .setBet(0)
endmethod
method loseBet takes nothing returns nothing
call .pcs.firePropertyChange("outcome", 0, -1, .bet)
call .setMoney(.money - .bet)
call .setBet(0)
endmethod
method push takes nothing returns nothing
call .pcs.firePropertyChange("outcome", 0, 0, 0)
call .setBet(0)
endmethod
method hit takes nothing returns nothing
if (.game != 0) then
call .game.hit.evaluate(this)
endif
endmethod
method stand takes nothing returns nothing
if (.game != 0) then
call .game.stand.evaluate(this)
endif
endmethod
method computeScore takes nothing returns nothing
local CardIterator it = .cards.iterator()
local Card card
local integer score = 0
local integer oldScore = .score
local integer aceCount = 0
loop
exitwhen (it.hasNext() == false)
set card = it.next()
if (card.getValue() == 1) then
set score = score + 11
set aceCount = aceCount + 1
else
set score = score + IMinBJ(card.getValue(), 10)
endif
if (score > 21 and aceCount > 0) then
set score = score - 10
set aceCount = aceCount - 1
endif
endloop
set .softScore = aceCount > 0
set .score = score
call .pcs.firePropertyChange("score", oldScore, .score, 0)
endmethod
method drawCard takes Deck deck returns nothing
local CardArray old = .cards.toArray()
local CardArray new
call .cards.addLast(deck.draw())
set new = .cards.toArray()
call .pcs.firePropertyChange("card", old, new, 0)
call old.destroy()
call new.destroy()
call .computeScore()
endmethod
method throwCards takes nothing returns nothing
local integer oldScore = .score
call .cards.clear()
set .score = 0
set .softScore = false
call .pcs.firePropertyChange("card", 0, .cards.toArray(), 0)
call .pcs.firePropertyChange("score", oldScore, 0, 0)
endmethod
method destroy takes nothing returns nothing
call this.cards.destroy()
call this.pcs.firePropertyChange("alive", 1, 0, 0)
call this.pcs.destroy()
endmethod
endstruct
struct Blackjack
static constant integer STATUS_IDLE = 0
static constant integer STATUS_BETTING = 1
static constant integer STATUS_DEALING = 2
static constant integer STATUS_REVEALING = 3
private integer numberOfDecks
private boolean hitOnSoft17
private Deck deck
private BlackjackPlayer dealer
private BlackjackPlayerSet players
private BlackjackPlayer spotlight
private BlackjackPlayerIterator spotlightIt
private integer status
private PropertyChangeSupport pcs
static method create takes integer numberOfDecks, boolean hitOnSoft17 returns Blackjack
local Blackjack this = Blackjack.allocate()
local integer i = 1
set this.numberOfDecks = numberOfDecks
set this.hitOnSoft17 = hitOnSoft17
set this.deck = Deck.create()
set this.dealer = BlackjackPlayer.create(1000000)
set this.players = BlackjackPlayerLinkedHashSet.create()
loop
exitwhen (i >= numberOfDecks)
call this.deck.addStandardDeckToTop()
set i = i + 1
endloop
set this.status = .STATUS_IDLE
set this.pcs = PropertyChangeSupport.create(this)
return this
endmethod
method getNumberOfDecks takes nothing returns integer
return .numberOfDecks
endmethod
method hitsOnSoft17 takes nothing returns boolean
return .hitOnSoft17
endmethod
method getStatus takes nothing returns integer
return .status
endmethod
method getPcs takes nothing returns PropertyChangeSupport
return .pcs
endmethod
method refill takes nothing returns nothing
local integer i = 0
call .deck.throwCards()
loop
exitwhen (i >= .numberOfDecks)
call .deck.addStandardDeckToTop()
set i = i + 1
endloop
endmethod
method changeStatus takes integer status returns nothing
local integer old = .status
local BlackjackPlayerIterator it
if (old != status) then
set .status = status
call .pcs.firePropertyChange("status", old, .status, 0)
set it = .players.iterator()
loop
exitwhen (it.hasNext() == false)
call it.next().getPcs().firePropertyChange("gamestatus", old, .status, 0)
endloop
endif
endmethod
method addPlayer takes BlackjackPlayer p returns boolean
local BlackjackPlayerArray old
local BlackjackPlayerArray new
if (.status == .STATUS_IDLE) then
set old = .players.toArray()
if (.players.add(p)) then
set new = .players.toArray()
call .pcs.firePropertyChange("player", old, new, 0)
call old.destroy()
call new.destroy()
return true
endif
call old.destroy()
call new.destroy()
endif
return false
endmethod
method removePlayer takes BlackjackPlayer p returns boolean
local BlackjackPlayerArray old
local BlackjackPlayerArray new
if (.status == .STATUS_IDLE) then
set old = .players.toArray()
if (.players.remove(p)) then
set new = .players.toArray()
call .pcs.firePropertyChange("player", old, .players.toArray(), 0)
call old.destroy()
call new.destroy()
return true
endif
call old.destroy()
call new.destroy()
endif
return false
endmethod
method advanceSpotlight takes nothing returns nothing
local BlackjackPlayer old
if (.status == .STATUS_BETTING) then
if (.spotlightIt.hasNext()) then
set old = .spotlight
if (old != 0) then
call old.exitSpotlight()
endif
set .spotlight = .spotlightIt.next()
call .spotlight.enterSpotlight()
call .pcs.firePropertyChange("spotlight", old, .spotlight, 0)
else
set old = .spotlight
if (old != 0) then
call old.exitSpotlight()
endif
call .spotlightIt.destroy()
set .spotlight = 0
call .pcs.firePropertyChange("spotlight", old, .spotlight, 0)
call .startDealing.evaluate()
endif
elseif (.status == .STATUS_DEALING) then
if (.spotlightIt.hasNext()) then
set old = .spotlight
if (old != 0) then
call old.exitSpotlight()
endif
set .spotlight = .spotlightIt.next()
call .spotlight.enterSpotlight()
call .pcs.firePropertyChange("spotlight", old, .spotlight, 0)
else
set old = .spotlight
if (old != 0) then
call old.exitSpotlight()
endif
call .spotlightIt.destroy()
set .spotlight = 0
call .pcs.firePropertyChange("spotlight", old, .spotlight, 0)
call .reveal.evaluate()
endif
endif
endmethod
method finalizeBet takes BlackjackPlayer p returns nothing
if (p == .spotlight and .status == .STATUS_BETTING and p.getBet() > 0) then
call .advanceSpotlight()
endif
endmethod
method stand takes BlackjackPlayer p returns nothing
if (p == .spotlight and .status == .STATUS_DEALING) then
call .advanceSpotlight()
endif
endmethod
method hit takes BlackjackPlayer p returns nothing
if (p == .spotlight and .status == .STATUS_DEALING) then
if (p.getScore() < 21) then
call p.drawCard(.deck)
if (p.getScore() >= 21) then
call .stand(p)
endif
endif
endif
endmethod
method startRound takes nothing returns nothing
if (.status == .STATUS_IDLE and .players.isEmpty() == false) then
call .deck.shuffle()
set .spotlightIt = .players.iterator()
call .changeStatus(.STATUS_BETTING)
call .advanceSpotlight()
endif
endmethod
method startDealing takes nothing returns nothing
local BlackjackPlayerIterator it = .players.iterator()
local BlackjackPlayer p
call .changeStatus(.STATUS_DEALING)
loop
exitwhen (it.hasNext() == false)
set p = it.next()
call p.drawCard(.deck)
call p.drawCard(.deck)
endloop
call it.destroy()
call .dealer.drawCard(.deck)
call .dealer.drawCard(.deck)
call .pcs.firePropertyChange("dealer", 0, .dealer, 0)
set .spotlightIt = .players.iterator()
call .advanceSpotlight()
endmethod
method reveal takes nothing returns nothing
local BlackjackPlayerIterator it = .players.iterator()
local BlackjackPlayer p
local integer score
call .changeStatus(.STATUS_REVEALING)
loop
set score = .dealer.getScore()
exitwhen (score > 17)
if (score == 17) then
if (.hitOnSoft17 == false or .dealer.isScoreSoft() == false) then
exitwhen (true)
endif
endif
call .dealer.drawCard(.deck)
endloop
call .pcs.firePropertyChange("dealer", 0, .dealer, 0)
call .spotlightIt.destroy()
endmethod
method endRound takes nothing returns nothing
local BlackjackPlayerIterator it = .players.iterator()
local BlackjackPlayer p
if (.dealer.getScore() > 21) then
loop
exitwhen (it.hasNext() == false)
set p = it.next()
if (p.getScore() <= 21) then
call p.winBet()
else
call p.push()
endif
call p.throwCards()
endloop
else
loop
exitwhen (it.hasNext() == false)
set p = it.next()
if (p.getScore() <= 21) then
if (p.getScore() == .dealer.getScore() and p.getScore() == 21) then
if (p.getCardCount() < .dealer.getCardCount()) then
call p.winBet()
elseif (p.getCardCount() > .dealer.getCardCount()) then
call p.loseBet()
else
call p.push()
endif
elseif (p.getScore() > .dealer.getScore()) then
call p.winBet()
elseif (p.getScore() < .dealer.getScore()) then
call p.loseBet()
else
call p.push()
endif
else
call p.loseBet()
endif
call p.throwCards()
endloop
endif
call .dealer.throwCards()
call .pcs.firePropertyChange("dealer", 0, .dealer, 0)
call it.destroy()
if (.deck.size() < 52) then
call .refill()
endif
call .changeStatus(.STATUS_IDLE)
endmethod
method destroy takes nothing returns nothing
call this.deck.throwCards()
call this.deck.destroy()
call this.players.destroy()
call this.pcs.firePropertyChange("alive", 1, 0, 0)
call this.pcs.destroy()
endmethod
endstruct
endlibrary
//TESH.scrollpos=0
//TESH.alwaysfold=0
//! runtextmacro Map("integer", "Integer", "INTEGER", "integer", "Integer", "INTEGER")
//! runtextmacro Collection("IntegerIntegerMapEntry", "IntegerIntegerMapEntry", "INTEGER_INTEGER_MAP_ENTRY")
//! runtextmacro LinkedHashMap("integer", "Integer", "INTEGER", "integer", "Integer", "INTEGER")
library BlackjackUI requires Blackjack
scope GameTable
private keyword cardTag
private keyword scoreTag
private struct GameTableListener extends PropertyChangeListener
private GameTable table
static method create takes GameTable table returns GameTableListener
local GameTableListener this = GameTableListener.allocate()
set this.table = table
return this
endmethod
method getGameTable takes nothing returns GameTable
return .table
endmethod
endstruct
private struct StatusListener extends GameTableListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
endmethod
endstruct
private struct SpotlightListener extends GameTableListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
endmethod
endstruct
private struct PlayerListener extends GameTableListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local BlackjackPlayerArray old = e.getOldProperty()
local BlackjackPlayerArray new = e.getNewProperty()
local Blackjack game = e.getSource()
local GameTable table = .getGameTable()
if (new.size() > old.size() and old.size() == 0) then
elseif (new.size() == 0) then
endif
endmethod
endstruct
private struct DealerListener extends GameTableListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local GameTable table = .getGameTable()
local Blackjack game = e.getSource()
local BlackjackPlayer dealer = e.getNewProperty()
local CardArray cards = dealer.getCards()
local integer score
local string s
local integer i = 0
if (cards.size() > 0) then
set s = ":: "
loop
exitwhen (i >= cards.size())
if (i == 0) then
if (game.getStatus() == Blackjack.STATUS_REVEALING) then
set s = s + cards[i].toString() + " :: "
else
set s = s + "? :: "
endif
else
set s = s + cards[i].toString() + " :: "
endif
set i = i + 1
endloop
call SetTextTagText(table.cardTag, s, 10 * 0.023 / 10)
call SetTextTagVisibility(table.cardTag, true)
if (game.getStatus() == Blackjack.STATUS_REVEALING) then
set score = dealer.getScore()
call SetTextTagText(table.scoreTag, I2S(score), 10 * 0.023 / 10)
if (score < 21) then
call SetTextTagColor(table.scoreTag, 255, 255, 0, 255)
call SetTextTagColor(table.cardTag, 255, 255, 0, 255)
elseif (score > 21) then
call SetTextTagColor(table.scoreTag, 255, 0, 0, 255)
call SetTextTagColor(table.cardTag, 255, 0, 0, 255)
else
call SetTextTagColor(table.scoreTag, 0, 255, 0, 255)
call SetTextTagColor(table.cardTag, 0, 255, 0, 255)
endif
call SetTextTagVisibility(table.scoreTag, true)
else
call SetTextTagText(table.scoreTag, "?", 10 * 0.023 / 10)
call SetTextTagColor(table.scoreTag, 255, 255, 255, 255)
call SetTextTagVisibility(table.scoreTag, true)
call SetTextTagColor(table.cardTag, 255, 255, 255, 255)
endif
else
call SetTextTagText(table.cardTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(table.cardTag, false)
call SetTextTagText(table.scoreTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(table.scoreTag, false)
endif
call cards.destroy()
endmethod
endstruct
struct GameTable extends PropertyChangeListener
static constant player OWNER = Player(15)
static constant integer UNIT_TYPE_ID = 'h001'
private static IntegerIntegerMap tables
private Blackjack game
private unit u
private PropertyChangeSupport statusPcl
private PropertyChangeSupport spotlightPcl
private PropertyChangeSupport playerPcl
private PropertyChangeSupport dealerPcl
texttag cardTag
texttag scoreTag
private texttag deckTag
private texttag hitTag
static method create takes real x, real y, integer numberOfDecks, boolean hitOnSoft17 returns GameTable
local GameTable this = GameTable.allocate()
local PropertyChangeSupport pcs
set this.game = Blackjack.create(numberOfDecks, hitOnSoft17)
set this.u = CreateUnit(.OWNER, .UNIT_TYPE_ID, x, y, 270)
set this.statusPcl = StatusListener.create(this)
set this.spotlightPcl = SpotlightListener.create(this)
set this.playerPcl = PlayerListener.create(this)
set this.dealerPcl = DealerListener.create(this)
set this.cardTag = CreateTextTag()
call SetTextTagColor(this.cardTag, 255, 255, 255, 255)
call SetTextTagText(this.cardTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(this.cardTag, false)
call SetTextTagPos(this.cardTag, GetUnitX(this.u), GetUnitY(this.u), 300.0)
set this.scoreTag = CreateTextTag()
call SetTextTagColor(this.scoreTag, 255, 255, 0, 255)
call SetTextTagText(this.scoreTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(this.scoreTag, false)
call SetTextTagPos(this.scoreTag, GetUnitX(this.u) - 50.0, GetUnitY(this.u), 300.0)
set this.deckTag = CreateTextTag()
call SetTextTagColor(this.deckTag, 255, 204, 0, 255)
call SetTextTagText(this.deckTag, I2S(numberOfDecks) + " Decks", 10 * 0.023 / 10)
call SetTextTagVisibility(this.deckTag, true)
call SetTextTagPos(this.deckTag, GetUnitX(this.u) - 150.0, GetUnitY(this.u), 400.0)
set this.hitTag = CreateTextTag()
call SetTextTagColor(this.hitTag, 255, 204, 0, 255)
if (hitOnSoft17) then
call SetTextTagText(this.hitTag, "Hits on Soft 17s", 10 * 0.023 / 10)
else
call SetTextTagText(this.hitTag, "Stands on all 17s", 10 * 0.023 / 10)
endif
call SetTextTagVisibility(this.hitTag, true)
call SetTextTagPos(this.hitTag, GetUnitX(this.u), GetUnitY(this.u), 400.0)
set pcs = this.game.getPcs()
call pcs.addListenerByProperty("alive", this)
call pcs.addListenerByProperty("status", this.statusPcl)
call pcs.addListenerByProperty("spotlight", this.spotlightPcl)
call pcs.addListenerByProperty("player", this.playerPcl)
call pcs.addListenerByProperty("dealer", this.dealerPcl)
call .tables.put(GetHandleId(this.u), this)
return this
endmethod
method getGame takes nothing returns Blackjack
return .game
endmethod
method getUnit takes nothing returns unit
return .u
endmethod
method onPropertyChange takes PropertyChangeEvent e returns nothing
if (e.getNewProperty() == 0) then
call .destroy()
endif
endmethod
static method getGameTable takes unit u returns GameTable
return .tables.get(GetHandleId(u))
endmethod
private static method onInit takes nothing returns nothing
set .tables = IntegerIntegerLinkedHashMap.create()
endmethod
endstruct
endscope
scope Avatar
private keyword cardTag
private keyword scoreTag
private struct AvatarListener extends PropertyChangeListener
private Avatar avatar
static method create takes Avatar avatar returns AvatarListener
local AvatarListener this = AvatarListener.allocate()
set this.avatar = avatar
return this
endmethod
method getAvatar takes nothing returns Avatar
return .avatar
endmethod
endstruct
private struct GameListener extends AvatarListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local Blackjack game = e.getNewProperty()
local unit u = .getAvatar().getUnit.evaluate()
if (game == 0) then
call UnitRemoveAbility(u, Avatar.LEAVE_SPELL_ABIL_ID)
call UnitRemoveAbility(u, Avatar.START_SPELL_ABIL_ID)
call UnitAddAbility(u, Avatar.JOIN_SPELL_ABIL_ID)
call DisplayTimedTextToPlayer(GetOwningPlayer(u), 0, 0, 5.00, "|cffffcc00You have left your current game.|r")
else
call UnitRemoveAbility(u, Avatar.JOIN_SPELL_ABIL_ID)
call UnitAddAbility(u, Avatar.LEAVE_SPELL_ABIL_ID)
call UnitAddAbility(u, Avatar.START_SPELL_ABIL_ID)
call DisplayTimedTextToPlayer(GetOwningPlayer(u), 0, 0, 5.00, "|cffffcc00You have joined a game.|r")
endif
set u = null
endmethod
endstruct
private struct StatusListener extends AvatarListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local integer old = e.getOldProperty()
local integer new = e.getNewProperty()
local Avatar avatar = .getAvatar()
local unit u = avatar.getUnit.evaluate()
if (e.getProperty() == "status") then
if (old == BlackjackPlayer.STATUS_BETTING and old != new) then
call UnitRemoveAbility(u, Avatar.FINALIZE_SPELL_ABIL_ID)
elseif (old == BlackjackPlayer.STATUS_DEALING and old != new) then
call UnitRemoveAbility(u, Avatar.HIT_SPELL_ABIL_ID)
call UnitRemoveAbility(u, Avatar.STAND_SPELL_ABIL_ID)
endif
if (new == BlackjackPlayer.STATUS_BETTING and old != new) then
call UnitAddAbility(u, Avatar.FINALIZE_SPELL_ABIL_ID)
elseif (new == BlackjackPlayer.STATUS_DEALING) then
call UnitAddAbility(u, Avatar.HIT_SPELL_ABIL_ID)
call UnitAddAbility(u, Avatar.STAND_SPELL_ABIL_ID)
endif
else
if (old == Blackjack.STATUS_IDLE and old != new) then
call UnitRemoveAbility(u, Avatar.START_SPELL_ABIL_ID)
elseif (new == Blackjack.STATUS_IDLE and old != new) then
call UnitAddAbility(u, Avatar.START_SPELL_ABIL_ID)
endif
if (old == Blackjack.STATUS_REVEALING and old != new) then
call UnitRemoveAbility(u, Avatar.END_SPELL_ABIL_ID)
elseif (new == Blackjack.STATUS_REVEALING and old != new) then
call UnitAddAbility(u, Avatar.END_SPELL_ABIL_ID)
if (avatar.getPlayer.evaluate().getScore() < 21) then
call SetTextTagColor(avatar.cardTag, 255, 255, 0, 255)
call SetTextTagColor(avatar.scoreTag, 255, 255, 0, 255)
endif
endif
endif
set u = null
endmethod
endstruct
private struct MoneyListener extends AvatarListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local integer amount = e.getNewProperty()
local integer slot
if (e.getProperty() == "money") then
set slot = Avatar.MONEY_ITEM_SLOT
else
set slot = Avatar.BET_ITEM_SLOT
endif
call SetItemCharges(UnitItemInSlot(.getAvatar().getUnit.evaluate(), slot), amount)
endmethod
endstruct
private struct CardListener extends AvatarListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local Avatar avatar = .getAvatar()
local string s
local CardArray old = e.getOldProperty()
local CardArray cards = e.getNewProperty()
local integer score
local integer i = 0
if (e.getProperty() == "card") then
if (cards.size() > 0) then
set s = ":: "
loop
exitwhen (i >= cards.size())
set s = s + cards[i].toString() + " :: "
set i = i + 1
endloop
call SetTextTagText(avatar.cardTag, s, 10 * 0.023 / 10)
call SetTextTagVisibility(avatar.cardTag, true)
else
call SetTextTagText(avatar.cardTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(avatar.cardTag, false)
endif
else
set score = e.getNewProperty()
set s = I2S(score)
if (score > 0) then
call SetTextTagText(avatar.scoreTag, s, 10 * 0.023 / 10)
if (score < 21) then
call SetTextTagColor(avatar.scoreTag, 255, 255, 255, 255)
elseif (score > 21) then
call SetTextTagColor(avatar.scoreTag, 255, 0, 0, 255)
else
call SetTextTagColor(avatar.scoreTag, 0, 255, 0, 255)
endif
call SetTextTagVisibility(avatar.scoreTag, true)
else
call SetTextTagText(avatar.scoreTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(avatar.scoreTag, false)
endif
endif
endmethod
endstruct
private struct OutcomeListener extends AvatarListener
method onPropertyChange takes PropertyChangeEvent e returns nothing
local integer new = e.getNewProperty()
local integer amount = e.getData()
if (new == 2) then
call DisplayTimedTextToPlayer(GetOwningPlayer(.getAvatar().getUnit.evaluate()), 0, 0, 10.00, "|cff00ff00You have won with a blackjack!|r |cffffcc00Winnings: " + I2S(amount) + " gold.|r")
elseif (new == 1) then
call DisplayTimedTextToPlayer(GetOwningPlayer(.getAvatar().getUnit.evaluate()), 0, 0, 10.00, "|cffffff00You have won!|r |cffffcc00Winnings: " + I2S(amount) + " gold.|r")
elseif (new == -1) then
call DisplayTimedTextToPlayer(GetOwningPlayer(.getAvatar().getUnit.evaluate()), 0, 0, 10.00, "|cffff0000You have lost. Better luck next time.|r |cffffcc00Losses: " + I2S(amount) + " gold.|r")
else
call DisplayTimedTextToPlayer(GetOwningPlayer(.getAvatar().getUnit.evaluate()), 0, 0, 10.00, "|cffffff00You have tied with the dealer.|r")
endif
endmethod
endstruct
struct Avatar extends PropertyChangeListener
static constant integer UNIT_TYPE_ID = 'h000'
static constant integer MONEY_ITEM_SLOT = 0
static constant integer BET_ITEM_SLOT = 1
static constant integer BET_UP_ITEM_SLOT = 4
static constant integer BET_DOWN_ITEM_SLOT = 5
static constant integer MONEY_ITEM_TYPE_ID = 'I000'
static constant integer BET_ITEM_TYPE_ID = 'I001'
static constant integer BET_UP_ITEM_TYPE_ID = 'I002'
static constant integer BET_DOWN_ITEM_TYPE_ID = 'I003'
static constant integer JOIN_SPELL_ABIL_ID = 'A000'
static constant integer LEAVE_SPELL_ABIL_ID = 'A001'
static constant integer START_SPELL_ABIL_ID = 'A005'
static constant integer END_SPELL_ABIL_ID = 'A008'
static constant integer FINALIZE_SPELL_ABIL_ID = 'A004'
static constant integer HIT_SPELL_ABIL_ID = 'A002'
static constant integer STAND_SPELL_ABIL_ID = 'A003'
private static constant real TIMER_PERIOD = 0.03
private static IntegerIntegerMap avatars
private static timer t = CreateTimer()
private static IntegerCollection c
private BlackjackPlayer p
private unit u
private PropertyChangeListener gamePcl
private PropertyChangeListener statusPcl
private PropertyChangeListener moneyPcl
private PropertyChangeListener cardPcl
private PropertyChangeListener outcomePcl
texttag cardTag
texttag scoreTag
private boolean dead = false
static method create takes player owner, real x, real y, integer money returns Avatar
local Avatar this = Avatar.allocate()
local PropertyChangeSupport pcs
set this.p = BlackjackPlayer.create(money)
set this.u = CreateUnit(owner, .UNIT_TYPE_ID, x, y, 270)
call UnitAddItemToSlotById(this.u, .MONEY_ITEM_TYPE_ID, .MONEY_ITEM_SLOT)
call SetItemCharges(UnitItemInSlot(this.u, .MONEY_ITEM_SLOT), money)
call UnitAddItemToSlotById(this.u, .BET_ITEM_TYPE_ID, .BET_ITEM_SLOT)
call SetItemCharges(UnitItemInSlot(this.u, .BET_ITEM_SLOT), 0)
call UnitAddItemToSlotById(this.u, .BET_UP_ITEM_TYPE_ID, .BET_UP_ITEM_SLOT)
call UnitAddItemToSlotById(this.u, .BET_DOWN_ITEM_TYPE_ID, .BET_DOWN_ITEM_SLOT)
call UnitAddAbility(this.u, .JOIN_SPELL_ABIL_ID)
set this.gamePcl = GameListener.create(this)
set this.statusPcl = StatusListener.create(this)
set this.moneyPcl = MoneyListener.create(this)
set this.cardPcl = CardListener.create(this)
set this.outcomePcl = OutcomeListener.create(this)
set this.cardTag = CreateTextTag()
call SetTextTagColor(this.cardTag, 255, 255, 255, 255)
call SetTextTagText(this.cardTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(this.cardTag, false)
set this.scoreTag = CreateTextTag()
call SetTextTagColor(this.scoreTag, 255, 255, 0, 255)
call SetTextTagText(this.scoreTag, "", 10 * 0.023 / 10)
call SetTextTagVisibility(this.scoreTag, false)
set pcs = this.p.getPcs()
call pcs.addListenerByProperty("alive", this)
call pcs.addListenerByProperty("game", this.gamePcl)
call pcs.addListenerByProperty("status", this.statusPcl)
call pcs.addListenerByProperty("gamestatus", this.statusPcl)
call pcs.addListenerByProperty("money", this.moneyPcl)
call pcs.addListenerByProperty("bet", this.moneyPcl)
call pcs.addListenerByProperty("card", this.cardPcl)
call pcs.addListenerByProperty("score", this.cardPcl)
call pcs.addListenerByProperty("outcome", this.outcomePcl)
call .avatars.put(GetHandleId(this.u), this)
if (.avatars.size() == 1) then
call TimerStart(.t, .TIMER_PERIOD, true, function Avatar.tick)
endif
return this
endmethod
method getPlayer takes nothing returns BlackjackPlayer
return .p
endmethod
method getUnit takes nothing returns unit
return .u
endmethod
method terminate takes nothing returns nothing
set .dead = true
endmethod
method onPropertyChange takes PropertyChangeEvent e returns nothing
if (e.getNewProperty() == 0) then
call .terminate()
endif
endmethod
method destroy takes nothing returns nothing
call this.gamePcl.destroy()
call this.moneyPcl.destroy()
call this.cardPcl.destroy()
call this.outcomePcl.destroy()
call DestroyTextTag(this.cardTag)
call DestroyTextTag(this.scoreTag)
endmethod
static method getAvatar takes unit u returns Avatar
return .avatars.get(GetHandleId(u))
endmethod
private static method tick takes nothing returns nothing
local IntegerIterator it = .c.iterator()
local Avatar this
loop
exitwhen (it.hasNext() == false)
set this = it.next()
if (this.dead) then
call it.remove()
if (.c.size() == 0) then
call PauseTimer(this.t)
endif
else
call SetTextTagPos(this.cardTag, GetUnitX(this.u), GetUnitY(this.u), 150.0)
call SetTextTagPos(this.scoreTag, GetUnitX(this.u) - 50.0, GetUnitY(this.u), 150.0)
endif
endloop
call it.destroy()
endmethod
private static method onSpellEffect takes nothing returns nothing
local integer id = GetSpellAbilityId()
local Avatar avatar = .avatars.get(GetHandleId(GetTriggerUnit()))
local GameTable table
if (avatar != 0) then
if (id == .JOIN_SPELL_ABIL_ID) then
set table = GameTable.getGameTable(GetSpellTargetUnit())
if (table != 0) then
call avatar.getPlayer().joinGame(table.getGame())
endif
elseif (id == .LEAVE_SPELL_ABIL_ID) then
call avatar.getPlayer().leaveGame()
elseif (id == .START_SPELL_ABIL_ID) then
call avatar.getPlayer().getGame().startRound()
elseif (id == .END_SPELL_ABIL_ID) then
call avatar.getPlayer().getGame().endRound()
elseif (id == .FINALIZE_SPELL_ABIL_ID) then
call avatar.getPlayer().finalizeBet()
elseif (id == .HIT_SPELL_ABIL_ID) then
call avatar.getPlayer().hit()
elseif (id == .STAND_SPELL_ABIL_ID) then
call avatar.getPlayer().stand()
endif
endif
endmethod
private static method onUseItem takes nothing returns nothing
local item i = GetManipulatedItem()
local item bet
local integer id = GetItemTypeId(i)
local Avatar avatar = .avatars.get(GetHandleId(GetTriggerUnit()))
if (avatar != 0) then
if (id == .BET_UP_ITEM_TYPE_ID) then
set bet = UnitItemInSlot(avatar.getUnit(), .BET_ITEM_SLOT)
call avatar.getPlayer().placeBet(GetItemCharges(bet) + 1)
elseif (id == .BET_DOWN_ITEM_TYPE_ID) then
set bet = UnitItemInSlot(avatar.getUnit(), .BET_ITEM_SLOT)
call avatar.getPlayer().placeBet(GetItemCharges(bet) - 1)
endif
endif
set i = null
set bet = null
endmethod
private static method onInit takes nothing returns nothing
local trigger t = CreateTrigger()
local integer i = 0
set .avatars = IntegerIntegerLinkedHashMap.create()
set .c = .avatars.values()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_SPELL_EFFECT)
call TriggerAddAction(t, function Avatar.onSpellEffect)
set t = CreateTrigger()
call TriggerRegisterAnyUnitEventBJ(t, EVENT_PLAYER_UNIT_USE_ITEM)
call TriggerAddAction(t, function Avatar.onUseItem)
endmethod
endstruct
endscope
endlibrary
//TESH.scrollpos=7
//TESH.alwaysfold=0
scope BlackjackInit initializer init
private function init takes nothing returns nothing
local integer i = 0
loop
exitwhen (i > 11)
if (GetPlayerSlotState(Player(i)) == PLAYER_SLOT_STATE_PLAYING) then
call Avatar.create(Player(i), 0, 0, 100)
endif
set i = i + 1
endloop
call GameTable.create(-1157, -8, 2, false)
call GameTable.create(-1157, -775, 4, false)
call GameTable.create(-391, -1288, 6, false)
call GameTable.create(372, -1288, 6, true)
call GameTable.create(1092, -775, 4, true)
call GameTable.create(1095, -8, 2, true)
//call ABMapEntrySet.create()
endfunction
endscope