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