7 /* Compares A and B, given auxiliary data AUX, and returns a
8 strcmp()-type result. */
10 typedef int algo_compare_func (const void *a, const void *b, const void *aux);
12 /* Tests a predicate on DATA, given auxiliary data AUX */
13 typedef bool algo_predicate_func (const void *data, const void *aux);
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);
19 /* A generally suitable random function. */
20 algo_random_func algo_default_random;
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
27 void *find (const void *array, size_t count, size_t size,
29 algo_compare_func *compare, const void *aux);
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
35 size_t count_equal (const void *array, size_t count, size_t size,
37 algo_compare_func *compare, const void *aux);
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
43 size_t count_if (const void *array, size_t count, size_t size,
44 algo_predicate_func *predicate, const void *aux);
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);
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);
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);
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);
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);
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,
82 algo_predicate_func *predicate, const void *aux);
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);
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,
97 algo_predicate_func *predicate, const void *aux);
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);
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,
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
115 void insert_range (void *array, size_t count, size_t size,
116 size_t idx, size_t n);
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
122 void insert_element (void *array, size_t count, size_t size,
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))
129 void move_element (void *array, size_t count, size_t size,
130 size_t old_idx, size_t new_idx);
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);
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
142 size_t remove_equal (void *array, size_t count, size_t size,
144 algo_compare_func *compare, const void *aux);
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,
152 algo_predicate_func *predicate, const void *aux);
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
158 void *binary_search (const void *array, size_t count, size_t size,
160 algo_compare_func *compare, const void *aux);
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
167 int lexicographical_compare_3way (const void *array1, size_t count1,
168 const void *array2, size_t count2,
170 algo_compare_func *compare, const void *aux);
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
180 size_t set_difference (const void *array1, size_t count1,
181 const void *array2, size_t count2,
184 algo_compare_func *compare, const void *aux);
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);
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
200 void push_heap (void *array, size_t count, size_t size,
201 algo_compare_func *compare, const void *aux);
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);
212 /* Turns ARRAY, which contains COUNT elements of SIZE bytes, into
213 a heap. Uses COMPARE to compare elements, passing AUX as
215 void make_heap (void *array, size_t count, size_t size,
216 algo_compare_func *compare, const void *aux);
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);
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);
233 #endif /* algorithm.h */