5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

リングバッファで自分より前のデータを検索する

Last updated at Posted at 2016-07-31

#リングバッファの構成

#define MAX 5
char ring[MAX];   //リングバッファ

ring[0] [1] [2] [3] [4]が確保される。

char write;       //書込み位置
char read;        //読込み位置
char work;
char point;       //検索ポイント
char search;


char lp,i;

#書込み動作

ring[ write ] = xxx;
write = (write + 1) % MAX;

書込み位置が 0→1→2→3→4→0 と変化する。

#読込み動作

work = ring[ read ];
read = (read +1) % MAX;

同じく読込み位置が 0→1→2→3→4→0 と変化する。

#自分より前のデータから検索位置まで検索させる動作

point = 2;

read の値が 0の場合検索するバッファは【4,3,2】
read の値が 1の場合検索するバッファは【0,4,3,2】
read の値が 2の場合検索するバッファは【-】
read の値が 3の場合検索するバッファは【2】
read の値が 4の場合検索するバッファは【3,2】

lp = (((read - point) < 0 ? read - pint + MAX : read - point);

/* search = (read -1) % MAX; */ /* wrong */
search = (read + MAX - 1 ) % MAX;

for(i=0;i<lp;i++){
    if (ring[search] == xxx) {
        //検索一致の処理
        break;
    }
    search = (search + 1) % MAX;
}
5
6
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
6

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?