Collections Overview
WARNING: This page may include outdated or invalid syntax. Examples are illustrative and will be updated to current Harneet standards (var/mandatory parameter types/valid function syntax).
The Harneet programming language provides a comprehensive set of data structures through its enhanced collections module. These data structures are designed for performance, memory efficiency, and ease of use, making them suitable for a wide range of applications from simple scripts to complex concurrent systems.
Available Data Structures
Core Collections
| Data Structure | Description | Use Cases | Time Complexity |
|---|---|---|---|
| Stack | Last-In-First-Out (LIFO) | Function calls, undo operations, expression evaluation | O(1) push/pop |
| Queue | First-In-First-Out (FIFO) | Task scheduling, breadth-first search, buffering | O(1) enqueue/dequeue |
| Set | Unique element collection | Membership testing, duplicate removal, set operations | O(1) average add/contains |
| LinkedList | Doubly-linked list | Dynamic insertion/deletion, iterator patterns | O(1) head/tail operations |
| BinarySearchTree | Self-balancing tree | Sorted data, range queries, ordered traversal | O(log n) search/insert |
Enhanced Collections
| Data Structure | Description | Performance Benefits |
|---|---|---|
| Enhanced Array | Optimized dynamic array | Pre-allocation, functional methods, memory pooling |
| Enhanced Map | High-performance hash map | Better hash functions, load balancing, concurrent access |
Concurrent Collections
All data structures have thread-safe variants for concurrent applications:
ConcurrentStack- Thread-safe stack with RWMutexConcurrentQueue- Thread-safe queue with lock-free optimizationsConcurrentSet- Thread-safe set with concurrent hash mapConcurrentMap- Thread-safe map with optimized locking
Getting Started
Basic Usage
Performance Considerations
The enhanced collections are designed with performance in mind:
- Memory Pre-allocation: Use
reserve()methods to pre-allocate memory for known sizes - Batch Operations: Prefer bulk operations when available
- Iterator Patterns: Use iterators for efficient traversal
- Concurrent Access: Use concurrent variants for multi-threaded applications
Memory Management
Collections automatically manage memory allocation and deallocation:
- Dynamic resizing with configurable growth factors
- Automatic shrinking when collections become sparse
- Memory pooling for frequently created/destroyed collections
- Garbage collection friendly implementations
Quick Reference
Import and Creation
Common Operations
| Common Operations | |
|---|---|
Performance Characteristics
Time Complexity Summary
| Operation | Stack | Queue | Set | LinkedList | BST |
|---|---|---|---|---|---|
| Insert | O(1) | O(1) | O(1)* | O(1)† | O(log n) |
| Remove | O(1) | O(1) | O(1)* | O(1)† | O(log n) |
| Search | O(n) | O(n) | O(1)* | O(n) | O(log n) |
| Access | O(n) | O(n) | O(1)* | O(n) | O(log n) |
*Average case for hash-based operations
†For head/tail operations
Space Complexity
All collections have O(n) space complexity where n is the number of elements, with additional overhead for:
- Hash tables: Load factor overhead (typically 25-50%)
- Trees: Parent/child pointers and balancing metadata
- Linked structures: Next/previous pointers
- Concurrent collections: Synchronization primitives
Best Practices
Choosing the Right Data Structure
- Use Stack when: You need LIFO behavior, implementing recursion, or managing function calls
- Use Queue when: You need FIFO behavior, task scheduling, or breadth-first algorithms
- Use Set when: You need unique elements, fast membership testing, or set operations
- Use LinkedList when: You need frequent insertion/deletion at arbitrary positions
- Use BinarySearchTree when: You need sorted data with fast search/insert/delete operations
- Use Enhanced Array when: You need dynamic arrays with functional programming methods
- Use Enhanced Map when: You need high-performance key-value storage
Performance Tips
- Pre-allocate: Use
reserve()methods when you know the expected size - Batch operations: Use bulk methods when available (e.g.,
add_all(),remove_all()) - Choose appropriate initial capacity: Avoid frequent resizing operations
- Use concurrent collections: Only when actually needed for multi-threaded access
- Profile memory usage: Monitor collection sizes in production applications
Error Handling
Collections follow consistent error handling patterns:
Migration Guide
If you're upgrading from basic collections to enhanced collections, see the Migration Guide for detailed instructions on updating your code.
Examples and Tutorials
- Stacks & Queues Guide - Comprehensive guide to LIFO and FIFO structures
- Sets Guide - Working with unique collections and set operations
- Linked Lists Guide - Dynamic list manipulation and iteration
- Trees Guide - Binary search trees and traversal algorithms
- Concurrent Collections Guide - Thread-safe data structures
- Performance Guide - Optimization techniques and benchmarks
API Reference
For detailed API documentation including all methods, parameters, and return types, see the individual data structure guides linked above.