The collections
package provides data structures for handling heaps and sets. This documentation covers the available structures and their functionalities.
This is WIP and we will continue to add new definitions to this package over time.
This package contains two primary structures:
- Heap: An implementation for managing heaps, supporting both min-heaps and max-heaps.
- Set: An implementation for managing unique collections of data with various set operations.
The Heap
structure allows for the creation of either a min-heap or a max-heap. The heap supports basic operations such as inserting elements, extracting the root, and peeking at the root.
fn new(type = "min")
- Parameters:
type
: A string specifying the type of heap. It can be either"min"
or"max"
. The default is"min"
. If an invalid type is provided, it defaults to"min"
.
Inserts a new value into the heap and maintains the heap property.
- Parameters:
value
: The value to be inserted into the heap.
Removes and returns the root element of the heap. Throws an error if the heap is empty.
- Returns:
- The root element of the heap.
Returns the root element without removing it. Throws an error if the heap is empty.
Returns the number of elements in the heap.
Returns whether the heap is empty.
Ensures the heap property is maintained after inserting a new element.
- Parameters:
index
: The index of the newly inserted element.
Ensures the heap property is maintained after removing the root element.
- Parameters:
index
: The index of the element being moved down the heap.
Swaps the elements at positions i
and j
in the heap.
- Parameters:
i
: The index of the first element.j
: The index of the second element.
The Set
structure provides a way to manage collections of unique elements. It supports various set operations such as union, intersection, and difference.
fn new(data = [])
- Parameters:
data
: A list of initial values for the set. The values are automatically made unique.
Adds a new item to the set, ensuring that the set remains unique.
- Parameters:
item
: The item to be added to the set.
Removes the specified item from the set.
- Parameters:
item
: The item to be removed.
Checks whether the set contains the specified item.
- Returns:
true
if the set contains the item,false
otherwise.
Returns the number of elements in the set.
Removes all elements from the set.
Returns a new set containing elements that are in the current set but not in the provided data.
- Parameters:
data
: A list or another set to compare with.
Returns true
if the set has no elements in common with the provided data.
Checks if the set shares any elements with the provided data.
- Returns:
true
if the sets intersect,false
otherwise.
Merges the provided data into the current set.
- Parameters:
data
: A list or set to merge with the current set.
Returns a new set that is the union of the current set and the provided data.
- Parameters:
data
: A list or set to combine with the current set.
Checks whether the current set is a subset of the provided data.
- Returns:
true
if the current set is a subset of the provided data,false
otherwise.
Checks whether the current set is a superset of the provided data.
- Returns:
true
if the current set is a superset,false
otherwise.
Returns a list of the set's elements.
Returns a string representation of the set.
Returns the string "Set"
to identify the type of the structure.
Extracts the input data from the provided data. It handles both sets and lists, throwing an error if the data is not valid.
- Parameters:
data
: The input data, either a set or a list.