Performing a memmove operation for a circular buffer

From: Clint Olsen (clint_at_0lsen.net)
Date: 11/30/04


Date: Tue, 30 Nov 2004 19:59:23 GMT

Hi:

I am implementing an array ADT in C, and my underlying representation is
a circular buffer so that append and prepend operations can take
constant time on average.

One of the last features I need to implement is a 'slice'-like operation
(think Perl). So, in the event I need to insert (or delete) items at a
particular subfield range into the array, I'll need to shift the
potentially data appropriately.

However, after thinking about it, it's not all that easy to do. The
subfield range and shift amount could result in overlap between the
source and the destination. This is further complicated by the fact
that the source and destination locations could be split by a
wrap-around from the end to the front of the array. I haven't decided
if I should just brute-force the darn thing or try to find an elegant
way to handle all the various cases. It would be best if I could make
the minimum alls to memmove and memcpy as possible, since this can be an
expensive operation for large arrays.

Suggestions?

Thanks,

-Clint