forked from Debian/bcftools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrbuf.h
134 lines (129 loc) · 3.3 KB
/
rbuf.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#ifndef __RBUF_H__
#define __RBUF_H__
typedef struct
{
int m,n,f; // m: allocated size, n: number of elements in the buffer, f: first element
}
rbuf_t;
/**
* rbuf_init() - initialize round buffer
* @rbuf: the rbuf_t holder
* @size: the maximum number of elements
*
*/
static inline void rbuf_init(rbuf_t *rbuf, int size)
{
rbuf->m = size; rbuf->n = rbuf->f = 0;
}
/**
* rbuf_kth() - get index of the k-th element of the round buffer
* @rbuf: the rbuf_t holder
* @k: 0-based index
*/
static inline int rbuf_kth(rbuf_t *rbuf, int k)
{
if ( k >= rbuf->n || k<0 ) return -1;
int i = k + rbuf->f;
if ( i >= rbuf->m ) i -= rbuf->m;
return i;
}
/**
* rbuf_last() - get index of the last element of the round buffer
* @rbuf: the rbuf_t holder
*
*/
#define rbuf_last(rbuf) rbuf_kth(rbuf, (rbuf)->n - 1)
/**
* rbuf_next() - get index of the next element in the round buffer
* @rbuf: the rbuf_t holder
* @i: pointer to the last rbuf index. Set to -1 before the first call.
*
* Sets i to the next position in the buffer. The return value indicates if
* the position points to a valid element (1) or if there are no more elements
* after *i (0). When the end is reached, *i is set to the first element in the
* buffer.
*/
static inline int rbuf_next(rbuf_t *rbuf, int *i)
{
if ( !rbuf->n ) return 0;
if ( *i==-1 ) { *i = rbuf->f; return 1; }
int n = (rbuf->f <= *i) ? *i - rbuf->f + 1 : *i + rbuf->m - rbuf->f + 1;
if ( ++(*i) >= rbuf->m ) *i = 0;
if ( n < rbuf->n ) return 1;
*i = rbuf->f;
return 0;
}
/**
* rbuf_prev() - get index of the previous element in the round buffer
* @rbuf: the rbuf_t holder
* @i: pointer to the last rbuf index. Set to -1 before the first call.
*
* Sets i to the previous position in the buffer. The return value indicates if
* the position points to a valid element (1) or if there are no more elements
* before *i (0).
*/
static inline int rbuf_prev(rbuf_t *rbuf, int *i)
{
if ( !rbuf->n || *i==rbuf->f ) return 0;
if ( *i==-1 )
{
*i = rbuf_last(rbuf);
return 1;
}
if ( --(*i) < 0 ) *i = rbuf->m - 1;
return 1;
}
/**
* rbuf_add() - register new element in the round buffer
* @rbuf: the rbuf_t holder
*
* Returns index of the newly inserted element.
*/
static inline int rbuf_add(rbuf_t *rbuf)
{
if ( rbuf->n < rbuf->m )
{
rbuf->n++;
int i = rbuf->f + rbuf->n;
return i <= rbuf->m ? i - 1 : i - rbuf->m - 1;
}
rbuf->f++;
if ( rbuf->f >= rbuf->m )
{
rbuf->f = 0;
return rbuf->m - 1;
}
return rbuf->f - 1;
}
/**
* rbuf_shift() - removes first element from the buffer
* @rbuf: the rbuf_t holder
*
* Returns index of the removed element.
*/
static inline int rbuf_shift(rbuf_t *rbuf)
{
if ( !rbuf->n ) return -1;
int ret = rbuf->f;
rbuf->f++;
if ( rbuf->f >= rbuf->m ) rbuf->f = 0;
rbuf->n--;
return ret;
}
/**
* rbuf_shift_n() - removes first n elements from the buffer
* @rbuf: the rbuf_t holder
* @n: number of elements to remove
*/
static inline void rbuf_shift_n(rbuf_t *rbuf, int n)
{
if ( n >= rbuf->n )
{
rbuf->n = rbuf->f = 0;
return;
}
rbuf->n -= n;
rbuf->f += n;
if ( rbuf->f >= rbuf->m ) rbuf->f -= rbuf->m;
}
#endif