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 elements equal to ELEMENT from ARRAY, which consists
99 of COUNT elements of SIZE bytes each. Returns the number of
100 remaining elements. AUX is passed to COMPARE as auxiliary
102 size_t remove_equal (void *array, size_t count, size_t size,
104 algo_compare_func *compare, void *aux);
106 /* Copies the COUNT elements of SIZE bytes each from ARRAY to
107 RESULT, except that elements for which PREDICATE is true are
108 not copied. Returns the number of elements copied. AUX is
109 passed to PREDICATE as auxiliary data. */
110 size_t remove_copy_if (const void *array, size_t count, size_t size,
112 algo_predicate_func *predicate, void *aux);
114 /* Searches ARRAY, which contains COUNT elements of SIZE bytes
115 each, for VALUE, using a binary search. ARRAY must ordered
116 according to COMPARE. AUX is passed to COMPARE as auxiliary
118 void *binary_search (const void *array, size_t count, size_t size,
120 algo_compare_func *compare, void *aux);
122 /* Lexicographically compares ARRAY1, which contains COUNT1
123 elements of SIZE bytes each, to ARRAY2, which contains COUNT2
124 elements of SIZE bytes, according to COMPARE. Returns a
125 strcmp()-type result. AUX is passed to COMPARE as auxiliary
127 int lexicographical_compare_3way (const void *array1, size_t count1,
128 const void *array2, size_t count2,
130 algo_compare_func *compare, void *aux);
132 /* Computes the generalized set difference, ARRAY1 minus ARRAY2,
133 into RESULT, and returns the number of elements written to
134 RESULT. If a value appears M times in ARRAY1 and N times in
135 ARRAY2, then it will appear max(M - N, 0) in RESULT. ARRAY1
136 and ARRAY2 must be sorted, and RESULT is sorted and stable.
137 ARRAY1 consists of COUNT1 elements, ARRAY2 of COUNT2 elements,
138 each SIZE bytes. AUX is passed to COMPARE as auxiliary
140 size_t set_difference (const void *array1, size_t count1,
141 const void *array2, size_t count2,
144 algo_compare_func *compare, void *aux);
146 /* Finds the first pair of adjacent equal elements in ARRAY,
147 which has COUNT elements of SIZE bytes. Returns the first
148 element in ARRAY such that COMPARE returns zero when it and
149 its successor element are compared. AUX is passed to COMPARE
150 as auxiliary data. */
151 void *adjacent_find_equal (const void *array, size_t count, size_t size,
152 algo_compare_func *compare, void *aux);
154 /* ARRAY contains COUNT elements of SIZE bytes each. Initially
155 the first COUNT - 1 elements of these form a heap, followed by
156 a single element not part of the heap. This function adds the
157 final element, forming a heap of COUNT elements in ARRAY.
158 Uses COMPARE to compare elements, passing AUX as auxiliary
160 void push_heap (void *array, size_t count, size_t size,
161 algo_compare_func *compare, void *aux);
163 /* ARRAY contains COUNT elements of SIZE bytes each. Initially
164 all COUNT elements form a heap. This function moves the
165 largest element in the heap to the final position in ARRAY and
166 reforms a heap of the remaining COUNT - 1 elements at the
167 beginning of ARRAY. Uses COMPARE to compare elements, passing
168 AUX as auxiliary data. */
169 void pop_heap (void *array, size_t count, size_t size,
170 algo_compare_func *compare, void *aux);
172 /* Turns ARRAY, which contains COUNT elements of SIZE bytes, into
173 a heap. Uses COMPARE to compare elements, passing AUX as
175 void make_heap (void *array, size_t count, size_t size,
176 algo_compare_func *compare, void *aux);
178 /* ARRAY contains COUNT elements of SIZE bytes each. Initially
179 all COUNT elements form a heap. This function turns the heap
180 into a fully sorted array. Uses COMPARE to compare elements,
181 passing AUX as auxiliary data. */
182 void sort_heap (void *array, size_t count, size_t size,
183 algo_compare_func *compare, void *aux);
185 /* ARRAY contains COUNT elements of SIZE bytes each. This
186 function tests whether ARRAY is a heap and returns 1 if so, 0
187 otherwise. Uses COMPARE to compare elements, passing AUX as
189 int is_heap (const void *array, size_t count, size_t size,
190 algo_compare_func *compare, void *aux);
193 #endif /* algorithm.h */