More work on hashmaps

This commit is contained in:
Simon Brooke 2021-08-06 00:24:19 +01:00
parent 6f54b92d32
commit 132f5fb268
No known key found for this signature in database
GPG key ID: A7A4F18D1D4DF987

View file

@ -49,9 +49,10 @@ struct cons_pointer lisp_get_hash(struct stack_frame *frame,
* Make a hashmap with this number of buckets, using this `hash_fn`. If * Make a hashmap with this number of buckets, using this `hash_fn`. If
* `hash_fn` is `NIL`, use the standard hash funtion. * `hash_fn` is `NIL`, use the standard hash funtion.
*/ */
struct cons_pointer make_hashmap( uint32_t n_buckets, struct cons_pointer hash_fn) { struct cons_pointer make_hashmap( uint32_t n_buckets,
struct cons_pointer result = make_vso(HASHTAG, struct cons_pointer hash_fn ) {
(sizeof(struct cons_pointer) * (n_buckets + 1)) + struct cons_pointer result =
make_vso( HASHTAG, ( sizeof( struct cons_pointer ) * ( n_buckets + 1 ) ) +
( sizeof( uint32_t ) * 2 ) ); ( sizeof( uint32_t ) * 2 ) );
struct hashmap_payload *payload = struct hashmap_payload *payload =
@ -66,12 +67,29 @@ struct cons_pointer make_hashmap( uint32_t n_buckets, struct cons_pointer hash_f
return result; return result;
} }
/**
* If this `ptr` is a pointer to a hashmap, return a new identical hashmap;
* else return `NIL`. TODO: should return an exception.
*/
struct cons_pointer clone_hashmap(struct cons_pointer ptr) { struct cons_pointer clone_hashmap(struct cons_pointer ptr) {
struct cons_pointer result = NIL; struct cons_pointer result = NIL;
if (hashmapp(ptr)) { if (hashmapp(ptr)) {
struct vector_space_object *from = pointer_to_vso( ptr );
if ( from != NULL ) {
struct hashmap_payload *from_pl = (struct hashmap_payload*)from->payload;
result = make_hashmap( from_pl->n_buckets, from_pl->hash_fn);
struct vector_space_object *to = pointer_to_vso(result);
struct hashmap_payload *to_pl = (struct hashmap_payload*)to->payload;
for (int i = 0; i < to_pl->n_buckets; i++) {
to_pl->buckets[i] = from_pl->buckets[i];
inc_ref(to_pl->buckets[i]);
}
}
} }
return result; return result;
} }