← alloc
alloc/collections/hashmap.kur
use package as alloc;
use alloc::heap::Uninit!;
use alloc::heap::{AllocError, HeapAllocator, Storage, abort};
use alloc::heap::checked_mul;
use core::cap::Exclusive!;
use core::mem::{Ref, sizeof};
use core::ptr::{null, is_null, offset};
use alloc::owned::Allocation!;
use core::mem::Region!;
use core::mem::RegionResume!;
use core::hash::{Hash, Eq, HashIn, HashState, Seed};
use core::iter::{Iterator, Pair};
use core::option::{Option, Result};
use core::process::trap;
forall K, V.
struct Entry {
key: *K,
val: *V,
next: *Entry[K, V],
}
forall K, V, 'k.
struct HashMap! {
alloc: Allocation![*Entry[K, V], 'k],
back: RegionResume![*Entry[K, V], 'k],
buckets: Ref[*Entry[K, V], 'k],
bucket_count: usize,
len: usize,
seed: Seed,
}
forall K, V.
unsafe fn try_alloc_buckets(n: usize) -> Result[(Uninit!, *(*Entry[K, V])), AllocError] {
let bytes = checked_mul(n, sizeof[*Entry[K, V]]());
match Storage::try_alloc(bytes) {
Err(e) => Result::Err.(e),
Ok(u) => {
let (raw, extent) = u.into_parts();
let base = unsafe (raw as *(*Entry[K, V]));
var i = 0usize;
while i < n {
let s = unsafe offset(base, i);
unsafe *s = null();
i = i + 1usize;
}
let u2 = unsafe HeapAllocator::adopt_raw_sized(raw, extent);
Result::Ok.((u2, base))
},
}
}
forall K, V.
unsafe fn alloc_buckets(n: usize) -> (Uninit!, *(*Entry[K, V])) {
match unsafe try_alloc_buckets[K, V](n) {
Ok(buckets) => buckets,
Err(e) => unsafe abort(),
}
}
forall T where Sized[T].
unsafe fn alloc_one(v: T) -> *T {
let raw = HeapAllocator::alloc(sizeof[T]()).into_raw();
let p = unsafe (raw as *T);
unsafe *p = v;
p
}
forall T.
unsafe fn free_one(p: *T) -> () {
let raw = unsafe (p as *u8);
HeapAllocator::drop(unsafe HeapAllocator::adopt_raw(raw));
}
forall K, V.
unsafe fn alloc_entry(key: *K, val: *V, next: *Entry[K, V]) -> *Entry[K, V] {
let u = HeapAllocator::alloc(sizeof[Entry[K, V]]());
let raw = u.into_raw();
let p = unsafe (raw as *Entry[K, V]);
let e = Entry.{ key, val, next };
unsafe *p = e;
p
}
forall K, V where Sized[K], Sized[V].
unsafe fn destroy_chain(head: *Entry[K, V], f: fn(K, V) -> ()) -> () {
var cur = head;
while !is_null(cur) {
let { key: kp, val: vp, next: nx } = unsafe *cur;
f(unsafe *kp, unsafe *vp);
unsafe free_one[K](kp);
unsafe free_one[V](vp);
HeapAllocator::drop(unsafe HeapAllocator::adopt_raw(unsafe (cur as *u8)));
cur = nx;
}
}
forall K, V where Sized[K], Sized[V].
impl HashMap![K, V] {
fn try_new(seed: Seed) -> Result[exists '1. (Exclusive!['1], HashMap![K, V, '1]), AllocError] {
let initial_buckets = 8usize;
match unsafe try_alloc_buckets[K, V](initial_buckets) {
Err(e) => Result::Err.(e),
Ok(buckets) => {
let (u, base) = buckets;
let exists 'open1. (a, rg) = unsafe Allocation!::adopt[*Entry[K, V]](u);
let (tag, r, bk) = rg.share();
Result::Ok.(pack 'open1. (tag, HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: initial_buckets, len: 0usize, seed }))
},
}
}
fn new(seed: Seed) -> exists '1. (Exclusive!['1], HashMap![K, V, '1]) {
match HashMap!::try_new[K, V](seed) {
Ok(map) => map,
Err(e) => unsafe abort(),
}
}
}
forall K, V, 'k where Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
fn len(&self) -> usize {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
self = HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed };
n
}
fn is_empty(&self) -> bool {
&self.len() == 0usize
}
fn destroy_with(self, tag: Exclusive!['k], f: fn(K, V) -> ()) -> () {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
var i = 0usize;
while i < bc {
let s = unsafe offset(base, i);
let head = unsafe *s;
let u = unsafe destroy_chain[K, V](head, f);
i = i + 1usize;
}
let rg = bk.resume(tag);
let u0 = a.release(rg);
HeapAllocator::drop(u0)
}
}
forall K where Hash[K].
fn hash_key(seed: Seed, &k: K) -> u64 {
let (back, state) = k.hash(HashState::new(seed));
k = back;
state.finish()
}
forall K where Eq[K, K], Sized[K].
unsafe fn keys_equal(a: *K, &b: K) -> bool {
let x = unsafe *a;
let (x, y, same) = Eq[K, K]::eq(x, b);
unsafe *a = x;
b = y;
same
}
forall K, V where Eq[K, K], Sized[K].
unsafe fn find_chain(head: *Entry[K, V], &key: K) -> Option[*V] {
var cur = head;
var found: Option[*V] = Option::None;
while unsafe (cur as usize) != 0usize {
let e = unsafe *cur;
let { key: k, val: vp, next: nx } = e;
cur = if unsafe keys_equal[K](k, &key) {
found = Option::Some.(vp);
null()
} else {
nx
};
}
found
}
forall K, V where Eq[K, K], Sized[K], Sized[V].
unsafe fn put_chain(head: *Entry[K, V], key_ptr: *K, val: V) -> (*Entry[K, V], Option[(K, V)], bool) {
var last = null();
var cur = head;
while !is_null(cur) {
let { key: k, val: vp, next: nx } = unsafe *cur;
if unsafe keys_equal[K](k, unsafe &*key_ptr) {
let stored = unsafe *k;
unsafe free_one[K](k);
unsafe *cur = Entry.{ key: key_ptr, val: vp, next: nx };
let old = unsafe *vp;
unsafe *vp = val;
return (head, Option::Some.((stored, old)), false);
}
last = cur;
cur = nx;
}
let added = unsafe alloc_entry[K, V](key_ptr, unsafe alloc_one[V](val), cur);
(unsafe link_after[K, V](head, last, added), Option::None, true)
}
forall K, V where Eq[K, K], Sized[K], Sized[V].
unsafe fn take_chain(head: *Entry[K, V], &key: K) -> (*Entry[K, V], Option[(K, V)], bool) {
var last = null();
var cur = head;
while !is_null(cur) {
let { key: k, val: vp, next: nx } = unsafe *cur;
if unsafe keys_equal[K](k, &key) {
let stored = unsafe *k;
let old = unsafe *vp;
unsafe free_one[K](k);
unsafe free_one[V](vp);
HeapAllocator::drop(unsafe HeapAllocator::adopt_raw(unsafe (cur as *u8)));
return (unsafe link_after[K, V](head, last, nx), Option::Some.((stored, old)), true);
}
last = cur;
cur = nx;
}
(head, Option::None, false)
}
forall K, V where Hash[K], Sized[K].
unsafe fn rehash_bucket_hashed(seed: Seed, head: *Entry[K, V], new_base: *(*Entry[K, V]), new_bc: usize) -> () {
var cur = head;
while unsafe (cur as usize) != 0usize {
let e = unsafe *cur;
let { key: k, val: vp, next: nx } = e;
let idx = (hash_key[K](seed, unsafe &*k) as usize) % new_bc;
let slot = unsafe offset(new_base, idx);
let old_head = unsafe *slot;
unsafe *cur = Entry.{ key: k, val: vp, next: old_head };
unsafe *slot = cur;
cur = nx;
}
}
forall K, V where Hash[K], Sized[K].
unsafe fn resize_hashed(seed: Seed, u_old: Uninit!, base: *(*Entry[K, V]), old_bc: usize) -> (Uninit!, usize) {
let new_bc = checked_mul(old_bc, 2usize);
let (u_new, new_base) = unsafe alloc_buckets[K, V](new_bc);
var i = 0usize;
while i < old_bc {
let head = unsafe *(unsafe offset(base, i));
let u = unsafe rehash_bucket_hashed[K, V](seed, head, new_base, new_bc);
i = i + 1usize;
}
let u2 = HeapAllocator::drop(u_old);
(u_new, new_bc)
}
forall K, C where Copy[K], HashIn[K, C].
fn hash_key_in(&cx: C, seed: Seed, key: K) -> u64 {
HashIn[K, C]::hash_in(&cx, key, HashState::new(seed)).finish()
}
forall K, V, C where Copy[K], HashIn[K, C], Sized[K].
unsafe fn find_chain_in(&cx: C, head: *Entry[K, V], key: K) -> Option[*V] {
var cur = head;
var found: Option[*V] = Option::None;
while unsafe (cur as usize) != 0usize {
let e = unsafe *cur;
let { key: k, val: vp, next: nx } = e;
cur = if HashIn[K, C]::eq_in(&cx, unsafe *k, key) {
found = Option::Some.(vp);
null()
} else {
nx
};
}
found
}
forall K, V, C where Copy[K], HashIn[K, C], Sized[K], Sized[V].
unsafe fn put_chain_in(&cx: C, head: *Entry[K, V], key: K, val: V) -> (*Entry[K, V], Option[V], bool) {
var last = null();
var cur = head;
while !is_null(cur) {
let { key: k, val: vp, next: nx } = unsafe *cur;
if HashIn[K, C]::eq_in(&cx, unsafe *k, key) {
unsafe *k = key;
let old = unsafe *vp;
unsafe *vp = val;
return (head, Option::Some.(old), false);
}
last = cur;
cur = nx;
}
let added = unsafe alloc_entry[K, V](unsafe alloc_one[K](key), unsafe alloc_one[V](val), cur);
(unsafe link_after[K, V](head, last, added), Option::None, true)
}
forall K, V, C where Copy[K], HashIn[K, C], Sized[K], Sized[V].
unsafe fn take_chain_in(&cx: C, head: *Entry[K, V], key: K) -> (*Entry[K, V], Option[V], bool) {
var last = null();
var cur = head;
while !is_null(cur) {
let { key: k, val: vp, next: nx } = unsafe *cur;
if HashIn[K, C]::eq_in(&cx, unsafe *k, key) {
let old = unsafe *vp;
unsafe free_one[K](k);
unsafe free_one[V](vp);
HeapAllocator::drop(unsafe HeapAllocator::adopt_raw(unsafe (cur as *u8)));
return (unsafe link_after[K, V](head, last, nx), Option::Some.(old), true);
}
last = cur;
cur = nx;
}
(head, Option::None, false)
}
forall K, V.
unsafe fn link_after(head: *Entry[K, V], last: *Entry[K, V], rest: *Entry[K, V]) -> *Entry[K, V] {
if is_null(last) {
return rest;
}
let { key, val, next } = unsafe *last;
unsafe *last = Entry.{ key, val, next: rest };
head
}
forall K, V, C where Copy[K], HashIn[K, C], Sized[K].
unsafe fn rehash_bucket_in(&cx: C, seed: Seed, head: *Entry[K, V], new_base: *(*Entry[K, V]), new_bc: usize) -> () {
var cur = head;
while unsafe (cur as usize) != 0usize {
let e = unsafe *cur;
let { key: k, val: vp, next: nx } = e;
let h = hash_key_in[K, C](&cx, seed, unsafe *k);
let idx = (h as usize) % new_bc;
let slot = unsafe offset(new_base, idx);
let old_head = unsafe *slot;
unsafe *cur = Entry.{ key: k, val: vp, next: old_head };
unsafe *slot = cur;
cur = nx;
}
}
forall K, V, C where Copy[K], HashIn[K, C], Sized[K].
unsafe fn resize_in(&cx: C, seed: Seed, u_old: Uninit!, base: *(*Entry[K, V]), old_bc: usize) -> (Uninit!, usize) {
let new_bc = checked_mul(old_bc, 2usize);
let (u_new, new_base) = unsafe alloc_buckets[K, V](new_bc);
var i = 0usize;
while i < old_bc {
let head = unsafe *(unsafe offset(base, i));
unsafe rehash_bucket_in[K, V, C](&cx, seed, head, new_base, new_bc);
i = i + 1usize;
}
let u2 = HeapAllocator::drop(u_old);
(u_new, new_bc)
}
forall K, V, 'k where Copy[K], Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
forall C where HashIn[K, C].
fn get_in(&self, &tag: Exclusive!['k], &cx: C, key: K) -> Option[Ref[V, 'k]] {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
let h = hash_key_in[K, C](&cx, seed, key);
let head = unsafe *(unsafe offset(base, (h as usize) % bc));
let found_vp = unsafe find_chain_in[K, V, C](&cx, head, key);
let found: Option[Ref[V, 'k]] = match found_vp {
Some(vp) => Option::Some.(unsafe Ref::from_raw(vp, m)),
None => Option::None,
};
self = HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed };
found
}
forall C where HashIn[K, C].
fn contains_in(&self, &tag: Exclusive!['k], &cx: C, key: K) -> bool {
(&self.get_in(&tag, &cx, key)).is_some()
}
forall C where HashIn[K, C].
fn insert_in(self, tag: Exclusive!['k], &cx: C, key: K, val: V) -> exists '2. (Exclusive!['2], HashMap![K, V, '2], Option[V]) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
let h = hash_key_in[K, C](&cx, seed, key);
let s = unsafe offset(base, (h as usize) % bc);
let head = unsafe *s;
let (new_head, found, is_new) = unsafe put_chain_in[K, V, C](&cx, head, key, val);
unsafe *s = new_head;
let new_len = if is_new { n + 1usize } else { n };
let should_resize = is_new && new_len > bc * 3usize / 4usize;
let rg = bk.resume(tag);
let u0 = a.release(rg);
let (u1, new_bc) = if should_resize {
unsafe resize_in[K, V, C](&cx, seed, u0, base, bc)
} else {
(u0, bc)
};
let exists 'open1. (a2, rg2) = unsafe Allocation!::adopt[*Entry[K, V]](u1);
let (tag2, r2, bk2) = rg2.share();
pack 'open1. (tag2, HashMap!.{ alloc: a2, back: bk2, buckets: r2, bucket_count: new_bc, len: new_len, seed }, found)
}
forall C where HashIn[K, C].
fn remove_in(self, tag: Exclusive!['k], &cx: C, key: K) -> exists '2. (Exclusive!['2], HashMap![K, V, '2], Option[V]) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
let h = hash_key_in[K, C](&cx, seed, key);
let s = unsafe offset(base, (h as usize) % bc);
let head = unsafe *s;
let (new_head, found, removed) = unsafe take_chain_in[K, V, C](&cx, head, key);
unsafe *s = new_head;
let new_len = if removed { n - 1usize } else { n };
let rg = bk.resume(tag);
let u0 = a.release(rg);
let exists 'open1. (a2, rg2) = unsafe Allocation!::adopt[*Entry[K, V]](u0);
let (tag2, r2, bk2) = rg2.share();
pack 'open1. (tag2, HashMap!.{ alloc: a2, back: bk2, buckets: r2, bucket_count: bc, len: new_len, seed }, found)
}
}
forall K, V, 'k where Hash[K], Eq[K, K], Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
fn contains(&self, &tag: Exclusive!['k], &key: K) -> bool {
(&self.get(&tag, &key)).is_some()
}
fn get(&self, &tag: Exclusive!['k], &key: K) -> Option[Ref[V, 'k]] {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
let idx = (hash_key[K](seed, &key) as usize) % bc;
let head = unsafe *(unsafe offset(base, idx));
let found_vp = unsafe find_chain[K, V](head, &key);
let found: Option[Ref[V, 'k]] = match found_vp {
Some(vp) => Option::Some.(unsafe Ref::from_raw(vp, m)),
None => Option::None,
};
self = HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed };
found
}
fn insert(self, tag: Exclusive!['k], key: K, val: V) -> exists '2. (Exclusive!['2], HashMap![K, V, '2], Option[(K, V)]) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
var key = key;
let idx = (hash_key[K](seed, &key) as usize) % bc;
let key_ptr = unsafe alloc_one[K](key);
let s = unsafe offset(base, idx);
let head = unsafe *s;
let (new_head, found, is_new) = unsafe put_chain[K, V](head, key_ptr, val);
unsafe *s = new_head;
let new_len = if is_new { n + 1usize } else { n };
let should_resize = is_new && new_len > bc * 3usize / 4usize;
let rg = bk.resume(tag);
let u0 = a.release(rg);
let (u1, new_bc) = if should_resize {
unsafe resize_hashed[K, V](seed, u0, base, bc)
} else {
(u0, bc)
};
let exists 'open1. (a2, rg2) = unsafe Allocation!::adopt[*Entry[K, V]](u1);
let (tag2, r2, bk2) = rg2.share();
pack 'open1. (tag2, HashMap!.{ alloc: a2, back: bk2, buckets: r2, bucket_count: new_bc, len: new_len, seed }, found)
}
fn remove(self, tag: Exclusive!['k], &key: K) -> exists '2. (Exclusive!['2], HashMap![K, V, '2], Option[(K, V)]) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
let idx = (hash_key[K](seed, &key) as usize) % bc;
let s = unsafe offset(base, idx);
let head = unsafe *s;
let (new_head, found, removed) = unsafe take_chain[K, V](head, &key);
unsafe *s = new_head;
let new_len = if removed { n - 1usize } else { n };
let rg = bk.resume(tag);
let u0 = a.release(rg);
let exists 'open1. (a2, rg2) = unsafe Allocation!::adopt[*Entry[K, V]](u0);
let (tag2, r2, bk2) = rg2.share();
pack 'open1. (tag2, HashMap!.{ alloc: a2, back: bk2, buckets: r2, bucket_count: bc, len: new_len, seed }, found)
}
}
forall K, Q where Eq[K, Q], Sized[K].
unsafe fn key_matches(k: *K, &q: Q) -> bool {
let x = unsafe *k;
let (x, y, same) = Eq[K, Q]::eq(x, q);
unsafe *k = x;
q = y;
same
}
forall K, V, Q where Eq[K, Q], Sized[K].
unsafe fn find_chain_by(head: *Entry[K, V], &probe: Q) -> Option[*V] {
var cur = head;
var found: Option[*V] = Option::None;
while unsafe (cur as usize) != 0usize {
let e = unsafe *cur;
let { key: k, val: vp, next: nx } = e;
cur = if unsafe key_matches[K, Q](k, &probe) {
found = Option::Some.(vp);
null()
} else {
nx
};
}
found
}
forall K, V, 'k where Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
forall Q where Hash[Q], Eq[K, Q].
fn find(&self, &tag: Exclusive!['k], &probe: Q) -> Option[Ref[V, 'k]] {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
let idx = (hash_key[Q](seed, &probe) as usize) % bc;
let head = unsafe *(unsafe offset(base, idx));
let found_vp = unsafe find_chain_by[K, V, Q](head, &probe);
let found: Option[Ref[V, 'k]] = match found_vp {
Some(vp) => Option::Some.(unsafe Ref::from_raw(vp, m)),
None => Option::None,
};
self = HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed };
found
}
}
forall K, V, 'k where Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
forall Env where Copy[Env].
fn each(&self, &tag: Exclusive!['k], f: closure[Env](&_: K, &_: V) -> ()) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
var i = 0usize;
while i < bc {
var cur = unsafe *(unsafe offset(base, i));
while !is_null(cur) {
let { key: kp, val: vp, next: nx } = unsafe *cur;
f(unsafe &*kp, unsafe &*vp);
cur = nx;
}
i = i + 1usize;
}
self = HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed };
}
forall C, Env where Copy[Env].
fn each_in(&self, &tag: Exclusive!['k], &cx: C, f: closure[Env](&_: K, &_: V, &_: C) -> ()) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
var i = 0usize;
while i < bc {
var cur = unsafe *(unsafe offset(base, i));
while !is_null(cur) {
let { key: kp, val: vp, next: nx } = unsafe *cur;
f(unsafe &*kp, unsafe &*vp, &cx);
cur = nx;
}
i = i + 1usize;
}
self = HashMap!.{ alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed };
}
fn clear_with(self, tag: Exclusive!['k], f: fn(K, V) -> ()) -> exists '2. (Exclusive!['2], HashMap![K, V, '2]) {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
var i = 0usize;
while i < bc {
let s = unsafe offset(base, i);
unsafe destroy_chain[K, V](unsafe *s, f);
unsafe *s = null();
i = i + 1usize;
}
let rg = bk.resume(tag);
let u0 = a.release(rg);
let exists 'open1. (a2, rg2) = unsafe Allocation!::adopt[*Entry[K, V]](u0);
let (tag2, r2, bk2) = rg2.share();
pack 'open1. (tag2, HashMap!.{ alloc: a2, back: bk2, buckets: r2, bucket_count: bc, len: 0usize, seed })
}
}
forall K, V where Copy[K], Copy[V].
fn forget_entry(key: K, value: V) -> () {
()
}
forall K, V, 'k where Copy[K], Copy[V], Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
fn destroy(self, tag: Exclusive!['k]) -> () {
self.destroy_with(tag, forget_entry[K, V])
}
}
forall K, V, 'k.
struct Drain! {
alloc: Allocation![*Entry[K, V], 'k],
back: RegionResume![*Entry[K, V], 'k],
tag: Exclusive!['k],
base: *(*Entry[K, V]),
bucket_count: usize,
next_bucket: usize,
cur: *Entry[K, V],
}
forall K, V, 'k where Sized[K], Sized[V].
impl HashMap![K, V, 'k] {
fn drain(self, tag: Exclusive!['k]) -> Drain![K, V, 'k] {
let { alloc: a, back: bk, buckets: r, bucket_count: bc, len: n, seed } = self;
let (base, m) = r.into_parts();
Drain!.{ alloc: a, back: bk, tag, base, bucket_count: bc, next_bucket: 0usize, cur: null() }
}
}
forall K, V, 'k where Sized[K], Sized[V].
impl Drain![K, V, 'k] {
fn take(&self) -> Option[(K, V)] {
var { alloc, back, tag, base, bucket_count, next_bucket, cur } = self;
while is_null(cur) && next_bucket < bucket_count {
cur = unsafe *(unsafe offset(base, next_bucket));
unsafe *(unsafe offset(base, next_bucket)) = null();
next_bucket = next_bucket + 1usize;
}
if is_null(cur) {
self = Drain!.{ alloc, back, tag, base, bucket_count, next_bucket, cur };
return Option::None;
}
let e = unsafe *cur;
let { key: kp, val: vp, next: nx } = e;
let k = unsafe *kp;
let v = unsafe *vp;
let u1 = unsafe free_one[K](kp);
let u2 = unsafe free_one[V](vp);
let u3 = HeapAllocator::drop(unsafe HeapAllocator::adopt_raw(unsafe (cur as *u8)));
self = Drain!.{ alloc, back, tag, base, bucket_count, next_bucket, cur: nx };
Option::Some.((k, v))
}
fn finish(self, f: fn(K, V) -> ()) -> () {
let { alloc, back, tag, base, bucket_count, next_bucket, cur } = self;
let u = unsafe destroy_chain[K, V](cur, f);
var i = next_bucket;
while i < bucket_count {
let head = unsafe *(unsafe offset(base, i));
let u = unsafe destroy_chain[K, V](head, f);
i = i + 1usize;
}
let rg = back.resume(tag);
HeapAllocator::drop(alloc.release(rg))
}
}
forall K, V, 'k where Sized[K], Sized[V].
instance Iterator[Drain![K, V, 'k] -> Pair[K, V]] {
fn next(&it: Drain![K, V, 'k]) -> Option[Pair[K, V]] {
let taken = &it.take();
match taken {
Some(entry) => {
let (k, v) = entry;
Option::Some.(Pair.{ a: k, b: v })
},
None => Option::None,
}
}
}
forall K, V.
exposed struct OwnedMap! {
inner: exists 'm. (Exclusive!['m], HashMap![K, V, 'm]),
}
forall K, V where Sized[K], Sized[V].
impl OwnedMap![K, V] {
fn new(seed: Seed) -> OwnedMap![K, V] {
OwnedMap!.{ inner: HashMap!::new[K, V](seed) }
}
fn try_new(seed: Seed) -> Result[OwnedMap![K, V], AllocError] {
match HashMap!::try_new[K, V](seed) {
Ok(inner) => Result::Ok.(OwnedMap!.{ inner }),
Err(e) => Result::Err.(e),
}
}
fn len(&self) -> usize {
var exists 'm. (tag, map) = self.inner;
let n = &map.len();
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
n
}
fn is_empty(&self) -> bool {
&self.len() == 0usize
}
fn drain(self) -> exists 'm. Drain![K, V, 'm] {
let exists 'm. (tag, map) = self.inner;
pack 'm. map.drain(tag)
}
fn destroy_with(self, f: fn(K, V) -> ()) -> () {
let exists 'm. (tag, map) = self.inner;
map.destroy_with(tag, f)
}
forall Env where Copy[Env].
fn each(&self, f: closure[Env](&_: K, &_: V) -> ()) {
var exists 'm. (tag, map) = self.inner;
&map.each(&tag, f);
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
}
forall C, Env where Copy[Env].
fn each_in(&self, &cx: C, f: closure[Env](&_: K, &_: V, &_: C) -> ()) {
var exists 'm. (tag, map) = self.inner;
&map.each_in(&tag, &cx, f);
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
}
fn clear_with(&self, f: fn(K, V) -> ()) {
let exists 'm. (tag, map) = self.inner;
let exists '2. (tag, map) = map.clear_with(tag, f);
self = OwnedMap!.{ inner: pack '2. (tag, map) };
}
}
forall K, V where Hash[K], Eq[K, K], Sized[K], Sized[V].
impl OwnedMap![K, V] {
fn insert(&self, key: K, val: V) -> Option[(K, V)] {
let exists 'm. (tag, map) = self.inner;
let exists '2. (tag, map, old) = map.insert(tag, key, val);
self = OwnedMap!.{ inner: pack '2. (tag, map) };
old
}
fn remove(&self, &key: K) -> Option[(K, V)] {
let exists 'm. (tag, map) = self.inner;
let exists '2. (tag, map, old) = map.remove(tag, &key);
self = OwnedMap!.{ inner: pack '2. (tag, map) };
old
}
fn contains(&self, &key: K) -> bool {
var exists 'm. (tag, map) = self.inner;
let found = &map.contains(&tag, &key);
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
found
}
forall Ret, Env where Copy[Env].
fn with(&self, &key: K, f: closure[Env](V) -> (V, Ret)) -> Option[Ret] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.get(&tag, &key);
let (tag, result) = match slot {
Some(r) => {
let ret = r.with(&tag, f);
(tag, Option::Some.(ret))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
result
}
forall Ret, Env where Copy[Env].
fn update(&self, &key: K, f: closure[Env](&_: V) -> Ret) -> Option[Ret] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.get(&tag, &key);
let (tag, result) = match slot {
Some(r) => {
let ret = r.update(&tag, f);
(tag, Option::Some.(ret))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
result
}
forall C, Ret, Env where Copy[Env].
fn update_in(&self, &key: K, &cx: C, f: closure[Env](&_: V, &_: C) -> Ret) -> Option[Ret] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.get(&tag, &key);
let (tag, result) = match slot {
Some(r) => {
let ret = r.update_in(&tag, &cx, f);
(tag, Option::Some.(ret))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
result
}
}
forall K, V where Sized[K], Sized[V].
impl OwnedMap![K, V] {
forall Q, Ret, Env where Hash[Q], Eq[K, Q], Copy[Env].
fn find_with(&self, &probe: Q, f: closure[Env](V) -> (V, Ret)) -> Option[Ret] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.find(&tag, &probe);
let (tag, result) = match slot {
Some(r) => {
let ret = r.with(&tag, f);
(tag, Option::Some.(ret))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
result
}
forall Q, Ret, Env where Hash[Q], Eq[K, Q], Copy[Env].
fn find_update(&self, &probe: Q, f: closure[Env](&_: V) -> Ret) -> Option[Ret] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.find(&tag, &probe);
let (tag, result) = match slot {
Some(r) => {
let ret = r.update(&tag, f);
(tag, Option::Some.(ret))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
result
}
}
forall K, V where Copy[V], Sized[K], Sized[V].
impl OwnedMap![K, V] {
forall Q where Hash[Q], Eq[K, Q].
fn find(&self, &probe: Q) -> Option[V] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.find(&tag, &probe);
let (tag, value) = match slot {
Some(r) => {
let value = r.copy(&tag);
(tag, Option::Some.(value))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
value
}
}
forall K, V where Hash[K], Eq[K, K], Copy[V], Sized[K], Sized[V].
impl OwnedMap![K, V] {
fn get(&self, &key: K) -> Option[V] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.get(&tag, &key);
let (tag, value) = match slot {
Some(r) => {
let value = r.copy(&tag);
(tag, Option::Some.(value))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
value
}
fn at(&self, &key: K) -> V {
match &self.get(&key) {
Some(v) => v,
None => trap(),
}
}
}
forall K, V where Copy[K], Sized[K], Sized[V].
impl OwnedMap![K, V] {
forall C where HashIn[K, C].
fn insert_in(&self, &cx: C, key: K, val: V) -> Option[V] {
let exists 'm. (tag, map) = self.inner;
let exists '2. (tag, map, old) = map.insert_in(tag, &cx, key, val);
self = OwnedMap!.{ inner: pack '2. (tag, map) };
old
}
forall C where HashIn[K, C].
fn remove_in(&self, &cx: C, key: K) -> Option[V] {
let exists 'm. (tag, map) = self.inner;
let exists '2. (tag, map, old) = map.remove_in(tag, &cx, key);
self = OwnedMap!.{ inner: pack '2. (tag, map) };
old
}
forall C where HashIn[K, C].
fn contains_in(&self, &cx: C, key: K) -> bool {
var exists 'm. (tag, map) = self.inner;
let found = &map.contains_in(&tag, &cx, key);
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
found
}
forall C, Ret, Env where HashIn[K, C], Copy[Env].
fn with_in(&self, &cx: C, key: K, f: closure[Env](V) -> (V, Ret)) -> Option[Ret] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.get_in(&tag, &cx, key);
let (tag, result) = match slot {
Some(r) => {
let ret = r.with(&tag, f);
(tag, Option::Some.(ret))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
result
}
}
forall K, V where Copy[K], Copy[V], Sized[K], Sized[V].
impl OwnedMap![K, V] {
forall C where HashIn[K, C].
fn get_in(&self, &cx: C, key: K) -> Option[V] {
var exists 'm. (tag, map) = self.inner;
let slot = &map.get_in(&tag, &cx, key);
let (tag, value) = match slot {
Some(r) => {
let value = r.copy(&tag);
(tag, Option::Some.(value))
},
None => (tag, Option::None),
};
self = OwnedMap!.{ inner: pack 'm. (tag, map) };
value
}
fn clear(&self) {
&self.clear_with(forget_entry[K, V]);
}
fn destroy(self) -> () {
let exists 'm. (tag, map) = self.inner;
map.destroy(tag)
}
}