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