This source file includes following definitions.
- i_ring_init
- i_ring_empty
- i_ring_push
- i_ring_pop
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19 #include <config.h>
20 #include "i-ring.h"
21
22 #include <stdlib.h>
23
24 void
25 i_ring_init (I_ring *ir, int default_val)
26 {
27 int i;
28 ir->ir_empty = true;
29 ir->ir_front = 0;
30 ir->ir_back = 0;
31 for (i = 0; i < I_RING_SIZE; i++)
32 ir->ir_data[i] = default_val;
33 ir->ir_default_val = default_val;
34 }
35
36 bool
37 i_ring_empty (I_ring const *ir)
38 {
39 return ir->ir_empty;
40 }
41
42 int
43 i_ring_push (I_ring *ir, int val)
44 {
45 unsigned int dest_idx = (ir->ir_front + !ir->ir_empty) % I_RING_SIZE;
46 int old_val = ir->ir_data[dest_idx];
47 ir->ir_data[dest_idx] = val;
48 ir->ir_front = dest_idx;
49 if (dest_idx == ir->ir_back)
50 ir->ir_back = (ir->ir_back + !ir->ir_empty) % I_RING_SIZE;
51 ir->ir_empty = false;
52 return old_val;
53 }
54
55 int
56 i_ring_pop (I_ring *ir)
57 {
58 int top_val;
59 if (i_ring_empty (ir))
60 abort ();
61 top_val = ir->ir_data[ir->ir_front];
62 ir->ir_data[ir->ir_front] = ir->ir_default_val;
63 if (ir->ir_front == ir->ir_back)
64 ir->ir_empty = true;
65 else
66 ir->ir_front = ((ir->ir_front + I_RING_SIZE - 1) % I_RING_SIZE);
67 return top_val;
68 }