How to efficiently wrap the index of a fixed-size circular buffer

前端 未结 6 1017
迷失自我
迷失自我 2021-01-03 04:50

I have a fixed size circular buffer (implemented as an array): upon initialization, the buffer gets filled with the specified maximum number of elements which allows the use

6条回答
  •  一向
    一向 (楼主)
    2021-01-03 05:07

    FWIW, you could always do a parallel array: i = next[i];

    But, really, I've always just done this: i++; if (i >= n) i = 0; OR i = (i+1) % n;

    Regardless, I'd be really surprised if this is ever a significant performance issue.

提交回复
热议问题