Skip to content

stdlib: add configurable IntMap with open addressing - #208

Merged
dslsdzc merged 1 commit into
developfrom
intmap
Oct 26, 2026
Merged

dslsdzc merged 1 commit into
developfrom
intmap

Conversation

@dslsdzc

@dslsdzc dslsdzc commented Oct 26, 2026

Copy link
Copy Markdown
Owner

概要

新增 src/stdlib/hashmap.cr:开放寻址的 IntMap(int → int)。

  • keys/values 存在 cell 缓冲区,占用位每槽 1 字节;线性探测 + 墓碑处理碰撞与删除。
  • 构造参数全部可调:
    • initial_cap
    • growth_num / growth_den(扩容比例)
    • load_num / load_den(装填因子)
    • seed
    • hash_kind(0 murmur 风格、1 golden-ratio、2 identity-xor、3 故意只有 7 桶的弱哈希,用于碰撞测试)
  • 默认 intMap():cap 16 / 2x 增长 / 3/4 装填 / seed 0 / kind 0。
  • 方法:put / get / has / remove / size / capacity / clear。

验证

  • selfhost-tests 全绿(新增 test_intmap.py)
  • bootstrap-tests 全绿
  • check 全绿
  • canary 5/5,值不变
  • test_intmap.py:默认 map、参数化增长(cap 4 / growth 3:2 / load 1:1)、弱哈希碰撞表、清空;
    以及 500 次确定性 LCG 操作与 Python dict 对拍(size、最终 capacity、100 个 key 的 get)。

备注

探测 helper 是自由函数(按值接收 IntMap),方法只调用 helper:
嵌套 &self 方法调用目前在 backend 段错误(已用最小 probe 复现),本批绕开该路径,
不改语言/后端。

- IntMap stores int keys and values in cell buffers with one occupancy
  byte per slot; linear probing plus tombstones handle collisions and
  deletion.
- Constructor parameters: initial capacity, growth ratio, load factor,
  hash seed and hash kind. Kinds include a murmur-style finalizer, a
  golden-ratio mix, identity-xor and a deliberately weak 7-bucket mode
  for collision tests.
- Probe helpers are free functions because nested methods currently
  fault in the backend; methods call only free helpers, never another
  method on the same receiver.
- test_intmap.py drives 500 deterministic LCG operations against a
  Python dict oracle and checks capacity growth plus every lookup.
@dslsdzc
dslsdzc merged commit ce849be into develop Oct 26, 2026
4 checks passed
@dslsdzc
dslsdzc deleted the intmap branch October 26, 2026 09:25
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant