@ Update execution plan to reflect completion, sync user edits
- Mark all round 2 tasks complete (211 tests, 4,111 lines, 15 commits)
- Update checklist and progress overview
- Sync README GitHub URL and submission doc personal info
Co-Authored-By: Claude Opus 4.7 noreply@anthropic.com @
版权所有:中国计算机学会技术支持:开源发展技术委员会
京ICP备13000930号-9
京公网安备 11010802047560号
moonbit-indexmap
A hash map that preserves insertion order, implemented in MoonBit — inspired by the popular Rust
indexmapcrate.Why indexmap?
MoonBit’s built-in
Map[K, V]does not guarantee iteration order. When you need to remember the order in which entries were inserted — for configuration parsing, LRU caches, JSON serialization, or deterministic tests — IndexMap is the solution.Features
OccupiedEntry/VacantEntryindexmapcrate where practicalInstallation
Add to your
moon.mod.json:Or clone directly:
Quick Start
IndexMap
Entry API
IndexSet
API Reference
IndexMap[K, V]
Construction | Method | Description | |——–|————-| |
new() -> IndexMap[K, V]| Create an empty map with default capacity | |with_capacity(n : Int) -> IndexMap[K, V]| Create with pre-allocated capacity | |from_array(entries : Array[(K, V)]) -> IndexMap[K, V]| Create from an array of pairs |Size queries | Method | Description | |——–|————-| |
len() -> Int| Number of entries | |is_empty() -> Bool| Whether map is empty | |capacity() -> Int| Current number of buckets | |load_factor() -> Double| Current load factor | |max_probe() -> Int| Maximum probe distance observed |Core operations | Method | Description | |——–|————-| |
insert(key : K, value : V) -> V?| Insert or update, returns old value | |get(key : K) -> V?| Look up value by key | |remove(key : K) -> V?| Remove and return value | |contains(key : K) -> Bool| Check if key exists | |clear()| Remove all entries |Entry API | Method | Description | |——–|————-| |
entry(key : K) -> EntryView[K, V]| Get entry for in-place manipulation |OccupiedEntry methods:
get() -> V,insert(value : V) -> V,remove() -> V,key() -> KVacantEntry methods:
insert(value : V) -> V,key() -> KIndex-based access | Method | Description | |——–|————-| |
get_index(i : Int) -> (K, V)?| Get entry by insertion-order position | |get_full(key : K) -> (K, V)?| Get key and value as a pair | |get_index_of(key : K) -> Int?| Get insertion-order position of key | |first() -> (K, V)?| Get first (earliest) entry | |last() -> (K, V)?| Get last (most recent) entry | |pop() -> (K, V)?| Remove and return last entry | |swap_remove_index(i : Int) -> (K, V)?| Swap-remove by position |Capacity management | Method | Description | |——–|————-| |
reserve(additional : Int)| Reserve space for more entries | |shrink_to_fit()| Shrink capacity to fit current size |Iteration | Method | Description | |——–|————-| |
iter() -> Iter[K, V]| Iterate (key, value) pairs in insertion order | |keys() -> Keys[K, V]| Iterate keys in insertion order | |values() -> Values[K, V]| Iterate values in insertion order | |for_each(f : (K, V) -> Unit)| Apply function to each entry |Iter methods:
next() -> (K, V)?,collect() -> Array[(K, V)],count_remaining() -> IntBulk operations | Method | Description | |——–|————-| |
retain(f : (K, V) -> Bool)| Keep entries matching predicate | |sort_by_key()| Sort entries by key (requires Compare) | |sort_by(cmp : ((K, V), (K, V)) -> Int)| Sort entries with custom comparator | |drain() -> Array[(K, V)]| Remove all entries, return as array | |extend(entries : Array[(K, V)])| Insert all entries from array |IndexSet[K]
new() -> IndexSet[K]with_capacity(n : Int) -> IndexSet[K]from_array(elements : Array[K]) -> IndexSet[K]len() -> Intis_empty() -> Boolcapacity() -> Intinsert(value : K) -> Booltrueif newcontains(value : K) -> Boolremove(value : K) -> Boolclear()iter() -> Keys[K, Unit]retain(f : (K) -> Bool)drain() -> Array[K]extend(elements : Array[K])is_disjoint(other : IndexSet[K]) -> Boolis_subset(other : IndexSet[K]) -> Boolis_superset(other : IndexSet[K]) -> BoolDesign
Internally, IndexMap uses two parallel structures:
Deletion uses tombstone markers to maintain probe chain integrity, with automatic rehashing when the tombstone ratio exceeds 25%.
Trade-offs vs. builtin Map
@builtin.MapIndexMapHashtraitHashtraitDevelopment
License
Apache 2.0 — see LICENSE for details.
Acknowledgments
This project is inspired by the Rust
indexmapcrate (Apache 2.0 / MIT dual-licensed).Built as part of the MoonBit Open Source Ecosystem Competition 2026.