Collections Module

The Language::Collections module provides generic data structures for storing and organizing data.

xxml
#import Language::Collections;

List<T>

A dynamically-sized list that grows automatically. Elements are stored contiguously and accessed by index.

Constraints: T: None (any type)

MethodParametersReturnsDescription
ConstructorList<T>^Creates empty list
addvalue: T^NoneAppends element to end
getindex: Integer&T^Returns element at index
setindex: Integer&, value: T^NoneUpdates element at index
removeindex: Integer&NoneRemoves element, shifts others
sizeInteger^Returns element count
isEmptyBool^Returns true if empty
clearNoneRemoves all elements
disposeNoneFrees allocated memory
xxml
Instantiate Collections::List<String>^ As <fruits> =
    Collections::List@String::Constructor();

Run fruits.add(String::Constructor("Apple"));
Run fruits.add(String::Constructor("Banana"));
Run fruits.add(String::Constructor("Cherry"));

// Access elements
Instantiate String^ As <first> = fruits.get(Integer::Constructor(0));
Run System::Console::printLine(first);  // Apple

// Update element
Run fruits.set(Integer::Constructor(1), String::Constructor("Blueberry"));

// Remove element
Run fruits.remove(Integer::Constructor(0));

// Check size
Run System::Console::printLine(fruits.size().toString());  // 2

Array<T, N>

Fixed-size array with compile-time constant length. More memory efficient than List for known sizes.

Constraints: T: None, N: compile-time integer constant

MethodParametersReturnsDescription
ConstructorArray<T,N>^Allocates array of size N
getindex: Integer&T^Returns element at index
setindex: Integer&, value: T^NoneSets element at index
sizeInteger^Returns array capacity (N)
fillvalue: T^NoneSets all elements to value
isValidIndexindex: Integer&Bool^Checks bounds
disposeNoneFrees allocated memory
xxml
// Create fixed-size array of 10 integers
Instantiate Collections::Array<Integer, 10>^ As <numbers> =
    Collections::Array@Integer, 10::Constructor();

// Fill with zeros
Run numbers.fill(Integer::Constructor(0));

// Set specific values
Run numbers.set(Integer::Constructor(0), Integer::Constructor(100));
Run numbers.set(Integer::Constructor(5), Integer::Constructor(50));

// Access element
Instantiate Integer^ As <value> = numbers.get(Integer::Constructor(5));

Note

Arrays have a fixed size determined at compile time. Use List<T> when you need dynamic sizing. Arrays are more memory-efficient when the size is known.

HashMap<K, V>

Hash table mapping keys to values with O(1) average-case operations using separate chaining.

Constraints: K: Hashable, Equatable | V: None

MethodParametersReturnsDescription
ConstructorHashMap<K,V>^Creates empty map
putkey: K^, value: V^NoneInserts or updates entry
getkey: K^V^Returns value for key
containsKeykey: K^Bool^Checks if key exists
removekey: K^Bool^Removes entry, returns success
sizeInteger^Returns entry count
isEmptyBool^Returns true if empty
clearNoneRemoves all entries
disposeNoneFrees allocated memory
xxml
Instantiate Collections::HashMap<String, Integer>^ As <ages> =
    Collections::HashMap@String, Integer::Constructor();

// Add entries
Run ages.put(String::Constructor("Alice"), Integer::Constructor(30));
Run ages.put(String::Constructor("Bob"), Integer::Constructor(25));

// Check and get
Instantiate String^ As <name> = String::Constructor("Alice");
If (ages.containsKey(name).toBool())
{
    Instantiate Integer^ As <age> = ages.get(name);
    Run System::Console::printLine(age.toString());  // 30
}

// Update existing
Run ages.put(String::Constructor("Alice"), Integer::Constructor(31));

// Remove entry
Run ages.remove(String::Constructor("Bob"));

Warning

Keys must implement both Hashable and Equatable constraints. Built-in types like String and Integer already satisfy these requirements.

Set<T>

Unordered collection of unique values. Duplicate additions are ignored.

Constraints: T: Hashable, Equatable

MethodParametersReturnsDescription
ConstructorSet<T>^Creates empty set
addvalue: T^Bool^Adds element, returns true if new
containsvalue: T&Bool^Checks if element exists
removevalue: T&Bool^Removes element, returns success
sizeInteger^Returns element count
isEmptyBool^Returns true if empty
clearNoneRemoves all elements
disposeNoneFrees allocated memory
xxml
Instantiate Collections::Set<String>^ As <tags> =
    Collections::Set@String::Constructor();

