/** @brief Remove a slice of the dynar, sliding the rest of the values to the left * * This function removes an n-sized slice that starts at element idx. It is equivalent to xbt_dynar_remove_at with a * NULL object argument if n equals to 1. * * Each of the removed elements is freed using the free_f function passed at dynar creation. */ void xbt_dynar_remove_n_at(xbt_dynar_t const dynar, const unsigned int n, const int idx) { unsigned long nb_shift; unsigned long offset; unsigned long cur; if (!n) return; _sanity_check_dynar(dynar); _check_inbound_idx(dynar, idx); _check_inbound_idx(dynar, idx + n - 1); if (dynar->free_f) { for (cur = idx; cur < idx + n; cur++) { dynar->free_f(_xbt_dynar_elm(dynar, cur)); } } nb_shift = dynar->used - n - idx; if (nb_shift) { offset = nb_shift * dynar->elmsize; memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + n), offset); } dynar->used -= n; }
/** @brief Remove the Nth dynar's element, sliding the previous values to the left * * Get the Nth element of a dynar, removing it from the dynar and moving * all subsequent values to one position left in the dynar. * * If the object argument of this function is a non-null pointer, the removed * element is copied to this address. If not, the element is freed using the * free_f function passed at dynar creation. */ void xbt_dynar_remove_at(xbt_dynar_t const dynar, const int idx, void *const object) { unsigned long nb_shift; unsigned long offset; _sanity_check_dynar(dynar); _check_inbound_idx(dynar, idx); if (object) { _xbt_dynar_get_elm(object, dynar, idx); } else if (dynar->free_f) { dynar->free_f(_xbt_dynar_elm(dynar, idx)); } nb_shift = dynar->used - 1 - idx; if (nb_shift) { offset = nb_shift * dynar->elmsize; memmove(_xbt_dynar_elm(dynar, idx), _xbt_dynar_elm(dynar, idx + 1), offset); } dynar->used--; }
/** @brief Set the Nth element of a dynar (expanded if needed). Previous value is freed * * \param dynar * \param idx * \param object * * Set the Nth element of a dynar, expanding the dynar if needed, AND DO free the previous value at this position. If * you don't want to free the previous content, use xbt_dynar_set(). */ void xbt_dynar_replace(xbt_dynar_t dynar, const unsigned long idx, const void *const object) { _sanity_check_dynar(dynar); if (idx < dynar->used && dynar->free_f) { void *const old_object = _xbt_dynar_elm(dynar, idx); dynar->free_f(old_object); } xbt_dynar_set(dynar, idx, object); }