1cf28ed85SJohn Marino /* definitions for 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 #include <stdbool.h> 18cf28ed85SJohn Marino #include "verify.h" 19cf28ed85SJohn Marino 20cf28ed85SJohn Marino enum { I_RING_SIZE = 4 }; 21cf28ed85SJohn Marino verify (1 <= I_RING_SIZE); 22cf28ed85SJohn Marino 23cf28ed85SJohn Marino /* When ir_empty is true, the ring is empty. 24cf28ed85SJohn Marino Otherwise, ir_data[B..F] are defined, where B..F is the contiguous 25cf28ed85SJohn Marino range of indices, modulo I_RING_SIZE, from back to front, inclusive. 26cf28ed85SJohn Marino Undefined elements of ir_data are always set to ir_default_val. 27cf28ed85SJohn Marino Popping from an empty ring aborts. 28cf28ed85SJohn Marino Pushing onto a full ring returns the displaced value. 29cf28ed85SJohn Marino An empty ring has F==B and ir_empty == true. 30cf28ed85SJohn Marino A ring with one entry still has F==B, but now ir_empty == false. */ 31cf28ed85SJohn Marino struct I_ring 32cf28ed85SJohn Marino { 33cf28ed85SJohn Marino int ir_data[I_RING_SIZE]; 34cf28ed85SJohn Marino int ir_default_val; 35cf28ed85SJohn Marino unsigned int ir_front; 36cf28ed85SJohn Marino unsigned int ir_back; 37cf28ed85SJohn Marino bool ir_empty; 38cf28ed85SJohn Marino }; 39cf28ed85SJohn Marino typedef struct I_ring I_ring; 40cf28ed85SJohn Marino 41cf28ed85SJohn Marino void i_ring_init (I_ring *ir, int ir_default_val); 42cf28ed85SJohn Marino int i_ring_push (I_ring *ir, int val); 43cf28ed85SJohn Marino int i_ring_pop (I_ring *ir); 44cf28ed85SJohn Marino bool i_ring_empty (I_ring const *ir) _GL_ATTRIBUTE_PURE; 45