GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
hashmap(3m) MBA Library Functions hashmap(3m)

hashmap - a rehashing hashmap

#include <mba/hashmap.h>



int hashmap_init(struct hashmap *h,
            unsigned int load_factor,
            hash_fn hash,
            cmp_fn cmp,
            void *context,
            struct allocator *al);

int hashmap_deinit(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
struct hashmap *hashmap_new(hash_fn hash, cmp_fn cmp, void *context, struct allocator *al);
int hashmap_del(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
int hashmap_clear(struct hashmap *h, del_fn key_del, del_fn data_del, void *context);
int hashmap_clean(struct hashmap *h); int hashmap_put(struct hashmap *h, void *key, void *data);
void *hashmap_get(const struct hashmap *h, const void *key);
int hashmap_is_empty(struct hashmap *h);
unsigned int hashmap_size(struct hashmap *h);
void hashmap_iterate(void *h, iter_t *iter);
void *hashmap_next(void *h, iter_t *iter);
int hashmap_remove(struct hashmap *h, void **key, void **data);

A hashmap(3m) object associates keys with data pointers. Large numbers of elements may be stored and retrieved efficiently.

Memory management of keys and data pointers is the resposibility of the user although del_fn function pointers (defined in allocator(3m)) may be specified with some hashmap functions to assist the user with this task.

init
The hashmap_init function initializes the memory at h as a hashmap with no elements. The load_factor parameter must be an integer between 1 and 100 indicating when the map should be resized. If a value of 0 is specified, the default value of 75 will be used meaning the map will be increased when 75 of every 100 spaces for elements are occupied.

If the hash parameter is not NULL it will be used to generate a hash value given a key and the specified context object. Given a set of keys the hash function should generate an even distribution of values. If the hash parameter is NULL a key's memory address will be used as it's hash value.

If the cmp parameter is not NULL it will used to compare two keys for equality. This function should return 0 if two keys are equal and non-zero if they are not. If the cmp parameter is NULL the memory addresses of the two keys will be compared.

The al parameter is an allocator(3m) from which all memory associated with this hashmap should be allocated. As with the allocator functions, a NULL allocator indicates the stdlib_allocator should be used.

The following example illustrates how to initialize a hashmap and use it to store the object data associated with the character string "name".

struct hashmap hm;
struct foo data, *out;
hashmap_init(&hm,
	0,                             /* default load factor of 75 */
	hash_text,                    /* default text hash function */
	cmp_text,                  /* default text compare function */
	NULL, /* hash_fn and cmp_fn function do not require context */
	NULL);                          /* use the stdlib_allocator */
hashmap_put(&hm, "name", &data);
out = hashmap_get(&hm, "name");
/* out now points to data */
deinit
The hashmap_deinit function deinitializes the hashmap h. If the key_del or data_del functions are not NULL they will be called with the context object and each key and/or data object in the map. Any memory associated with the hashmap will be released.
new
The hashmap_new function allocates memory for a new hashmap object and initializes it with hashmap_init
del
The hashmap_del function deinitializes the hashmap h with the hashmap_deinit function and then releases the h object itself.
clear
The hashmap_clear function clears all elements from the hashmap h. If the key_del or data_del functions are not NULL they will be called with the context object and each key and/or data object in the map.
clean
The hashmap_clean function will release excess memory allocated by the hashmap h. See the allocator_set_reclaim function.
put
Put a data pointer into the map with the key key. If an element with the same key already exists in the map, -1 will be returned and errno will be set to EEXIST. If another error occurs, -1 will be returned and errno will be set to an appropriate value.
get
Retrieve a data pointer from the map with the key key.
is_empty
Returns 1 if the map is empty and 0 otherwise.
size
Returns the number of data pointers in the map.
iterate, next
Enumerate each key in the map. The hashmap_iterate function initializes the iter object to point to the beginning of the map. With each call to hashmap_next, a key will be returned. When all keys have been enumerated, hashmap_next will return NULL. Keys are not returned in any particular order.

Modifying the map during the enumeration is permitted however should adding or removing data cause the table to be resized, not all keys may be enumerated and some keys may be returned more than once. Therefore, to make multiple modifications during the enumeration it may be desirable to first create a snapshot of the keys in an array or list.

remove
The hashmap_remove function removes the element associated with key from the hashmap h and stores pointers to the original key and data in the provided key and data parameters.

The following is an example of removing an element from a hashmap.

char *key = name;
struct foo *data;
hashmap_remove(hm, (void **)&key, (void **)&data);
/* free data if necessary */

init
The hashmap_init function returns 0 on success or -1 for failure in which case errno will be set to an appropriate value.
deinit
The hashmap_deinit function returns 0 on success or -1 for failure in which case errno will be set to an appropriate value.
new
The hashmap_new function returns a new struct hashmap * object that contains no elements or NULL if the operation failed in which case errno will be set to an appropriate value.
del
The hashmap_del function returns 0 on success or -1 for failure in which case errno will be set to an appropriate value.
clear
The hashmap_clear function returns 0 on success or -1 for failure in which case errno will be set to an appropriate value.
clean
The hashmap_clean function returns the number of unused elements released (possibly 0) or -1 if an error occured in which case errno will be set to an appropriate value.
get
The hashmap_get function returns the data pointer being retrieved or NULL if the element was not found. NULL will also be returned if the h or key parameters are NULL but this function does not set errno to any value.
size
The hashmap_size function returns the number of data pointers in the map. If h is NULL, zero is returned.
next
The hashmap_next function returns the next key in the map or NULL if all keys have been enumerated.
remove
The hashmap_remove function returns 0 on success or -1 for failure in which case errno will be set to an appropriate value.
April 29, 2005 libmba-0.9.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.