map.h
HashMap
type defined
in the hashmap.h
interface, the implementation of the
Map
type uses a balanced binary tree, which offers
logarithmic performance and sorted iteration.
In most applications, the restriction of keys to strings is easy
to circumvent. The simplest strategy is to convert key values to
strings before inserting them into the map. A more general strategy
is to use the bst.h
interface instead.
Type | |
This type is the ADT used to represent the map. | |
Functions | |
Allocates a new map with no entries. | |
Frees the storage associated with the map. | |
Returns the number of elements in the map. | |
Returns true if the map has no entries. | |
Removes all entries from the map. | |
Creates a copy of the map. | |
Associates key with value in the map. | |
Returns the value associated with key in the map, or NULL , if no such value exists. | |
Checks to see if the map contains the specified key. | |
Removes the key and its value from the map. | |
Iterates through the map and calls the function fn on each entry. |
typedef struct MapCDT *Map;
Map newMap();
Usage:
map = newMap();
void freeMap(Map map);
Usage:
freeMap(map);
int size(Map map);
Usage:
n = size(map);
bool isEmpty(Map map);
true
if the map has no entries.
Usage:
if (isEmpty(map)) . . .
void clear(Map map);
Usage:
clear(map);
Map clone(Map map);
clone
function copies
only the first level of the structure and does not copy the individual
elements.
Usage:
newmap = clone(map);
void put(Map map, string key, void *value);
key
with value
in the map.
Each call to put
supersedes any previous definition
for key
.
Usage:
put(map, key, value);
void *get(Map map, string key);
key
in the map,
or NULL
, if no such value exists.
Usage:
void *value = get(map, key);
bool containsKey(Map map, string key);
Usage:
if (containsKey(map, key)) . . .
void remove(Map map, string key);
Usage:
remove(map, key);
void map(Map map, proc fn, void *data);
fn
on
each entry. The callback function takes the following arguments:
data
pointer
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);