- /* drop the element #idx... and readjust the array */
- if (array->head < array->tail) {
- /* Make sure it's within the boundaries */
- g_assert (array->head < idx && idx <= array->tail);
- element = array->array[idx];
- /* ends not wrapped */
- /* move head-idx to head+1 */
- memcpy (&array->array[array->head + 1],
- &array->array[array->head], (idx - array->head) * sizeof (gpointer));
+
+ /* non-wrapped case */
+ if (first_item_index < last_item_index) {
+ g_assert (first_item_index < idx && idx < last_item_index);
+ /* move everything beyond idx one step towards zero in array */
+ memmove (&array->array[idx],
+ &array->array[idx + 1], (last_item_index - idx) * sizeof (gpointer));
+ /* tail might wrap, ie if tail == 0 (and last_item_index == size) */
+ array->tail = (array->tail - 1 + array->size) % array->size;
+ array->length--;
+ return element;
+ }
+
+ /* only wrapped cases left */
+ g_assert (first_item_index > last_item_index);
+
+ if (idx < last_item_index) {
+ /* idx is before last_item_index, move data towards zero */
+ memmove (&array->array[idx],
+ &array->array[idx + 1], (last_item_index - idx) * sizeof (gpointer));
+ /* tail should not wrap in this case! */
+ g_assert (array->tail > 0);