This is a bare-bones implementation of a table mapping from strings to objects. The client uses the defined fields to access the contents of mappings and to traverse the table. A table is declared and initialized like this:
struct strtbl *pairstbl;
...
pairstbl = newStrTbl();
Entries are made with addToStrTbl
, the value for an entry can be set with setValInStrTbl
, and a pair can be deleted with removeRowFromStrTbl
. Table traversal uses the internal fields of the table. Insertions are inserted in order by key value, so traversal will visit each row in order of the keys. Here is typical traversal code:
for (k = 0; k < pairstbl->used; k++) {
struct row *rx = pairstbl->rows+k;
struct YourContentType *px = (struct pair *)rx->entry;
{ ... process *px ... }
}
These structure types are defined:
struct row {char *key; void *entry;}
- Structure to store a key-entry pair.
struct strtbl {int size, used; enum EQCHOICE eqcode; struct row *rows;};
- The
rows
field points to an array of pairs. It has size
elements of which the first used
are meaningful. When duplicate keys are entered they are treated according to the eqcode
.
-
struct strtbl *newStrTbl()
- Create a new
strtbl
value.
An enum
provides parameter values for two methods:
-
EQCHOICE
- Determines how an insertion by
addEntryStrTbl
treats equal keys. Default FIRST
. Options are
FIRST
- keep only the first instance
LATEST
- keep only the most recent
-
BEFORE
- keep all values, placing new values first
-
AFTER
- keep all values, placing new values last
The methods are:
void setEqChoice(enum EQCHOICE choice, struct strtbl *st)
- Choose treatment of equal keys.
void addToStrTbl(char *key, void *val, struct strtbl *st)
- Add an entry to a strtbl.
void setValInStrTbl(char *key, void *val, struct strtbl *st)
- Change the value associated with a given key in the StrTbl.
If the key does not yet have a value, a new row is created.
If the key has multiple values, all but one are removed
and the remainng one is set to the new value.
void *getEntryFromStrTbl(char *key, struct strtbl *st)
- Return the first value associated with key in strtbl st.
If there is no such row, NULL is returned.
void removeRowFromStrTbl(char *key, struct strtbl *st)
- Remove a row for a given key.
Only the first is removed;
if there are others for the same key, they remain.