More constness
[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 /* Moves an element in ARRAY, which consists of COUNT elements of
112    SIZE bytes each, from OLD_IDX to NEW_IDX, shifting around
113    other elements as needed.  Runs in O(abs(OLD_IDX - NEW_IDX))
114    time. */
115 void move_element (void *array, size_t count, size_t size,
116                    size_t old_idx, size_t new_idx);
117
118 /* Removes elements equal to ELEMENT from ARRAY, which consists
119    of COUNT elements of SIZE bytes each.  Returns the number of
120    remaining elements.  AUX is passed to COMPARE as auxiliary
121    data. */
122 size_t remove_equal (void *array, size_t count, size_t size,
123                      void *element,
124                      algo_compare_func *compare, const void *aux);
125
126 /* Copies the COUNT elements of SIZE bytes each from ARRAY to
127    RESULT, except that elements for which PREDICATE is true are
128    not copied.  Returns the number of elements copied.  AUX is
129    passed to PREDICATE as auxiliary data.  */
130 size_t remove_copy_if (const void *array, size_t count, size_t size,
131                        void *result,
132                        algo_predicate_func *predicate, const void *aux);
133
134 /* Searches ARRAY, which contains COUNT elements of SIZE bytes
135    each, for VALUE, using a binary search.  ARRAY must ordered
136    according to COMPARE.  AUX is passed to COMPARE as auxiliary
137    data. */
138 void *binary_search (const void *array, size_t count, size_t size,
139                      void *value,
140                      algo_compare_func *compare, const void *aux);
141
142 /* Lexicographically compares ARRAY1, which contains COUNT1
143    elements of SIZE bytes each, to ARRAY2, which contains COUNT2
144    elements of SIZE bytes, according to COMPARE.  Returns a
145    strcmp()-type result.  AUX is passed to COMPARE as auxiliary
146    data. */
147 int lexicographical_compare_3way (const void *array1, size_t count1,
148                                   const void *array2, size_t count2,
149                                   size_t size,
150                                   algo_compare_func *compare, const void *aux);
151
152 /* Computes the generalized set difference, ARRAY1 minus ARRAY2,
153    into RESULT, and returns the number of elements written to
154    RESULT.  If a value appears M times in ARRAY1 and N times in
155    ARRAY2, then it will appear max(M - N, 0) in RESULT.  ARRAY1
156    and ARRAY2 must be sorted, and RESULT is sorted and stable.
157    ARRAY1 consists of COUNT1 elements, ARRAY2 of COUNT2 elements,
158    each SIZE bytes.  AUX is passed to COMPARE as auxiliary
159    data. */
160 size_t set_difference (const void *array1, size_t count1,
161                        const void *array2, size_t count2,
162                        size_t size,
163                        void *result,
164                        algo_compare_func *compare, const void *aux);
165
166 /* Finds the first pair of adjacent equal elements in ARRAY,
167    which has COUNT elements of SIZE bytes.  Returns the first
168    element in ARRAY such that COMPARE returns true when it and
169    its successor element are compared.  AUX is passed to COMPARE
170    as auxiliary data. */
171 void *adjacent_find_equal (const void *array, size_t count, size_t size,
172                            algo_compare_func *compare, const void *aux);
173
174 /* ARRAY contains COUNT elements of SIZE bytes each.  Initially
175    the first COUNT - 1 elements of these form a heap, followed by
176    a single element not part of the heap.  This function adds the
177    final element, forming a heap of COUNT elements in ARRAY.
178    Uses COMPARE to compare elements, passing AUX as auxiliary
179    data. */
180 void push_heap (void *array, size_t count, size_t size,
181                 algo_compare_func *compare, const void *aux);
182
183 /* ARRAY contains COUNT elements of SIZE bytes each.  Initially
184    all COUNT elements form a heap.  This function moves the
185    largest element in the heap to the final position in ARRAY and
186    reforms a heap of the remaining COUNT - 1 elements at the
187    beginning of ARRAY.  Uses COMPARE to compare elements, passing
188    AUX as auxiliary data. */
189 void pop_heap (void *array, size_t count, size_t size,
190                algo_compare_func *compare, const void *aux);
191
192 /* Turns ARRAY, which contains COUNT elements of SIZE bytes, into
193    a heap.  Uses COMPARE to compare elements, passing AUX as
194    auxiliary data. */
195 void make_heap (void *array, size_t count, size_t size,
196                 algo_compare_func *compare, const void *aux);
197
198 /* ARRAY contains COUNT elements of SIZE bytes each.  Initially
199    all COUNT elements form a heap.  This function turns the heap
200    into a fully sorted array.  Uses COMPARE to compare elements,
201    passing AUX as auxiliary data. */
202 void sort_heap (void *array, size_t count, size_t size,
203                 algo_compare_func *compare, const void *aux);
204
205 /* ARRAY contains COUNT elements of SIZE bytes each.  This
206    function tests whether ARRAY is a heap and returns true if so, 
207    false otherwise.  Uses COMPARE to compare elements, passing 
208    AUX as auxiliary data. */
209 bool is_heap (const void *array, size_t count, size_t size,
210              algo_compare_func *compare, const void *aux);
211
212
213 #endif /* algorithm.h */