| www.delorie.com/gnu/docs/avl/libavl_17.html | search |
![]() Buy GNU books! | |
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
These functions insert and delete items in tables. There is also a function for searching a table without modifying it.
The design behind the insertion functions takes into account a couple of important issues:
void **tbl_probe (struct tbl_table *, void *); void *tbl_insert (struct tbl_table *, void *); void *tbl_replace (struct tbl_table *, void *); void *tbl_delete (struct tbl_table *, const void *); void *tbl_find (const struct tbl_table *, const void *); |
Each of these functions takes a table to manipulate as its first argument and a table item as its second argument, here called table and item, respectively. Both arguments must be non-null in all cases. All but tbl_probe() return a table item or a null pointer.
The pointer returned can be used to replace the item found or inserted by a different item. This must only be done if the replacement item has the same position relative to the other items in the table as did the original item. That is, for existing item e, replacement item r, and the table's comparison function f(), the return values of f(e, x) and f(r, x) must have the same sign for every other item x currently in the table. Calling any other table function invalidates the pointer returned and it must not be referenced subsequently.
Exercises:
1. Functions tbl_insert() and tbl_replace() return NULL in two very different situations: an error or successful insertion. Why is this not necessarily a design mistake? [answer]
2. Suggest a reason for disallowing insertion of a null item. [answer]
3. Write generic implementations of tbl_insert() and tbl_replace() in terms of tbl_probe(). [answer]
| [ < ] | [ > ] | [ << ] | [ Up ] | [ >> ] | [Top] | [Contents] | [Index] | [ ? ] |
| webmaster donations bookstore | delorie software privacy |
| Copyright © 2003 by The Free Software Foundation | Updated Jun 2003 |