简体   繁体   English

数组的循环缓冲区?

[英]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 如果每个元素的大小都相同,即D1 x D2,则可以分配一个数组

double data[D1*D2*numElem]

and use row/column major indexing ( i*D2 + j or i*D1 + j) to access values within each element. 并使用行/列主索引(i * D2 + j或i * D1 + j)访问每个元素中的值。 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. 如果容器是稳定的并且在公共元素上进行迭代,则单个缓冲区可能会更好。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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