Fix interpolation in title screen replays
[neverball] / share / queue.c
1 /*
2  * Copyright (C) 2009 Neverball authors
3  *
4  * NEVERBALL is  free software; you can redistribute  it and/or modify
5  * it under the  terms of the GNU General  Public License as published
6  * by the Free  Software Foundation; either version 2  of the License,
7  * or (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT  ANY  WARRANTY;  without   even  the  implied  warranty  of
11  * MERCHANTABILITY or  FITNESS FOR A PARTICULAR PURPOSE.   See the GNU
12  * General Public License for more details.
13  */
14
15 #include <stdlib.h>
16 #include <assert.h>
17 #include "queue.h"
18 #include "list.h"
19
20 struct queue
21 {
22     List head;
23     List tail;
24 };
25
26 Queue queue_new(void)
27 {
28     Queue new;
29
30     if ((new = malloc(sizeof (*new))))
31         new->head = new->tail = list_cons(NULL, NULL);
32
33     return new;
34 }
35
36 void queue_free(Queue q)
37 {
38     assert(queue_empty(q));
39     free(q->head);
40     free(q);
41 }
42
43 int queue_empty(Queue q)
44 {
45     assert(q);
46     return q->head == q->tail;
47 }
48
49 void queue_enq(Queue q, void *data)
50 {
51     assert(q);
52
53     q->tail->data = data;
54     q->tail->next = list_cons(NULL, NULL);
55
56     q->tail = q->tail->next;
57 }
58
59 void *queue_deq(Queue q)
60 {
61     void *data = NULL;
62
63     if (!queue_empty(q))
64     {
65         data    = q->head->data;
66         q->head = list_rest(q->head);
67     }
68
69     return data;
70 }