* expr-prs.c: (cmp_func) Removed.
(parse_function) Use binary_search() algorithm.
(compare_functions) New function.
(init_func_tab) Use sort() algorithm.
* algorithm.c: (binary_search) New algorithm.
+Fri Dec 19 15:54:45 2003 Ben Pfaff <blp@gnu.org>
+
+ * expr-prs.c: (cmp_func) Removed.
+ (parse_function) Use binary_search() algorithm.
+ (compare_functions) New function.
+ (init_func_tab) Use sort() algorithm.
+
+ * algorithm.c: (binary_search) New algorithm.
+
Fri Dec 19 15:50:45 2003 Ben Pfaff <blp@gnu.org>
* descript.q: (display) Use sort() algorithm instead of qsort().
*/
#include <config.h>
+#include <assert.h>
+#include <limits.h>
#include <stdlib.h>
#include <string.h>
#include "alloc.h"
return copy_if (array, count, size, result, not, &pred_aux);
}
\f
+/* Searches ARRAY, which contains COUNT of SIZE bytes each, using
+ a binary search. Returns any element that equals VALUE, if
+ one exists, or a null pointer otherwise. ARRAY must ordered
+ according to COMPARE. AUX is passed to COMPARE as auxiliary
+ data. */
+void *
+binary_search (const void *array, size_t count, size_t size,
+ void *value,
+ algo_compare_func *compare, void *aux)
+{
+ assert (array != NULL);
+ assert (count <= INT_MAX);
+ assert (aux != NULL);
+
+ if (count != 0)
+ {
+ const unsigned char *first = array;
+ int low = 0;
+ int high = count - 1;
+
+ while (low < high)
+ {
+ int middle = (low + high) / 2;
+ const unsigned char *element = first + middle * size;
+ int cmp = compare (value, element, aux);
+
+ if (cmp > 0)
+ low = middle + 1;
+ else if (cmp < 0)
+ high = middle - 1;
+ else
+ return (void *) element;
+ }
+ }
+ return NULL;
+}
+\f
/* Copyright (C) 1991, 1992, 1996, 1997, 1999 Free Software Foundation, Inc.
This file is part of the GNU C Library.
Written by Douglas C. Schmidt (schmidt@ics.uci.edu).
void *result,
algo_predicate_func *predicate, void *aux);
+/* Searches ARRAY, which contains COUNT of SIZE bytes each, for
+ VALUE, using a binary search. ARRAY must ordered according to
+ COMPARE. AUX is passed to COMPARE as auxiliary data. */
+void *binary_search (const void *array, size_t count, size_t size,
+ void *value,
+ algo_compare_func *compare, void *aux);
+
#endif /* sort-algo.h */
#include <ctype.h>
#include <float.h>
#include <stdlib.h>
+#include "algorithm.h"
#include "alloc.h"
#include "error.h"
#include "expr.h"
union any_node *);
static int type_check (union any_node **n, int type, int flags);
+static algo_compare_func compare_functions;
static void init_func_tab (void);
-static int cmp_func (const void *a, const void *b);
#if DEBUGGING
static void debug_print_tree (union any_node *, int);
struct function f;
f.s = fname;
- fp = bsearch (&f, func_tab, func_count, sizeof *func_tab, cmp_func);
+ fp = binary_search (func_tab, func_count, sizeof *func_tab, &f,
+ compare_functions, NULL);
}
if (!fp)
{"SUBSTR", OP_SUBSTR, generic_str_func, "ssn/n"},
};
+/* An algo_compare_func that compares functions A and B based on
+ their names. */
static int
-cmp_func (const void *a, const void *b)
+compare_functions (const void *a_, const void *b_, void *aux unused)
{
- return strcmp (*(char **) a, *(char **) b);
+ const struct function *a = a_;
+ const struct function *b = b_;
+
+ return strcmp (a->s, b->s);
}
static void
}
func_count = sizeof func_tab / sizeof *func_tab;
- qsort (func_tab, func_count, sizeof *func_tab, cmp_func);
+ sort (func_tab, func_count, sizeof *func_tab, compare_functions, NULL);
}
\f
/* Debug output. */