Debian Codegraph @

bcftools/rbuf.h

raw symbols SCIP
1 /* rbuf.h -- round buffers.
2
3 Copyright (C) 2013-2014, 2017 Genome Research Ltd.
4
5 Author: Petr Danecek <[email protected]>
6
7 Permission is hereby granted, free of charge, to any person obtaining a copy
8 of this software and associated documentation files (the "Software"), to deal
9 in the Software without restriction, including without limitation the rights
10 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
11 copies of the Software, and to permit persons to whom the Software is
12 furnished to do so, subject to the following conditions:
13
14 The above copyright notice and this permission notice shall be included in
15 all copies or substantial portions of the Software.
16
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
18 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
19 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
20 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
21 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
22 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
23 THE SOFTWARE. */
24
25 #ifndef __RBUF_H__
26 #define __RBUF_H__
27
28 #include <string.h>
29
30 typedef struct
31 {
32 int m,n,f; // m: allocated size, n: number of elements in the buffer, f: first element
33 }
34 rbuf_t;
35
36 /**
37 * rbuf_init() - initialize round buffer
38 * @rbuf: the rbuf_t holder
39 * @size: the maximum number of elements
40 *
41 */
42 static inline void rbuf_init(rbuf_t *rbuf, int size)
43 {
44 rbuf->m = size; rbuf->n = rbuf->f = 0;
45 }
46 /**
47 * rbuf_kth() - get index of the k-th element of the round buffer
48 * @rbuf: the rbuf_t holder
49 * @k: 0-based index. If negative, return k-th element from the end, 1-based
50 */
51 static inline int rbuf_kth(rbuf_t *rbuf, int k)
52 {
53 if ( k >= rbuf->n ) return -1;
54 if ( k < 0 )
55 {
56 k = rbuf->n + k;
57 if ( k < 0 ) return -1;
58 }
59 int i = k + rbuf->f;
60 if ( i >= rbuf->m ) i -= rbuf->m;
61 return i;
62 }
63 /**
64 * rbuf_last() - get index of the last element of the round buffer
65 * @rbuf: the rbuf_t holder
66 */
67 #define rbuf_last(rbuf) rbuf_kth(rbuf, -1)
68
69 /**
70 * rbuf_l2ridx() - get 0-based rbuf index which corresponds to i-th linear index
71 * @rbuf: the rbuf_t holder
72 * @idx: 0-based linear index
73 *
74 * Returns 0-based circular index or -1 if out of bounds
75 */
76 static inline int rbuf_l2ridx(rbuf_t *rbuf, int idx)
77 {
78 if ( idx < 0 || idx >= rbuf->n ) return -1;
79 if ( idx >= rbuf->f )
80 {
81 int i = idx - rbuf->f;
82 if ( i >= rbuf->n ) return -1;
83 return i;
84 }
85 int i = rbuf->m - rbuf->f + idx;
86 if ( i >= rbuf->n ) return -1;
87 return i;
88 }
89
90 /**
91 * rbuf_next() - get index of the next element in the round buffer
92 * @rbuf: the rbuf_t holder
93 * @i: pointer to the last rbuf index. Set to -1 before the first call.
94 *
95 * Sets i to the next position in the buffer. The return value indicates if
96 * the position points to a valid element (1) or if there are no more elements
97 * after *i (0). When the end is reached, *i is set to the first element in the
98 * buffer.
99 */
100 static inline int rbuf_next(rbuf_t *rbuf, int *i)
101 {
102 if ( !rbuf->n ) return 0;
103 if ( *i==-1 ) { *i = rbuf->f; return 1; }
104 int n = (rbuf->f <= *i) ? *i - rbuf->f + 1 : *i + rbuf->m - rbuf->f + 1;
105 if ( ++(*i) >= rbuf->m ) *i = 0;
106 if ( n < rbuf->n ) return 1;
107 *i = rbuf->f;
108 return 0;
109 }
110 /**
111 * rbuf_prev() - get index of the previous element in the round buffer
112 * @rbuf: the rbuf_t holder
113 * @i: pointer to the last rbuf index. Set to -1 before the first call.
114 *
115 * Sets i to the previous position in the buffer. The return value indicates if
116 * the position points to a valid element (1) or if there are no more elements
117 * before *i (0).
118 */
119 static inline int rbuf_prev(rbuf_t *rbuf, int *i)
120 {
121 if ( !rbuf->n || *i==rbuf->f ) return 0;
122 if ( *i==-1 )
123 {
124 *i = rbuf_last(rbuf);
125 return 1;
126 }
127 if ( --(*i) < 0 ) *i = rbuf->m - 1;
128 return 1;
129 }
130 /**
131 * rbuf_prepend() - register new element at the start of the round buffer
132 * @rbuf: the rbuf_t holder
133 *
134 * Returns index of the newly inserted element.
135 */
136 static inline int rbuf_prepend(rbuf_t *rbuf)
137 {
138 if ( rbuf->n < rbuf->m ) rbuf->n++;
139
140 rbuf->f = rbuf->f > 0 ? rbuf->f - 1 : rbuf->m - 1;
141 return rbuf->f;
142 }
143 /**
144 * rbuf_append() - register new element at the end of the round buffer
145 * @rbuf: the rbuf_t holder
146 *
147 * Returns index of the newly inserted element.
148 */
149 static inline int rbuf_append(rbuf_t *rbuf)
150 {
151 if ( rbuf->n < rbuf->m )
152 {
153 rbuf->n++;
154 int i = rbuf->f + rbuf->n;
155 return i <= rbuf->m ? i - 1 : i - rbuf->m - 1;
156 }
157
158 rbuf->f++;
159 if ( rbuf->f >= rbuf->m )
160 {
161 rbuf->f = 0;
162 return rbuf->m - 1;
163 }
164 return rbuf->f - 1;
165 }
166 /**
167 * rbuf_shift() - removes first element from the buffer
168 * @rbuf: the rbuf_t holder
169 *
170 * Returns index of the removed element.
171 */
172 static inline int rbuf_shift(rbuf_t *rbuf)
173 {
174 if ( !rbuf->n ) return -1;
175 int ret = rbuf->f;
176 rbuf->f++;
177 if ( rbuf->f >= rbuf->m ) rbuf->f = 0;
178 rbuf->n--;
179 return ret;
180 }
181 /**
182 * rbuf_shift_n() - removes first n elements from the buffer
183 * @rbuf: the rbuf_t holder
184 * @n: number of elements to remove
185 */
186 static inline void rbuf_shift_n(rbuf_t *rbuf, int n)
187 {
188 if ( n >= rbuf->n )
189 {
190 rbuf->n = rbuf->f = 0;
191 return;
192 }
193 rbuf->n -= n;
194 rbuf->f += n;
195 if ( rbuf->f >= rbuf->m ) rbuf->f -= rbuf->m;
196 }
197
198 /**
199 * rbuf_expand0() - expand round buffer and set the newly allocated elements to 0
200 * @rbuf: the rbuf holder
201 * @type_t: data type
202 * @n: requested number of elements
203 * @data: data array to be realloced
204 *
205 * Note: The new array is linearized and leaves the rbuf.f offset untouched,
206 * thus the size of the new buffer is determined by the current position.
207 */
208 #define rbuf_expand0(rbuf,type_t,n,data) \
209 { \
210 if ( n > (rbuf)->m ) \
211 { \
212 int m = n + (rbuf)->f; \
213 m--, m|=m>>1, m|=m>>2, m|=m>>4, m|=m>>8, m|=m>>16, m++; /* kroundup32 */ \
214 data = (type_t*) realloc(data, sizeof(type_t)*m); \
215 type_t *ptr = data; \
216 memset(ptr+(rbuf)->m,0,sizeof(type_t)*(m-(rbuf)->m)); \
217 if ( (rbuf)->f ) \
218 { \
219 memcpy(ptr+(rbuf)->m,ptr,sizeof(type_t)*(rbuf)->f); \
220 memset(ptr,0,sizeof(type_t)*(rbuf)->f); \
221 } \
222 (rbuf)->m = m; \
223 } \
224 }
225
226 /**
227 * rbuf_remove_kth() - remove k-th rbuf element (0-based) and memmove the data block
228 * @rbuf: the rbuf holder
229 * @type_t: data type
230 * @k: k-th element to remove
231 * @data: data array to be modified
232 */
233 #define rbuf_remove_kth(rbuf, type_t, kth, data) \
234 { \
235 int k = rbuf_kth(rbuf, kth); \
236 if ( k < (rbuf)->f ) /* shrink from back */ \
237 { \
238 int l = rbuf_kth(rbuf, -1); \
239 if ( k < l ) \
240 { \
241 type_t tmp = (data)[k]; \
242 memmove(data+k, data+k+1, (l - k)*sizeof(type_t)); \
243 (data)[l] = tmp; \
244 } \
245 (rbuf)->n--; \
246 } \
247 else /* shrink from front */ \
248 { \
249 if ( k > (rbuf)->f ) \
250 { \
251 type_t tmp = (data)[k]; \
252 memmove(&data[(rbuf)->f+1], &data[(rbuf)->f], (k - (rbuf)->f)*sizeof(type_t)); \
253 (data)[(rbuf)->f] = tmp; \
254 } \
255 (rbuf)->f++; \
256 (rbuf)->n--; \
257 if ( (rbuf)->f == (rbuf)->m ) (rbuf)->f = 0; \
258 } \
259 }
260
261 #endif