简体   繁体   中英

Circular buffer of an array?

I have a two dimensional double I need to keep in a circular buffer. I'd prefer to keep it together in one buffer instead of having to keep up with two single dimensional buffers. Is it possible to do this easily, or would it be best to go ahead and just use two buffers?

If the size of each element is going to be the same, ie D1 x D2, you can just allocate an array

double data[D1*D2*numElem]

and use row/column major indexing ( i*D2 + j or i*D1 + j) to access values within each element. Of course whenever you resize or insert/delete an element a new buffer will have to be allocated and values copied. If the size of each element is variable or you think insertion/deletion/resize are going to be common use two buffers. If the container is stable and iteration over elements common a single buffer may be better.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM