5 Data structure where all elements (members) have the same size and are
6 located consecutively in memory.
10 #ifndef _DRAGONSTD_ARRAY_H_ // include guard
11 #define _DRAGONSTD_ARRAY_H_
13 #include <stddef.h> // for size_t
14 #include <sys/types.h> // for ssize_t
15 #include "bits/callback.h"
20 size_t ext; // extra space
22 size_t mbs; // member size
23 size_t siz; // used space
24 size_t cap; // available space
27 void array_ini(Array *array, size_t mmb, size_t ext);
29 Initializes the array.
31 The array will have the member size of mmb and the extra space set to ext.
32 mmb should be bigger than 0 and may not be changed after the initialization.
33 ext can be 0 or bigger and may be changed any time.
35 This function should be called before calling any other functions on the
38 This function should not be called on an array that has been initialized before,
39 unless the array has a capacity of 0. Otherwise a memory leak will occur.
42 void array_rlc(Array *array);
44 Reallocates the array's memory to match it's capacity.
46 This function should be called every time the capacity has changed.
49 void array_grw(Array *array, size_t n);
51 Grows the array by n bytes.
53 This function increases the arrays size by n bytes. If this exceeds the capacity of
54 the array, the capacity set to the size and the extra space ext is added to it.
56 If the capacity is changed, the array is reallocated.
58 If n is zero, the array's capacity may still grow by extra space if it exactly
59 matches the current size.
62 void array_shr(Array *array, size_t n);
64 Shrinks the array by n bytes.
66 This function decreases the arrays size by n bytes.
68 If the array has additional capacity left after it has been shrunk, the capacity
69 is set to the new size and the array is reallocated to fit the new capacity.
70 For n > 0, this is always the case, for n = 0, this may be the case.
72 Note that calling this function with n = 0 is useful to shrink the array's memory to
73 exactly fit it's used size.
76 void array_put(Array *array, const void *ptr, size_t n);
78 Grows the array by 1 and inserts ptr at the index n.
80 This function inserts the memory pointed to by ptr before the array member at
81 index n, moving all elements from that index to the end of the array.
83 After this operation, the inserted element will be _at_ the index n.
85 The memory that ptr points to, which's size is assumed to be at least as big as the
86 array's member size is copied into the arrays memory.
88 n should be in the range from 0 to the array's size.
91 void array_add(Array *array, const void *ptr);
93 Grows the array by 1 and appends ptr at the end of the array.
95 This function's result is equivalent to calling array_put(array, ptr, array->siz),
96 but it is slightly faster since it saves unnecessary calls.
99 void array_cpy(Array *array, void **ptr, size_t *n);
101 Allocates a buffer big enough to fit the array's used size.
102 Copies the array's contents into the allocated buffer.
103 Returns the buffer in ptr and the size in n.
105 Note that the returned size is the number of elements, not the number of bytes.
108 void array_cln(Array *dst, Array *src);
110 Clones the array src to the array dst.
112 dst is initialized to have the same configuration (member size, extra space) as src.
114 After the operation, the contents of the array dst are be the same as those of src.
115 The size of dst and src are the same, the capacity of dst however is the same as
116 the size of dst and src (which might not equal the capacity of src).
118 Since array_ini is called on dst, it should be uninitialized, empty or deleted.
121 void array_rcy(Array *array);
125 This function sets the used size of the array to 0 but leaves the capacity unchanged.
126 The array's memory is not free'd and the array can be reused.
129 void array_clr(Array *array);
133 This function frees the arrays memory. If this is not called when the array's
134 reference is dropped, a memory leak occurs, unless the array is empty (capacity
135 of 0), in which case the function does not need to be called. The function works
136 fine on empty arrays however.
138 After this, the array is empty and can be reused.
141 void array_srt(Array *array, Comparator cmp);
143 Sorts the array using the quicksort algorithm.
145 Comparator must not be NULL.
146 Wraps the qsort C-library routine. Please refer to it's documentation.
149 ssize_t array_fnd(Array *array, const void *ptr, size_t *idx, Comparator cmp);
151 Searches the sorted array for the element ptr.
152 Returns the index of the element, or -1 if it wasn't found.
154 If idx is not NULL, a pointer to the last searched index is saved to where it
155 points to. This is the index ptr would need to be inserted at to keep the order.
157 It is assumed that the array has been sorted by array_srt before (or was empty),
158 and the order has been kept and the same comparator is used.
161 size_t array_ins(Array *array, const void *ptr, Comparator cmp);
163 Inserts an element into a sorted array, keeping the order.
164 Returns the index the element has been inserted at.
166 Calls array_fnd and array_put.
168 It is assumed that the array has been sorted by array_srt before (or was empty),
169 and the order has been kept and the same comparator is used.
172 #endif // _DRAGONSTD_ARRAY_H_