xref: /dflybsd-src/contrib/grep/lib/i-ring.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
1cf28ed85SJohn Marino /* a simple ring buffer
2*09d4459fSDaniel Fojt    Copyright (C) 2006, 2009-2020 Free Software Foundation, Inc.
3cf28ed85SJohn Marino 
4cf28ed85SJohn Marino    This program is free software: you can redistribute it and/or modify
5cf28ed85SJohn Marino    it under the terms of the GNU General Public License as published by
6cf28ed85SJohn Marino    the Free Software Foundation; either version 3 of the License, or
7cf28ed85SJohn Marino    (at your option) any later version.
8cf28ed85SJohn Marino 
9cf28ed85SJohn Marino    This program is distributed in the hope that it will be useful,
10cf28ed85SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
11cf28ed85SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12cf28ed85SJohn Marino    GNU General Public License for more details.
13cf28ed85SJohn Marino 
14cf28ed85SJohn Marino    You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
16cf28ed85SJohn Marino 
17cf28ed85SJohn Marino /* written by Jim Meyering */
18cf28ed85SJohn Marino 
19cf28ed85SJohn Marino #include <config.h>
20cf28ed85SJohn Marino #include "i-ring.h"
21cf28ed85SJohn Marino 
22cf28ed85SJohn Marino #include <stdlib.h>
23cf28ed85SJohn Marino 
24cf28ed85SJohn Marino void
i_ring_init(I_ring * ir,int default_val)25cf28ed85SJohn Marino i_ring_init (I_ring *ir, int default_val)
26cf28ed85SJohn Marino {
27cf28ed85SJohn Marino   int i;
28cf28ed85SJohn Marino   ir->ir_empty = true;
29cf28ed85SJohn Marino   ir->ir_front = 0;
30cf28ed85SJohn Marino   ir->ir_back = 0;
31cf28ed85SJohn Marino   for (i = 0; i < I_RING_SIZE; i++)
32cf28ed85SJohn Marino     ir->ir_data[i] = default_val;
33cf28ed85SJohn Marino   ir->ir_default_val = default_val;
34cf28ed85SJohn Marino }
35cf28ed85SJohn Marino 
36cf28ed85SJohn Marino bool
i_ring_empty(I_ring const * ir)37cf28ed85SJohn Marino i_ring_empty (I_ring const *ir)
38cf28ed85SJohn Marino {
39cf28ed85SJohn Marino   return ir->ir_empty;
40cf28ed85SJohn Marino }
41cf28ed85SJohn Marino 
42cf28ed85SJohn Marino int
i_ring_push(I_ring * ir,int val)43cf28ed85SJohn Marino i_ring_push (I_ring *ir, int val)
44cf28ed85SJohn Marino {
45cf28ed85SJohn Marino   unsigned int dest_idx = (ir->ir_front + !ir->ir_empty) % I_RING_SIZE;
46cf28ed85SJohn Marino   int old_val = ir->ir_data[dest_idx];
47cf28ed85SJohn Marino   ir->ir_data[dest_idx] = val;
48cf28ed85SJohn Marino   ir->ir_front = dest_idx;
49cf28ed85SJohn Marino   if (dest_idx == ir->ir_back)
50cf28ed85SJohn Marino     ir->ir_back = (ir->ir_back + !ir->ir_empty) % I_RING_SIZE;
51cf28ed85SJohn Marino   ir->ir_empty = false;
52cf28ed85SJohn Marino   return old_val;
53cf28ed85SJohn Marino }
54cf28ed85SJohn Marino 
55cf28ed85SJohn Marino int
i_ring_pop(I_ring * ir)56cf28ed85SJohn Marino i_ring_pop (I_ring *ir)
57cf28ed85SJohn Marino {
58cf28ed85SJohn Marino   int top_val;
59cf28ed85SJohn Marino   if (i_ring_empty (ir))
60cf28ed85SJohn Marino     abort ();
61cf28ed85SJohn Marino   top_val = ir->ir_data[ir->ir_front];
62cf28ed85SJohn Marino   ir->ir_data[ir->ir_front] = ir->ir_default_val;
63cf28ed85SJohn Marino   if (ir->ir_front == ir->ir_back)
64cf28ed85SJohn Marino     ir->ir_empty = true;
65cf28ed85SJohn Marino   else
66cf28ed85SJohn Marino     ir->ir_front = ((ir->ir_front + I_RING_SIZE - 1) % I_RING_SIZE);
67cf28ed85SJohn Marino   return top_val;
68cf28ed85SJohn Marino }
69