[Bf-committers] Proposed changes re: procedural textures

Emil Brink emil at obsession.se
Thu Nov 30 10:51:12 CET 2006


Mathias Panzenböck wrote:
[...]
> The only ugly thing at the moment:
> The hash_get_sorted_keys() function uses it's own quick-sort 
> implementation (the one from Wikipedia). Somehow stdlibs qsort didn't 
> work. I don't know what I did wrong. I left the qsort call as comment in 
> the source, maybe someone knows what's wrong.

Sure. :)

When sorting an array of pointers, the comparison function will get as
inputs a pair of pointers to those pointers, not the pointers them-
selves.

The attached patch is how I usually do it, and seems to work. I didn't
look over the rest of the code, or the API even, but it seemed well-
written and was very easy to test thank to the included test binary.

I hope that helps, regards

/Emil
-------------- next part --------------
--- hash.c	2006-11-28 23:26:15.000000000 +0100
+++ /home/emil/tmp/hash-1.0-rc1/src/hash.c	2006-11-30 10:48:15.000000000 +0100
@@ -370,7 +370,16 @@ int hash_has(const hash_table_t * table,
 	return OCCUPIED(ptr);
 }
 
-// XXX: can anyone tell me why stdlibs qsort is not working?
+/* String comparison function for qsort(), on an array of string pointers. Input pointers
+ * are pointer to pointers, so we need to dereference them once to get the strings.
+*/
+static int cmp_strings(const void *a, const void *b)
+{
+	const char	*ap = *(const char **) a, *bp = *(const char **) b;
+
+	return strcmp(ap, bp);
+}
+
 hash_const_str_t * hash_get_sorted_keys(const hash_table_t * table) {
 	hash_const_str_t * keys = NULL;
 	
@@ -390,10 +399,8 @@ hash_const_str_t * hash_get_sorted_keys(
 			++ i;
 		}
 
-		if(table->used > 1) {
-//			qsort(keys, table->used, sizeof(hash_const_str_t), (int (*)(const void*,const void*))strcmp);
-			q_sort(keys,0,table->used - 1);
-		}
+		if(table->used > 1)
+			qsort(keys, table->used, sizeof(hash_const_str_t), cmp_strings);
 	}
 
 	return keys;


More information about the Bf-committers mailing list