Skip to content

Commit

Permalink
Make compare/hash functions take void pointers, removing the need to
Browse files Browse the repository at this point in the history
cast the functions to the appropriate type when used.
  • Loading branch information
fragglet committed Dec 23, 2005
1 parent 91d1b76 commit d99527b
Show file tree
Hide file tree
Showing 14 changed files with 80 additions and 77 deletions.
16 changes: 14 additions & 2 deletions src/compare-int.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,13 +37,25 @@ POSSIBILITY OF SUCH DAMAGE.

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

int int_equal(int *location1, int *location2)
int int_equal(void *vlocation1, void *vlocation2)
{
int *location1;
int *location2;

location1 = (int *) vlocation1;
location2 = (int *) vlocation2;

return *location1 == *location2;
}

int int_compare(int *location1, int *location2)
int int_compare(void *vlocation1, void *vlocation2)
{
int *location1;
int *location2;

location1 = (int *) vlocation1;
location2 = (int *) vlocation2;

if (*location1 < *location2) {
return -1;
} else if (*location1 > *location2) {
Expand Down
4 changes: 2 additions & 2 deletions src/compare-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ extern "C" {
* two values are not equal.
*/

int int_equal(int *location1, int *location2);
int int_equal(void *location1, void *location2);

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

int int_compare(int *location1, int *location2);
int int_compare(void *location1, void *location2);

#ifdef __cplusplus
}
Expand Down
9 changes: 2 additions & 7 deletions src/compare-pointer.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,14 +45,9 @@ int pointer_equal(void *location1, void *location2)

int pointer_compare(void *location1, void *location2)
{
unsigned long a1, a2;

a1 = (unsigned long) location1;
a2 = (unsigned long) location2;

if (a1 < a2) {
if (location1 < location2) {
return -1;
} else if (a1 > a2) {
} else if (location1 > location2) {
return 1;
} else {
return 0;
Expand Down
18 changes: 9 additions & 9 deletions src/compare-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -41,16 +41,16 @@ POSSIBILITY OF SUCH DAMAGE.

/* Comparison functions for strings */

int string_equal(char *string1, char *string2)
int string_equal(void *string1, void *string2)
{
return strcmp(string1, string2) == 0;
return strcmp((char *) string1, (char *) string2) == 0;
}

int string_compare(char *string1, char *string2)
int string_compare(void *string1, void *string2)
{
int result;

result = strcmp(string1, string2);
result = strcmp((char *) string1, (char *) string2);

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

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

int string_nocase_equal(char *string1, char *string2)
int string_nocase_equal(void *string1, void *string2)
{
return string_nocase_compare(string1, string2) == 0;
return string_nocase_compare((char *) string1, (char *) string2) == 0;
}

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

int string_nocase_compare(char *string1, char *string2)
int string_nocase_compare(void *string1, void *string2)
{
char *p1;
char *p2;
int c1, c2;

/* Iterate over each character in the strings */

p1 = string1;
p2 = string2;
p1 = (char *) string1;
p2 = (char *) string2;

for (;;) {

Expand Down
8 changes: 4 additions & 4 deletions src/compare-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ extern "C" {
* not equal.
*/

int string_equal(char *string1, char *string2);
int string_equal(void *string1, void *string2);

/**
* Compare two strings.
Expand All @@ -75,7 +75,7 @@ int string_equal(char *string1, char *string2);
* zero if the two strings are equal.
*/

int string_compare(char *string1, char *string2);
int string_compare(void *string1, void *string2);

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

int string_nocase_equal(char *string1, char *string2);
int string_nocase_equal(void *string1, void *string2);

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

int string_nocase_compare(char *string1, char *string2);
int string_nocase_compare(void *string1, void *string2);

#ifdef __cplusplus
}
Expand Down
6 changes: 5 additions & 1 deletion src/hash-int.c
Original file line number Diff line number Diff line change
Expand Up @@ -37,8 +37,12 @@ POSSIBILITY OF SUCH DAMAGE.

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

unsigned long int_hash(int *location)
unsigned long int_hash(void *vlocation)
{
int *location;

location = (int *) vlocation;

return (unsigned long) *location;
}

2 changes: 1 addition & 1 deletion src/hash-int.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern "C" {
* @return A hash key for the value at the location.
*/

unsigned long int_hash(int *location);
unsigned long int_hash(void *location);

#ifdef __cplusplus
}
Expand Down
4 changes: 2 additions & 2 deletions src/hash-string.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ POSSIBILITY OF SUCH DAMAGE.

/* String hash function */

unsigned long string_hash(char *string)
unsigned long string_hash(void *string)
{
/* This is the djb2 string hash function */

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

unsigned long string_nocase_hash(char *string)
unsigned long string_nocase_hash(void *string)
{
unsigned long result = 5381;
unsigned char *p;
Expand Down
4 changes: 2 additions & 2 deletions src/hash-string.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,7 @@ extern "C" {
* @return A hash key for the string.
*/

unsigned long string_hash(char *string);
unsigned long string_hash(void *string);

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

unsigned long string_nocase_hash(char *string);
unsigned long string_nocase_hash(void *string);

#ifdef __cplusplus
}
Expand Down
15 changes: 6 additions & 9 deletions test/test-arraylist.c
Original file line number Diff line number Diff line change
Expand Up @@ -290,20 +290,17 @@ void test_arraylist_index_of(void)

val = entries[i];

index = arraylist_index_of(arraylist, (ArrayListEqualFunc) int_equal,
&val);
index = arraylist_index_of(arraylist, int_equal, &val);

assert(index == i);
}

/* Check invalid values */

val = 0;
assert(arraylist_index_of(arraylist, (ArrayListEqualFunc) int_equal,
&val) < 0);
assert(arraylist_index_of(arraylist, int_equal, &val) < 0);
val = 57;
assert(arraylist_index_of(arraylist, (ArrayListEqualFunc) int_equal,
&val) < 0);
assert(arraylist_index_of(arraylist, int_equal, &val) < 0);
}

void test_arraylist_clear(void)
Expand Down Expand Up @@ -343,7 +340,7 @@ void test_arraylist_sort(void)
arraylist_prepend(arraylist, &entries[i]);
}

arraylist_sort(arraylist, (ArrayListCompareFunc) int_compare);
arraylist_sort(arraylist, int_compare);

/* List length is unchanged */

Expand All @@ -362,7 +359,7 @@ void test_arraylist_sort(void)

arraylist = arraylist_new(5);

arraylist_sort(arraylist, (ArrayListCompareFunc) int_compare);
arraylist_sort(arraylist, int_compare);

assert(arraylist->length == 0);

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

arraylist_prepend(arraylist, &entries[0]);
arraylist_sort(arraylist, (ArrayListCompareFunc) int_compare);
arraylist_sort(arraylist, int_compare);

assert(arraylist->length == 1);
assert(arraylist->data[0] == &entries[0]);
Expand Down
15 changes: 5 additions & 10 deletions test/test-hashtable.c
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,7 @@ HashTable *generate_hashtable(void)

/* Allocate a new hash table */

hashtable = hash_table_new((HashTableHashFunc) int_hash,
(HashTableEqualFunc) int_equal);
hashtable = hash_table_new(int_hash, int_equal);

/* Insert lots of values */

Expand All @@ -73,8 +72,7 @@ void test_hash_table_new(void)
{
HashTable *hashtable;

hashtable = hash_table_new((HashTableHashFunc) int_hash,
(HashTableEqualFunc) int_equal);
hashtable = hash_table_new(int_hash, int_equal);

assert(hashtable != NULL);
}
Expand All @@ -83,8 +81,7 @@ void test_hash_table_free(void)
{
HashTable *hashtable;

hashtable = hash_table_new((HashTableHashFunc) int_hash,
(HashTableEqualFunc) int_equal);
hashtable = hash_table_new(int_hash, int_equal);

/* Add some values */

Expand Down Expand Up @@ -191,8 +188,7 @@ void test_hash_table_foreach(void)

/* Test iterating over an empty table */

hashtable = hash_table_new((HashTableHashFunc) int_hash,
(HashTableEqualFunc) int_equal);
hashtable = hash_table_new(int_hash, int_equal);

hash_table_foreach_count = 0;

Expand Down Expand Up @@ -254,8 +250,7 @@ void test_hash_table_foreach_remove(void)

/* Test iterating over an empty table */

hashtable = hash_table_new((HashTableHashFunc) int_hash,
(HashTableEqualFunc) int_equal);
hashtable = hash_table_new(int_hash, int_equal);

hash_table_foreach_remove_count = 0;
hash_table_foreach_remove_removed = 0;
Expand Down
20 changes: 10 additions & 10 deletions test/test-list.c
Original file line number Diff line number Diff line change
Expand Up @@ -267,29 +267,29 @@ void test_list_remove_data(void)
/* Test removing invalid data */

val = 0;
assert(list_remove_data(&list, (ListEqualFunc) int_equal, &val) == 0);
assert(list_remove_data(&list, int_equal, &val) == 0);
val = 56;
assert(list_remove_data(&list, (ListEqualFunc) int_equal, &val) == 0);
assert(list_remove_data(&list, int_equal, &val) == 0);
check_list_integrity(list);

/* Remove the number 8 from the list */

val = 8;
assert(list_remove_data(&list, (ListEqualFunc) int_equal, &val) == 1);
assert(list_remove_data(&list, int_equal, &val) == 1);
assert(list_length(list) == num_entries - 1);
check_list_integrity(list);

/* Remove the number 4 from the list (occurs multiple times) */

val = 4;
assert(list_remove_data(&list, (ListEqualFunc) int_equal, &val) == 4);
assert(list_remove_data(&list, int_equal, &val) == 4);
assert(list_length(list) == num_entries - 5);
check_list_integrity(list);

/* Remove the number 89 from the list (first entry) */

val = 89;
assert(list_remove_data(&list, (ListEqualFunc) int_equal, &val) == 1);
assert(list_remove_data(&list, int_equal, &val) == 1);
assert(list_length(list) == num_entries - 6);
check_list_integrity(list);
}
Expand All @@ -308,7 +308,7 @@ void test_list_sort(void)
list_prepend(&list, &entries[i]);
}

list_sort(&list, (ListCompareFunc) int_compare);
list_sort(&list, int_compare);

/* List length is unchanged */

Expand All @@ -327,7 +327,7 @@ void test_list_sort(void)

list = NULL;

list_sort(&list, (ListCompareFunc) int_compare);
list_sort(&list, int_compare);

assert(list == NULL);
}
Expand Down Expand Up @@ -355,7 +355,7 @@ void test_list_find_data(void)

val = entries[i];

result = list_find_data(list, (ListEqualFunc) int_equal, &val);
result = list_find_data(list, int_equal, &val);

assert(result != NULL);

Expand All @@ -366,9 +366,9 @@ void test_list_find_data(void)
/* Check some invalid values return NULL */

val = 0;
assert(list_find_data(list, (ListEqualFunc) int_equal, &val) == NULL);
assert(list_find_data(list, int_equal, &val) == NULL);
val = 56;
assert(list_find_data(list, (ListEqualFunc) int_equal, &val) == NULL);
assert(list_find_data(list, int_equal, &val) == NULL);
}

void test_list_to_array(void)
Expand Down
Loading

0 comments on commit d99527b

Please sign in to comment.