diff --git a/LICENSE.txt b/LICENSE.txt new file mode 100644 index 0000000..5e9eb6c --- /dev/null +++ b/LICENSE.txt @@ -0,0 +1,7 @@ +Copyright 2022 clibraries + +Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. diff --git a/array_alg.h b/array_alg.h index 853bc93..881a336 100644 --- a/array_alg.h +++ b/array_alg.h @@ -441,11 +441,36 @@ ALGDEF T *NS(partition)( void *predicate_ctx ); -ALGDEF T *NS(partition_copy)( +/// Partition a range by copying the elements that satisfy a predicate into one buffer, +/// and the ones that fail to another. +/// Note that this function is stable. +ALGDEF void NS(partition_copy)( const T *first, const T *last, - T *restrict out_false, - T *restrict out_true, + T **out_true, + T **out_false, + int (*predicate)(const T*, void*), + void *predicate_ctx + ); + +/// Calls malloc. +ALGDEF T *NS(stable_partition)( + T *first, + T *last, + int (*predicate)(const T*, void*), + void *predicate_ctx + ); + +/// Like above, but does not call `malloc`. +/// You must provide a buffer. +/// requires: +/// - sizeof(buffer) >= the number of false entries in the range [first, last) +// Note that sizeof(buffer) >= last - first will be sufficient. + +ALGDEF T *NS(stable_partition_with_buffer)( + T *first, + T *last, + T *buffer, int (*predicate)(const T*, void*), void *predicate_ctx ); @@ -644,8 +669,7 @@ ALGDEF void NS(insertion_sort_stable)( /// Sort an array in a stable way with a merge sort variety. /// By stable, we mean that the order of equivalent elements is preserved. -/// Note this function allocates memory with malloc. - +/// Calls malloc. ALGDEF void NS(stable_sort)( T* first, T* last, @@ -653,8 +677,8 @@ ALGDEF void NS(stable_sort)( void* compare_ctx ); -/// Like above, but does not call `malloc`. -/// You provide a buffer.` +/// Like above, but does not call malloc. +/// You must provide a buffer. /// requires: /// - sizeof(buffer) >= (last - first) / 2 ALGDEF void NS(stable_sort_with_buffer)( @@ -673,6 +697,15 @@ ALGDEF void NS(partial_sort)( void *compare_ctx ); +ALGDEF T *NS(partial_sort_copy)( + const T *first, + const T *last, + T *out_first, + T *out_last, + int (*compare)(const T*, const T*, void*), + void *compare_ctx + ); + ALGDEF void NS(nth_element)( T *first, T *nth, @@ -712,51 +745,6 @@ ALGDEF T* NS(insert_n)( size_t n ); -// == PRIVATE == - -ALGDEF void NS(_insertion_sort_unguarded)( - T *restrict first, - T *last, - int (*compare)(const T*, const T*, void*), - void *compare_ctx - ); - -ALGDEF void NS(_rotate_right_by_one)( - T *first, - T *last - ); - -/// Hoare partitioning: -/// https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme -/// -/// Partially sorts [first, last) into two parts, divided -/// by a pointer m such that: -/// for all a in [first, m) and b in [m, last) a <= b. -/// -/// requires: -/// - first != last -ALGDEF T *NS(_sort_partition)( - T *first, - T *last, - int (*compare)(const T*, const T*, void*), - void* compare_ctx - ); - -ALGDEF T *NS(_quick_sort_early_stop)( - T *first, - T *last, - int (*compare)(const T*, const T*, void*), - void* compare_ctx - ); - -ALGDEF T *NS(_merge_sort_adaptive_with_buffer_n)( - T* first, - size_t count, - T* buffer, - int (*compare)(const T* a, const T* b, void*), - void* compare_ctx - ); - #ifdef ARRAY_ALG_IMPLEMENTATION ALGDEF T *NS(find_if)( @@ -1506,11 +1494,41 @@ ALGDEF T *NS(partition)( return out; } -ALGDEF T *NS(partition_copy)( +ALGDEF T *NS(stable_partition_with_buffer)( + T *first, + T *last, + T *buffer, + int (*predicate)(const T*, void*), + void *predicate_ctx + ) +{ + T *out_false = buffer; + T *out_true = first; + NS(partition_copy)(first, last, &out_true, &out_false, predicate, predicate_ctx); + memcpy(out_true, buffer, sizeof(T) * (out_false - buffer)); + return out_true; +} + +ALGDEF T *NS(stable_partition)( + T *first, + T *last, + int (*predicate)(const T*, void*), + void *predicate_ctx + ) +{ + // TODO: this is not the best implementation + if (first == last) return last; + T *buffer = malloc((last - first) * sizeof(T)); + T *point = NS(stable_partition_with_buffer)(first, last, buffer, predicate, predicate_ctx); + free(buffer); + return point; +} + +ALGDEF void NS(partition_copy)( const T *first, const T *last, - T *restrict out_false, - T *restrict out_true, + T **out_true, + T **out_false, int (*predicate)(const T*, void*), void *predicate_ctx ) { @@ -1519,19 +1537,19 @@ ALGDEF T *NS(partition_copy)( { if (predicate(first, predicate_ctx)) { - *out_true = *first; - ++out_true; + *(*out_true) = *first; + ++(*out_true); } else { - *out_false = *first; - ++out_false; + *(*out_false) = *first; + ++(*out_false); } ++first; } - return out_false; } + ALGDEF T *NS(partition_point_n)( const T *first, size_t n, @@ -1599,7 +1617,7 @@ typedef struct { void* compare_ctx; } NS(_lower_upper_bound_closure); -ALGDEF int NS(_lower_bound_predicate)(const T* x, void* ctx) { +static int NS(_lower_bound_predicate)(const T* x, void* ctx) { const NS(_lower_upper_bound_closure)* c = ctx; return c->compare(x, c->value, c->compare_ctx) < 0; } @@ -1620,8 +1638,7 @@ ALGDEF T *NS(lower_bound)( return NS(partition_point)(first, last, NS(_lower_bound_predicate), &ctx); } - -ALGDEF int NS(_upper_bound_predicate)(const T* x, void* ctx) { +static int NS(_upper_bound_predicate)(const T* x, void* ctx) { const NS(_lower_upper_bound_closure)* c = ctx; return c->compare(c->value, x, c->compare_ctx) >= 0; } @@ -1899,7 +1916,7 @@ ALGDEF void NS(sort_heap)( } } -ALGDEF void NS(_insertion_sort_unguarded)( +static void NS(_insertion_sort_unguarded)( T *restrict first, T *last, int (*compare)(const T*, const T*, void*), @@ -1942,7 +1959,16 @@ ALGDEF void NS(insertion_sort)( NS(_insertion_sort_unguarded)(suffix, last, compare, compare_ctx); } -ALGDEF T* NS(_sort_partition)( +/// Hoare partitioning: +/// https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme +/// +/// Partially sorts [first, last) into two parts, divided +/// by a pointer m such that: +/// for all a in [first, m) and b in [m, last) a <= b. +/// +/// requires: +/// - first != last +static T* NS(_sort_partition)( T *first, T *last, int (*compare)(const T*, const T*, void*), @@ -1972,7 +1998,7 @@ ALGDEF T* NS(_sort_partition)( } } -ALGDEF T *NS(_quick_sort_early_stop)( +static T *NS(_quick_sort_early_stop)( T *first, T *last, int (*compare)(const T*, const T*, void*), @@ -2003,7 +2029,7 @@ ALGDEF void NS(sort)( NS(_insertion_sort_unguarded)(first, last, compare, compare_ctx); } -ALGDEF void NS(_rotate_right_by_one)( +static void NS(_rotate_right_by_one)( T *first, T *last ) { @@ -2033,7 +2059,7 @@ ALGDEF void NS(insertion_sort_stable)( NS(_insertion_sort_unguarded)(suffix, last, compare, compare_ctx); } -ALGDEF T *NS(_merge_sort_adaptive_with_buffer_n)( +static T *NS(_merge_sort_adaptive_with_buffer_n)( T* first, size_t count, T* buffer, @@ -2109,6 +2135,47 @@ ALGDEF void NS(partial_sort)( NS(sort_heap)(first, middle, compare, compare_ctx); } +ALGDEF T *NS(partial_sort_copy)( + const T *first, + const T *last, + T *out_first, + T *out_last, + int (*compare)(const T*, const T*, void*), + void *compare_ctx + ) +{ + if (first == last || out_first == out_last) return out_first; + T *out = out_first; + *out = *first; + ++out; + ++first; + + while (out != out_last) + { + if (first == last) { + NS(sort_heap)(out_first, out, compare, compare_ctx); + return out; + } + *out = *first; + ++first; + ++out; + NS(push_heap)(out_first, out, compare, compare_ctx); + } + + while (first != last) + { + if (compare(first, out_first, compare_ctx) < 0) + { + NS(pop_heap)(out_first, out, compare, compare_ctx); + *(out - 1) = *first; + NS(push_heap)(out_first, out, compare, compare_ctx); + } + ++first; + } + NS(sort_heap)(out_first, out, compare, compare_ctx); + return out; +} + ALGDEF void NS(nth_element)( T *first, T *nth, diff --git a/test/defs.h b/test/defs.h index 6bbb336..9895dcd 100644 --- a/test/defs.h +++ b/test/defs.h @@ -25,4 +25,13 @@ typedef struct { #define ARRAY_ALG_PREFIX person_array_ #include "../array_alg.h" +// Import private functions for testing. +#define ARRAY_ALG_STATIC +#define ARRAY_ALG_IMPLEMENTATION +#define ARRAY_ALG_TYPE int +#define ARRAY_ALG_PREFIX intv_private_ +#include "../array_alg.h" +#undef ARRAY_ALG_IMPLEMENTATION +#undef ARRAY_ALG_STATIC + #endif // DEFS_H_ diff --git a/test/tests.c b/test/tests.c index df2252d..ab74988 100644 --- a/test/tests.c +++ b/test/tests.c @@ -390,9 +390,15 @@ void test_partition(void) { int numbers[] = { 1, 2, 3, 4, 5, 6}; int odd[3]; int even[3]; - int* false_end = intv_partition_copy(numbers, numbers + 6, odd, even, pred_is_even, NULL); - assert(false_end - odd == 3); + + int *out_odd = odd; + int *out_even = even; + + intv_partition_copy(numbers, numbers + 6, &out_even, &out_odd, pred_is_even, NULL); + + assert(out_odd - odd == 3); + assert(out_even - even == 3); for (int i = 0; i < 3; ++i) { assert(odd[i] % 2 == 1); @@ -401,6 +407,19 @@ void test_partition(void) { } } +void test_stable_partition(void) { + int numbers[] = { 1, 2, 3, 4, 5, 6}; + int *point = intv_stable_partition(numbers, numbers + 6, pred_is_even, NULL); + + assert(point - numbers == 3); + + int expected[] = { 2, 4, 6, 1, 3, 5 }; + + for (int i = 0; i < ARRAY_LEN(numbers); ++i) { + assert(numbers[i] == expected[i]); + } +} + void test_is_sorted(void) { { int numbers[] = { 1, 2, 3, 4, 5, 6}; @@ -547,7 +566,7 @@ void test_sort_partition() { nums[i] = ARRAY_ALG_RANDOM(100); } - int* part = intv__sort_partition(nums, nums + N, compare_int, NULL); + int* part = intv_private__sort_partition(nums, nums + N, compare_int, NULL); if (!intv_all_of(nums, part, _is_less_equal_than, part)) { print_array(nums, N); printf("partition: %d\n", *part); @@ -589,6 +608,46 @@ void test_partial_sort() { } } +void test_partial_sort_copy() { + { + enum { N = 100 }; + int nums[N]; + for (int i = 0; i < N; ++i) { + nums[i] = i; + } + + enum { M = 10 }; + for (int iteration = 0; iteration < 1000; ++iteration) { + intv_random_shuffle(nums, nums + N); + + int top[M]; + intv_partial_sort_copy(nums, nums + N, top, top + M, compare_int, NULL); + + for (int i = 0; i < M; ++i) { + assert(top[i] == i); + } + } + } + { + enum { N = 10 }; + int nums[N]; + for (int i = 0; i < N; ++i) { + nums[i] = i; + } + intv_reverse(nums, nums + N); + + enum { M = 100 }; + + int top[M]; + int *out = intv_partial_sort_copy(nums, nums + N, top, top + M, compare_int, NULL); + assert(out == top + N); + + for (int i = 0; i < N; ++i) { + assert(top[i] == i); + } + } +} + typedef void (*SortFunc)(int*, int*, int (*cmp)(const int*, const int*, void*), void*); static inline @@ -800,6 +859,7 @@ int main() { printf("-- test_minmax --\n"); test_minmax(); printf("-- test_minmax_element --\n"); test_minmax_element(); printf("-- test_partition --\n"); test_partition(); + printf("-- test_stable_partition --\n"); test_stable_partition(); printf("-- test_is_sorted --\n"); test_is_sorted(); printf("-- test_binary_search --\n"); test_binary_search(); printf("-- test_permutation --\n"); test_permutation(); @@ -811,18 +871,17 @@ int main() { printf("-- test_sort_partition -- \n"); test_sort_partition(); printf("-- test_nth_element --\n"); test_nth_element(); printf("-- test_partial_sort --\n"); test_partial_sort(); + printf("-- test_partial_sort_copy --\n"); test_partial_sort_copy(); printf("-- test_heap_sort --\n"); test_heap_sort(); printf("-- test_insertion_sort --\n"); test_insertion_sort(); printf("-- test_stable_sort --\n"); test_stable_sort(); printf("-- test_sort --\n"); test_sort(); printf("-- test_c_qsort --\n"); test_c_qsort(); - // EXTENSIONS printf("-- test_is_strictly_increasing --\n"); test_is_strictly_increasing(); printf("-- test_insert--\n"); test_insert(); - // BENCHMARKS printf("BENCHMARKS\n"); printf("-- heap_sort --\n"); benchmark_sort(intv_heap_sort, 1000000);
Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.
Alternative Proxies: