From 3b0fced0a85b3f40e7138340dfb1ddcc630d2d07 Mon Sep 17 00:00:00 2001 From: Martin Quinson Date: Thu, 10 May 2012 10:40:10 +0200 Subject: [PATCH] new function xbt_dynar_search_or_negative() It is useful when you have less than 2 million elements in your dynar and don't want of the extra complexity of catching exceptions when the element is not found. --- ChangeLog | 5 +++++ include/xbt/dynar.h | 1 + src/xbt/dynar.c | 24 +++++++++++++++++++++++- 3 files changed, 29 insertions(+), 1 deletion(-) diff --git a/ChangeLog b/ChangeLog index 095ef382ec..607038f753 100644 --- a/ChangeLog +++ b/ChangeLog @@ -110,6 +110,11 @@ SimGrid (3.7) NOT RELEASED; urgency=low deprecated in the next release. - Dicts of scalar elements (xbt_dicti_*) are deprecated. - Multi-level dictionaries are deprecated. + * Dynars: + - new function xbt_dynar_search_or_negative() that is useful when + you have less than 2 million elements in your dynar and don't + want of the extra complexity of catching exceptions when the + element is not found. * mmalloc module: - Cleanups and simplifications to make it maintainable again. - Exotic features (such as memalign and valloc) were removed. diff --git a/include/xbt/dynar.h b/include/xbt/dynar.h index c86b868a23..87b501c655 100644 --- a/include/xbt/dynar.h +++ b/include/xbt/dynar.h @@ -94,6 +94,7 @@ XBT_PUBLIC(void) xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, void *const dst); XBT_PUBLIC(unsigned int) xbt_dynar_search(xbt_dynar_t const dynar, void *elem); +XBT_PUBLIC(signed int) xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const elem); XBT_PUBLIC(int) xbt_dynar_member(xbt_dynar_t const dynar, void *elem); XBT_PUBLIC(void) xbt_dynar_sort(xbt_dynar_t const dynar, int_f_cpvoid_cpvoid_t compar_fn); diff --git a/src/xbt/dynar.c b/src/xbt/dynar.c index 2b6a2873d7..f979c2188a 100644 --- a/src/xbt/dynar.c +++ b/src/xbt/dynar.c @@ -523,7 +523,9 @@ xbt_dynar_remove_at(xbt_dynar_t const dynar, /** @brief Returns the position of the element in the dynar * - * Raises not_found_error if not found. + * Raises not_found_error if not found. If you have less than 2 millions elements, + * you probably want to use #xbt_dynar_search_or_negative() instead, so that you + * don't have to TRY/CATCH on element not found. */ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem) { @@ -541,6 +543,26 @@ unsigned int xbt_dynar_search(xbt_dynar_t const dynar, void *const elem) dynar); } +/** @brief Returns the position of the element in the dynar (or -1 if not found) + * + * Note that usually, the dynar indices are unsigned integers. If you have more + * than 2 million elements in your dynar, this very function will not work (but the other will). + */ +signed int xbt_dynar_search_or_negative(xbt_dynar_t const dynar, void *const elem) +{ + unsigned long it; + + _dynar_lock(dynar); + for (it = 0; it < dynar->used; it++) + if (!memcmp(_xbt_dynar_elm(dynar, it), elem, dynar->elmsize)) { + _dynar_unlock(dynar); + return it; + } + + _dynar_unlock(dynar); + return -1; +} + /** @brief Returns a boolean indicating whether the element is part of the dynar */ int xbt_dynar_member(xbt_dynar_t const dynar, void *const elem) { -- 2.20.1