FastMap<K, V> class
A high-performance map implementation optimized for callback management.
FastMap provides O(1) complexity for all primary operations by maintaining a dual data structure: a List for sequential access and a Map for key-to-index lookups. This hybrid approach is ideal for scenarios where iteration order doesn't matter but fast access by both key and index is required.
Design
The implementation uses:
List<V> _values: Stores values for O(1) sequential iterationMap<K, int> _keyToIndex: Maps keys to indices for O(1) lookup
Performance Characteristics
| Operation | Time Complexity | Description |
|---|---|---|
set() |
O(1) amortized | Add or update key-value pair |
delete() |
O(1) | Remove by key using swap-with-last |
get() / [] |
O(1) | Retrieve value by key |
getByIndex() |
O(1) | Direct access by list index |
clear() |
O(n) | Remove all entries |
Example
final map = FastMap<String, int>();
map.set('a', 1);
map.set('b', 2);
print(map['a']); // 1
print(map.getByIndex(0)); // 1
map.delete('a');
print(map.length); // 1
Use Cases
- Callback registration/removal (the primary use in this library)
- Event listener management
- Plugin/middleware systems
- Any scenario requiring fast add/remove with iteration
Constructors
- FastMap()
Properties
Methods
-
clear(
) → void - Removes all entries from the map.
-
delete(
K key) → void -
Removes the entry with the given
key. -
get(
K key) → V? -
Returns the value associated with
key, ornullif not found. -
getByIndex(
int index) → V -
Returns the value at the given
indexin the internal list. -
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
set(
K key, V value) → void -
Sets the
valuefor the givenkey. -
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited
-
operator [](
dynamic key) → V? - Operator overload to access values by key.