Reference

Data structures & costs

Compare operation costs, ordering, invariants and common algorithm notation.

Scope and conventions

Abstract data structures and conventional implementations. Costs use a unit-cost RAM model unless stated; n is item count, V vertices, E edges and k key/output length. Native containers can have different contracts. Expected, amortized and worst-case bounds are distinguished.

Invariants & selection

Equality, hashing and ordering must agree

Operations & costs

A key contract defines logical equality. Equal hash-map keys need equal hashes; an ordered structure needs a consistent comparison.

Fragment

Operations & costs · Equality, hashing and ordering must agree
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Equal(a,b) -> hash(a) = hash(b)
A collision does not imply Equal(a,b).

Mutating a stored key’s hash/order-relevant fields can break lookup invariants.

Specify duplicate and iteration behavior

Operations & costs

Map replacement, multimap insertion and multiset counting are different contracts. Insertion order, key order and priority order are also different.

Fragment

Operations & costs · Specify duplicate and iteration behavior
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Map: one associated value per logical key
Multimap: potentially multiple values per key
Set: membership
Multiset: membership with counts

Native map/set APIs differ across languages; use the Fundamentals guide for their concrete syntax.

Check reference and iterator lifetime

Operations & costs

Reallocation, insertion, deletion or rehashing may invalidate a reference/iterator according to the native container. A complexity bound does not define that lifetime.

Fragment

Operations & costs · Check reference and iterator lifetime
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Before retaining a handle, ask:
- Can this operation move storage?
- Can this node be removed?
- Does the API permit mutation during iteration?

Choose by required operations

Operations & costs

Start with required queries, updates, order and memory constraints; then choose an implementation whose guarantees meet them.

Fragment

Operations & costs · Choose by required operations
Follow appearance
GitHub Light
GitHub Dark
Rosé Pine
Catppuccin Mocha
VS Code Dark+
Fast indexed reads        contiguous array/vector
Membership by key         hash set/map
Ordered/range queries     sorted array or balanced tree
Repeated best-priority    heap
Prefix matching           trie
Connectivity under unions disjoint set

This is a shortlist, not a universal winner. Workload sizes, key costs and native API contracts can change the choice.