24#ifndef SC_CONTAINERS_H
25#define SC_CONTAINERS_H
44#define sc_hash_rot(x,k) (((x) << (k)) | ((x) >> (32 - (k))))
45#define sc_hash_mix(a,b,c) ((void) \
46 (a -= c, a ^= sc_hash_rot(c, 4), c += b, \
47 b -= a, b ^= sc_hash_rot(a, 6), a += c, \
48 c -= b, c ^= sc_hash_rot(b, 8), b += a, \
49 a -= c, a ^= sc_hash_rot(c,16), c += b, \
50 b -= a, b ^= sc_hash_rot(a,19), a += c, \
51 c -= b, c ^= sc_hash_rot(b, 4), b += a))
52#define sc_hash_final(a,b,c) ((void) \
53 (c ^= b, c -= sc_hash_rot(b,14), \
54 a ^= c, a -= sc_hash_rot(c,11), \
55 b ^= a, b -= sc_hash_rot(a,25), \
56 c ^= b, c -= sc_hash_rot(b,16), \
57 a ^= c, a -= sc_hash_rot(c, 4), \
58 b ^= a, b -= sc_hash_rot(a,14), \
59 c ^= b, c -= sc_hash_rot(b,24)))
73 const void *v2,
const void *u);
107#define SC_ARRAY_IS_OWNER(a) ((a)->byte_alloc >= 0)
109#define SC_ARRAY_BYTE_ALLOC(a) ((size_t) \
110 (SC_ARRAY_IS_OWNER (a) ? (a)->byte_alloc : -((a)->byte_alloc + 1)))
135#define sc_array_new_size(s,c) (sc_array_new_count ((s), (c)))
145 size_t offset,
size_t length);
154 size_t elem_size,
size_t elem_count);
181 size_t elem_size,
size_t elem_count);
191 size_t elem_size,
size_t elem_count);
204 size_t offset,
size_t length);
233 size_t elem_size,
size_t elem_count);
331 int (*compar) (
const void *,
340 int (*compar) (
const void *,
358 int (*compar) (
const void *,
369 int (*compar) (
const void *,
378 size_t index,
void *data);
443 int (*compar) (
const void *,
457 int (*compar) (
const void *,
469 SC_ASSERT (iz < array->elem_count);
482sc_array_index_null (
sc_array_t * array,
size_t iz)
484 SC_ASSERT (iz <= array->elem_count);
497 SC_ASSERT (i >= 0 && (
size_t) i < array->elem_count);
507sc_array_index_long (
sc_array_t * array,
long l)
509 SC_ASSERT (l >= 0 && (
size_t) l < array->elem_count);
519sc_array_index_ssize_t (
sc_array_t * array, ssize_t is)
521 SC_ASSERT (is >= 0 && (
size_t) is < array->elem_count);
523 return (
void *) (array->
array + (array->
elem_size * (size_t) is));
531sc_array_index_int16 (
sc_array_t * array, int16_t i16)
533 SC_ASSERT (i16 >= 0 && (
size_t) i16 < array->elem_count);
535 return (
void *) (array->
array + (array->
elem_size * (size_t) i16));
543sc_array_position (
sc_array_t * array,
void *element)
547 SC_ASSERT (array->
array <= (
char *) element);
548 SC_ASSERT (((
char *) element - array->
array) %
551 position = ((
char *) element - array->
array) / (ptrdiff_t) array->
elem_size;
552 SC_ASSERT (0 <= position && position < (ptrdiff_t) array->
elem_count);
554 return (
size_t) position;
578sc_array_push_count (
sc_array_t * array,
size_t add_count)
581 const size_t new_count = old_count + add_count;
603 return sc_array_push_count (array, 1);
641 size_t stamp_unit,
size_t elem_size);
757 ret = *(
void **) sc_array_pop (freed);
766#ifdef SC_ENABLE_DEBUG
786#ifdef SC_ENABLE_DEBUG
794 *(
void **) sc_array_push (freed) = elem;
913 size_t resize_checks, resize_actions;
1077 void *v,
size_t *position);
1091 void *v,
size_t *position);
Support for process management (memory allocation, logging, etc.)
void sc_array_permute(sc_array_t *array, sc_array_t *newindices, int keepperm)
Given permutation newindices, permute array in place.
unsigned int(* sc_hash_function_t)(const void *v, const void *u)
Function to compute a hash value of an object.
Definition: sc_containers.h:66
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.
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.
struct sc_list sc_list_t
The sc_list object provides a linked list.
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.
sc_link_t * sc_list_insert(sc_list_t *list, sc_link_t *pred, void *data)
Insert an element after a given list position.
struct sc_recycle_array sc_recycle_array_t
The sc_recycle_array object provides an array of slots that can be reused.
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.
void sc_mempool_reset(sc_mempool_t *mempool)
Same as sc_mempool_destroy, but does not free the pointer.
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.
void sc_list_init(sc_list_t *list, sc_mempool_t *allocator)
Initialize a list object with an external link allocator.
void sc_array_init(sc_array_t *array, size_t elem_size)
Initializes an already allocated (or static) array structure.
size_t sc_hash_array_memory_used(sc_hash_array_t *ha)
Calculate the memory used by a hash array.
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.
void sc_recycle_array_reset(sc_recycle_array_t *rec_array)
Reset a recycle array.
sc_mempool_t * sc_mempool_new(size_t elem_size)
Creates a new mempool structure with the zero_and_persist option off.
void sc_array_rewind(sc_array_t *array, size_t new_count)
Shorten an array without reallocating it.
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.
int sc_hash_remove(sc_hash_t *hash, void *v, void **found)
Remove an object from a hash table.
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...
int(* sc_hash_foreach_t)(void **v, const void *u)
Function to call on every data item of a hash table.
Definition: sc_containers.h:80
void sc_array_copy(sc_array_t *dest, sc_array_t *src)
Copy the contents of one array into another.
void * sc_list_pop(sc_list_t *list)
Remove an element from the front of the list.
void sc_mstamp_reset(sc_mstamp_t *mst)
Free all memory in a stamp structure and all items previously returned.
size_t sc_mstamp_memory_used(sc_mstamp_t *mst)
Return memory size in bytes of all data allocated in the container.
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.
void sc_hash_destroy(sc_hash_t *hash)
Destroy a hash table.
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).
void sc_mempool_destroy(sc_mempool_t *mempool)
Destroy a mempool structure.
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.
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.
void sc_hash_destroy_null(sc_hash_t **phash)
Destroy a hash table and set its pointer to NULL.
void sc_array_resize(sc_array_t *array, size_t new_count)
Sets the element count to new_count.
void sc_list_unlink(sc_list_t *list)
Unlink all list elements without returning them to the mempool.
unsigned int sc_hash_function_string(const void *s, const void *u)
Compute a hash value from a null-terminated string.
void * sc_recycle_array_insert(sc_recycle_array_t *rec_array, size_t *position)
Insert an object into the recycle array.
void sc_hash_truncate(sc_hash_t *hash)
Remove all entries from a hash table in O(N).
size_t sc_hash_memory_used(sc_hash_t *hash)
Calculate the memory used by a hash table.
struct sc_array sc_array_t
The sc_array object provides a dynamic array of equal-size elements.
struct sc_hash_array sc_hash_array_t
The sc_hash_array implements an array backed up by a hash table.
void sc_array_destroy_null(sc_array_t **parray)
Destroys an array structure and sets the pointer to NULL.
void sc_array_reset(sc_array_t *array)
Sets the array count to zero and frees all elements.
size_t sc_mempool_memory_used(sc_mempool_t *mempool)
Calculate the memory used by a memory pool.
void * sc_recycle_array_remove(sc_recycle_array_t *rec_array, size_t position)
Remove an object from the recycle array.
void * sc_mstamp_alloc(sc_mstamp_t *mst)
Return a new item.
void sc_array_sort(sc_array_t *array, int(*compar)(const void *, const void *))
Sorts the array in ascending order wrt.
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.
Definition: sc_containers.h:377
struct sc_hash sc_hash_t
The sc_hash implements a hash table.
void sc_array_uniq(sc_array_t *array, int(*compar)(const void *, const void *))
Removed duplicate entries from a sorted array.
void sc_mempool_destroy_null(sc_mempool_t **pmempool)
Destroy a mempool structure.
void sc_hash_array_truncate(sc_hash_array_t *hash_array)
Remove all elements from the hash array.
struct sc_link sc_link_t
The sc_link structure is one link of a linked list.
sc_list_t * sc_list_new(sc_mempool_t *allocator)
Allocate a new, empty linked list.
void sc_array_destroy(sc_array_t *array)
Destroys an array structure.
void sc_recycle_array_init(sc_recycle_array_t *rec_array, size_t elem_size)
Initialize a recycle array.
size_t sc_array_memory_used(sc_array_t *array, int is_dynamic)
Calculate the memory used by an array.
struct sc_mempool sc_mempool_t
The sc_mempool object provides a large pool of equal-size elements.
void sc_mstamp_truncate(sc_mstamp_t *mst)
Free all memory in a stamp structure and initialize it anew.
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.
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.
void sc_mempool_init(sc_mempool_t *mempool, size_t elem_size)
Same as sc_mempool_new, but for an already allocated sc_mempool_t pointer.
void sc_array_memset(sc_array_t *array, int c)
Run memset on the array storage.
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.
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.
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...
void sc_hash_unlink(sc_hash_t *hash)
Unlink all hash elements without returning them to the mempool.
sc_array_t * sc_array_new(size_t elem_size)
Creates a new array structure with 0 elements.
void sc_mempool_truncate(sc_mempool_t *mempool)
Invalidates all previously returned pointers, resets count to 0.
void sc_hash_unlink_destroy(sc_hash_t *hash)
Same effect as unlink and destroy, but in O(1).
void sc_list_reset(sc_list_t *list)
Remove all elements from a list in O(N).
int sc_array_is_sorted(sc_array_t *array, int(*compar)(const void *, const void *))
Check whether the array is sorted wrt.
struct sc_mstamp sc_mstamp_t
A data container to create memory items of the same size.
void sc_list_destroy(sc_list_t *list)
Destroy a linked list structure in O(N).
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->e...
size_t sc_list_memory_used(sc_list_t *list, int is_dynamic)
Calculate the total memory used by a list.
void sc_hash_array_destroy(sc_hash_array_t *hash_array)
Destroy a hash array.
int sc_hash_array_is_valid(sc_hash_array_t *hash_array)
Check the internal consistency of a hash array.
int sc_hash_lookup(sc_hash_t *hash, void *v, void ***found)
Check if an object is contained in the hash table.
sc_link_t * sc_list_prepend(sc_list_t *list, void *data)
Insert a list element at the beginning of the list.
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.
void sc_mstamp_init(sc_mstamp_t *mst, size_t stamp_unit, size_t elem_size)
Initialize a memory stamp container.
void sc_hash_foreach(sc_hash_t *hash, sc_hash_foreach_t fn)
Invoke a callback for every member of the hash table.
void sc_array_truncate(sc_array_t *array)
Sets the array count to zero, but does not free elements.
sc_link_t * sc_list_append(sc_list_t *list, void *data)
Insert a list element at the end of the list.
#define SC_ARRAY_IS_OWNER(a)
test whether the sc_array_t owns its array
Definition: sc_containers.h:107
void * sc_list_remove(sc_list_t *list, sc_link_t *pred)
Remove an element after a given list position.
int sc_array_is_equal(sc_array_t *array, sc_array_t *other)
Check whether two arrays have equal size, count, and content.
void sc_hash_print_statistics(int package_id, int log_priority, sc_hash_t *hash)
Compute and print statistical information about the occupancy.
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).
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.
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.
int(* sc_equal_function_t)(const void *v1, const void *v2, const void *u)
Function to check equality of two objects.
Definition: sc_containers.h:72
unsigned int sc_array_checksum(sc_array_t *array)
Computes the adler32 checksum of array data (see zlib documentation).
The sc_array object provides a dynamic array of equal-size elements.
Definition: sc_containers.h:91
size_t elem_count
number of valid elements
Definition: sc_containers.h:94
size_t elem_size
size of a single element
Definition: sc_containers.h:93
char * array
linear array to store elements
Definition: sc_containers.h:102
ssize_t byte_alloc
number of allocated bytes or -(number of viewed bytes + 1) if this is a view: the "+ 1" distinguishes...
Definition: sc_containers.h:97
Definition: sc_containers.h:1018
The sc_hash_array implements an array backed up by a hash table.
Definition: sc_containers.h:1031
The sc_hash implements a hash table.
Definition: sc_containers.h:904
size_t elem_count
total number of objects contained
Definition: sc_containers.h:906
sc_mempool_t * allocator
must allocate sc_link_t
Definition: sc_containers.h:915
sc_array_t * slots
the slot count is slots->elem_count
Definition: sc_containers.h:909
void * user_data
user data passed to hash function
Definition: sc_containers.h:910
The sc_link structure is one link of a linked list.
Definition: sc_containers.h:800
The sc_list object provides a linked list.
Definition: sc_containers.h:809
The sc_mempool object provides a large pool of equal-size elements.
Definition: sc_containers.h:686
sc_array_t freed
buffers the freed elements
Definition: sc_containers.h:694
sc_mstamp_t mstamp
fixed-size chunk allocator
Definition: sc_containers.h:693
size_t elem_count
number of valid elements
Definition: sc_containers.h:689
size_t elem_size
size of a single element
Definition: sc_containers.h:688
int zero_and_persist
Boolean; is set in constructor.
Definition: sc_containers.h:690
A data container to create memory items of the same size.
Definition: sc_containers.h:613
size_t cur_snext
Next number within a stamp.
Definition: sc_containers.h:617
size_t stamp_size
Bytes allocated in a stamp.
Definition: sc_containers.h:616
sc_array_t remember
Collects all stamps.
Definition: sc_containers.h:619
size_t per_stamp
Number of items per stamp.
Definition: sc_containers.h:615
char * current
Memory of current stamp.
Definition: sc_containers.h:618
size_t elem_size
Input parameter: size per item.
Definition: sc_containers.h:614
The sc_recycle_array object provides an array of slots that can be reused.
Definition: sc_containers.h:1108