Collections
HashMap
HashMap<K, V, H = Hasher<K>> is a hash map for key-value storage:
HashMap<string, int> scores = HashMap<string, int>();scores.Set("Alice", 95);scores.Set("Bob", 87);
int val = scores.Get("Alice");Console.WriteLine($"Count: {scores.Count}"); // Count: 2Console.WriteLine($"Has Alice: {scores.ContainsKey("Alice")}"); // Has Alice: truescores.Remove("Bob");scores.Clear();HashSet
HashSet<T, H = Hasher<T>> stores unique values with O(1) lookup:
HashSet<string> tags = HashSet<string>();tags.Set("cera");tags.Set("jit");tags.Set("cera"); // duplicate ignored
Console.WriteLine($"Count: {tags.Count}"); // Count: 2Console.WriteLine($"Has jit: {tags.Contains("jit")}"); // Has jit: truetags.Remove("jit");Stack
Stack<T> is a LIFO (last-in, first-out) collection:
Stack<int> stack = Stack<int>();stack.Push(10);stack.Push(20);stack.Push(30);
int top = stack.Pop(); // 30int next = stack.Peek; // 20Console.WriteLine($"Count: {stack.Count}"); // Count: 2Console.WriteLine($"Has 10: {stack.Contains(10)}"); // Has 10: trueQueue
Queue<T> is a FIFO (first-in, first-out) collection:
Queue<string> q = Queue<string>();q.Enqueue("first");q.Enqueue("second");q.Enqueue("third");
string front = q.Dequeue(); // "first"string next = q.Peek; // "second"Console.WriteLine($"Count: {q.Count}"); // Count: 2Console.WriteLine($"Has third: {q.Contains("third")}"); // Has third: trueOther Map Types
Cera provides several specialized map implementations:
| Type | Description |
|---|---|
FlatHashMap<K, V, H> | Cache-friendly open-addressing map |
NodeHashMap<K, V, H> | Pointer-stable node-based map |
TreeMap<K, V> | Sorted map with Min, Max properties and Floor, Ceil methods |
DenseMap<K, V> | Compact map optimized for integer-range keys |
All share the same API as HashMap: Set, Get, ContainsKey, Remove, Clear, and the Count property.
TreeMap adds ordered operations:
TreeMap<int, string> tree = TreeMap<int, string>();tree.Set(10, "ten");tree.Set(20, "twenty");tree.Set(30, "thirty");
string val = tree.Get(20); // "twenty"int lo = tree.Min; // 10int hi = tree.Max; // 30int f = tree.Floor(25); // 20int c = tree.Ceil(25); // 30DenseMap takes a key range in the constructor:
DenseMap<int, string> dm = DenseMap<int, string>(0, 100);dm.Set(42, "answer");Console.WriteLine(dm.Get(42)); // answerCollection Initializer Lists
Collections support initializer list syntax for concise construction. The initializer calls the appropriate insertion method (Set, Push, Enqueue) automatically:
// HashMap — key:value pairs call Set(key, value)HashMap<string, int> scores = HashMap<string, int>() { "Alice": 95, "Bob": 87 };
// HashSet — values call Set(value)HashSet<string> tags = HashSet<string>() { "cera", "jit", "gc" };
// Stack — values call Push(value)Stack<int> stack = Stack<int>() { 10, 20, 30 };
// Queue — values call Enqueue(value)Queue<string> q = Queue<string>() { "first", "second", "third" };
// Works with all map typesTreeMap<int, string> tree = TreeMap<int, string>() { 1: "one", 2: "two" };FlatHashMap<string, int> flat = FlatHashMap<string, int>() { "x": 10 };NodeHashMap<string, int> node = NodeHashMap<string, int>() { "y": 20 };For map types, use key: value pairs. For single-value collections (HashSet, Stack, Queue), use comma-separated values.
Hashing
Hash-based collections delegate hashing to a hasher type passed as a template parameter (H). The default is Hasher<K>, which calls (hash)key:
// Default Hasher<T> calls (hash)value for any typetype Hasher<T> { static hash Compute(T value) { return (hash)value; }}Built-in (hash) conversions exist for int, string, float, and bool. User types provide explicit hash() conversion operators:
type Point { int x; int y; explicit hash() { return (hash)(this.x * 31 + this.y); }}
// User types work as HashMap keys via their explicit hash()HashMap<Point, string> map = HashMap<Point, string>();Custom Hashers
Override the default by providing a type with a static Compute() method:
type CaseInsensitiveHasher { static hash Compute(string s) { return (hash)s.ToLower(); }}HashMap<string, int, CaseInsensitiveHasher> map = HashMap<string, int, CaseInsensitiveHasher>();Containers call H.Compute(key) internally, where H is the hasher template parameter.