Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

array: reversal of comparison function arguments, to match that of bs…

…earch(3)
  • Loading branch information...
commit 5258c746e4a69082d0395ed0cd214e8e3d4dcde3 1 parent 965b25b
Natacha Porté authored
Showing with 18 additions and 17 deletions.
  1. +14 −14 array.c
  2. +2 −1  array.h
  3. +2 −2 markdown.c
View
28 array.c
@@ -142,10 +142,10 @@ arr_sorted_find(struct array *arr, void *key, array_cmp_fn cmp) {
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
- ret = cmp(ptr + cu * arr->unit, key);
+ ret = cmp(key, ptr + cu * arr->unit);
if (ret == 0) return ptr + cu * arr->unit;
- else if (ret < 0) mi = cu;
- else /* if (ret > 0) */ ma = cu; }
+ else if (ret < 0) ma = cu;
+ else /* if (ret > 0) */ mi = cu; }
return 0; }
@@ -159,14 +159,14 @@ arr_sorted_find_i(struct array *arr, void *key, array_cmp_fn cmp) {
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
- ret = cmp(ptr + cu * arr->unit, key);
+ ret = cmp(key, ptr + cu * arr->unit);
if (ret == 0) {
while (cu < arr->size && ret == 0) {
cu += 1;
- ret = cmp(ptr + cu * arr->unit, key); }
+ ret = cmp(key, ptr + cu * arr->unit); }
return cu; }
- else if (ret < 0) mi = cu;
- else /* if (ret > 0) */ ma = cu; }
+ else if (ret < 0) ma = cu;
+ else /* if (ret > 0) */ mi = cu; }
return ma; }
@@ -264,10 +264,10 @@ parr_sorted_find(struct parray *arr, void *key, array_cmp_fn cmp) {
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
- ret = cmp(arr->item[cu], key);
+ ret = cmp(key, arr->item[cu]);
if (ret == 0) return arr->item[cu];
- else if (ret < 0) mi = cu;
- else /* if (ret > 0) */ ma = cu; }
+ else if (ret < 0) ma = cu;
+ else /* if (ret > 0) */ mi = cu; }
return 0; }
@@ -280,14 +280,14 @@ parr_sorted_find_i(struct parray *arr, void *key, array_cmp_fn cmp) {
ma = arr->size;
while (mi < ma - 1) {
cu = mi + (ma - mi) / 2;
- ret = cmp(arr->item[cu], key);
+ ret = cmp(key, arr->item[cu]);
if (ret == 0) {
while (cu < arr->size && ret == 0) {
cu += 1;
- ret = cmp(arr->item[cu], key); }
+ ret = cmp(key, arr->item[cu]); }
return cu; }
- else if (ret < 0) mi = cu;
- else /* if (ret > 0) */ ma = cu; }
+ else if (ret < 0) ma = cu;
+ else /* if (ret > 0) */ mi = cu; }
return ma; }
View
3  array.h
@@ -42,7 +42,7 @@ struct parray {
/* array_cmp_fn • comparison functions for sorted arrays */
-typedef int (*array_cmp_fn)(void *array_entry, void *key);
+typedef int (*array_cmp_fn)(void *key, void *array_entry);
@@ -83,6 +83,7 @@ void
arr_remove(struct array *, int);
/* arr_sorted_find • O(log n) search in a sorted array, returning entry */
+/* equivalent to bsearch(key, arr->base, arr->size, arr->unit, cmp) */
void *
arr_sorted_find(struct array *, void *key, array_cmp_fn cmp);
View
4 markdown.c
@@ -106,9 +106,9 @@ static struct html_tag block_tags[] = {
/* cmp_link_ref • comparison function for link_ref sorted arrays */
static int
-cmp_link_ref(void *array_entry, void *key) {
+cmp_link_ref(void *key, void *array_entry) {
struct link_ref *lr = array_entry;
- return bufcasecmp(lr->id, key); }
+ return bufcasecmp(key, lr->id); }
/* cmp_html_tag • comparison function for bsearch() (stolen from discount) */
Please sign in to comment.
Something went wrong with that request. Please try again.