Files
aji/std/map.aji
T
dev 57603860df cli: add safe owning numeric-key maps
Register std.map with bounded U64Map<V>, exact key equality and explicit
allocation/growth. Preserve returned ownership, old slots on reserve failure
and original-authority refunds; support Copy values, owners and resources.

Cover collision/tombstone/wrap behavior, independent dictionary traces,
quota/cleanup/borrow/task rules, source/AIR parity and hosted/package CLI.
Publish the API, roadmap and measured documentation cost. General key
hashing/equality remains Q-037; numeric maps do not satisfy that gate.
2026-10-07 08:36:23 +03:00

284 lines
12 KiB
Rust

module std.map
import std.hash;
import std.option;
struct Slot<V> { key: u64, state: SlotState, value: std.option.Option<V> }
enum SlotState { empty, deleted, occupied }
struct U64Map<V> { storage: buffer<Slot<V>>, length: usize }
struct Entry<V> { key: u64, value: V }
enum NewResult<V> { ok(U64Map<V>), err(alloc_error) }
enum ReserveResult { ok, err(alloc_error) }
enum InsertResult<V> { inserted, replaced(V), full(V) }
enum FreeResult<V> { ok, not_empty(U64Map<V>) }
fn new<V>(capacity: usize, heap: cap<mem.alloc>) -> NewResult<V> effects [mem.alloc] {
let maximum = 0usize -% 1usize;
if capacity > maximum / 2usize { return NewResult::<V>::err(alloc_error::budget_exceeded); }
let count = capacity * 2usize;
match buffer.new<Slot<V>>(count, heap) {
ok(storage) => {
{ mut slots = storage;
for index in 0usize..count {
let empty = Slot::<V> { key: 0, state: SlotState::empty, value: std.option.Option::<V>::none };
buffer.push(slots, move empty) or trap;
}
}
return NewResult::<V>::ok(U64Map::<V> { storage: move storage, length: 0 });
}
err(error) => { return NewResult::<V>::err(error); }
}
}
fn len<V>(map: ref<U64Map<V>>) -> usize effects [] { return map.length; }
fn capacity<V>(map: ref<U64Map<V>>) -> usize effects [] {
ref storage = map.storage;
return buffer.capacity(storage) / 2usize;
}
fn insert<V>(map: mut<U64Map<V>>, key: u64, value: V) -> InsertResult<V> effects [] {
mut storage = map.storage;
let count = buffer.capacity(storage);
if count == 0usize { return InsertResult::<V>::full(move value); }
mut slots = buffer.view(storage);
var wire: [u8; 8] = zero;
for byte in 0usize..8usize { wire[byte] = (key >> ((byte as! u64) * 8u64)) as% u8; }
let hash = std.hash.fnv1a64(ref wire);
var position: usize = (hash % (count as! u64)) as! usize;
var chosen: usize = count;
for probe in 0usize..count {
var found: bool = false;
var stop: bool = false;
{ ref slot = slots[position];
match slot.state {
SlotState::occupied => { found = slot.key == key; }
SlotState::deleted => { if chosen == count { chosen = position; } }
SlotState::empty => { if chosen == count { chosen = position; } stop = true; }
}
}
if found {
mut slot = slots[position];
let replacement = std.option.Option::<V>::some(move value);
let previous = value.swap(mut slot.value, move replacement);
match move previous {
std.option.Option::<V>::some(old) => { return InsertResult::<V>::replaced(move old); }
std.option.Option::<V>::none => { trap; }
}
}
if stop { break; }
position = if position == count - 1usize { 0usize } else { position + 1usize };
}
if map.length == count / 2usize { return InsertResult::<V>::full(move value); }
if chosen == count { trap; }
mut slot = slots[chosen];
let replacement = std.option.Option::<V>::some(move value);
let previous = value.swap(mut slot.value, move replacement);
match move previous {
std.option.Option::<V>::none => {}
std.option.Option::<V>::some(unexpected) => { trap; }
}
slot.key = key;
slot.state = SlotState::occupied;
map.length = map.length + 1usize;
return InsertResult::<V>::inserted;
}
fn contains<V>(map: ref<U64Map<V>>, key: u64) -> bool effects [] {
ref storage = map.storage;
let count = buffer.capacity(storage);
if count == 0usize { return false; }
ref slots = buffer.view(storage);
var wire: [u8; 8] = zero;
for byte in 0usize..8usize { wire[byte] = (key >> ((byte as! u64) * 8u64)) as% u8; }
let hash = std.hash.fnv1a64(ref wire);
var position: usize = (hash % (count as! u64)) as! usize;
for probe in 0usize..count {
{ ref slot = slots[position];
match slot.state {
SlotState::occupied => { if slot.key == key { return true; } }
SlotState::empty => { return false; }
SlotState::deleted => {}
}
}
position = if position == count - 1usize { 0usize } else { position + 1usize };
}
return false;
}
fn get<V: Copy>(map: ref<U64Map<V>>, key: u64) -> std.option.Option<V> effects [] {
ref storage = map.storage;
let count = buffer.capacity(storage);
if count == 0usize { return std.option.Option::<V>::none; }
ref slots = buffer.view(storage);
var wire: [u8; 8] = zero;
for byte in 0usize..8usize { wire[byte] = (key >> ((byte as! u64) * 8u64)) as% u8; }
let hash = std.hash.fnv1a64(ref wire);
var position: usize = (hash % (count as! u64)) as! usize;
for probe in 0usize..count {
{ ref slot = slots[position];
match slot.state {
SlotState::occupied => {
if slot.key == key {
match slot.value {
std.option.Option::<V>::some(value) => { return std.option.Option::<V>::some(value); }
std.option.Option::<V>::none => { trap; }
}
}
}
SlotState::empty => { return std.option.Option::<V>::none; }
SlotState::deleted => {}
}
}
position = if position == count - 1usize { 0usize } else { position + 1usize };
}
return std.option.Option::<V>::none;
}
fn remove<V>(map: mut<U64Map<V>>, key: u64) -> std.option.Option<V> effects [] {
mut storage = map.storage;
let count = buffer.capacity(storage);
if count == 0usize { return std.option.Option::<V>::none; }
mut slots = buffer.view(storage);
var wire: [u8; 8] = zero;
for byte in 0usize..8usize { wire[byte] = (key >> ((byte as! u64) * 8u64)) as% u8; }
let hash = std.hash.fnv1a64(ref wire);
var position: usize = (hash % (count as! u64)) as! usize;
for probe in 0usize..count {
{ mut slot = slots[position];
match slot.state {
SlotState::occupied => {
if slot.key == key {
let empty = std.option.Option::<V>::none;
let previous = value.swap(mut slot.value, move empty);
slot.state = SlotState::deleted;
map.length = map.length - 1usize;
match move previous {
std.option.Option::<V>::some(value) => { return std.option.Option::<V>::some(move value); }
std.option.Option::<V>::none => { trap; }
}
}
}
SlotState::empty => { return std.option.Option::<V>::none; }
SlotState::deleted => {}
}
}
position = if position == count - 1usize { 0usize } else { position + 1usize };
}
return std.option.Option::<V>::none;
}
fn pop<V>(map: mut<U64Map<V>>) -> std.option.Option<Entry<V>> effects [] {
if map.length == 0usize { return std.option.Option::<Entry<V>>::none; }
mut storage = map.storage;
let count = buffer.capacity(storage);
mut slots = buffer.view(storage);
for index in 0usize..count {
mut slot = slots[index];
match slot.state {
SlotState::occupied => {
let key = slot.key;
let empty = std.option.Option::<V>::none;
let previous = value.swap(mut slot.value, move empty);
slot.state = SlotState::deleted;
map.length = map.length - 1usize;
match move previous {
std.option.Option::<V>::some(value) => {
return std.option.Option::<Entry<V>>::some(Entry::<V> { key: key, value: move value });
}
std.option.Option::<V>::none => { trap; }
}
}
SlotState::empty => {}
SlotState::deleted => {}
}
}
trap;
}
fn free<V>(map: U64Map<V>) -> FreeResult<V> effects [] {
if map.length != 0usize { return FreeResult::<V>::not_empty(move map); }
let (storage, length) = struct.unpack(move map);
{ mut slots = storage;
while buffer.len(slots) != 0usize {
let slot = buffer.pop(slots) or trap;
let (key, state, value) = struct.unpack(move slot);
match state {
SlotState::empty => {}
SlotState::deleted => {}
SlotState::occupied => { trap; }
}
match move value {
std.option.Option::<V>::none => {}
std.option.Option::<V>::some(unexpected) => { trap; }
}
}
}
buffer.free(move storage) or trap;
return FreeResult::<V>::ok;
}
fn reserve<V>(map: mut<U64Map<V>>, capacity: usize, heap: cap<mem.alloc>) -> ReserveResult effects [mem.alloc] {
let old_length = map.length;
{ ref storage = map.storage;
if capacity <= buffer.capacity(storage) / 2usize { return ReserveResult::ok; }
}
match std.map.new::<V>(capacity, heap) {
NewResult::<V>::err(error) => { return ReserveResult::err(error); }
NewResult::<V>::ok(next) => {
{ mut old_storage = map.storage;
mut new_storage = next.storage;
let old_count = buffer.capacity(old_storage);
let count = buffer.capacity(new_storage);
mut old_slots = buffer.view(old_storage);
mut new_slots = buffer.view(new_storage);
for index in 0usize..old_count {
var occupied: bool = false;
var key: u64 = 0;
{ ref slot = old_slots[index];
key = slot.key;
match slot.state {
SlotState::occupied => { occupied = true; }
SlotState::empty => {}
SlotState::deleted => {}
}
}
if occupied {
var wire: [u8; 8] = zero;
for byte in 0usize..8usize { wire[byte] = (key >> ((byte as! u64) * 8u64)) as% u8; }
let hash = std.hash.fnv1a64(ref wire);
var position: usize = (hash % (count as! u64)) as! usize;
var found: bool = false;
for probe in 0usize..count {
{ ref slot = new_slots[position];
match slot.state {
SlotState::empty => { found = true; }
SlotState::deleted => { trap; }
SlotState::occupied => {}
}
}
if found { break; }
position = if position == count - 1usize { 0usize } else { position + 1usize };
}
if not found { trap; }
let vacant = Slot::<V> { key: 0, state: SlotState::empty, value: std.option.Option::<V>::none };
let transferred = value.swap(mut old_slots[index], move vacant);
let empty = value.swap(mut new_slots[position], move transferred);
let (unused_key, unused_state, payload) = struct.unpack(move empty);
match move payload {
std.option.Option::<V>::none => {}
std.option.Option::<V>::some(unexpected) => { trap; }
}
}
}
}
{ mut replacement = next; replacement.length = old_length; }
map.length = 0;
let previous = value.swap(mut map, move next);
match std.map.free::<V>(move previous) {
FreeResult::<V>::ok => {}
FreeResult::<V>::not_empty(unexpected) => { trap; }
}
return ReserveResult::ok;
}
}
}