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);
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);
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);
18 /* A generally suitable random function. */
19 algo_random_func algo_default_random;
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
26 void *find (const void *array, size_t count, size_t size,
28 algo_compare_func *compare, void *aux);
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
34 size_t count_equal (const void *array, size_t count, size_t size,
36 algo_compare_func *compare, void *aux);
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
42 size_t count_if (const void *array, size_t count, size_t size,
43 algo_predicate_func *predicate, void *aux);
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 (void *array, size_t count, size_t size,
49 algo_compare_func *compare, void *aux);
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);
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);
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);
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);
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,
81 algo_predicate_func *predicate, void *aux);
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);
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,
96 algo_predicate_func *predicate, void *aux);
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);
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,
110 /* Removes elements equal to ELEMENT from ARRAY, which consists
111 of COUNT elements of SIZE bytes each. Returns the number of
112 remaining elements. AUX is passed to COMPARE as auxiliary
114 size_t remove_equal (void *array, size_t count, size_t size,
116 algo_compare_func *compare, void *aux);
118 /* Copies the COUNT elements of SIZE bytes each from ARRAY to
119 RESULT, except that elements for which PREDICATE is true are
120 not copied. Returns the number of elements copied. AUX is
121 passed to PREDICATE as auxiliary data. */
122 size_t remove_copy_if (const void *array, size_t count, size_t size,
124 algo_predicate_func *predicate, void *aux);
126 /* Searches ARRAY, which contains COUNT elements of SIZE bytes
127 each, for VALUE, using a binary search. ARRAY must ordered
128 according to COMPARE. AUX is passed to COMPARE as auxiliary
130 void *binary_search (const void *array, size_t count, size_t size,
132 algo_compare_func *compare, void *aux);
134 /* Lexicographically compares ARRAY1, which contains COUNT1
135 elements of SIZE bytes each, to ARRAY2, which contains COUNT2
136 elements of SIZE bytes, according to COMPARE. Returns a
137 strcmp()-type result. AUX is passed to COMPARE as auxiliary
139 int lexicographical_compare_3way (const void *array1, size_t count1,
140 const void *array2, size_t count2,
142 algo_compare_func *compare, void *aux);
144 /* Computes the generalized set difference, ARRAY1 minus ARRAY2,
145 into RESULT, and returns the number of elements written to
146 RESULT. If a value appears M times in ARRAY1 and N times in
147 ARRAY2, then it will appear max(M - N, 0) in RESULT. ARRAY1
148 and ARRAY2 must be sorted, and RESULT is sorted and stable.
149 ARRAY1 consists of COUNT1 elements, ARRAY2 of COUNT2 elements,
150 each SIZE bytes. AUX is passed to COMPARE as auxiliary
152 size_t set_difference (const void *array1, size_t count1,
153 const void *array2, size_t count2,
156 algo_compare_func *compare, void *aux);
158 /* Finds the first pair of adjacent equal elements in ARRAY,
159 which has COUNT elements of SIZE bytes. Returns the first
160 element in ARRAY such that COMPARE returns zero when it and
161 its successor element are compared. AUX is passed to COMPARE
162 as auxiliary data. */
163 void *adjacent_find_equal (const void *array, size_t count, size_t size,
164 algo_compare_func *compare, void *aux);
166 /* ARRAY contains COUNT elements of SIZE bytes each. Initially
167 the first COUNT - 1 elements of these form a heap, followed by
168 a single element not part of the heap. This function adds the
169 final element, forming a heap of COUNT elements in ARRAY.
170 Uses COMPARE to compare elements, passing AUX as auxiliary
172 void push_heap (void *array, size_t count, size_t size,
173 algo_compare_func *compare, void *aux);
175 /* ARRAY contains COUNT elements of SIZE bytes each. Initially
176 all COUNT elements form a heap. This function moves the
177 largest element in the heap to the final position in ARRAY and
178 reforms a heap of the remaining COUNT - 1 elements at the
179 beginning of ARRAY. Uses COMPARE to compare elements, passing
180 AUX as auxiliary data. */
181 void pop_heap (void *array, size_t count, size_t size,
182 algo_compare_func *compare, void *aux);
184 /* Turns ARRAY, which contains COUNT elements of SIZE bytes, into
185 a heap. Uses COMPARE to compare elements, passing AUX as
187 void make_heap (void *array, size_t count, size_t size,
188 algo_compare_func *compare, void *aux);
190 /* ARRAY contains COUNT elements of SIZE bytes each. Initially
191 all COUNT elements form a heap. This function turns the heap
192 into a fully sorted array. Uses COMPARE to compare elements,
193 passing AUX as auxiliary data. */
194 void sort_heap (void *array, size_t count, size_t size,
195 algo_compare_func *compare, void *aux);
197 /* ARRAY contains COUNT elements of SIZE bytes each. This
198 function tests whether ARRAY is a heap and returns 1 if so, 0
199 otherwise. Uses COMPARE to compare elements, passing AUX as
201 int is_heap (const void *array, size_t count, size_t size,
202 algo_compare_func *compare, void *aux);
205 #endif /* algorithm.h */