-
Notifications
You must be signed in to change notification settings - Fork 0
/
generic-queue.c
153 lines (132 loc) · 2.95 KB
/
generic-queue.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
/************************************************************************
*
* File name: generic-queue.c
* Description: Generic Queue's Implementation
* Author: dennis_fan(dennis_fan@outlook.com), 2013/2/26
* Version: 1.0
*************************************************************************/
#include "generic-queue.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#define MIN_QUEUE_SIZE (4)
/* Create a new queue with capacity */
Queue
queue_create(int elemsize, int capacity, PfCbFree freefn)
{
Queue que;
que = malloc(sizeof(struct QueueRecord));
if ( que == NULL ) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
que->elemsize = elemsize;
que->capacity = capacity > MIN_QUEUE_SIZE ? capacity : MIN_QUEUE_SIZE;
que->array = malloc(elemsize * que->capacity);
if ( que->array == NULL ) {
fprintf(stderr, "Out of memory\n");
exit(1);
}
que->front = 1;
que->rear = 0;
que->size = 0;
que->freefn = freefn;
return que;
}
/* Dispose the queue */
void
queue_dispose(Queue que)
{
if (que != NULL) {
queue_make_empty(que);
free(que->array);
free(que);
}
}
/* Make the give queue empty */
void
queue_make_empty(Queue que)
{
if ( que->freefn ) {
int i;
for ( i = 0; i < que->size; ++i) {
free((char *)que->array +
que->elemsize * i);
}
}
que->size = 0;
que->front = 1;
que->rear = 0;
}
/* Return true if the queue is empty */
int
queue_is_empty(Queue que)
{
return que->size == 0;
}
/* Return true if the queue is full */
int
queue_is_full(Queue que)
{
return que->size == que->capacity;
}
static int
successor(Queue que, int index)
{
if ( ++index == que->capacity)
index = 0;
return index;
}
/* Insert a new element onto queue(rear) */
void
queue_enqueue(Queue que, ElementAddr elemaddr)
{
void *target;
if ( queue_is_full(que) ) {
fprintf(stderr, "Full queue\n");
exit(1);
}
que->rear = successor(que, que->rear);
target = (char *)que->array + que->elemsize * que->rear;
memcpy(target, elemaddr, que->elemsize);
que->size++;
}
/* Delete the front element off the queue */
void
queue_dequeue(Queue que)
{
if ( queue_is_empty(que) ) {
fprintf(stderr, "Empty queue\n");
exit(1);
}
if ( que->freefn ) {
void *target = (char *)que->array +
que->front * que->elemsize;
que->freefn(target);
}
que->size--;
que->front = successor(que, que->front);
}
/* Fetch the front element from the queue */
void
queue_front(Queue que, ElementAddr elemaddr)
{
void *target = (char *)que->array +
que->front * que->elemsize;
memcpy(elemaddr, target, que->elemsize);
}
/* Fetch and Delete the front element from the queue */
void
queue_front_and_dequeue(Queue que, ElementAddr elemaddr)
{
void *target;
if ( queue_is_empty(que) ) {
fprintf(stderr, "Empty queue\n");
exit(1);
}
target = (char *)que->array +
que->front * que->elemsize;
memcpy(elemaddr, target, que->elemsize);
que->size--;
que->front = successor(que, que->front);
}