]> git.lizzy.rs Git - dragonstd.git/blob - array.h
Style: no longer put space after unary operators
[dragonstd.git] / array.h
1 /*
2         Array
3         -----
4
5         Data structure where all elements (members) have the same size and are
6                 located consecutively in memory.
7
8 */
9
10 #ifndef _DRAGONSTD_ARRAY_H_ // include guard
11 #define _DRAGONSTD_ARRAY_H_
12
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)
17
18 typedef struct {
19         /* public */
20         void *ptr;  // memory
21         size_t ext; // extra space
22         /* private */
23         size_t mbs; // member size
24         size_t siz; // used space
25         size_t cap; // available space
26 } Array;
27
28 void array_ini(Array *array, size_t mmb, size_t ext);
29 /*
30         Initializes the array.
31
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.
35
36         This function should be called before calling any other functions on the
37                 array.
38
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.
41 */
42
43 void array_rlc(Array *array);
44 /*
45         Reallocates the array's memory to match it's capacity.
46
47         This function should be called every time the capacity has changed.
48 */
49
50 void array_grw(Array *array, size_t n);
51 /*
52         Grows the array by n bytes.
53
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.
56
57         If the capacity is changed, the array is reallocated.
58
59         If n is zero, the array's capacity may still grow by extra space if it exactly
60                 matches the current size.
61 */
62
63 void array_shr(Array *array, size_t n);
64 /*
65         Shrinks the array by n bytes.
66
67         This function decreases the arrays size by n bytes.
68
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.
72
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.
75 */
76
77 void array_put(Array *array, const void *ptr, size_t n);
78 /*
79         Grows the array by 1 and inserts ptr at the index n.
80
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.
83
84         After this operation, the inserted element will be _at_ the index n.
85
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.
88
89         n should be in the range from 0 to the array's size.
90 */
91
92 void array_apd(Array *array, const void *ptr);
93 /*
94         Grows the array by 1 and appends ptr at the end of the array.
95
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.
98 */
99
100 void array_cpy(Array *array, void **ptr, size_t *n);
101 /*
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.
105
106         Note that the returned size is the number of elements, not the number of bytes.
107 */
108
109 void array_cln(Array *dst, Array *src);
110 /*
111         Clones the array src to the array dst.
112
113         dst is initialized to have the same configuration (member size, extra space) as src.
114
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).
118
119         Since array_ini is called on dst, it should be uninitialized, empty or deleted.
120 */
121
122 void array_rcy(Array *array);
123 /*
124         Recycles the array.
125
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.
128 */
129
130 void array_clr(Array *array);
131 /*
132         Clears the array.
133
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.
138
139         After this, the array is empty and can be reused.
140 */
141
142 void array_srt(Array *array, Comparator cmp);
143 /*
144         Sorts the array using the quicksort algorithm.
145
146         Comparator must not be NULL.
147         Wraps the qsort C-library routine. Please refer to it's documentation.
148 */
149
150 ssize_t array_fnd(Array *array, const void *ptr, size_t *idx, Comparator cmp);
151 /*
152         Searches the sorted array for the element ptr.
153         Returns the index of the element, or -1 if it wasn't found.
154
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.
157
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.
160 */
161
162 size_t array_ins(Array *array, const void *ptr, Comparator cmp);
163 /*
164         Inserts an element into a sorted array, keeping the order.
165         Returns the index the element has been inserted at.
166
167         Calls array_fnd and array_put.
168
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.
171 */
172
173 #endif // _DRAGONSTD_ARRAY_H_