Defines | |
#define | SMAT_TABLE_INIT(flags, resize, extra) |
Sparse matrix table static initializer. More... | |
#define | st_verify(table) |
Sparse matrix table verification macro. More... | |
#define | st_flags(table) |
Sparse matrix table flags. More... | |
#define | st_frozen(table) |
Determine if a sparse matrix is frozen. More... | |
#define | st_modulus(table) |
Sparse matrix table modulus. More... | |
#define | st_count(table) |
Sparse matrix table count. More... | |
#define | st_extra(table) |
Extra pointer data in a sparse matrix table. More... | |
#define | st_size(table) |
Sparse matrix table memory size. More... | |
#define | SMAT_HEAD_INIT(elem, object) |
Sparse matrix list head static initializer. More... | |
#define | sh_verify(head) |
Sparse matrix list head verification macro. More... | |
#define | sh_elem(head) |
Sparse matrix list head element macro. More... | |
#define | sh_table(head) |
Sparse matrix list head table pointer. More... | |
#define | sh_frozen(head) |
Determine if a sparse matrix is frozen. More... | |
#define | sh_count(head) |
Sparse matrix list count. More... | |
#define | sh_first(head) |
First element in sparse matrix list. More... | |
#define | sh_last(head) |
Last element in sparse matrix list. More... | |
#define | sh_object(head) |
Object represented by a sparse matrix list head. More... | |
#define | sh_size(head) |
Sparse matrix list memory size. More... | |
#define | se_verify(entry) |
Sparse matrix entry verification macro. More... | |
#define | se_table(entry) |
Sparse matrix entry table. More... | |
#define | _se_link(entry) |
Sparse matrix entry linked list element. More... | |
#define | se_flags(entry) |
Sparse matrix entry flags. More... | |
#define | se_hash(entry) |
Sparse matrix table entry hash value. More... | |
#define | se_next(entry, n) |
Next element in sparse matrix list. More... | |
#define | se_prev(entry, n) |
Previous element in sparse matrix list. More... | |
#define | se_lflags(entry, n) |
Flags associated with an entry in a sparse matrix list. More... | |
#define | se_object(entry, n) |
Object associated with an entry in a sparse matrix list. More... | |
Typedefs | |
typedef struct _smat_table_s | smat_table_t |
Sparse matrix table. More... | |
typedef struct _smat_head_s | smat_head_t |
Sparse matrix list head. More... | |
typedef struct _smat_entry_s | smat_entry_t |
Sparse matrix entry. More... | |
typedef unsigned long (* | smat_resize_t )(smat_table_t *, unsigned long) |
Sparse matrix table resize callback. More... | |
typedef unsigned long (* | smat_iter_t )(smat_table_t *, smat_entry_t *, void *) |
Sparse matrix iteration callback. More... | |
typedef unsigned long (* | smat_comp_t )(db_key_t *, smat_entry_t *) |
Sparse matrix comparison callback. More... | |
typedef enum _smat_loc_e | smat_loc_t |
Sparse matrix location. More... | |
Enumerations | |
enum | _smat_loc_e { SMAT_LOC_FIRST, SMAT_LOC_SECOND } |
Sparse matrix location. More... | |
Functions | |
unsigned long | smat_cleanup (void) |
Clean up the smat free list. More... | |
unsigned long | smat_freemem (void) |
Report how much memory is used by the free list. More... | |
unsigned long | st_init (smat_table_t *table, unsigned long flags, smat_resize_t resize, void *extra, unsigned long init_mod) |
unsigned long | st_add (smat_table_t *table, smat_entry_t **entry_p, smat_head_t *head1, link_loc_t loc1, smat_entry_t *ent1, smat_head_t *head2, link_loc_t loc2, smat_entry_t *ent2) |
Add an entry to a sparse matrix. More... | |
unsigned long | st_remove (smat_table_t *table, smat_entry_t *entry) |
Remove an entry from a sparse matrix. More... | |
unsigned long | st_find (smat_table_t *table, smat_entry_t **entry_p, smat_head_t *head1, smat_head_t *head2) |
Find an entry in a sparse matrix. More... | |
unsigned long | st_iter (smat_table_t *table, smat_iter_t iter_func, void *extra) |
Iterate over each entry in a sparse matrix. More... | |
unsigned long | st_flush (smat_table_t *table, smat_iter_t flush_func, void *extra) |
Flush a sparse matrix. More... | |
unsigned long | st_resize (smat_table_t *table, unsigned long new_size) |
Resize a sparse matrix table. More... | |
unsigned long | st_free (smat_table_t *table) |
Free memory used by an empty sparse matrix table. More... | |
unsigned long | sh_init (smat_head_t *head, smat_loc_t elem, void *object) |
Dynamically initialize a sparse matrix row or column head. More... | |
unsigned long | sh_move (smat_head_t *head, smat_entry_t *elem, link_loc_t loc, smat_entry_t *elem2) |
Move an entry within a row or column list. More... | |
unsigned long | sh_find (smat_head_t *head, smat_entry_t **elem_p, smat_comp_t comp_func, smat_entry_t *start, db_key_t *key) |
Find an entry in a row or column of a sparse matrix. More... | |
unsigned long | sh_iter (smat_head_t *head, smat_iter_t iter_func, void *extra) |
Iterate over each entry in a row or column of a sparse matrix. More... |
A sparse matrix solves this problem by only allocating memory for the cells in the full matrix which are actually used. That is, no memory is allocated to represent Alice reporting to Bob unless Alice actually does report to Bob. This is a simple concept, but fairly difficult to implement efficiently--how do you tell if Alice reports to Bob? The solution utilized by this library is to combine the strengths of linked lists and hash tables. Each cell is in two linked lists, rooted at the rows and columns of the matrix, but a hash table is used when attempting to look up a given cell. If the cell is allocated, then there will be an entry in the hash table, and finding the given cell is as fast as a hash table look-up.
Because sparse matrices are so complicated, there are three structures and a variety of operations used. Two of the structures, smat_table_t and smat_head_t, are caller-allocated. However, the third structure, smat_entry_t, must be allocated by the library. To avoid too much overhead from malloc(), a free list is used. The free list may be managed with the smat_cleanup() and smat_freemem() calls.
The smat_table_t contains the hash table. Only one of these need be allocated per type of association--for instance, in the above example, only one smat_table_t needs to be allocated to represent the manager-employee relationship.
The smat_head_t contains the linked list. There are actually two kinds of these structures--one is SMAT_LOC_FIRST, which could be regarded as a ``row,'' and the other is SMAT_LOC_SECOND, which could be regarded as a ``column.'' Which one is used for which type of data is irrelevant, as long as consistency is maintained. For the above example, a smat_head_t for a manager may be SMAT_LOC_FIRST, and one for an employee must then be SMAT_LOC_SECOND. (These values are set when initializing the smat_head_t structure.)
An association may be created with the st_add() function, which allows an arbitrary ordering in the associated linked lists by the same mechanism as for the linked list component of the library. An association may be removed with st_remove(), or looked up with st_find(). If iteration over all associations is desired, use the st_iter() function. Removing all associations from a table may be performed with st_flush(), which optionally calls a user-defined clean-up function. The associated hash table may be resized with st_resize(), and the bucket table may be released with st_free().
An association may also be reordered within the linked lists using the sh_move() function. If a particular entry is desired, use the sh_find() function with a user-defined comparison function to locate it. Iteration may be performed with the sh_iter() function, and all entries in a given linked list may be removed with the sh_flush() function, which again may optionally call a user-defined clean-up function.
|
This macro statically initializes a smat_head_t.
|
|
This macro statically initializes a smat_table_t.
|
|
For internal use only. |
|
This macro retrieves a set of user-defined flags associated with the entry. It may be used as an lvalue to set those flags.
|
|
This macro retrieves the hash value of the given sparse matrix entry. If the sparse matrix hash been resized, this value may not be the same as a previous value.
|
|
This macro retrieves a set of user-defined flags associated with the entry in a sparse matrix list. It may be used as an lvalue to set those flags.
|
|
This macro retrieves a pointer to the link_elem_t for the next element in the sparse matrix list.
|
|
This macro retrieves a pointer to one of the object represented by the entry. It may be used as an lvalue to change the object pointed to. Care should be taken when using this feature.
|
|
This macro retrieves a pointer to the link_elem_t for the previous element in the sparse matrix list.
|
|
This macro retrieves a pointer to the table that the sparse matrix entry is in.
|
|
This macro verifies that a given pointer actually does point to a sparse matrix entry.
|
|
This macro retrieves the number of elements in the sparse matrix list rooted at
|
|
This macro retrieves the position indicator for the sparse matrix head. It will return one of SMAT_LOC_FIRST or SMAT_LOC_SECOND.
|
|
This macro retrieves a pointer to the smat_entry_t for the first element in the sparse matrix list.
|
|
This macro returns a non-zero value if the matrix is currently frozen. The sparse matrix may be frozen if there is an iteration in progress.
|
|
This macro retrieves a pointer to the smat_entry_t for the last element in the sparse matrix list.
|
|
This macro retrieves a pointer to the object referenced by the sparse matrix list head.
|
|
This macro returns the physical size of the memory allocated by the library for this sparse matrix list.
|
|
If there are any elements in this sparse matrix list head, this macro will retrieve a pointer to the table in which they reside.
|
|
This macro verifies that a given pointer actually does point to a sparse matrix head.
|
|
This macro retrieves the total number of items actually in the sparse matrix table.
|
|
This macro retrieves the extra pointer data associated with a particular sparse matrix table.
|
|
This macro retrieves the flags associated with the sparse matrix table. Only HASH_FLAG_AUTOGROW and HASH_FLAG_AUTOSHRINK have any meaning to the application; all other bits are reserved for use in the library. This macro may be used as an lvalue, but care must be taken to avoid modifying the library-specific bits.
|
|
This macro returns a non-zero value if the matrix is currently frozen. The sparse matrix may be frozen if there is an iteration in progress.
|
|
This macro retrieves the number of buckets allocated for the sparse matrix table. An application may wish to save this value between invocations to avoid the overhead of growing the table while filling it with data.
|
|
This macro returns the physical size of the memory allocated by the library for this sparse matrix table.
|
|
This macro verifies that a given pointer actually does point to a sparse matrix table.
|
|
This function pointer references a callback used by sh_find(). It should return 0 if the sparse matrix entry represented by the second argument matches the key passed as the first argument. |
|
This structure is allocated by the library and represents a single element in a sparse matrix. |
|
This structure is the head of a linked list of sparse matrix entries. |
|
This function pointer references a callback used by st_iter(), st_flush(), sh_iter(), and sh_flush(). It should return 0 for success. A non-zero return value will terminate the operation and will become the return value of the call. |
|
See the documentation for the enumeration _smat_loc_e. |
|
This function pointer references a callback that will be called with both the old and new sparse matrix table sizes whenever a sparse matrix's hash table table is resized. It should return non-zero only when the resize should be inhibited. |
|
This structure is the basis of all sparse matrices maintained by this library. |
|
This enumeration is used to specify whether an element is a row or column element. It should be referenced by the typedef smat_loc_t. |
|
This function iterates through the given row or column of a sparse matrix looking for an element that matches the given
|
|
This function dynamically initializes a sparse matrix row or column linked list head. The
|
|
This function iterates over a row or column of a sparse matrix, executing the given
|
|
This function allows the specified entry to be shifted within the linked list describing the row or column. It is very similar to the ll_move() function.
|
|
This function frees all smat_entry_t objects on the internal free list. It is always successful and returns 0. |
|
This function returns the amount of memory being used by the internal free list of smat_entry_t objects.
|
|
This function adds an entry to a sparse matrix. The entry is referenced in three different places, thus the complex set of arguments. This function will allocate a smat_entry_t and return it through the
|
|
This function looks up the entry matching the given
|
|
This function flushes a sparse matrix--that is, it removes each entry from the matrix. If a
|
|
This function releases the memory used by the bucket table of the empty hash table associated with a sparse matrix.
|
|
This function dynamically initializes a sparse matrix table.
|
|
This function iterates over every entry in a sparse matrix (in an unspecified order), executing the given
|
|
This function removes the given entry from the specified sparse matrix.
|
|
This function resizes the hash table associated with a sparse matrix based on the
|