hashmap.h

This interface defines a map abstraction that associates string keys with values. The implementation uses a hash table, which offers constant-time performance but does not support iterating through the keys in order.
Functions
HashMap This type is the ADT used to represent a map from strings to values.
newHashMap() Allocates a new map with no entries.
freeHashMap(map) Frees the storage associated with the map.
size(map) Returns the number of elements in the map.
isEmpty(map) Returns true if the map has no entries.
clear(map) Removes all entries from the map.
clone(map) Creates a copy of the map.
put(map, key, value) Associates key with value in the map.
get(map, key) Returns the value associated with key in the map, or NULL, if no such value exists.
containsKey(map, key) Checks to see if the map contains the specified key.
remove(map, key) Removes the key and its value from the map.
map(map, fn, data) Iterates through the map and calls the function fn on each entry.

Function detail


typedef struct HashMapCDT *HashMap;
This type is the ADT used to represent a map from strings to values.

HashMap newHashMap(void);
Allocates a new map with no entries.

Usage:

map = newHashMap();

void freeHashMap(HashMap map);
Frees the storage associated with the map.

Usage:

freeHashMap(map);

int size(HashMap map);
Returns the number of elements in the map.

Usage:

n = size(map);

bool isEmpty(HashMap map);
Returns true if the map has no entries.

Usage:

if (isEmpty(map)) . . .

void clear(HashMap map);
Removes all entries from the map.

Usage:

clear(map);

HashMap clone(HashMap map);
Creates a copy of the map. The clone function copies only the first level of the structure and does not copy the individual elements.

Usage:

newmap = clone(map);

void put(HashMap map, string key, void *value);
Associates key with value in the map. Each call to put supersedes any previous definition for key.

Usage:

put(map, key, value);

void *get(HashMap map, string key);
Returns the value associated with key in the map, or NULL, if no such value exists.

Usage:

void *value = get(map, key);

bool containsKey(HashMap map, string key);
Checks to see if the map contains the specified key.

Usage:

if (containsKey(map, key)) . . .

void remove(HashMap map, string key);
Removes the key and its value from the map.

Usage:

remove(map, key);

void map(HashMap map, proc fn, void *data);
Iterates through the map and calls the function fn on each entry. The callback function takes the following arguments: The data pointer allows the client to pass state information to the function fn, if necessary. If no such information is required, this argument should be NULL.

Usage:

map(map, fn, data);