libsc
2.8.7
The SC library provides support for parallel scientific applications.
|
Dynamic containers such as lists, arrays, and hash tables. More...
#include <sc.h>
Go to the source code of this file.
Data Structures | |
struct | sc_array |
The sc_array object provides a dynamic array of equal-size elements. More... | |
struct | sc_mstamp |
A data container to create memory items of the same size. More... | |
struct | sc_mempool |
The sc_mempool object provides a large pool of equal-size elements. More... | |
struct | sc_link |
The sc_link structure is one link of a linked list. More... | |
struct | sc_list |
The sc_list object provides a linked list. More... | |
struct | sc_hash |
The sc_hash implements a hash table. More... | |
struct | sc_hash_array |
The sc_hash_array implements an array backed up by a hash table. More... | |
struct | sc_recycle_array |
The sc_recycle_array object provides an array of slots that can be reused. More... | |
Macros | |
#define | sc_hash_rot(x, k) (((x) << (k)) | ((x) >> (32 - (k)))) |
Bijective bit rotation as building block for hash functions. More... | |
#define | sc_hash_mix(a, b, c) |
Integer bit mixer as building block for hash functions. More... | |
#define | sc_hash_final(a, b, c) |
Integer bit operations as building block for hash functions. More... | |
#define | SC_ARRAY_IS_OWNER(a) ((a)->byte_alloc >= 0) |
Test whether the sc_array_t owns its array. | |
#define | SC_ARRAY_BYTE_ALLOC(a) |
Return the allocated size of the array. More... | |
#define | sc_array_new_size(s, c) (sc_array_new_count ((s), (c))) |
Deprecated: use sc_array_new_count. | |
Typedefs | |
typedef unsigned int(* | sc_hash_function_t) (const void *v, const void *u) |
Function to compute a hash value of an object. More... | |
typedef int(* | sc_equal_function_t) (const void *v1, const void *v2, const void *u) |
Function to check equality of two objects. More... | |
typedef int(* | sc_hash_foreach_t) (void **v, const void *u) |
Function to call on every data item of a hash table or hash array. More... | |
typedef struct sc_array | sc_array_t |
The sc_array object provides a dynamic array of equal-size elements. More... | |
typedef size_t(* | sc_array_type_t) (sc_array_t *array, size_t index, void *data) |
Function to determine the enumerable type of an object in an array. More... | |
typedef struct sc_mstamp | sc_mstamp_t |
A data container to create memory items of the same size. More... | |
typedef struct sc_mempool | sc_mempool_t |
The sc_mempool object provides a large pool of equal-size elements. More... | |
typedef struct sc_link | sc_link_t |
The sc_link structure is one link of a linked list. | |
typedef struct sc_list | sc_list_t |
The sc_list object provides a linked list. | |
typedef struct sc_hash | sc_hash_t |
The sc_hash implements a hash table. More... | |
typedef struct sc_hash_array_data | sc_hash_array_data_t |
Internal context structure for sc_hash_array. | |
typedef struct sc_hash_array | sc_hash_array_t |
The sc_hash_array implements an array backed up by a hash table. More... | |
typedef struct sc_recycle_array | sc_recycle_array_t |
The sc_recycle_array object provides an array of slots that can be reused. More... | |
Functions | |
size_t | sc_array_memory_used (sc_array_t *array, int is_dynamic) |
Calculate the memory used by an array. More... | |
sc_array_t * | sc_array_new (size_t elem_size) |
Creates a new array structure with 0 elements. More... | |
sc_array_t * | sc_array_new_count (size_t elem_size, size_t elem_count) |
Creates a new array structure with a given length (number of elements). More... | |
sc_array_t * | sc_array_new_view (sc_array_t *array, size_t offset, size_t length) |
Creates a new view of an existing sc_array_t. More... | |
sc_array_t * | sc_array_new_data (void *base, size_t elem_size, size_t elem_count) |
Creates a new view of an existing plain C array. More... | |
void | sc_array_destroy (sc_array_t *array) |
Destroys an array structure. More... | |
void | sc_array_destroy_null (sc_array_t **parray) |
Destroys an array structure and sets the pointer to NULL. More... | |
void | sc_array_init (sc_array_t *array, size_t elem_size) |
Initializes an already allocated (or static) array structure. More... | |
void | sc_array_init_size (sc_array_t *array, size_t elem_size, size_t elem_count) |
Initializes an already allocated (or static) array structure and allocates a given number of elements. More... | |
void | sc_array_init_count (sc_array_t *array, size_t elem_size, size_t elem_count) |
Initializes an already allocated (or static) array structure and allocates a given number of elements. More... | |
void | sc_array_init_view (sc_array_t *view, sc_array_t *array, size_t offset, size_t length) |
Initializes an already allocated (or static) view from existing sc_array_t. More... | |
void | sc_array_init_reshape (sc_array_t *view, sc_array_t *array, size_t elem_size, size_t elem_count) |
Initialize an already allocated (or static) view from existing sc_array_t. More... | |
void | sc_array_init_data (sc_array_t *view, void *base, size_t elem_size, size_t elem_count) |
Initializes an already allocated (or static) view from given plain C data. More... | |
void | sc_array_memset (sc_array_t *array, int c) |
Run memset on the array storage. More... | |
void | sc_array_reset (sc_array_t *array) |
Sets the array count to zero and frees all elements. More... | |
void | sc_array_truncate (sc_array_t *array) |
Sets the array count to zero, but does not free elements. More... | |
void | sc_array_rewind (sc_array_t *array, size_t new_count) |
Shorten an array without reallocating it. More... | |
void | sc_array_resize (sc_array_t *array, size_t new_count) |
Sets the element count to new_count. More... | |
void | sc_array_copy (sc_array_t *dest, sc_array_t *src) |
Copy the contents of one array into another. More... | |
void | sc_array_copy_into (sc_array_t *dest, size_t dest_offset, sc_array_t *src) |
Copy the contents of one array into some portion of another. More... | |
void | sc_array_move_part (sc_array_t *dest, size_t dest_offset, sc_array_t *src, size_t src_offset, size_t count) |
Copy part of one array into another using memmove (3). More... | |
void | sc_array_sort (sc_array_t *array, int(*compar)(const void *, const void *)) |
Sorts the array in ascending order wrt. More... | |
int | sc_array_is_sorted (sc_array_t *array, int(*compar)(const void *, const void *)) |
Check whether the array is sorted wrt. More... | |
int | sc_array_is_equal (sc_array_t *array, sc_array_t *other) |
Check whether two arrays have equal size, count, and content. More... | |
void | sc_array_uniq (sc_array_t *array, int(*compar)(const void *, const void *)) |
Removed duplicate entries from a sorted array. More... | |
ssize_t | sc_array_bsearch (sc_array_t *array, const void *key, int(*compar)(const void *, const void *)) |
Performs a binary search on an array. More... | |
void | sc_array_split (sc_array_t *array, sc_array_t *offsets, size_t num_types, sc_array_type_t type_fn, void *data) |
Compute the offsets of groups of enumerable types in an array. More... | |
int | sc_array_is_permutation (sc_array_t *array) |
Determine whether array is an array of size_t's whose entries include every integer 0 <= i < array->elem_count. More... | |
void | sc_array_permute (sc_array_t *array, sc_array_t *newindices, int keepperm) |
Given permutation newindices, permute array in place. More... | |
unsigned int | sc_array_checksum (sc_array_t *array) |
Computes the adler32 checksum of array data (see zlib documentation). More... | |
size_t | sc_array_pqueue_add (sc_array_t *array, void *temp, int(*compar)(const void *, const void *)) |
Adds an element to a priority queue. More... | |
size_t | sc_array_pqueue_pop (sc_array_t *array, void *result, int(*compar)(const void *, const void *)) |
Pops the smallest element from a priority queue. More... | |
void | sc_mstamp_init (sc_mstamp_t *mst, size_t stamp_unit, size_t elem_size) |
Initialize a memory stamp container. More... | |
void | sc_mstamp_reset (sc_mstamp_t *mst) |
Free all memory in a stamp structure and all items previously returned. More... | |
void | sc_mstamp_truncate (sc_mstamp_t *mst) |
Free all memory in a stamp structure and initialize it anew. More... | |
void * | sc_mstamp_alloc (sc_mstamp_t *mst) |
Return a new item. More... | |
size_t | sc_mstamp_memory_used (sc_mstamp_t *mst) |
Return memory size in bytes of all data allocated in the container. More... | |
size_t | sc_mempool_memory_used (sc_mempool_t *mempool) |
Calculate the memory used by a memory pool. More... | |
sc_mempool_t * | sc_mempool_new (size_t elem_size) |
Creates a new mempool structure with the zero_and_persist option off. More... | |
sc_mempool_t * | sc_mempool_new_zero_and_persist (size_t elem_size) |
Creates a new mempool structure with the zero_and_persist option on. More... | |
void | sc_mempool_init (sc_mempool_t *mempool, size_t elem_size) |
Same as sc_mempool_new, but for an already allocated object. More... | |
void | sc_mempool_destroy (sc_mempool_t *mempool) |
Destroy a mempool structure. More... | |
void | sc_mempool_destroy_null (sc_mempool_t **pmempool) |
Destroy a mempool structure. More... | |
void | sc_mempool_reset (sc_mempool_t *mempool) |
Same as sc_mempool_destroy, but does not free the pointer. More... | |
void | sc_mempool_truncate (sc_mempool_t *mempool) |
Invalidates all previously returned pointers, resets count to 0. More... | |
size_t | sc_list_memory_used (sc_list_t *list, int is_dynamic) |
Calculate the total memory used by a list. More... | |
sc_list_t * | sc_list_new (sc_mempool_t *allocator) |
Allocate a new, empty linked list. More... | |
void | sc_list_destroy (sc_list_t *list) |
Destroy a linked list structure in O(N). More... | |
void | sc_list_init (sc_list_t *list, sc_mempool_t *allocator) |
Initialize a list object with an external link allocator. More... | |
void | sc_list_reset (sc_list_t *list) |
Remove all elements from a list in O(N). More... | |
void | sc_list_unlink (sc_list_t *list) |
Unlink all list elements without returning them to the mempool. More... | |
sc_link_t * | sc_list_prepend (sc_list_t *list, void *data) |
Insert a list element at the beginning of the list. More... | |
sc_link_t * | sc_list_append (sc_list_t *list, void *data) |
Insert a list element at the end of the list. More... | |
sc_link_t * | sc_list_insert (sc_list_t *list, sc_link_t *pred, void *data) |
Insert an element after a given list position. More... | |
void * | sc_list_remove (sc_list_t *list, sc_link_t *pred) |
Remove an element after a given list position. More... | |
void * | sc_list_pop (sc_list_t *list) |
Remove an element from the front of the list. More... | |
unsigned int | sc_hash_function_string (const void *s, const void *u) |
Compute a hash value from a null-terminated string. More... | |
size_t | sc_hash_memory_used (sc_hash_t *hash) |
Calculate the memory used by a hash table. More... | |
sc_hash_t * | sc_hash_new (sc_hash_function_t hash_fn, sc_equal_function_t equal_fn, void *user_data, sc_mempool_t *allocator) |
Create a new hash table. More... | |
void | sc_hash_destroy (sc_hash_t *hash) |
Destroy a hash table. More... | |
void | sc_hash_destroy_null (sc_hash_t **phash) |
Destroy a hash table and set its pointer to NULL. More... | |
void | sc_hash_truncate (sc_hash_t *hash) |
Remove all entries from a hash table in O(N). More... | |
void | sc_hash_unlink (sc_hash_t *hash) |
Unlink all hash elements without returning them to the mempool. More... | |
void | sc_hash_unlink_destroy (sc_hash_t *hash) |
Same effect as unlink and destroy, but in O(1). More... | |
int | sc_hash_lookup (sc_hash_t *hash, void *v, void ***found) |
Check if an object is contained in the hash table. More... | |
int | sc_hash_insert_unique (sc_hash_t *hash, void *v, void ***found) |
Insert an object into a hash table if it is not contained already. More... | |
int | sc_hash_remove (sc_hash_t *hash, void *v, void **found) |
Remove an object from a hash table. More... | |
void | sc_hash_foreach (sc_hash_t *hash, sc_hash_foreach_t fn) |
Invoke a callback for every member of the hash table. More... | |
void | sc_hash_print_statistics (int package_id, int log_priority, sc_hash_t *hash) |
Compute and print statistical information about the occupancy. More... | |
size_t | sc_hash_array_memory_used (sc_hash_array_t *ha) |
Calculate the memory used by a hash array. More... | |
sc_hash_array_t * | sc_hash_array_new (size_t elem_size, sc_hash_function_t hash_fn, sc_equal_function_t equal_fn, void *user_data) |
Create a new hash array. More... | |
void | sc_hash_array_destroy (sc_hash_array_t *hash_array) |
Destroy a hash array. More... | |
int | sc_hash_array_is_valid (sc_hash_array_t *hash_array) |
Check the internal consistency of a hash array. More... | |
void | sc_hash_array_truncate (sc_hash_array_t *hash_array) |
Remove all elements from the hash array. More... | |
int | sc_hash_array_lookup (sc_hash_array_t *hash_array, void *v, size_t *position) |
Check if an object is contained in a hash array. More... | |
void * | sc_hash_array_insert_unique (sc_hash_array_t *hash_array, void *v, size_t *position) |
Insert an object into a hash array if it is not contained already. More... | |
void | sc_hash_array_foreach (sc_hash_array_t *hash_array, sc_hash_foreach_t fn) |
Invoke a callback for every member of the hash array. More... | |
void | sc_hash_array_rip (sc_hash_array_t *hash_array, sc_array_t *rip) |
Extract the array data from a hash array and destroy everything else. More... | |
void | sc_recycle_array_init (sc_recycle_array_t *rec_array, size_t elem_size) |
Initialize a recycle array. More... | |
void | sc_recycle_array_reset (sc_recycle_array_t *rec_array) |
Reset a recycle array. More... | |
void * | sc_recycle_array_insert (sc_recycle_array_t *rec_array, size_t *position) |
Insert an object into the recycle array. More... | |
void * | sc_recycle_array_remove (sc_recycle_array_t *rec_array, size_t position) |
Remove an object from the recycle array. More... | |
Dynamic containers such as lists, arrays, and hash tables.
#define SC_ARRAY_BYTE_ALLOC | ( | a | ) |
Return the allocated size of the array.
#define sc_hash_final | ( | a, | |
b, | |||
c | |||
) |
Integer bit operations as building block for hash functions.
[in,out] | a | First in/out value (32-bit integer). |
[in,out] | b | Second in/out value (32-bit integer). |
[in,out] | c | Third in/out value (32-bit integer). |
#define sc_hash_mix | ( | a, | |
b, | |||
c | |||
) |
Integer bit mixer as building block for hash functions.
[in,out] | a | First in/out value (32-bit integer). |
[in,out] | b | Second in/out value (32-bit integer). |
[in,out] | c | Third in/out value (32-bit integer). |
#define sc_hash_rot | ( | x, | |
k | |||
) | (((x) << (k)) | ((x) >> (32 - (k)))) |
Bijective bit rotation as building block for hash functions.
[in] | x | Input value (32-bit integer). |
[in] | k | Bit shift amount (<= 32). |
typedef struct sc_array sc_array_t |
The sc_array object provides a dynamic array of equal-size elements.
Elements are accessed by their 0-based index. Their address may change. The number of elements (== elem_count) of the array can be changed by sc_array_resize and sc_array_rewind. Elements can be sorted with sc_array_sort. If the array is sorted, it can be searched with sc_array_bsearch. A priority queue is implemented with pqueue_add and pqueue_pop (untested).
typedef size_t(* sc_array_type_t) (sc_array_t *array, size_t index, void *data) |
Function to determine the enumerable type of an object in an array.
[in] | array | Array containing the object. |
[in] | index | The location of the object. |
[in] | data | Arbitrary user data. |
typedef int(* sc_equal_function_t) (const void *v1, const void *v2, const void *u) |
Function to check equality of two objects.
[in] | v1 | Pointer to first object checked for equality. |
[in] | v2 | Pointer to second object checked for equality. |
[in] | u | Arbitrary user data. |
typedef struct sc_hash_array sc_hash_array_t |
The sc_hash_array implements an array backed up by a hash table.
This enables O(1) access for array elements.
typedef int(* sc_hash_foreach_t) (void **v, const void *u) |
Function to call on every data item of a hash table or hash array.
[in] | v | The address of the pointer to the current object. |
[in] | u | Arbitrary user data. |
typedef unsigned int(* sc_hash_function_t) (const void *v, const void *u) |
Function to compute a hash value of an object.
[in] | v | The object to hash. |
[in] | u | Arbitrary user data. |
The sc_hash implements a hash table.
It uses an array which has linked lists as elements.
typedef struct sc_mempool sc_mempool_t |
The sc_mempool object provides a large pool of equal-size elements.
The pool grows dynamically for element allocation. Elements are referenced by their address which never changes. Elements can be freed (that is, returned to the pool) and are transparently reused. If the zero_and_persist option is selected, new elements are initialized to all zeros on creation, and the contents of an element are not touched between freeing and re-returning it.
typedef struct sc_mstamp sc_mstamp_t |
A data container to create memory items of the same size.
Allocations are bundled so it's fast for small memory sizes. The items created will remain valid until the container is destroyed. There is no option to return an item to the container. See sc_mempool_t for that purpose.
typedef struct sc_recycle_array sc_recycle_array_t |
The sc_recycle_array object provides an array of slots that can be reused.
It keeps a list of free slots in the array which will be used for insertion while available. Otherwise, the array is grown.
ssize_t sc_array_bsearch | ( | sc_array_t * | array, |
const void * | key, | ||
int(*)(const void *, const void *) | compar | ||
) |
Performs a binary search on an array.
The array must be sorted.
[in] | array | A sorted array to search in. |
[in] | key | An element to be searched for. |
[in] | compar | The comparison function to be used. |
unsigned int sc_array_checksum | ( | sc_array_t * | array | ) |
Computes the adler32 checksum of array data (see zlib documentation).
This is a faster checksum than crc32, and it works with zeros as data.
void sc_array_copy | ( | sc_array_t * | dest, |
sc_array_t * | src | ||
) |
Copy the contents of one array into another.
Both arrays must have equal element sizes. The source array may be a view. We use memcpy (3): If the two arrays overlap, results are undefined.
[in] | dest | Array (not a view) will be resized and get new data. |
[in] | src | Array used as source of new data, will not be changed. |
void sc_array_copy_into | ( | sc_array_t * | dest, |
size_t | dest_offset, | ||
sc_array_t * | src | ||
) |
Copy the contents of one array into some portion of another.
Both arrays must have equal element sizes. Either array may be a view. The destination array must be large enough. We use memcpy (3): If the two arrays overlap, results are undefined.
[in] | dest | Array will be written into. Its element count must be at least dest_offset + src->elem_count. |
[in] | dest_offset | First index in dest array to be overwritten. As every index, it refers to elements, not bytes. |
[in] | src | Array used as source of new data, will not be changed. |
void sc_array_destroy | ( | sc_array_t * | array | ) |
Destroys an array structure.
[in] | array | The array to be destroyed. |
void sc_array_destroy_null | ( | sc_array_t ** | parray | ) |
Destroys an array structure and sets the pointer to NULL.
[in,out] | parray | Pointer to address of array to be destroyed. On output, *parray is NULL. |
void sc_array_init | ( | sc_array_t * | array, |
size_t | elem_size | ||
) |
Initializes an already allocated (or static) array structure.
[in,out] | array | Array structure to be initialized. |
[in] | elem_size | Size of one array element in bytes. |
void sc_array_init_count | ( | sc_array_t * | array, |
size_t | elem_size, | ||
size_t | elem_count | ||
) |
Initializes an already allocated (or static) array structure and allocates a given number of elements.
This function supersedes sc_array_init_size.
[in,out] | array | Array structure to be initialized. |
[in] | elem_size | Size of one array element in bytes. |
[in] | elem_count | Number of initial array elements. |
void sc_array_init_data | ( | sc_array_t * | view, |
void * | base, | ||
size_t | elem_size, | ||
size_t | elem_count | ||
) |
Initializes an already allocated (or static) view from given plain C data.
The array view returned does not require sc_array_reset (doesn't hurt though).
[in,out] | view | Array structure to be initialized. |
[in] | base | The data must not be moved while view is alive. |
[in] | elem_size | Size of one array element in bytes. |
[in] | elem_count | The length of the view in element units. The view cannot be resized to exceed this length. It is not necessary to call sc_array_reset later. |
void sc_array_init_reshape | ( | sc_array_t * | view, |
sc_array_t * | array, | ||
size_t | elem_size, | ||
size_t | elem_count | ||
) |
Initialize an already allocated (or static) view from existing sc_array_t.
The total data size of the view is the same, but size and count may differ. The array view returned does not require sc_array_reset (doesn't hurt though).
[in,out] | view | Array structure to be initialized. |
[in] | array | The array must not be resized while view is alive. |
[in] | elem_size | Size of one array element of the view in bytes. The product of size and count of array must be the same as elem_size * elem_count. |
[in] | elem_count | The length of the view in element units. The view cannot be resized to exceed this length. It is not necessary to call sc_array_reset later. |
void sc_array_init_size | ( | sc_array_t * | array, |
size_t | elem_size, | ||
size_t | elem_count | ||
) |
Initializes an already allocated (or static) array structure and allocates a given number of elements.
Deprecated: use sc_array_init_count.
[in,out] | array | Array structure to be initialized. |
[in] | elem_size | Size of one array element in bytes. |
[in] | elem_count | Number of initial array elements. |
void sc_array_init_view | ( | sc_array_t * | view, |
sc_array_t * | array, | ||
size_t | offset, | ||
size_t | length | ||
) |
Initializes an already allocated (or static) view from existing sc_array_t.
The array view returned does not require sc_array_reset (doesn't hurt though).
[in,out] | view | Array structure to be initialized. |
[in] | array | The array must not be resized while view is alive. |
[in] | offset | The offset of the viewed section in element units. This offset cannot be changed until the view is reset. |
[in] | length | The length of the view in element units. The view cannot be resized to exceed this length. It is not necessary to call sc_array_reset later. |
int sc_array_is_equal | ( | sc_array_t * | array, |
sc_array_t * | other | ||
) |
Check whether two arrays have equal size, count, and content.
Either array may be a view. Both arrays will not be changed.
[in] | array | One array to be compared. |
[in] | other | A second array to be compared. |
int sc_array_is_permutation | ( | sc_array_t * | array | ) |
Determine whether array is an array of size_t's whose entries include every integer 0 <= i < array->elem_count.
[in] | array | An array. |
int sc_array_is_sorted | ( | sc_array_t * | array, |
int(*)(const void *, const void *) | compar | ||
) |
Check whether the array is sorted wrt.
the comparison function.
[in] | array | The array to check. |
[in] | compar | The comparison function to be used. |
size_t sc_array_memory_used | ( | sc_array_t * | array, |
int | is_dynamic | ||
) |
Calculate the memory used by an array.
[in] | array | The array. |
[in] | is_dynamic | True if created with sc_array_new, false if initialized with sc_array_init |
void sc_array_memset | ( | sc_array_t * | array, |
int | c | ||
) |
Run memset on the array storage.
We pass the character to memset unchanged. Thus, care must be taken when setting values below -1 or above 127, just as with standard memset (3).
[in,out] | array | This array's storage will be overwritten. |
[in] | c | Character to overwrite every byte with. |
void sc_array_move_part | ( | sc_array_t * | dest, |
size_t | dest_offset, | ||
sc_array_t * | src, | ||
size_t | src_offset, | ||
size_t | count | ||
) |
Copy part of one array into another using memmove (3).
Both arrays must have equal element sizes. Either array may be a view. The destination array must be large enough. We use memmove (3): The two arrays may overlap.
[in] | dest | Array will be written into. Its element count must be at least dest_offset + count. |
[in] | dest_offset | First index in dest array to be overwritten. As every index, it refers to elements, not bytes. |
[in] | src | Array will be read from. Its element count must be at least src_offset + count. |
[in] | src_offset | First index in src array to be used. As every index, it refers to elements, not bytes. |
[in] | count | Number of entries copied. |
sc_array_t* sc_array_new | ( | size_t | elem_size | ) |
Creates a new array structure with 0 elements.
[in] | elem_size | Size of one array element in bytes. |
sc_array_t* sc_array_new_count | ( | size_t | elem_size, |
size_t | elem_count | ||
) |
Creates a new array structure with a given length (number of elements).
[in] | elem_size | Size of one array element in bytes. |
[in] | elem_count | Initial number of array elements. |
sc_array_t* sc_array_new_data | ( | void * | base, |
size_t | elem_size, | ||
size_t | elem_count | ||
) |
Creates a new view of an existing plain C array.
[in] | base | The data must not be moved while view is alive. |
[in] | elem_size | Size of one array element in bytes. |
[in] | elem_count | The length of the view in element units. The view cannot be resized to exceed this length. |
sc_array_t* sc_array_new_view | ( | sc_array_t * | array, |
size_t | offset, | ||
size_t | length | ||
) |
Creates a new view of an existing sc_array_t.
[in] | array | The array must not be resized while view is alive. |
[in] | offset | The offset of the viewed section in element units. This offset cannot be changed until the view is reset. |
[in] | length | The length of the viewed section in element units. The view cannot be resized to exceed this length. |
void sc_array_permute | ( | sc_array_t * | array, |
sc_array_t * | newindices, | ||
int | keepperm | ||
) |
Given permutation newindices, permute array in place.
The data that on input is contained in array[i] will be contained in array[newindices[i]] on output. The entries of newindices will be altered unless keepperm is true.
[in,out] | array | An array. |
[in,out] | newindices | Permutation array (see sc_array_is_permutation). |
[in] | keepperm | If true, newindices will be unchanged by the algorithm; if false, newindices will be the identity permutation on output, but the algorithm will only use O(1) space. |
size_t sc_array_pqueue_add | ( | sc_array_t * | array, |
void * | temp, | ||
int(*)(const void *, const void *) | compar | ||
) |
Adds an element to a priority queue.
[in,out] | array | Valid priority queue object. |
[in] | temp | Pointer to unused allocated memory of elem_size. |
[in] | compar | The comparison function to be used. |
size_t sc_array_pqueue_pop | ( | sc_array_t * | array, |
void * | result, | ||
int(*)(const void *, const void *) | compar | ||
) |
Pops the smallest element from a priority queue.
[in,out] | array | Valid priority queue object. |
[out] | result | Pointer to unused allocated memory of elem_size. |
[in] | compar | The comparison function to be used. |
void sc_array_reset | ( | sc_array_t * | array | ) |
Sets the array count to zero and frees all elements.
This function turns a view into a newly initialized array.
[in,out] | array | Array structure to be reset. |
void sc_array_resize | ( | sc_array_t * | array, |
size_t | new_count | ||
) |
Sets the element count to new_count.
If the array is not a view, reallocation takes place occasionally. If the array is a view, new_count must not be greater than the element count of the view when it was created. The original offset of the view cannot be changed.
[in,out] | array | The element count and address is modified. |
[in] | new_count | New element count of the array. If it is zero and the array is not a view, the effect equals sc_array_reset. |
void sc_array_rewind | ( | sc_array_t * | array, |
size_t | new_count | ||
) |
Shorten an array without reallocating it.
[in,out] | array | The element count of this array is modified. |
[in] | new_count | Must be less or equal than the array's count. If it is less, the number of elements in the array is reduced without reallocating memory. The exception is a new_count of zero specified for an array that is not a view: In this case sc_array_reset is equivalent. |
void sc_array_sort | ( | sc_array_t * | array, |
int(*)(const void *, const void *) | compar | ||
) |
Sorts the array in ascending order wrt.
the comparison function.
[in] | array | The array to sort. |
[in] | compar | The comparison function to be used. |
void sc_array_split | ( | sc_array_t * | array, |
sc_array_t * | offsets, | ||
size_t | num_types, | ||
sc_array_type_t | type_fn, | ||
void * | data | ||
) |
Compute the offsets of groups of enumerable types in an array.
[in] | array | Array that is sorted in ascending order by type. If k indexes array, then 0 <= type_fn (array, k, data) < num_types. |
[in,out] | offsets | An initialized array of type size_t that is resized to num_types + 1 entries. The indices j of array that contain objects of type k are offsets[k] <= j < offsets[k + 1]. If there are no objects of type k, then offsets[k] = offset[k + 1]. |
[in] | num_types | The number of possible types of objects in array. |
[in] | type_fn | Returns the type of an object in the array. |
[in] | data | Arbitrary user data passed to type_fn. |
void sc_array_truncate | ( | sc_array_t * | array | ) |
Sets the array count to zero, but does not free elements.
Not allowed for views.
[in,out] | array | Array structure to be truncated. |
void sc_array_uniq | ( | sc_array_t * | array, |
int(*)(const void *, const void *) | compar | ||
) |
Removed duplicate entries from a sorted array.
This function is not allowed for views.
[in,out] | array | The array size will be reduced as necessary. |
[in] | compar | The comparison function to be used. |
void sc_hash_array_destroy | ( | sc_hash_array_t * | hash_array | ) |
Destroy a hash array.
[in,out] | hash_array | Valid hash array is deallocated. |
void sc_hash_array_foreach | ( | sc_hash_array_t * | hash_array, |
sc_hash_foreach_t | fn | ||
) |
Invoke a callback for every member of the hash array.
[in,out] | hash_array | Valid hash array. |
[in] | fn | Callback executed on every hash array element. |
void* sc_hash_array_insert_unique | ( | sc_hash_array_t * | hash_array, |
void * | v, | ||
size_t * | position | ||
) |
Insert an object into a hash array if it is not contained already.
The object is not copied into the array. Use the return value for that. New objects are guaranteed to be added at the end of the array.
[in,out] | hash_array | Valid hash array. |
[in] | v | A pointer to the object. Used for search only. |
[out] | position | If position != NULL, *position is set to the array position of the already contained, or if not present, the new object. |
int sc_hash_array_is_valid | ( | sc_hash_array_t * | hash_array | ) |
Check the internal consistency of a hash array.
[in] | hash_array | Hash array structure is checked for validity. |
int sc_hash_array_lookup | ( | sc_hash_array_t * | hash_array, |
void * | v, | ||
size_t * | position | ||
) |
Check if an object is contained in a hash array.
[in,out] | hash_array | Valid hash array. |
[in] | v | A pointer to the object. |
[out] | position | If position != NULL, *position is set to the array position of the already contained object if found. |
size_t sc_hash_array_memory_used | ( | sc_hash_array_t * | ha | ) |
Calculate the memory used by a hash array.
[in] | ha | The hash array. |
sc_hash_array_t* sc_hash_array_new | ( | size_t | elem_size, |
sc_hash_function_t | hash_fn, | ||
sc_equal_function_t | equal_fn, | ||
void * | user_data | ||
) |
Create a new hash array.
[in] | elem_size | Size of one array element in bytes. |
[in] | hash_fn | Function to compute the hash value. |
[in] | equal_fn | Function to test two objects for equality. |
[in] | user_data | Anonymous context data stored in the hash array. |
void sc_hash_array_rip | ( | sc_hash_array_t * | hash_array, |
sc_array_t * | rip | ||
) |
Extract the array data from a hash array and destroy everything else.
[in] | hash_array | The hash array is destroyed after extraction. |
[in] | rip | Array structure that will be overwritten. All previous array data (if any) will be leaked. The filled array can be freed with sc_array_reset. |
void sc_hash_array_truncate | ( | sc_hash_array_t * | hash_array | ) |
Remove all elements from the hash array.
[in,out] | hash_array | Hash array to truncate. |
void sc_hash_destroy | ( | sc_hash_t * | hash | ) |
Destroy a hash table.
If the allocator is owned, this runs in O(1), otherwise in O(N).
void sc_hash_destroy_null | ( | sc_hash_t ** | phash | ) |
Destroy a hash table and set its pointer to NULL.
Destruction is done using sc_hash_destroy.
[in,out] | phash | Address of pointer to hash table. On output, pointer is NULLed. |
void sc_hash_foreach | ( | sc_hash_t * | hash, |
sc_hash_foreach_t | fn | ||
) |
Invoke a callback for every member of the hash table.
The hashing and equality functions are not called from within this function.
[in,out] | hash | Valid hash table. |
[in] | fn | Callback executed on every hash table element. |
unsigned int sc_hash_function_string | ( | const void * | s, |
const void * | u | ||
) |
Compute a hash value from a null-terminated string.
This hash function is NOT cryptographically safe! Use libcrypt then.
[in] | s | Null-terminated string to be hashed. |
[in] | u | Not used. |
int sc_hash_insert_unique | ( | sc_hash_t * | hash, |
void * | v, | ||
void *** | found | ||
) |
Insert an object into a hash table if it is not contained already.
[in,out] | hash | Valid hash table. |
[in] | v | The object to be inserted. |
[out] | found | If found != NULL, *found is set to the address of the pointer to the already contained, or if not present, the new object. You can assign to **found to override. |
int sc_hash_lookup | ( | sc_hash_t * | hash, |
void * | v, | ||
void *** | found | ||
) |
Check if an object is contained in the hash table.
[in] | hash | Valid hash table. |
[in] | v | The object to be looked up. |
[out] | found | If found != NULL, *found is set to the address of the pointer to the already contained object if the object is found. You can assign to **found to override. |
size_t sc_hash_memory_used | ( | sc_hash_t * | hash | ) |
Calculate the memory used by a hash table.
[in] | hash | The hash table. |
sc_hash_t* sc_hash_new | ( | sc_hash_function_t | hash_fn, |
sc_equal_function_t | equal_fn, | ||
void * | user_data, | ||
sc_mempool_t * | allocator | ||
) |
Create a new hash table.
The number of hash slots is chosen dynamically.
[in] | hash_fn | Function to compute the hash value. |
[in] | equal_fn | Function to test two objects for equality. |
[in] | user_data | User data passed through to the hash function. |
[in] | allocator | Memory allocator for sc_link_t, can be NULL. |
void sc_hash_print_statistics | ( | int | package_id, |
int | log_priority, | ||
sc_hash_t * | hash | ||
) |
Compute and print statistical information about the occupancy.
[in] | package_id | Library package id for logging. |
[in] | log_priority | Priority for logging; see sc_log. |
[in] | hash | Valid hash table. |
int sc_hash_remove | ( | sc_hash_t * | hash, |
void * | v, | ||
void ** | found | ||
) |
Remove an object from a hash table.
[in,out] | hash | Valid hash table. |
[in] | v | The object to be removed. |
[out] | found | If found != NULL, *found is set to the object that is removed if that exists. |
void sc_hash_truncate | ( | sc_hash_t * | hash | ) |
Remove all entries from a hash table in O(N).
If the allocator is owned, it calls sc_hash_unlink and sc_mempool_truncate. Otherwise, it calls sc_list_reset on every hash slot which is slower.
void sc_hash_unlink | ( | sc_hash_t * | hash | ) |
Unlink all hash elements without returning them to the mempool.
If the allocator is not owned, this runs faster than sc_hash_truncate, but is dangerous because of potential memory leaks.
[in,out] | hash | Hash structure to be unlinked. |
void sc_hash_unlink_destroy | ( | sc_hash_t * | hash | ) |
Same effect as unlink and destroy, but in O(1).
This is dangerous because of potential memory leaks.
[in] | hash | Hash structure to be unlinked and destroyed. |
Insert a list element at the end of the list.
[in,out] | list | Valid list object. |
[in] | data | A new link is created holding this data. |
void sc_list_destroy | ( | sc_list_t * | list | ) |
Destroy a linked list structure in O(N).
[in,out] | list | All memory allocated for this list is freed. |
void sc_list_init | ( | sc_list_t * | list, |
sc_mempool_t * | allocator | ||
) |
Initialize a list object with an external link allocator.
[in,out] | list | List structure to be initialized. |
[in] | allocator | External memory allocator for sc_link_t, which must exist already. |
Insert an element after a given list position.
[in,out] | list | Valid list object. |
[in,out] | pred | The predecessor of the element to be inserted. |
[in] | data | A new link is created holding this data. |
size_t sc_list_memory_used | ( | sc_list_t * | list, |
int | is_dynamic | ||
) |
Calculate the total memory used by a list.
[in] | list | The list. |
[in] | is_dynamic | True if created with sc_list_new, false if initialized with sc_list_init |
sc_list_t* sc_list_new | ( | sc_mempool_t * | allocator | ) |
Allocate a new, empty linked list.
[in] | allocator | Memory allocator for sc_link_t, can be NULL in which case an internal allocator is created. |
void* sc_list_pop | ( | sc_list_t * | list | ) |
Remove an element from the front of the list.
[in,out] | list | Valid, non-empty list object. |
Insert a list element at the beginning of the list.
[in,out] | list | Valid list object. |
[in] | data | A new link is created holding this data. |
Remove an element after a given list position.
[in,out] | list | Valid, non-empty list object. |
[in] | pred | The predecessor of the element to be removed. If pred == NULL, the first element is removed, which is equivalent to calling sc_list_pop (list). |
void sc_list_reset | ( | sc_list_t * | list | ) |
Remove all elements from a list in O(N).
[in,out] | list | List structure to be emptied. |
void sc_list_unlink | ( | sc_list_t * | list | ) |
Unlink all list elements without returning them to the mempool.
This runs in O(1) but is dangerous because the link memory stays alive.
[in,out] | list | List structure to be unlinked. |
void sc_mempool_destroy | ( | sc_mempool_t * | mempool | ) |
Destroy a mempool structure.
All elements that are still in use are invalidated.
[in,out] | mempool | Its memory is freed. |
void sc_mempool_destroy_null | ( | sc_mempool_t ** | pmempool | ) |
Destroy a mempool structure.
All elements that are still in use are invalidated.
[in,out] | pmempool | Address of pointer to memory pool. Its memory is freed, pointer is NULLed. |
void sc_mempool_init | ( | sc_mempool_t * | mempool, |
size_t | elem_size | ||
) |
Same as sc_mempool_new, but for an already allocated object.
[out] | mempool | Allocated memory is overwritten and initialized. |
[in] | elem_size | Size of one element in bytes. |
size_t sc_mempool_memory_used | ( | sc_mempool_t * | mempool | ) |
Calculate the memory used by a memory pool.
[in] | mempool | The memory pool. |
sc_mempool_t* sc_mempool_new | ( | size_t | elem_size | ) |
Creates a new mempool structure with the zero_and_persist option off.
The contents of any elements returned by sc_mempool_alloc are undefined.
[in] | elem_size | Size of one element in bytes. |
sc_mempool_t* sc_mempool_new_zero_and_persist | ( | size_t | elem_size | ) |
Creates a new mempool structure with the zero_and_persist option on.
The memory of newly created elements is zero'd out, and the contents of an element are not touched between freeing and re-returning it.
[in] | elem_size | Size of one element in bytes. |
void sc_mempool_reset | ( | sc_mempool_t * | mempool | ) |
Same as sc_mempool_destroy, but does not free the pointer.
[in,out] | mempool | Valid mempool object is deallocated. The structure memory itself stays alive. |
void sc_mempool_truncate | ( | sc_mempool_t * | mempool | ) |
Invalidates all previously returned pointers, resets count to 0.
[in,out] | mempool | Valid mempool is truncated. |
void* sc_mstamp_alloc | ( | sc_mstamp_t * | mst | ) |
Return a new item.
The memory returned will stay legal until container is destroyed or truncated.
[in,out] | mst | Properly initialized stamp container. |
void sc_mstamp_init | ( | sc_mstamp_t * | mst, |
size_t | stamp_unit, | ||
size_t | elem_size | ||
) |
Initialize a memory stamp container.
We provide allocation of fixed-size memory items without allocating new memory in every request. Instead we block the allocations in what we call a stamp of multiple items. Even if no allocations are done, the container's internal memory must be freed eventually by sc_mstamp_reset.
[in,out] | mst | Legal pointer to a stamp structure. |
[in] | stamp_unit | Size of each memory block that we allocate. If it is larger than the element size, we may place more than one element in it. Passing 0 is legal and forces stamps that hold one item each. |
[in] | elem_size | Size of each item. Passing 0 is legal. In that case, sc_mstamp_alloc returns NULL. |
size_t sc_mstamp_memory_used | ( | sc_mstamp_t * | mst | ) |
Return memory size in bytes of all data allocated in the container.
[in] | mst | Properly initialized stamp container. |
void sc_mstamp_reset | ( | sc_mstamp_t * | mst | ) |
Free all memory in a stamp structure and all items previously returned.
[in,out] | mst | Properly initialized stamp container. On output, the structure is undefined. |
void sc_mstamp_truncate | ( | sc_mstamp_t * | mst | ) |
Free all memory in a stamp structure and initialize it anew.
Equivalent to calling sc_mstamp_reset followed by sc_mstamp_init with the same stamp_unit and elem_size.
[in,out] | mst | Properly initialized stamp container. On output, its elements have been freed and it is ready for further use. |
void sc_recycle_array_init | ( | sc_recycle_array_t * | rec_array, |
size_t | elem_size | ||
) |
Initialize a recycle array.
[out] | rec_array | Uninitialized turned into a recycle array. |
[in] | elem_size | Size of the objects to be stored in the array. |
void* sc_recycle_array_insert | ( | sc_recycle_array_t * | rec_array, |
size_t * | position | ||
) |
Insert an object into the recycle array.
The object is not copied into the array. Use the return value for that.
[in,out] | rec_array | Valid recycle array. |
[out] | position | If position != NULL, *position is set to the array position of the inserted object. |
void* sc_recycle_array_remove | ( | sc_recycle_array_t * | rec_array, |
size_t | position | ||
) |
Remove an object from the recycle array.
It must be valid.
[in,out] | rec_array | Valid recycle array. |
[in] | position | Index into the array for the object to remove. |
void sc_recycle_array_reset | ( | sc_recycle_array_t * | rec_array | ) |
Reset a recycle array.
As with all _reset functions, calling _init, then any array operations, then _reset is memory neutral.