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" // for Comparator
16 #include "bits/compare.h" // for cmp_ref (not used in file)
21 size_t ext; // extra space
23 size_t mbs; // member size
24 size_t siz; // used space
25 size_t cap; // available space
28 void array_ini(Array *array, size_t mmb, size_t ext);
30 Initializes the array.
32 The array will have the member size of mmb and the extra space set to ext.
33 mmb should be bigger than 0 and may not be changed after the initialization.
34 ext can be 0 or bigger and may be changed any time.
36 This function should be called before calling any other functions on the
39 This function should not be called on an array that has been initialized before,
40 unless the array has a capacity of 0. Otherwise a memory leak will occur.
43 void array_rlc(Array *array);
45 Reallocates the array's memory to match it's capacity.
47 This function should be called every time the capacity has changed.
50 void array_grw(Array *array, size_t n);
52 Grows the array by n bytes.
54 This function increases the arrays size by n bytes. If this exceeds the capacity of
55 the array, the capacity set to the size and the extra space ext is added to it.
57 If the capacity is changed, the array is reallocated.
59 If n is zero, the array's capacity may still grow by extra space if it exactly
60 matches the current size.
63 void array_shr(Array *array, size_t n);
65 Shrinks the array by n bytes.
67 This function decreases the arrays size by n bytes.
69 If the array has additional capacity left after it has been shrunk, the capacity
70 is set to the new size and the array is reallocated to fit the new capacity.
71 For n > 0, this is always the case, for n = 0, this may be the case.
73 Note that calling this function with n = 0 is useful to shrink the array's memory to
74 exactly fit it's used size.
77 void array_put(Array *array, const void *ptr, size_t n);
79 Grows the array by 1 and inserts ptr at the index n.
81 This function inserts the memory pointed to by ptr before the array member at
82 index n, moving all elements from that index to the end of the array.
84 After this operation, the inserted element will be _at_ the index n.
86 The memory that ptr points to, which's size is assumed to be at least as big as the
87 array's member size is copied into the arrays memory.
89 n should be in the range from 0 to the array's size.
92 void array_apd(Array *array, const void *ptr);
94 Grows the array by 1 and appends ptr at the end of the array.
96 This function's result is equivalent to calling array_put(array, ptr, array->siz),
97 but it is slightly faster since it saves unnecessary calls.
100 void array_cpy(Array *array, void **ptr, size_t *n);
102 Allocates a buffer big enough to fit the array's used size.
103 Copies the array's contents into the allocated buffer.
104 Returns the buffer in ptr and the size in n.
106 Note that the returned size is the number of elements, not the number of bytes.
109 void array_cln(Array *dst, Array *src);
111 Clones the array src to the array dst.
113 dst is initialized to have the same configuration (member size, extra space) as src.
115 After the operation, the contents of the array dst are be the same as those of src.
116 The size of dst and src are the same, the capacity of dst however is the same as
117 the size of dst and src (which might not equal the capacity of src).
119 Since array_ini is called on dst, it should be uninitialized, empty or deleted.
122 void array_rcy(Array *array);
126 This function sets the used size of the array to 0 but leaves the capacity unchanged.
127 The array's memory is not free'd and the array can be reused.
130 void array_clr(Array *array);
134 This function frees the arrays memory. If this is not called when the array's
135 reference is dropped, a memory leak occurs, unless the array is empty (capacity
136 of 0), in which case the function does not need to be called. The function works
137 fine on empty arrays however.
139 After this, the array is empty and can be reused.
142 void array_srt(Array *array, Comparator cmp);
144 Sorts the array using the quicksort algorithm.
146 Comparator must not be NULL.
147 Wraps the qsort C-library routine. Please refer to it's documentation.
150 ssize_t array_fnd(Array *array, const void *ptr, size_t *idx, Comparator cmp);
152 Searches the sorted array for the element ptr.
153 Returns the index of the element, or -1 if it wasn't found.
155 If idx is not NULL, a pointer to the last searched index is saved to where it
156 points to. This is the index ptr would need to be inserted at to keep the order.
158 It is assumed that the array has been sorted by array_srt before (or was empty),
159 and the order has been kept and the same comparator is used.
162 size_t array_ins(Array *array, const void *ptr, Comparator cmp);
164 Inserts an element into a sorted array, keeping the order.
165 Returns the index the element has been inserted at.
167 Calls array_fnd and array_put.
169 It is assumed that the array has been sorted by array_srt before (or was empty),
170 and the order has been kept and the same comparator is used.
173 #endif // _DRAGONSTD_ARRAY_H_