Skip to content

Commit d99527b

Browse files
committed
Make compare/hash functions take void pointers, removing the need to
cast the functions to the appropriate type when used.
1 parent 91d1b76 commit d99527b

14 files changed

+80
-77
lines changed

src/compare-int.c

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,13 +37,25 @@ POSSIBILITY OF SUCH DAMAGE.
3737

3838
/* Comparison functions for a pointer to an integer */
3939

40-
int int_equal(int *location1, int *location2)
40+
int int_equal(void *vlocation1, void *vlocation2)
4141
{
42+
int *location1;
43+
int *location2;
44+
45+
location1 = (int *) vlocation1;
46+
location2 = (int *) vlocation2;
47+
4248
return *location1 == *location2;
4349
}
4450

45-
int int_compare(int *location1, int *location2)
51+
int int_compare(void *vlocation1, void *vlocation2)
4652
{
53+
int *location1;
54+
int *location2;
55+
56+
location1 = (int *) vlocation1;
57+
location2 = (int *) vlocation2;
58+
4759
if (*location1 < *location2) {
4860
return -1;
4961
} else if (*location1 > *location2) {

src/compare-int.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ extern "C" {
6161
* two values are not equal.
6262
*/
6363

64-
int int_equal(int *location1, int *location2);
64+
int int_equal(void *location1, void *location2);
6565

6666
/**
6767
* Compare the integer values pointed at by two pointers.
@@ -74,7 +74,7 @@ int int_equal(int *location1, int *location2);
7474
* they are equal.
7575
*/
7676

77-
int int_compare(int *location1, int *location2);
77+
int int_compare(void *location1, void *location2);
7878

7979
#ifdef __cplusplus
8080
}

src/compare-pointer.c

Lines changed: 2 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -45,14 +45,9 @@ int pointer_equal(void *location1, void *location2)
4545

4646
int pointer_compare(void *location1, void *location2)
4747
{
48-
unsigned long a1, a2;
49-
50-
a1 = (unsigned long) location1;
51-
a2 = (unsigned long) location2;
52-
53-
if (a1 < a2) {
48+
if (location1 < location2) {
5449
return -1;
55-
} else if (a1 > a2) {
50+
} else if (location1 > location2) {
5651
return 1;
5752
} else {
5853
return 0;

src/compare-string.c

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -41,16 +41,16 @@ POSSIBILITY OF SUCH DAMAGE.
4141

4242
/* Comparison functions for strings */
4343

44-
int string_equal(char *string1, char *string2)
44+
int string_equal(void *string1, void *string2)
4545
{
46-
return strcmp(string1, string2) == 0;
46+
return strcmp((char *) string1, (char *) string2) == 0;
4747
}
4848

49-
int string_compare(char *string1, char *string2)
49+
int string_compare(void *string1, void *string2)
5050
{
5151
int result;
5252

53-
result = strcmp(string1, string2);
53+
result = strcmp((char *) string1, (char *) string2);
5454

5555
if (result < 0) {
5656
return -1;
@@ -63,25 +63,25 @@ int string_compare(char *string1, char *string2)
6363

6464
/* Comparison functions for strings, which ignore the case of letters. */
6565

66-
int string_nocase_equal(char *string1, char *string2)
66+
int string_nocase_equal(void *string1, void *string2)
6767
{
68-
return string_nocase_compare(string1, string2) == 0;
68+
return string_nocase_compare((char *) string1, (char *) string2) == 0;
6969
}
7070

7171
/* On many systems, strcasecmp or stricmp will give the same functionality
7272
* as this function. However, it is non-standard and cannot be relied
7373
* on to be present. */
7474

75-
int string_nocase_compare(char *string1, char *string2)
75+
int string_nocase_compare(void *string1, void *string2)
7676
{
7777
char *p1;
7878
char *p2;
7979
int c1, c2;
8080

8181
/* Iterate over each character in the strings */
8282

83-
p1 = string1;
84-
p2 = string2;
83+
p1 = (char *) string1;
84+
p2 = (char *) string2;
8585

8686
for (;;) {
8787

src/compare-string.h

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ extern "C" {
6262
* not equal.
6363
*/
6464

65-
int string_equal(char *string1, char *string2);
65+
int string_equal(void *string1, void *string2);
6666

6767
/**
6868
* Compare two strings.
@@ -75,7 +75,7 @@ int string_equal(char *string1, char *string2);
7575
* zero if the two strings are equal.
7676
*/
7777

78-
int string_compare(char *string1, char *string2);
78+
int string_compare(void *string1, void *string2);
7979

8080
/**
8181
* Compare two strings to determine if they are equal, ignoring the
@@ -87,7 +87,7 @@ int string_compare(char *string1, char *string2);
8787
* not equal.
8888
*/
8989

90-
int string_nocase_equal(char *string1, char *string2);
90+
int string_nocase_equal(void *string1, void *string2);
9191

9292
/**
9393
* Compare two strings, ignoring the case of letters.
@@ -100,7 +100,7 @@ int string_nocase_equal(char *string1, char *string2);
100100
* zero if the two strings are equal.
101101
*/
102102

103-
int string_nocase_compare(char *string1, char *string2);
103+
int string_nocase_compare(void *string1, void *string2);
104104

105105
#ifdef __cplusplus
106106
}

src/hash-int.c

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,12 @@ POSSIBILITY OF SUCH DAMAGE.
3737

3838
/* Hash function for a pointer to an integer */
3939

40-
unsigned long int_hash(int *location)
40+
unsigned long int_hash(void *vlocation)
4141
{
42+
int *location;
43+
44+
location = (int *) vlocation;
45+
4246
return (unsigned long) *location;
4347
}
4448

src/hash-int.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" {
5454
* @return A hash key for the value at the location.
5555
*/
5656

57-
unsigned long int_hash(int *location);
57+
unsigned long int_hash(void *location);
5858

5959
#ifdef __cplusplus
6060
}

src/hash-string.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.
3939

4040
/* String hash function */
4141

42-
unsigned long string_hash(char *string)
42+
unsigned long string_hash(void *string)
4343
{
4444
/* This is the djb2 string hash function */
4545

@@ -59,7 +59,7 @@ unsigned long string_hash(char *string)
5959
/* The same function, with a tolower on every character so that
6060
* case is ignored. This code is duplicated for performance. */
6161

62-
unsigned long string_nocase_hash(char *string)
62+
unsigned long string_nocase_hash(void *string)
6363
{
6464
unsigned long result = 5381;
6565
unsigned char *p;

src/hash-string.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ extern "C" {
5454
* @return A hash key for the string.
5555
*/
5656

57-
unsigned long string_hash(char *string);
57+
unsigned long string_hash(void *string);
5858

5959
/**
6060
* Generate a hash key from a string, ignoring the case of letters.
@@ -63,7 +63,7 @@ unsigned long string_hash(char *string);
6363
* @return A hash key for the string.
6464
*/
6565

66-
unsigned long string_nocase_hash(char *string);
66+
unsigned long string_nocase_hash(void *string);
6767

6868
#ifdef __cplusplus
6969
}

test/test-arraylist.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -290,20 +290,17 @@ void test_arraylist_index_of(void)
290290

291291
val = entries[i];
292292

293-
index = arraylist_index_of(arraylist, (ArrayListEqualFunc) int_equal,
294-
&val);
293+
index = arraylist_index_of(arraylist, int_equal, &val);
295294

296295
assert(index == i);
297296
}
298297

299298
/* Check invalid values */
300299

301300
val = 0;
302-
assert(arraylist_index_of(arraylist, (ArrayListEqualFunc) int_equal,
303-
&val) < 0);
301+
assert(arraylist_index_of(arraylist, int_equal, &val) < 0);
304302
val = 57;
305-
assert(arraylist_index_of(arraylist, (ArrayListEqualFunc) int_equal,
306-
&val) < 0);
303+
assert(arraylist_index_of(arraylist, int_equal, &val) < 0);
307304
}
308305

309306
void test_arraylist_clear(void)
@@ -343,7 +340,7 @@ void test_arraylist_sort(void)
343340
arraylist_prepend(arraylist, &entries[i]);
344341
}
345342

346-
arraylist_sort(arraylist, (ArrayListCompareFunc) int_compare);
343+
arraylist_sort(arraylist, int_compare);
347344

348345
/* List length is unchanged */
349346

@@ -362,7 +359,7 @@ void test_arraylist_sort(void)
362359

363360
arraylist = arraylist_new(5);
364361

365-
arraylist_sort(arraylist, (ArrayListCompareFunc) int_compare);
362+
arraylist_sort(arraylist, int_compare);
366363

367364
assert(arraylist->length == 0);
368365

@@ -371,7 +368,7 @@ void test_arraylist_sort(void)
371368
arraylist = arraylist_new(5);
372369

373370
arraylist_prepend(arraylist, &entries[0]);
374-
arraylist_sort(arraylist, (ArrayListCompareFunc) int_compare);
371+
arraylist_sort(arraylist, int_compare);
375372

376373
assert(arraylist->length == 1);
377374
assert(arraylist->data[0] == &entries[0]);

0 commit comments

Comments
 (0)