pforth  0943e5e71114f286dbb2474d55aabf91df02d104
dict.c File Reference
#include <stdlib.h>
#include <stdio.h>
#include <limits.h>
#include <string.h>
#include "pforth.h"

Functions

dict_tdict_create (uint32_t size)
 
void dict_free (dict_t *dict, uint32_t size)
 
int hash (dict_t *hashtable, const char *key)
 
dict_entrydict_newkv (const char *key, const pforth_word_ptr value)
 
pforth_word_ptr dict_set (dict_t *dict, const char *key, const pforth_word_ptr value)
 
pforth_word_ptr dict_get (dict_t *dict, const char *key)
 

Function Documentation

dict_t* dict_create ( uint32_t  size)

Create a new hash table dictionary.

Parameters
sizenumber of elements
Returns
pointer to created dictionary
void dict_free ( dict_t dict,
uint32_t  size 
)
pforth_word_ptr dict_get ( dict_t dict,
const char *  key 
)

Retrieve the word from the dictionary.

Parameters
dictdictionary to use
keyFORTH word in ASCII form
Returns
pointer to word inside the dictionary. Do not harm it.
dict_entry* dict_newkv ( const char *  key,
const pforth_word_ptr  value 
)

Create a key-value pair for dictionary.

Internal function to be used only inside dictionary.

Parameters
keythe FORTH word in ASCII
valuepointer to the actual pforth_word structure
Returns
newly allocated key-value pair
pforth_word_ptr dict_set ( dict_t dict,
const char *  key,
const pforth_word_ptr  value 
)

Insert word into dictionary.

The word will be copied into the dictionary. The value must be freed in caller afterwards.

Parameters
dictdictionary to use
keyFORTH word in ASCII form
valuepointer to pforth_word struct with the word.
Returns
pointer to the newly created word
int hash ( dict_t hashtable,
const char *  key 
)

djb2 hash function

Hope it'll be more or less unique to create hash table for FORTH words

Parameters
hashtablepointer to hash table
keynull-terminated key string
Returns
index in hash table