问题描述
在无需使用 for 循环重新排列所有元素的情况下,完成将一个元素插入到其他元素之前的最有效方法是什么?我尝试了循环缓冲区实现,但它不起作用,我感到很沮丧。我把问题简化了,所以希望每个人都更容易。
int * buffer = (int *) calloc(L_buffer,sizeof(int));
int i,j,K,out;
while(1) {
i = rand();
K = 3;
/* Calculate output */
out = buffer[i] buffer[i+1] + buffer[K];
/* Shift array elements by one and insert new value */
for(j = 0; j < L_buffer-1; j++)
buffer[j+1] = buffer[j];
buffer[0] = new_value;
}
解决办法: 我编辑了@Eric Postpischil 的解决方案以满足我的需求。通过将函数扩展到以下,我可以在两个方向上推进当前索引并始终环绕。
int * cb_element(CircularBuffer *cb,ptrdiff_t i) {
if ( cb->current < 0)
cb->current += cb->size;
ptrdiff_t index = cb->current + i;
if ((size_t) index >= cb->size)
index -= cb->size;
return &cb->array[index];
}
所以我可以
out = *cb_element(&cb,i) + *cb_element(&cb,i+1);
cb.current--;
*cb_element(&cb,0) = new_value;
真的很整洁!
解决方法
可能有比使用这么多行更优雅的方法。
使用无符号数学对 % L_array
数组索引访问进行“修改”。此外,如果 L_array
是无符号的 2 次幂,% L_array
会导致简单地发出和代码。
output = p_dynamic[i] + p_dynamic[(i+1)% L_array] + p_dynamic[(i+2)% L_array];
注意:除非代码依赖于未定义的行为,否则 --p_dynamic < p_start
永远不会为真。用于比较 p_dynamic
的有效值为 array[0]...array[length]
,不包括 array[-1]
。
对于循环缓冲区,插入看起来很可疑。 for
循环应该没有理由shift。而是调整对缓冲区末尾的引用。
考虑使用以下内容:
array = calloc(L_array,sizeof *array);
size_t length = 0; // current length
size_t begin = 0; // Index of eldest sample
size_t end = 0; // Index to store the next sample
插入
if (length < L_array) {
length++;
array[end] = new_value;
end = (end+1)%L_array;
}
提取
if (length > 0) {
length--;
sample = array[begin];
begin = (begin+1)%L_array;
}
从 i
开始采样 3 个元素
if (length >= 3) {
output = array[(begin+i) % L_array] + array[(begin+i+1) % L_array] +
array[(begin+i+2) % L_array];
}
,
int *p_dynamic = array[0];
int *p_start = array[0];
int *p_end = array[L_array - 1];
您没有告诉我们这些变量的目的是什么,所以我们不知道您是否以合理的方式使用它们来实现它们的预期目的。
if (--p_dynamic < p_start)
当 p_dynamic
指向 array[0]
时,--p_dynamic
的行为不是由 C 标准定义的。该标准仅定义了从数组元素 0 到刚好超出数组最后一个元素的指针算法。
int K = 3;
int i = some_int;
int output;
output = array[i] + array[i+1] + array[K];
同样,你没有告诉我们意图,所以我们不知道你在这里想要达到什么目的。为什么 K
是 3?这是每次使用代码时的固定常量还是只是一个示例?它与i
有任何关系吗?您的意思是 array[K]
表示将始终添加实际元素 array[3]
,还是表示将添加距循环缓冲区当前起点偏移 3 处的元素?
if (p_dynamic[K] > p_end &&
p_dynamic[i] < p_end &&
p_dynamic[i+1] < p_end)
这是没有意义的,因为 p_dynamic
是一个指向 int
的指针,所以 p_dynamic[K]
是一个 int
,而 p_end
是一个指向 int
的指针{1}},因此 p_dynamic[K] > p_end
尝试将 int
与指针进行比较。我怀疑您试图询问元素 p_dynamic[K]
是否会超出数组的末尾。您可以使用 p_dynamic + K > p_end
执行此操作,但与 --p_dynamic
一样,如果它超出数组末尾,则 C 标准未定义指针算术。
通过创建一个函数来帮助您访问数组元素来简化您的代码。制作一个描述循环缓冲区的结构。简单地说,我假设您有一个固定的缓冲区大小 Size
个元素。
typedef struct
{
int array[Size];
ptrdiff_t current;
} CircularBuffer;
成员 current
将是当前缓冲区中最旧元素的索引(下标),即循环缓冲区的当前“开始”。 (ptrdiff_t
类型在标头 <stddef.h>
中定义。它可以用于数组下标。
然后创建一个函数,该函数为您提供指向索引为 i
的元素的指针:
int *CBElement(CircularBuffer *cb,ptrdiff_t i)
{
ptrdiff_t index = i + current;
if (Size <= index) index -= Size;
return &cb->array[index];
}
那么就可以引用数组元素K
,例如*CBElement(&cb,K)
:
output = *CBElement(&cb,i) + *CBElement(&cb,i+1) + *CBElement(&cb,K);
您也可以在数组中放入新值:
*CBElement(&cb,i) = NewValue;
要推进缓冲区,请以明显的方式更新 current
成员,并在新的最后一个元素 (*CBElement(&cb,Size-1)
) 中放置一个新值。
这应该可以简化使代码工作的任务。运行后,您可以考虑对缓冲区实现进行优化和细化。