parrotcode: Hash table | |
Contents | C |
src/hash.c - Hash table
A hashtable contains an array of bucket indexes.
Buckets are nodes in a linked list,
each containing a void *
key and value.
During hash creation,
the types of key and value as well as appropriate compare and hashing functions can be set.
This hash implementation uses just one piece of malloced memory.
The hash->bs
bucket store points to this region.
This hash doesn't move during GC, therefore a lot of the old caveats don't apply.
key_hash_STRING
value
.
See also string.c.STRING_compare
pointer_compare
key_hash_pointer
cstring_compare
key_hash
and compare
functions.key_hash_int
key_hash
function.int_compare
compare
function.parrot_dump_hash
parrot_mark_hash
hash_thaw
pinfo
is the visit info,
(see include/parrot/pmc_freeze.h>).expand_hash
MAXFULL_PERCENT
% of N as the number of buckets.
This way,
as soon as we run out of buckets on the free list,
we know that it's time to resize the hashtable. bucket_index = hash(key) % parrot_hash_size
parrot_hash_size
(the old parrot_hash_size
). In fact, because the hashtable is always a power of two in size, it depends only on the next bit in the hash value, after the ones previously used.->next
pointers, and they'll be all over memory.)parrot_new_hash
hptr
.parrot_new_pmc_hash
parrot_new_cstring_hash
hptr
.parrot_new_hash_x
hptr
.Hash*
is seen, it doesn't get properly marked (only the Hash*
buffer is marked, not its contents). By passing the **hptr
up to the Hash's init function, the newly constructed PMC is on the stack including this newly constructed Hash, so that it gets marked properly.parrot_new_pmc_hash_x
container
PMC gets stored in the Hash end the newly created Hash is in PMC_struct_val(container).parrot_new_pointer_hash
parrot_new_INTVAL_hash
flags
can be PObj_constant_FLAG
or 0.parrot_hash_size
parrot_hash_get_idx
parrot_hash_get_bucket
key
.parrot_hash_get
key
or NULL
if no bucket is found.parrot_hash_exists
parrot_hash_put
key
is not copied.parrot_hash_delete
parrot_hash_clone
hash
to dest
.docs/pdds/pdd08_keys.pod.
Future optimizations:
realloc
.)
|