array: Fix typo in comment.
[pspp-builds.git] / src / libpspp / array.h
1 #ifndef ALGORITHM_H
2 #define ALGORITHM_H 1
3
4 #include <stddef.h>
5 #include <stdbool.h>
6
7 /* Compares A and B, given auxiliary data AUX, and returns a
8    strcmp()-type result. */
9
10 typedef int algo_compare_func (const void *a, const void *b, const void *aux);
11
12 /* Tests a predicate on DATA, given auxiliary data AUX */
13 typedef bool algo_predicate_func (const void *data, const void *aux);
14
15 /* Returns a random number in the range 0 through MAX exclusive,
16    given auxiliary data AUX. */
17 typedef unsigned algo_random_func (unsigned max, const void *aux);
18
19 /* A generally suitable random function. */
20 algo_random_func algo_default_random;
21
22 /* Finds an element in ARRAY, which contains COUNT elements of
23    SIZE bytes each, using COMPARE for comparisons.  Returns the
24    first element in ARRAY that matches TARGET, or a null pointer
25    on failure.  AUX is passed to each comparison as auxiliary
26    data. */
27 void *find (const void *array, size_t count, size_t size,
28             const void *target,
29             algo_compare_func *compare, const void *aux);
30
31 /* Counts and return the number of elements in ARRAY, which
32    contains COUNT elements of SIZE bytes each, which are equal to
33    ELEMENT as compared with COMPARE.  AUX is passed as auxiliary
34    data to COMPARE. */
35 size_t count_equal (const void *array, size_t count, size_t size,
36                     const void *element,
37                     algo_compare_func *compare, const void *aux);
38
39 /* Counts and return the number of elements in ARRAY, which
40    contains COUNT elements of SIZE bytes each, for which
41    PREDICATE returns true.  AUX is passed as auxiliary data to
42    PREDICATE. */
43 size_t count_if (const void *array, size_t count, size_t size,
44                  algo_predicate_func *predicate, const void *aux);
45
46 /* Sorts ARRAY, which contains COUNT elements of SIZE bytes each,
47    using COMPARE for comparisons.  AUX is passed to each
48    comparison as auxiliary data. */
49 void sort (void *array, size_t count, size_t size,
50            algo_compare_func *compare, const void *aux);
51
52 /* Tests whether ARRAY, which contains COUNT elements of SIZE
53    bytes each, is sorted in order according to COMPARE.  AUX is
54    passed to COMPARE as auxiliary data. */
55 bool is_sorted (const void *array, size_t count, size_t size,
56                algo_compare_func *compare, const void *aux);
57
58 /* Makes the elements in ARRAY unique, by moving up duplicates,
59    and returns the new number of elements in the array.  Sorted
60    arrays only.  Arguments same as for sort() above. */
61 size_t unique (void *array, size_t count, size_t size,
62                algo_compare_func *compare, const void *aux);
63
64 /* Helper function that calls sort(), then unique(). */
65 size_t sort_unique (void *array, size_t count, size_t size,
66                     algo_compare_func *compare, const void *aux);
67
68 /* Reorders ARRAY, which contains COUNT elements of SIZE bytes
69    each, so that the elements for which PREDICATE returns true
70    precede those for which PREDICATE returns false.  AUX is passed
71    as auxiliary data to PREDICATE.  Returns the number of
72    elements for which PREDICATE returns true.  Not stable. */
73 size_t partition (void *array, size_t count, size_t size,
74                   algo_predicate_func *predicate, const void *aux);
75
76 /* Checks whether ARRAY, which contains COUNT elements of SIZE
77    bytes each, is partitioned such that PREDICATE returns true
78    for the first TRUE_CNT elements and zero for the remaining
79    elements.  AUX is passed as auxiliary data to PREDICATE. */
80 bool is_partitioned (const void *array, size_t count, size_t size,
81                     size_t true_cnt,
82                     algo_predicate_func *predicate, const void *aux);
83
84 /* Randomly reorders ARRAY, which contains COUNT elements of SIZE
85    bytes each.  Uses RANDOM as a source of random data, passing
86    AUX as the auxiliary data.  RANDOM may be null to use a
87    default random source. */
88 void random_shuffle (void *array, size_t count, size_t size,
89                      algo_random_func *random, const void *aux);
90
91 /* Copies the COUNT elements of SIZE bytes each from ARRAY to
92    RESULT, except that elements for which PREDICATE is false are
93    not copied.  Returns the number of elements copied.  AUX is
94    passed to PREDICATE as auxiliary data.  */
95 size_t copy_if (const void *array, size_t count, size_t size,
96                 void *result,
97                 algo_predicate_func *predicate, const void *aux);
98
99 /* Removes N elements starting at IDX from ARRAY, which consists
100    of COUNT elements of SIZE bytes each, by shifting the elements
101    following them, if any, into its position. */
102 void remove_range (void *array, size_t count, size_t size,
103                    size_t idx, size_t n);
104
105 /* Removes element IDX from ARRAY, which consists of COUNT
106    elements of SIZE bytes each, by shifting the elements
107    following it, if any, into its position. */
108 void remove_element (void *array, size_t count, size_t size,
109                      size_t idx);
110
111 /* Makes room for N elements starting at IDX in ARRAY, which
112    initially consists of COUNT elements of SIZE bytes each, by
113    shifting elements IDX...COUNT (exclusive) to the right by N
114    positions. */
115 void insert_range (void *array, size_t count, size_t size,
116                    size_t idx, size_t n);
117
118 /* Makes room for a new element at IDX in ARRAY, which initially
119    consists of COUNT elements of SIZE bytes each, by shifting
120    elements IDX...COUNT (exclusive) to the right by one
121    position. */
122 void insert_element (void *array, size_t count, size_t size,
123                      size_t idx);
124
125 /* Moves an element in ARRAY, which consists of COUNT elements of
126    SIZE bytes each, from OLD_IDX to NEW_IDX, shifting around
127    other elements as needed.  Runs in O(abs(OLD_IDX - NEW_IDX))
128    time. */
129 void move_element (void *array, size_t count, size_t size,
130                    size_t old_idx, size_t new_idx);
131
132 /* Moves N elements in ARRAY starting at OLD_IDX, which consists
133    of COUNT elements of SIZE bytes each, so that they now start
134    at NEW_IDX, shifting around other elements as needed. */
135 void move_range (void *array, size_t count, size_t size,
136                  size_t old_idx, size_t new_idx, size_t n);
137
138 /* Removes elements equal to ELEMENT from ARRAY, which consists
139    of COUNT elements of SIZE bytes each.  Returns the number of
140    remaining elements.  AUX is passed to COMPARE as auxiliary
141    data. */
142 size_t remove_equal (void *array, size_t count, size_t size,
143                      void *element,
144                      algo_compare_func *compare, const void *aux);
145
146 /* Copies the COUNT elements of SIZE bytes each from ARRAY to
147    RESULT, except that elements for which PREDICATE is true are
148    not copied.  Returns the number of elements copied.  AUX is
149    passed to PREDICATE as auxiliary data.  */
150 size_t remove_copy_if (const void *array, size_t count, size_t size,
151                        void *result,
152                        algo_predicate_func *predicate, const void *aux);
153
154 /* Searches ARRAY, which contains COUNT elements of SIZE bytes
155    each, for VALUE, using a binary search.  ARRAY must ordered
156    according to COMPARE.  AUX is passed to COMPARE as auxiliary
157    data. */
158 void *binary_search (const void *array, size_t count, size_t size,
159                      void *value,
160                      algo_compare_func *compare, const void *aux);
161
162 /* Lexicographically compares ARRAY1, which contains COUNT1
163    elements of SIZE bytes each, to ARRAY2, which contains COUNT2
164    elements of SIZE bytes, according to COMPARE.  Returns a
165    strcmp()-type result.  AUX is passed to COMPARE as auxiliary
166    data. */
167 int lexicographical_compare_3way (const void *array1, size_t count1,
168                                   const void *array2, size_t count2,
169                                   size_t size,
170                                   algo_compare_func *compare, const void *aux);
171
172 /* Computes the generalized set difference, ARRAY1 minus ARRAY2,
173    into RESULT, and returns the number of elements written to
174    RESULT.  If a value appears M times in ARRAY1 and N times in
175    ARRAY2, then it will appear max(M - N, 0) in RESULT.  ARRAY1
176    and ARRAY2 must be sorted, and RESULT is sorted and stable.
177    ARRAY1 consists of COUNT1 elements, ARRAY2 of COUNT2 elements,
178    each SIZE bytes.  AUX is passed to COMPARE as auxiliary
179    data. */
180 size_t set_difference (const void *array1, size_t count1,
181                        const void *array2, size_t count2,
182                        size_t size,
183                        void *result,
184                        algo_compare_func *compare, const void *aux);
185
186 /* Finds the first pair of adjacent equal elements in ARRAY,
187    which has COUNT elements of SIZE bytes.  Returns the first
188    element in ARRAY such that COMPARE returns true when it and
189    its successor element are compared.  AUX is passed to COMPARE
190    as auxiliary data. */
191 void *adjacent_find_equal (const void *array, size_t count, size_t size,
192                            algo_compare_func *compare, const void *aux);
193
194 /* ARRAY contains COUNT elements of SIZE bytes each.  Initially
195    the first COUNT - 1 elements of these form a heap, followed by
196    a single element not part of the heap.  This function adds the
197    final element, forming a heap of COUNT elements in ARRAY.
198    Uses COMPARE to compare elements, passing AUX as auxiliary
199    data. */
200 void push_heap (void *array, size_t count, size_t size,
201                 algo_compare_func *compare, const void *aux);
202
203 /* ARRAY contains COUNT elements of SIZE bytes each.  Initially
204    all COUNT elements form a heap.  This function moves the
205    largest element in the heap to the final position in ARRAY and
206    reforms a heap of the remaining COUNT - 1 elements at the
207    beginning of ARRAY.  Uses COMPARE to compare elements, passing
208    AUX as auxiliary data. */
209 void pop_heap (void *array, size_t count, size_t size,
210                algo_compare_func *compare, const void *aux);
211
212 /* Turns ARRAY, which contains COUNT elements of SIZE bytes, into
213    a heap.  Uses COMPARE to compare elements, passing AUX as
214    auxiliary data. */
215 void make_heap (void *array, size_t count, size_t size,
216                 algo_compare_func *compare, const void *aux);
217
218 /* ARRAY contains COUNT elements of SIZE bytes each.  Initially
219    all COUNT elements form a heap.  This function turns the heap
220    into a fully sorted array.  Uses COMPARE to compare elements,
221    passing AUX as auxiliary data. */
222 void sort_heap (void *array, size_t count, size_t size,
223                 algo_compare_func *compare, const void *aux);
224
225 /* ARRAY contains COUNT elements of SIZE bytes each.  This
226    function tests whether ARRAY is a heap and returns true if so,
227    false otherwise.  Uses COMPARE to compare elements, passing
228    AUX as auxiliary data. */
229 bool is_heap (const void *array, size_t count, size_t size,
230              algo_compare_func *compare, const void *aux);
231
232
233 #endif /* algorithm.h */