Collections Module
The Language::Collections module provides generic data structures for storing and organizing data.
#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)
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | List<T>^ | Creates empty list |
| add | value: T^ | None | Appends element to end |
| get | index: Integer& | T^ | Returns element at index |
| set | index: Integer&, value: T^ | None | Updates element at index |
| remove | index: Integer& | None | Removes element, shifts others |
| size | — | Integer^ | Returns element count |
| isEmpty | — | Bool^ | Returns true if empty |
| clear | — | None | Removes all elements |
| dispose | — | None | Frees allocated memory |
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()); // 2Array<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
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | Array<T,N>^ | Allocates array of size N |
| get | index: Integer& | T^ | Returns element at index |
| set | index: Integer&, value: T^ | None | Sets element at index |
| size | — | Integer^ | Returns array capacity (N) |
| fill | value: T^ | None | Sets all elements to value |
| isValidIndex | index: Integer& | Bool^ | Checks bounds |
| dispose | — | None | Frees allocated memory |
// 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
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
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | HashMap<K,V>^ | Creates empty map |
| put | key: K^, value: V^ | None | Inserts or updates entry |
| get | key: K^ | V^ | Returns value for key |
| containsKey | key: K^ | Bool^ | Checks if key exists |
| remove | key: K^ | Bool^ | Removes entry, returns success |
| size | — | Integer^ | Returns entry count |
| isEmpty | — | Bool^ | Returns true if empty |
| clear | — | None | Removes all entries |
| dispose | — | None | Frees allocated memory |
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
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
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | Set<T>^ | Creates empty set |
| add | value: T^ | Bool^ | Adds element, returns true if new |
| contains | value: T& | Bool^ | Checks if element exists |
| remove | value: T& | Bool^ | Removes element, returns success |
| size | — | Integer^ | Returns element count |
| isEmpty | — | Bool^ | Returns true if empty |
| clear | — | None | Removes all elements |
| dispose | — | None | Frees allocated memory |
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)
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | Stack<T>^ | Creates empty stack |
| push | value: T^ | None | Pushes element onto top |
| pop | — | T^ | Removes and returns top |
| peek | — | T^ | Returns top without removing |
| size | — | Integer^ | Returns element count |
| isEmpty | — | Bool^ | Returns true if empty |
| clear | — | None | Removes all elements |
| dispose | — | None | Frees allocated memory |
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, 1Queue<T>
First-In-First-Out (FIFO) data structure using a circular buffer for efficient operations.
Constraints: T: None (any type)
| Method | Parameters | Returns | Description |
|---|---|---|---|
| Constructor | — | Queue<T>^ | Creates empty queue |
| enqueue | value: T^ | None | Adds element to back |
| dequeue | — | T^ | Removes and returns front |
| peek | — | T^ | Returns front without removing |
| size | — | Integer^ | Returns element count |
| isEmpty | — | Bool^ | Returns true if empty |
| clear | — | None | Removes all elements |
| dispose | — | None | Frees allocated memory |
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 CMemory Management
All collections allocate memory dynamically. Call dispose() when a collection is no longer needed to free the underlying memory.
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
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 ]