.\" .\" Copyright 2004 Michael B. Allen .\" .TH hashmap 3m "Apr 29, 2005" "libmba-0.9.1" "MBA Library Functions" .SH NAME hashmap \- a rehashing hashmap .SH SYNOPSIS .nf .B #include .sp .sp .sp .BI "int hashmap_init(struct hashmap *" h ", .BI " unsigned int " load_factor ", .BI " hash_fn " hash ", .BI " cmp_fn " cmp ", .BI " void *" context ", .BI " struct allocator *" al "); .br .BI "int hashmap_deinit(struct hashmap *" h ", .BI " del_fn " key_del ", .BI " del_fn " data_del ", .BI " void *" context "); .br .BI "struct hashmap *hashmap_new(hash_fn " hash ", cmp_fn " cmp ", void *" context ", struct allocator *" al "); .br .BI "int hashmap_del(struct hashmap *" h ", del_fn " key_del ", del_fn " data_del ", void *" context "); .br .BI "int hashmap_clear(struct hashmap *" h ", del_fn " key_del ", del_fn " data_del ", void *" context "); .br .BI "int hashmap_clean(struct hashmap *" h "); .br .sp .BI "int hashmap_put(struct hashmap *" h ", void *" key ", void *" data "); .br .BI "void *hashmap_get(const struct hashmap *" h ", const void *" key "); .br .BI "int hashmap_is_empty(struct hashmap *" h "); .br .BI "unsigned int hashmap_size(struct hashmap *" h "); .br .BI "void hashmap_iterate(void *" h ", iter_t *" iter "); .br .BI "void *hashmap_next(void *" h ", iter_t *" iter "); .br .BI "int hashmap_remove(struct hashmap *" h ", void **" key ", void **" data "); .br .fi .SH DESCRIPTION A .BR "hashmap" (3m) object associates keys with data pointers. Large numbers of elements may be stored and retrieved efficiently. .sp Memory management of keys and data pointers is the resposibility of the user although .B del_fn function pointers (defined in .BR "allocator" (3m)) may be specified with some .B hashmap functions to assist the user with this task. .PP .TP .B init The .B hashmap_init function initializes the memory at .I h as a .B hashmap with no elements. The .I 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. .sp If the .I hash parameter is not .B NULL it will be used to generate a hash value given a key and the specified .I context object. Given a set of keys the .I hash function should generate an even distribution of values. If the .I hash parameter is .B NULL a key's memory address will be used as it's hash value. .sp If the .I cmp parameter is not .B 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 .I cmp parameter is .B NULL the memory addresses of the two keys will be compared. .sp The .I al parameter is an .BR "allocator" (3m) from which all memory associated with this .B hashmap should be allocated. As with the .B allocator functions, a .B NULL allocator indicates the .B stdlib_allocator should be used. .sp The following example illustrates how to initialize a .B hashmap and use it to store the object .I data associated with the character string "name". .PP .RS .nf .ta 4n 19n 31n 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 */ .ta .fi .RE .PP .TP .B deinit The .B hashmap_deinit function deinitializes the .B hashmap .IR "h" . If the .I key_del or .I data_del functions are not .B NULL they will be called with the .I context object and each key and/or data object in the map. Any memory associated with the .B hashmap will be released. .TP .B new The .B hashmap_new function allocates memory for a new .B hashmap object and initializes it with .B hashmap_init .TP .B del The .B hashmap_del function deinitializes the .B hashmap .I h with the .B hashmap_deinit function and then releases the .I h object itself. .TP .B clear The .B hashmap_clear function clears all elements from the .B hashmap .IR "h" . If the .I key_del or .I data_del functions are not .B NULL they will be called with the .I context object and each key and/or data object in the map. .TP .B clean The .B hashmap_clean function will release excess memory allocated by the .B hashmap .IR "h" . See the .B allocator_set_reclaim function. .TP .B put Put a data pointer into the map with the key .IR "key" . If an element with the same key already exists in the map, -1 will be returned and .I errno will be set to .IR "EEXIST" . If another error occurs, -1 will be returned and .I errno will be set to an appropriate value. .TP .B get Retrieve a data pointer from the map with the key .IR "key" . .TP .B is_empty Returns 1 if the map is empty and 0 otherwise. .TP .B size Returns the number of data pointers in the map. .TP .B iterate\fR, \fBnext Enumerate each key in the map. The .I hashmap_iterate function initializes the .I iter object to point to the beginning of the map. With each call to .IR "hashmap_next" , a key will be returned. When all keys have been enumerated, .I hashmap_next will return .IR "NULL" . Keys are not returned in any particular order. .sp 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. .TP .B remove The .B hashmap_remove function removes the element associated with .I key from the .B hashmap .I h and stores pointers to the original key and data in the provided .I key and .I data parameters. .sp The following is an example of removing an element from a .BR "hashmap" . .PP .RS .nf .ta 4n 19n 31n char *key = name; struct foo *data; hashmap_remove(hm, (void **)&key, (void **)&data); /* free data if necessary */ .ta .fi .RE .PP .SH RETURNS .TP .B init The .B hashmap_init function returns 0 on success or -1 for failure in which case .I errno will be set to an appropriate value. .TP .B deinit The .B hashmap_deinit function returns 0 on success or -1 for failure in which case .I errno will be set to an appropriate value. .TP .B new The .B hashmap_new function returns a new .I struct hashmap * object that contains no elements or .B NULL if the operation failed in which case .I errno will be set to an appropriate value. .TP .B del The .B hashmap_del function returns 0 on success or -1 for failure in which case .I errno will be set to an appropriate value. .TP .B clear The .B hashmap_clear function returns 0 on success or -1 for failure in which case .I errno will be set to an appropriate value. .TP .B clean The .B hashmap_clean function returns the number of unused elements released (possibly 0) or -1 if an error occured in which case .I errno will be set to an appropriate value. .TP .B get The .B hashmap_get function returns the data pointer being retrieved or .B NULL if the element was not found. .B NULL will also be returned if the .I h or .I key parameters are .I NULL but this function does not set .I errno to any value. .TP .B size The .B hashmap_size function returns the number of data pointers in the map. If .I h is .IR "NULL" , zero is returned. .TP .B next The .B hashmap_next function returns the next key in the map or .I NULL if all keys have been enumerated. .TP .B remove The .B hashmap_remove function returns 0 on success or -1 for failure in which case .I errno will be set to an appropriate value.