arraydeque icon indicating copy to clipboard operation
arraydeque copied to clipboard

Access raw single backing slice?

Open jgarvin opened this issue 3 years ago • 1 comments

In circumstances where a user knows they are not going to pop_back/front passed a particular item, but may otherwise be doing various pushes and pops, it would be nice to be able to hold on to an index/iterator that would keep working as long as the specific item being referred to hasn't been removed. The most straightforward way to do this is to give the user the index of an item in the array and an interface for accessing it (where the index is used directly on the raw array backing the container, without taking into account the current location of head or tail).

You can kind of get this effect now by calling as_slices or as_mut_slices right when the container is created and only using the first slice, but this relies on knowledge of implementation details for the initial container (e.g. since the buffer is circular in principle there is no reason that tail couldn't start at N/2). Is there a better way?

jgarvin avatar Feb 12 '21 04:02 jgarvin

It's possible to have an external indexer to remember the raw index on the slice and it can be used to access the ArrayDeque while hiding the details. Dfs of petgragh pretty much does the same thing.

The interface may be:

let mut queue = ArrayDeque::new();
let item: Option<_> = queue.get_slot(5);

andylokandy avatar Dec 31 '21 15:12 andylokandy