Project

General

Profile

Statistics
| Revision:

root / trunk / code / projects / libwireless / lib / queue.c @ 242

History | View | Annotate | Download (3.74 KB)

1
/**
2
 * Copyright (c) 2007 Colony Project
3
 * 
4
 * Permission is hereby granted, free of charge, to any person
5
 * obtaining a copy of this software and associated documentation
6
 * files (the "Software"), to deal in the Software without
7
 * restriction, including without limitation the rights to use,
8
 * copy, modify, merge, publish, distribute, sublicense, and/or sell
9
 * copies of the Software, and to permit persons to whom the
10
 * Software is furnished to do so, subject to the following
11
 * conditions:
12
 * 
13
 * The above copyright notice and this permission notice shall be
14
 * included in all copies or substantial portions of the Software.
15
 * 
16
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17
 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
18
 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19
 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
20
 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
21
 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
22
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
23
 * OTHER DEALINGS IN THE SOFTWARE.
24
 **/
25

    
26
/**
27
 * @file queue.c
28
 * @brief Queue Implementation
29
 *
30
 * An implementation of a queue for the colony robots.
31
 *
32
 * @author Brian Coltin, Colony Project, CMU Robotics Club
33
 **/
34

    
35
#include "queue.h"
36
#include <wl_defs.h>
37

    
38
#include <stdlib.h>
39
#include <stdio.h>
40

    
41
/**
42
 * @struct node_def
43
 * A node in the queue.
44
 **/
45
typedef struct node_def
46
{
47
        /** The item at this point in the queue. **/
48
        void* val;
49
        /** The next node in the queue. **/
50
        struct node_def* next;
51
} Node;
52

    
53
/**
54
 * Create a queue.
55
 *
56
 * @return the newly created queue.
57
 **/
58
Queue* queue_create()
59
{
60
        Queue* q = (Queue*)malloc(sizeof(Queue));
61
        
62
        if (q == NULL)
63
        {
64
                WL_DEBUG_PRINT("Out of memory.\r\n");
65
                return NULL;
66
        }
67
        
68
        q->head = NULL;
69
        q->size = 0;
70
        return q;
71
}
72

    
73
/**
74
 * Destroys a queue, freeing memory.
75
 *
76
 * @param q the queue to destroy
77
 **/
78
void queue_destroy(Queue* q)
79
{
80
        Node* t = q->head;
81
        while (t != NULL)
82
        {
83
                Node* t2 = t->next;
84
                free(t);
85
                t = t2;
86
        }
87
        free(q);
88
}
89

    
90
/**
91
 * Add an element to a queue.
92
 *
93
 * @param q the queue to add an element to
94
 * @param item the item to add to the queue
95
 **/
96
void queue_add(Queue* q, void* item)
97
{
98
        Node* n = (Node*)malloc(sizeof(Node));
99
        n->val = item;
100
        n->next = NULL;
101
        
102
        if (q->head == NULL)
103
        {
104
                q->head = n;
105
                q->tail = n;
106
        }
107
        else
108
        {
109
                q->tail->next = n;
110
                q->tail = n;
111
        }
112
        
113
        q->size++;
114
}
115

    
116
/**
117
 * Remove an element from the front of a queue.
118
 *
119
 * @param q the queue to remove the element from
120
 *
121
 * @return the element which was removed
122
 **/
123
void* queue_remove(Queue* q)
124
{
125
        Node* first = q->head;
126
        if (first == NULL)
127
        {
128
                WL_DEBUG_PRINT("Attempting to remove item \
129
                        from empty queue.\r\n");
130
                WL_DEBUG_PRINT_INT(queue_size(q));
131
        }
132
        void* ret = first->val;
133
        q->head = first->next;
134
        if (q->tail == first)
135
                q->tail = NULL;
136
        free(first);
137
        q->size--;
138
        return ret;
139
}
140

    
141
/**
142
 * Remove all instances of a given element from a queue.
143
 *
144
 * @param q the queue to remove the elements from
145
 * @param item the element to remove all instances of
146
 **/
147
void queue_remove_all(Queue* q, void* item)
148
{
149
        Node* curr = q->head;
150
        Node* prev = NULL;
151
        
152
        while (curr != NULL)
153
        {
154
                Node* next = curr->next;
155
                if (curr->val == item)
156
                {
157
                        if (q->head == curr)
158
                                q->head = next;
159
                        if (q->tail == curr)
160
                                q->tail = prev;
161
                        if (prev != NULL)
162
                                prev->next = next;
163
                        free(curr);
164
                        q->size--;
165
                }
166
                else
167
                        prev = curr;
168
                curr = next;
169
        }
170
}
171

    
172
/**
173
 * Get the number of elements in the queue.
174
 *
175
 * @param q the queue to get the size of
176
 * @return the size of the queue
177
 **/
178
int queue_size(Queue* q)
179
{
180
        return q->size;
181
}
182

    
183
/**
184
 * Check if the queue is empty.
185
 * 
186
 * @param q the queue to check
187
 * @return nonzero if the queue is empty, 0 otherwise
188
 **/
189
int queue_is_empty(Queue* q)
190
{
191
        return q->size == 0;
192
}
193