// Add unique values
Run tags.add(String::Constructor("xxml"));
Run tags.add(String::Constructor("programming"));
Run tags.add(String::Constructor("xxml"));  // Ignored (duplicate)

Run System::Console::printLine(tags.size().toString());  // 2

// Check membership
If (tags.contains(String::Constructor("xxml")).toBool())
{
    Run System::Console::printLine(String::Constructor("Found tag!"));
}

Stack<T>

Last-In-First-Out (LIFO) data structure. Elements are pushed and popped from the top.

Constraints: T: None (any type)

MethodParametersReturnsDescription
ConstructorStack<T>^Creates empty stack
pushvalue: T^NonePushes element onto top
popT^Removes and returns top
peekT^Returns top without removing
sizeInteger^Returns element count
isEmptyBool^Returns true if empty
clearNoneRemoves all elements
disposeNoneFrees allocated memory
xxml
Instantiate Collections::Stack<Integer>^ As <history> =
    Collections::Stack@Integer::Constructor();

// Push elements
Run history.push(Integer::Constructor(1));
Run history.push(Integer::Constructor(2));
Run history.push(Integer::Constructor(3));

// Peek at top
Instantiate Integer^ As <top> = history.peek();
Run System::Console::printLine(top.toString());  // 3

// Pop elements (LIFO order)
While (history.isEmpty().not().toBool())
{
    Instantiate Integer^ As <value> = history.pop();
    Run System::Console::printLine(value.toString());
}
// Output: 3, 2, 1

Queue<T>

First-In-First-Out (FIFO) data structure using a circular buffer for efficient operations.

Constraints: T: None (any type)

MethodParametersReturnsDescription
ConstructorQueue<T>^Creates empty queue
enqueuevalue: T^NoneAdds element to back
dequeueT^Removes and returns front
peekT^Returns front without removing
sizeInteger^Returns element count
isEmptyBool^Returns true if empty
clearNoneRemoves all elements
disposeNoneFrees allocated memory
xxml
Instantiate Collections::Queue<String>^ As <tasks> =
    Collections::Queue@String::Constructor();

// Enqueue tasks
Run tasks.enqueue(String::Constructor("Task A"));
Run tasks.enqueue(String::Constructor("Task B"));
Run tasks.enqueue(String::Constructor("Task C"));

// Process tasks (FIFO order)
While (tasks.isEmpty().not().toBool())
{
    Instantiate String^ As <task> = tasks.dequeue();
    Run System::Console::printLine(String::Constructor("Processing: ").append(task));
}
// Output: Task A, Task B, Task C

Memory Management

All collections allocate memory dynamically. Call dispose() when a collection is no longer needed to free the underlying memory.

xxml
Instantiate Collections::List<Integer>^ As <numbers> =
    Collections::List@Integer::Constructor();

// Use the list...
Run numbers.add(Integer::Constructor(1));
Run numbers.add(Integer::Constructor(2));

// Clean up when done
Run numbers.dispose();

Complete Example

task_manager.xxml
1#import Language::Core;
2#import Language::Collections;
3#import Language::System;
4
5[ Entrypoint
6 {
7 // Task queue for processing
8 Instantiate Collections::Queue<String>^ As <taskQueue> =
9 Collections::Queue@String::Constructor();
10
11 // Completed task set (for deduplication)
12 Instantiate Collections::Set<String>^ As <completed> =
13 Collections::Set@String::Constructor();
14
15 // Add tasks
16 Run taskQueue.enqueue(String::Constructor("Initialize"));
17 Run taskQueue.enqueue(String::Constructor("Process"));
18 Run taskQueue.enqueue(String::Constructor("Finalize"));
19
20 // Process all tasks
21 While (taskQueue.isEmpty().not().toBool())
22 {
23 Instantiate String^ As <task> = taskQueue.dequeue();
24 Run System::Console::printLine(String::Constructor("Running: ").append(task));
25 Run completed.add(task);
26 }
27
28 // Report
29 Run System::Console::printLine(
30 String::Constructor("Completed ")
31 .append(completed.size().toString())
32 .append(String::Constructor(" tasks"))
33 );
34
35 // Cleanup
36 Run taskQueue.dispose();
37 Run completed.dispose();
38
39 Exit(0);
40 }
41]

See Also