summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorDavid Robillard <d@drobilla.net>2018-11-14 01:21:25 +0100
committerDavid Robillard <d@drobilla.net>2018-11-14 12:10:06 +0100
commitf928e67b46bca4922532d909bddd116f3d991179 (patch)
tree37ded66bd0f3d5bdab9591f0beee5e4131bf07ec
parentaefc020257e5cf89ecec036a33ef1637297cecf7 (diff)
downloadzix-f928e67b46bca4922532d909bddd116f3d991179.tar.gz
zix-f928e67b46bca4922532d909bddd116f3d991179.tar.bz2
zix-f928e67b46bca4922532d909bddd116f3d991179.zip
Make comparator functions take const user data
-rw-r--r--test/btree_test.c4
-rw-r--r--test/sorted_array_test.c2
-rw-r--r--test/tree_bench.c15
-rw-r--r--test/tree_test.c2
-rw-r--r--zix/btree.c4
-rw-r--r--zix/btree.h4
-rw-r--r--zix/common.h4
7 files changed, 22 insertions, 13 deletions
diff --git a/test/btree_test.c b/test/btree_test.c
index 121d424..8257add 100644
--- a/test/btree_test.c
+++ b/test/btree_test.c
@@ -49,7 +49,7 @@ unique_rand(uint32_t i)
}
static int
-int_cmp(const void* a, const void* b, void* user_data)
+int_cmp(const void* a, const void* b, const void* user_data)
{
const uintptr_t ia = (uintptr_t)a;
const uintptr_t ib = (uintptr_t)b;
@@ -90,7 +90,7 @@ wildcard_cut(int test_num, size_t n_elems)
/** Wildcard comparator where 0 matches anything >= wildcard_cut(n_elems). */
static int
-wildcard_cmp(const void* a, const void* b, void* user_data)
+wildcard_cmp(const void* a, const void* b, const void* user_data)
{
const TestContext* ctx = (TestContext*)user_data;
const size_t n_elems = ctx->n_elems;
diff --git a/test/sorted_array_test.c b/test/sorted_array_test.c
index b235753..e7faa7a 100644
--- a/test/sorted_array_test.c
+++ b/test/sorted_array_test.c
@@ -32,7 +32,7 @@
unsigned seed = 1;
static int
-int_cmp(const void* a, const void* b, void* user_data)
+int_cmp(const void* a, const void* b, const void* user_data)
{
const intptr_t ia = *(const intptr_t*)a;
const intptr_t ib = *(const intptr_t*)b;
diff --git a/test/tree_bench.c b/test/tree_bench.c
index adb395f..58535e4 100644
--- a/test/tree_bench.c
+++ b/test/tree_bench.c
@@ -45,7 +45,7 @@ unique_rand(uint32_t i)
}
static int
-int_cmp(const void* a, const void* b, void* user_data)
+int_cmp(const void* a, const void* b, const void* user_data)
{
const intptr_t ia = (intptr_t)a;
const intptr_t ib = (intptr_t)b;
@@ -60,6 +60,12 @@ int_cmp(const void* a, const void* b, void* user_data)
}
static int
+g_int_cmp(const void* a, const void* b, void* user_data)
+{
+ return int_cmp(a, b, user_data);
+}
+
+static int
test_fail(const char* fmt, ...)
{
va_list args;
@@ -304,7 +310,7 @@ bench_glib(size_t n_elems,
struct timespec insert_start = bench_start();
for (size_t i = 0; i < n_elems; ++i) {
r = unique_rand(i);
- GSequenceIter* iter = g_sequence_insert_sorted(t, (void*)r, int_cmp, NULL);
+ GSequenceIter* iter = g_sequence_insert_sorted(t, (void*)r, g_int_cmp, NULL);
if (!iter || g_sequence_iter_is_end(iter)) {
return test_fail("Failed to insert %zu\n", r);
}
@@ -315,7 +321,7 @@ bench_glib(size_t n_elems,
struct timespec search_start = bench_start();
for (size_t i = 0; i < n_elems; ++i) {
r = unique_rand(i);
- GSequenceIter* iter = g_sequence_lookup(t, (void*)r, int_cmp, NULL);
+ GSequenceIter* iter = g_sequence_lookup(t, (void*)r, g_int_cmp, NULL);
if (!iter || g_sequence_iter_is_end(iter)) {
return test_fail("Failed to find %zu\n", r);
}
@@ -335,7 +341,8 @@ bench_glib(size_t n_elems,
struct timespec del_start = bench_start();
for (size_t i = 0; i < n_elems; ++i) {
r = unique_rand(i);
- GSequenceIter* iter = g_sequence_lookup(t, (void*)r, int_cmp, NULL);
+ GSequenceIter* iter =
+ g_sequence_lookup(t, (void*)r, g_int_cmp, NULL);
if (!iter || g_sequence_iter_is_end(iter)) {
return test_fail("Failed to remove %zu\n", r);
}
diff --git a/test/tree_test.c b/test/tree_test.c
index b806acd..ac85ea4 100644
--- a/test/tree_test.c
+++ b/test/tree_test.c
@@ -31,7 +31,7 @@
unsigned seed = 1;
static int
-int_cmp(const void* a, const void* b, void* user_data)
+int_cmp(const void* a, const void* b, const void* user_data)
{
const intptr_t ia = (intptr_t)a;
const intptr_t ib = (intptr_t)b;
diff --git a/zix/btree.c b/zix/btree.c
index 78a5a0d..f337f7d 100644
--- a/zix/btree.c
+++ b/zix/btree.c
@@ -36,7 +36,7 @@ struct ZixBTreeImpl {
ZixBTreeNode* root;
ZixDestroyFunc destroy;
ZixComparator cmp;
- void* cmp_data;
+ const void* cmp_data;
size_t size;
unsigned height; ///< Number of levels, i.e. root only has height 1
};
@@ -109,7 +109,7 @@ zix_btree_node_new(const bool leaf)
ZIX_API ZixBTree*
zix_btree_new(const ZixComparator cmp,
- void* const cmp_data,
+ const void* const cmp_data,
const ZixDestroyFunc destroy)
{
ZixBTree* t = (ZixBTree*)malloc(sizeof(ZixBTree));
diff --git a/zix/btree.h b/zix/btree.h
index 29c01d3..46daa24 100644
--- a/zix/btree.h
+++ b/zix/btree.h
@@ -1,5 +1,5 @@
/*
- Copyright 2011-2014 David Robillard <http://drobilla.net>
+ Copyright 2011-2016 David Robillard <http://drobilla.net>
Permission to use, copy, modify, and/or distribute this software for any
purpose with or without fee is hereby granted, provided that the above
@@ -57,7 +57,7 @@ typedef struct ZixBTreeIterImpl ZixBTreeIter;
*/
ZIX_API ZixBTree*
zix_btree_new(ZixComparator cmp,
- void* cmp_data,
+ const void* cmp_data,
ZixDestroyFunc destroy);
/**
diff --git a/zix/common.h b/zix/common.h
index 71e8a22..a59c033 100644
--- a/zix/common.h
+++ b/zix/common.h
@@ -86,7 +86,9 @@ zix_strerror(const ZixStatus status)
/**
Function for comparing two elements.
*/
-typedef int (*ZixComparator)(const void* a, const void* b, void* user_data);
+typedef int (*ZixComparator)(const void* a,
+ const void* b,
+ const void* user_data);
/**
Function for testing equality of two elements.