summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBruno Haible <bruno@clisp.org>2018-12-11 20:43:22 +0100
committerBruno Haible <bruno@clisp.org>2018-12-11 20:43:22 +0100
commit353348a7e26212d596c846461cef49670b8b225d (patch)
tree1191318553829056410ed5b99092efd243b553c3
parent83a66dd9c601577164c4f24a324d0b9cf40c8456 (diff)
downloadgnulib-353348a7e26212d596c846461cef49670b8b225d.tar.gz
array-set: Optimize.
* lib/gl_array_set.c (gl_array_search, gl_array_remove): Test equals_fn outside the loop, not inside the loop.
-rw-r--r--ChangeLog6
-rw-r--r--lib/gl_array_set.c50
2 files changed, 44 insertions, 12 deletions
diff --git a/ChangeLog b/ChangeLog
index 82e3a499cc..787d538cef 100644
--- a/ChangeLog
+++ b/ChangeLog
@@ -1,5 +1,11 @@
2018-12-11 Bruno Haible <bruno@clisp.org>
+ array-set: Optimize.
+ * lib/gl_array_set.c (gl_array_search, gl_array_remove): Test equals_fn
+ outside the loop, not inside the loop.
+
+2018-12-11 Bruno Haible <bruno@clisp.org>
+
times: Fix tests.
* tests/test-times.c (doublecmp): Implement a total order.
diff --git a/lib/gl_array_set.c b/lib/gl_array_set.c
index dd77ef13be..8e985affc8 100644
--- a/lib/gl_array_set.c
+++ b/lib/gl_array_set.c
@@ -79,11 +79,22 @@ gl_array_search (gl_set_t set, const void *elt)
{
gl_setelement_equals_fn equals = set->base.equals_fn;
const void **elements = set->elements;
- size_t i;
+ if (equals != NULL)
+ {
+ size_t i;
- for (i = 0; i < count; i++)
- if (equals != NULL ? equals (elements[i], elt) : elements[i] == elt)
- return true;
+ for (i = 0; i < count; i++)
+ if (equals (elements[i], elt))
+ return true;
+ }
+ else
+ {
+ size_t i;
+
+ for (i = 0; i < count; i++)
+ if (elements[i] == elt)
+ return true;
+ }
}
return false;
}
@@ -155,14 +166,29 @@ gl_array_remove (gl_set_t set, const void *elt)
{
gl_setelement_equals_fn equals = set->base.equals_fn;
const void **elements = set->elements;
- size_t i;
-
- for (i = 0; i < count; i++)
- if (equals != NULL ? equals (elements[i], elt) : elements[i] == elt)
- {
- gl_array_remove_at (set, i);
- return true;
- }
+
+ if (equals != NULL)
+ {
+ size_t i;
+
+ for (i = 0; i < count; i++)
+ if (equals (elements[i], elt))
+ {
+ gl_array_remove_at (set, i);
+ return true;
+ }
+ }
+ else
+ {
+ size_t i;
+
+ for (i = 0; i < count; i++)
+ if (elements[i] == elt)
+ {
+ gl_array_remove_at (set, i);
+ return true;
+ }
+ }
}
return false;
}