Skip to content

Commit 9e014e8

Browse files
committed
Better handle private members using static.
1 parent 443558d commit 9e014e8

File tree

3 files changed

+26
-57
lines changed

3 files changed

+26
-57
lines changed

array_alg.h

Lines changed: 16 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -745,52 +745,6 @@ ALGDEF T* NS(insert_n)(
745745
size_t n
746746
);
747747

748-
// == PRIVATE ==
749-
750-
ALGDEF void NS(_insertion_sort_unguarded)(
751-
T *restrict first,
752-
T *last,
753-
int (*compare)(const T*, const T*, void*),
754-
void *compare_ctx
755-
);
756-
757-
ALGDEF void NS(_rotate_right_by_one)(
758-
T *first,
759-
T *last
760-
);
761-
762-
/// Hoare partitioning:
763-
/// https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme
764-
///
765-
/// Partially sorts [first, last) into two parts, divided
766-
/// by a pointer m such that:
767-
/// for all a in [first, m) and b in [m, last) a <= b.
768-
///
769-
/// requires:
770-
/// - first != last
771-
ALGDEF T *NS(_sort_partition)(
772-
T *first,
773-
T *last,
774-
int (*compare)(const T*, const T*, void*),
775-
void* compare_ctx
776-
);
777-
778-
ALGDEF T *NS(_quick_sort_early_stop)(
779-
T *first,
780-
T *last,
781-
int (*compare)(const T*, const T*, void*),
782-
void* compare_ctx
783-
);
784-
785-
ALGDEF T *NS(_merge_sort_adaptive_with_buffer_n)(
786-
T* first,
787-
size_t count,
788-
T* buffer,
789-
int (*compare)(const T* a, const T* b, void*),
790-
void* compare_ctx
791-
);
792-
793-
794748
#ifdef ARRAY_ALG_IMPLEMENTATION
795749

796750
ALGDEF T *NS(find_if)(
@@ -1663,7 +1617,7 @@ typedef struct {
16631617
void* compare_ctx;
16641618
} NS(_lower_upper_bound_closure);
16651619

1666-
ALGDEF int NS(_lower_bound_predicate)(const T* x, void* ctx) {
1620+
static int NS(_lower_bound_predicate)(const T* x, void* ctx) {
16671621
const NS(_lower_upper_bound_closure)* c = ctx;
16681622
return c->compare(x, c->value, c->compare_ctx) < 0;
16691623
}
@@ -1684,8 +1638,7 @@ ALGDEF T *NS(lower_bound)(
16841638
return NS(partition_point)(first, last, NS(_lower_bound_predicate), &ctx);
16851639
}
16861640

1687-
1688-
ALGDEF int NS(_upper_bound_predicate)(const T* x, void* ctx) {
1641+
static int NS(_upper_bound_predicate)(const T* x, void* ctx) {
16891642
const NS(_lower_upper_bound_closure)* c = ctx;
16901643
return c->compare(c->value, x, c->compare_ctx) >= 0;
16911644
}
@@ -1963,7 +1916,7 @@ ALGDEF void NS(sort_heap)(
19631916
}
19641917
}
19651918

1966-
ALGDEF void NS(_insertion_sort_unguarded)(
1919+
static void NS(_insertion_sort_unguarded)(
19671920
T *restrict first,
19681921
T *last,
19691922
int (*compare)(const T*, const T*, void*),
@@ -2006,7 +1959,16 @@ ALGDEF void NS(insertion_sort)(
20061959
NS(_insertion_sort_unguarded)(suffix, last, compare, compare_ctx);
20071960
}
20081961

2009-
ALGDEF T* NS(_sort_partition)(
1962+
/// Hoare partitioning:
1963+
/// https://en.wikipedia.org/wiki/Quicksort#Hoare_partition_scheme
1964+
///
1965+
/// Partially sorts [first, last) into two parts, divided
1966+
/// by a pointer m such that:
1967+
/// for all a in [first, m) and b in [m, last) a <= b.
1968+
///
1969+
/// requires:
1970+
/// - first != last
1971+
static T* NS(_sort_partition)(
20101972
T *first,
20111973
T *last,
20121974
int (*compare)(const T*, const T*, void*),
@@ -2036,7 +1998,7 @@ ALGDEF T* NS(_sort_partition)(
20361998
}
20371999
}
20382000

2039-
ALGDEF T *NS(_quick_sort_early_stop)(
2001+
static T *NS(_quick_sort_early_stop)(
20402002
T *first,
20412003
T *last,
20422004
int (*compare)(const T*, const T*, void*),
@@ -2067,7 +2029,7 @@ ALGDEF void NS(sort)(
20672029
NS(_insertion_sort_unguarded)(first, last, compare, compare_ctx);
20682030
}
20692031

2070-
ALGDEF void NS(_rotate_right_by_one)(
2032+
static void NS(_rotate_right_by_one)(
20712033
T *first,
20722034
T *last
20732035
) {
@@ -2097,7 +2059,7 @@ ALGDEF void NS(insertion_sort_stable)(
20972059
NS(_insertion_sort_unguarded)(suffix, last, compare, compare_ctx);
20982060
}
20992061

2100-
ALGDEF T *NS(_merge_sort_adaptive_with_buffer_n)(
2062+
static T *NS(_merge_sort_adaptive_with_buffer_n)(
21012063
T* first,
21022064
size_t count,
21032065
T* buffer,

test/defs.h

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,4 +25,13 @@ typedef struct {
2525
#define ARRAY_ALG_PREFIX person_array_
2626
#include "../array_alg.h"
2727

28+
// Import private functions for testing.
29+
#define ARRAY_ALG_STATIC
30+
#define ARRAY_ALG_IMPLEMENTATION
31+
#define ARRAY_ALG_TYPE int
32+
#define ARRAY_ALG_PREFIX intv_private_
33+
#include "../array_alg.h"
34+
#undef ARRAY_ALG_IMPLEMENTATION
35+
#undef ARRAY_ALG_STATIC
36+
2837
#endif // DEFS_H_

test/tests.c

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -566,7 +566,7 @@ void test_sort_partition() {
566566
nums[i] = ARRAY_ALG_RANDOM(100);
567567
}
568568

569-
int* part = intv__sort_partition(nums, nums + N, compare_int, NULL);
569+
int* part = intv_private__sort_partition(nums, nums + N, compare_int, NULL);
570570
if (!intv_all_of(nums, part, _is_less_equal_than, part)) {
571571
print_array(nums, N);
572572
printf("partition: %d\n", *part);
@@ -878,12 +878,10 @@ int main() {
878878
printf("-- test_sort --\n"); test_sort();
879879
printf("-- test_c_qsort --\n"); test_c_qsort();
880880

881-
882881
// EXTENSIONS
883882
printf("-- test_is_strictly_increasing --\n"); test_is_strictly_increasing();
884883
printf("-- test_insert--\n"); test_insert();
885884

886-
887885
// BENCHMARKS
888886
printf("BENCHMARKS\n");
889887
printf("-- heap_sort --\n"); benchmark_sort(intv_heap_sort, 1000000);

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

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:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy