How do negative logical indices map onto a ring buffer?

remainder pairs with the floor quotient. For positive capacity it yields a slot from zero up to the capacity exclusive.

numpy.remainder in practice

In a four-slot ring, logical index minus one maps to slot three and four maps to zero. Keep a sequence number to detect stale data after slots are reused.

import numpy as np
np.remainder([-1,0,4,5],4)

Result [3, 0, 0, 1]

中文

循环缓冲区怎样处理负的逻辑下标?

remainder 返回与向下取整商配套的余数,正容量下结果位于零与容量之间且不含上界。

numpy.remainder 算例

四格缓冲区的逻辑下标 -1 对应槽 3,4 对应槽 0。物理槽号只能定位位置,判断数据新旧还要保存序列号。

import numpy as np
np.remainder([-1,0,4,5],4)

结果 [3, 0, 0, 1]

References / 参考来源

Keep reading / 继续阅读