#ifndef __SMAP_H__ #define __SMAP_H__ #include /** * A map from integers to strings. */ struct ismap; typedef struct ismap ismap; /** * Creates an empty map that uses the given hash function. * * @param h a pointer to takes a string and returns its hash code */ ismap *ismap_create(int (*h)(int n)); /** * Returns the number of (key, value) pairs in the given map. * * @param m a map, non-NULL * @return the size of m */ int ismap_size(const ismap *m); /** * Adds a copy of the given key with value to this map. * If the key is already present then the old value is replaced. * The caller retains ownership of the value. * * @param m a map, non-NULL * @param key a string, non-NULL * @param value a pointer to an integer */ void ismap_put(ismap *m, int key, char *value); /** * Determines if the given key is present in this map. * * @param m a map, non-NULL * @param key a string, non-NULL * @return true if key is present in this map, false otherwise */ bool ismap_contains_key(const ismap *m, int key); /** * Returns the value associated with the given key in this map. * If the key is not present in this map then the returned value is * NULL. * * @param m a map, non-NULL * @param key a string, non-NULL * @return the assocated value, or NULL if they key is not present */ char *ismap_get(ismap *m, int key); /** * Calls the given function for each (key, value) pair in this map. * * @param m a map, non-NULL * @param f a pointer to a function that takes a key and a value, non-NULL */ void ismap_for_each(ismap* m, void (*f)(int k, char *v)); /** * Calls the given function for each (key, value) pair in this map, passing * the extra argument we well. * * @param m a map, non-NULL * @param f a pointer to a function that takes a key, a value, and an * extra piece of information; non-NULL * @param arg a pointer */ void ismap_for_each_r(ismap *m, void (*f)(int , char *, void *), void *arg); /** * Destroys the given map. * * @param m a map, non-NULL */ void ismap_destroy(ismap *s); #endif