(root)/
glib-2.79.0/
girepository/
cmph/
vqueue.c
       1  #include "vqueue.h"
       2  #include <stdio.h>
       3  #include <assert.h>
       4  #include <stdlib.h>
       5  struct __vqueue_t
       6  {
       7    cmph_uint32 * values;
       8    cmph_uint32 beg, end, capacity;
       9  };
      10  
      11  vqueue_t * vqueue_new(cmph_uint32 capacity)
      12  {
      13    size_t capacity_plus_one = capacity + 1;
      14    vqueue_t *q = (vqueue_t *)malloc(sizeof(vqueue_t));
      15    assert(q);
      16    q->values = (cmph_uint32 *)calloc(capacity_plus_one, sizeof(cmph_uint32));
      17    q->beg = q->end = 0;
      18    q->capacity = (cmph_uint32) capacity_plus_one;
      19    return q;
      20  }
      21  
      22  cmph_uint8 vqueue_is_empty(vqueue_t * q)
      23  {
      24    return (cmph_uint8)(q->beg == q->end);
      25  }
      26  
      27  void vqueue_insert(vqueue_t * q, cmph_uint32 val)
      28  {
      29    assert((q->end + 1)%q->capacity != q->beg); // Is queue full?
      30    q->end = (q->end + 1)%q->capacity;
      31    q->values[q->end] = val;
      32  }
      33  
      34  cmph_uint32 vqueue_remove(vqueue_t * q)
      35  {
      36    assert(!vqueue_is_empty(q)); // Is queue empty?
      37    q->beg = (q->beg + 1)%q->capacity;
      38    return q->values[q->beg];
      39  }
      40  
      41  void vqueue_print(vqueue_t * q)
      42  {
      43    cmph_uint32 i;
      44    for (i = q->beg; i != q->end; i = (i + 1)%q->capacity)
      45      fprintf(stderr, "%u\n", q->values[(i + 1)%q->capacity]);
      46  } 
      47  
      48  void vqueue_destroy(vqueue_t *q)
      49  {
      50    free(q->values); q->values = NULL; free(q);
      51  }