24#ifndef SC_CONTAINERS_H
25#define SC_CONTAINERS_H
56#define sc_hash_rot(x,k) (((x) << (k)) | ((x) >> (32 - (k))))
63#define sc_hash_mix(a,b,c) ((void) \
64 (a -= c, a ^= sc_hash_rot(c, 4), c += b, \
65 b -= a, b ^= sc_hash_rot(a, 6), a += c, \
66 c -= b, c ^= sc_hash_rot(b, 8), b += a, \
67 a -= c, a ^= sc_hash_rot(c,16), c += b, \
68 b -= a, b ^= sc_hash_rot(a,19), a += c, \
69 c -= b, c ^= sc_hash_rot(b, 4), b += a))
76#define sc_hash_final(a,b,c) ((void) \
77 (c ^= b, c -= sc_hash_rot(b,14), \
78 a ^= c, a -= sc_hash_rot(c,11), \
79 b ^= a, b -= sc_hash_rot(a,25), \
80 c ^= b, c -= sc_hash_rot(b,16), \
81 a ^= c, a -= sc_hash_rot(c, 4), \
82 b ^= a, b -= sc_hash_rot(a,14), \
83 c ^= b, c -= sc_hash_rot(b,24)))
99 const void *v2,
const void *u);
134#define SC_ARRAY_IS_OWNER(a) ((a)->byte_alloc >= 0)
137#define SC_ARRAY_BYTE_ALLOC(a) ((size_t) \
138 (SC_ARRAY_IS_OWNER (a) ? (a)->byte_alloc : -((a)->byte_alloc + 1)))
163#define sc_array_new_size(s,c) (sc_array_new_count ((s), (c)))
173 size_t offset,
size_t length);
182 size_t elem_size,
size_t elem_count);
209 size_t elem_size,
size_t elem_count);
219 size_t elem_size,
size_t elem_count);
232 size_t offset,
size_t length);
261 size_t elem_size,
size_t elem_count);
359 int (*compar) (
const void *,
368 int (*compar) (
const void *,
386 int (*compar) (
const void *,
397 int (*compar) (
const void *,
406 size_t index,
void *data);
470 size_t pos,
void *newval,
471 int (*compar) (
const void *,
486 int (*compar) (
const void *,
506 size_t pos,
void *newval,
507 int (*compar) (
const void *,
523 int (*compar) (
const void *,
534 SC_ASSERT (iz < array->elem_count);
548 SC_ASSERT (iz <= array->elem_count);
561 SC_ASSERT (i >= 0 && (
size_t) i < array->elem_count);
573 SC_ASSERT (l >= 0 && (
size_t) l < array->elem_count);
585 SC_ASSERT (is >= 0 && (
size_t) is < array->elem_count);
587 return (
void *) (array->
array + (array->
elem_size * (size_t) is));
597 SC_ASSERT (i16 >= 0 && (
size_t) i16 < array->elem_count);
599 return (
void *) (array->
array + (array->
elem_size * (size_t) i16));
611 SC_ASSERT (array->
array <= (
char *) element);
612 SC_ASSERT (((
char *) element - array->
array) %
615 position = ((
char *) element - array->
array) / (ptrdiff_t) array->
elem_size;
616 SC_ASSERT (0 <= position && position < (ptrdiff_t) array->
elem_count);
618 return (
size_t) position;
643 const size_t new_count = old_count + add_count;
702 size_t stamp_unit,
size_t elem_size);
833#ifdef SC_ENABLE_DEBUG
853#ifdef SC_ENABLE_DEBUG
1154 void *v,
size_t *position);
1169 void *v,
size_t *position);
Support for process management (memory allocation, logging, etc.)
size_t sc_array_pqueue_pop(sc_array_t *array, void *newval, int(*compar)(const void *, const void *))
Pop the largest element from a priority queue.
void * sc_array_index_null(sc_array_t *array, size_t iz)
Returns a pointer to an array element or NULL at the array's end.
Definition sc_containers.h:546
void sc_array_permute(sc_array_t *array, sc_array_t *newindices, int keepperm)
Given permutation newindices, permute array in place.
void * sc_array_index_ssize_t(sc_array_t *array, ssize_t is)
Returns a pointer to an array element indexed by a ssize_t.
Definition sc_containers.h:583
void * sc_array_index_long(sc_array_t *array, long l)
Returns a pointer to an array element indexed by a plain long.
Definition sc_containers.h:571
void * sc_mempool_alloc(sc_mempool_t *mempool)
Allocate a single element.
Definition sc_containers.h:816
void sc_mempool_free(sc_mempool_t *mempool, void *elem)
Return a previously allocated element to the pool.
Definition sc_containers.h:847
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:90
struct sc_hash_array_data sc_hash_array_data_t
Internal context structure for sc_hash_array.
Definition sc_containers.h:1094
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 or hash array.
Definition sc_containers.h:106
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.
void * sc_array_push_count(sc_array_t *array, size_t add_count)
Enlarge an array by a number of elements.
Definition sc_containers.h:640
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_hash_array_foreach(sc_hash_array_t *hash_array, sc_hash_foreach_t fn)
Invoke a callback for every member of the hash array.
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:405
struct sc_hash sc_hash_t
The sc_hash implements a hash table.
size_t sc_array_position(sc_array_t *array, void *element)
Return the index of an object in an array identified by a pointer.
Definition sc_containers.h:607
size_t sc_array_pqueue_siftup(sc_array_t *array, size_t pos, void *newval, int(*compar)(const void *, const void *))
Replace an element in a priority queue with another and sift up.
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.
void * sc_array_index(sc_array_t *array, size_t iz)
Returns a pointer to an array element.
Definition sc_containers.h:532
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_index_int(sc_array_t *array, int i)
Returns a pointer to an array element indexed by a plain int.
Definition sc_containers.h:559
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_index_int16(sc_array_t *array, int16_t i16)
Returns a pointer to an array element indexed by a int16_t.
Definition sc_containers.h:595
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 object.
void sc_array_memset(sc_array_t *array, int c)
Run memset on the array storage.
size_t sc_array_pqueue_siftdown(sc_array_t *array, size_t maxcount, size_t pos, void *newval, int(*compar)(const void *, const void *))
Replace an element in a priority queue with another and sift down.
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:134
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).
size_t sc_array_pqueue_insert(sc_array_t *array, void *newval, int(*compar)(const void *, const void *))
Add an element to a priority queue.
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.
void * sc_array_push(sc_array_t *array)
Enlarge an array by one element.
Definition sc_containers.h:662
void * sc_array_pop(sc_array_t *array)
Remove the last element from an array and return a pointer to it.
Definition sc_containers.h:627
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:98
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:118
size_t elem_count
number of valid elements
Definition sc_containers.h:121
size_t elem_size
size of a single element
Definition sc_containers.h:120
char * array
linear array to store elements
Definition sc_containers.h:129
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:124
The sc_hash_array implements an array backed up by a hash table.
Definition sc_containers.h:1100
sc_hash_array_data_t * internal_data
Private context data.
Definition sc_containers.h:1107
sc_hash_t * h
Hash map pointing into element array.
Definition sc_containers.h:1106
void * user_data
Context passed by the user.
Definition sc_containers.h:1102
sc_array_t a
Array storing the elements.
Definition sc_containers.h:1105
The sc_hash implements a hash table.
Definition sc_containers.h:971
size_t elem_count
total number of objects contained
Definition sc_containers.h:973
size_t resize_checks
Running count of resize checks.
Definition sc_containers.h:980
sc_mempool_t * allocator
Must allocate sc_link_t objects.
Definition sc_containers.h:983
int allocator_owned
Boolean designating allocator ownership.
Definition sc_containers.h:982
sc_array_t * slots
The slot count is slots->elem_count.
Definition sc_containers.h:977
sc_hash_function_t hash_fn
Function called to compute the hash value.
Definition sc_containers.h:978
void * user_data
User data passed to hash function.
Definition sc_containers.h:974
sc_equal_function_t equal_fn
Function called to check objects for equality.
Definition sc_containers.h:979
size_t resize_actions
Running count of resize actions.
Definition sc_containers.h:981
The sc_link structure is one link of a linked list.
Definition sc_containers.h:867
struct sc_link * next
Pointer to list successor element.
Definition sc_containers.h:869
void * data
Arbitrary payload.
Definition sc_containers.h:868
The sc_list object provides a linked list.
Definition sc_containers.h:876
sc_link_t * first
Pointer to first element in list.
Definition sc_containers.h:879
int allocator_owned
Boolean to designate owned allocator.
Definition sc_containers.h:883
size_t elem_count
Number of elements in this list.
Definition sc_containers.h:878
sc_link_t * last
Pointer to last element in list.
Definition sc_containers.h:880
sc_mempool_t * allocator
Must allocate objects of sc_link_t.
Definition sc_containers.h:884
The sc_mempool object provides a large pool of equal-size elements.
Definition sc_containers.h:747
sc_array_t freed
buffers the freed elements
Definition sc_containers.h:755
sc_mstamp_t mstamp
fixed-size chunk allocator
Definition sc_containers.h:754
size_t elem_count
number of valid elements
Definition sc_containers.h:750
size_t elem_size
size of a single element
Definition sc_containers.h:749
int zero_and_persist
Boolean; is set in constructor.
Definition sc_containers.h:751
A data container to create memory items of the same size.
Definition sc_containers.h:674
size_t cur_snext
Next number within a stamp.
Definition sc_containers.h:678
size_t stamp_size
Bytes allocated in a stamp.
Definition sc_containers.h:677
sc_array_t remember
Collects all stamps.
Definition sc_containers.h:680
size_t per_stamp
Number of items per stamp.
Definition sc_containers.h:676
char * current
Memory of current stamp.
Definition sc_containers.h:679
size_t elem_size
Input parameter: size per item.
Definition sc_containers.h:675
The sc_recycle_array object provides an array of slots that can be reused.
Definition sc_containers.h:1193
sc_array_t f
Cache of freed objects.
Definition sc_containers.h:1199
sc_array_t a
Array of objects contained.
Definition sc_containers.h:1198
size_t elem_count
Number of valid entries.
Definition sc_containers.h:1195