API Reference
Summary¶
RedBlackDbTree is a utility module allowing sorting of objects based on their natural order or a given comparator.
RedBlackDbTree has a special function called RedBlackDbTree.new() which is used to instantiate a new RedBlackDbTree Instance. This function can take a comparator as a parameter to compare and sort objects by.
Methods¶
Add(object: any): nil
Adds the given object to the tree, if it isn't already in the tree.
AddAll(...): nil
Adds the given objects to the tree using Add.
Remove(object: any): nil
Removes the given object from the tree, if it is present in the tree.
RemoveAll(...): nil
Removes the given objects to the tree using Remove.
ContainsObject(object): nil
Returns true if the tree contains the given object, false otherwise.
UpdateObject(object, dict{[property] = update}): nil
Applies the updates given in the dictionary to the object.
Clear(): nil
Removes all objects from the tree, making it empty.
IsEmpty(): nil
Returns true if the tree is empty, false otherwise.
Height(): number
Returns the height of the tree. A one-node tree has height 0.
__len(): number
Returns the number of non-nil nodes in the tree accesed by the # size operator.
Min(): object
Returns smallest object in the tree
Max(): object
Returns largest object in the tree
RemoveMin(): nil
Removes the smallest object from the tree.
RemoveMax(): nil
Removes the largest object from the tree.
PreOrderArray(): {object}
Returns a new array of the tree's objects in pre-order format.
InOrderArray(): {object}
Returns a new array of the tree's objects in the in-order format.
PostOrderArray(): {object}
Returns a new array of the tree's objects in post-order format.
PreOrderPrint(): nil
Prints the tree's objects in pre-order format.
InOrderPrint(): nil
Prints the tree's objects in the in-order format.
PostOrderPrint(): nil
Prints the tree's objects in post-order format.
Constructors¶
RedBlackDbTree.new¶
Creates a new, empty RedBlackDbTree using the natural ordering of its objects.Caution
The only objects who may be sorted by the default comparator are Strings, numbers or Metatables with comparison methods.
Creates a new, empty RedBlackDbTree, ordered according to the given
comparator
.
The comparator should take two arguments representing the objects passed into it. The main 3 cases are listed below:
-
If
object1
is "smaller" thanobject2
, a negative number should be returned -
If
object1
is "greater" thanobject2
, a positive number should be returned -
If the two objects are considered equal, 0 should be returned
Methods¶
Info
self
is an active RedBlackDbTree Instance.
Add¶
Addsobject
to the tree, given it is not in the array already.
Caution
If object
is mutable, it must be updated using UpdateObject
.
Alternatively, the object can be removed, updated then added back in.
AddAll¶
Adds all the given objects in the tuple...
If one object is given, it is assumed to be a table in which all values will be added.
Remove¶
Removesobject
from the tree.
RemoveAll¶
Removes all the given objects in the tuple...
If one object is given, it is assumed to be a table in which all values will be removed.
ContainsObject¶
Returns true if the tree containsobject
, false otherwise.
UpdateObject¶
-
Removes the
object
from the tree -
Applies the
update
to theproperty
of theobject
for all properties indict
-
Adds the updated
object
back into the tree at its correct location
Caution
Only mutable objects may be updated this way. An error will be thrown otherwise.
Clear¶
Remove all objects from the tree, making it empty.IsEmpty¶
Returns true if the tree is empty, false otherwise.Height¶
Returns the height of the tree. A one-node tree has height 0.__len¶
Returns the number of non-nil nodes in the tree.self
should be a RedBlackDbTree instance.
Min¶
Returns the smallest object in the tree.Caution
An error will be produced if there are no objects in the tree.
Max¶
Returns the largest object in the tree.Caution
An error will be produced if there are no objects in the tree.
RemoveMin¶
Removes the smallest object from the tree.Caution
An error will be produced if there are no objects in the tree.
RemoveMax¶
Removes the largest object from the treeCaution
An error will be produced if there are no objects in the tree.