-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbounded_queue.c
executable file
·151 lines (130 loc) · 3.8 KB
/
bounded_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
/*
Author: Marc Lee (Changhwan)
duckid: clee3
Title: CIS415 project2 bounded_queue
This is my own work
*/
#include <stdio.h>
#include <stdlib.h>
#include "bounded_queue.h"
struct bounded_queue
{
int size; // capacity
int len; // current size
void **buffer; // storage
long long head; // Point Newest element
long long tail; // Point Oldest element
};
int RoundIDToBufferIndex(int size, long long index)
{
long long value = (index % ((long long)size));
return (int)value;
}
BoundedQueue *BB_MallocBoundedQueue(long size)
{
BoundedQueue *new_queue = (BoundedQueue *) malloc (sizeof(BoundedQueue));
if (new_queue == NULL) {
return NULL;
}
new_queue->buffer = (void **) malloc(size*sizeof(void *));
if (new_queue->buffer == NULL) {
free(new_queue);
return NULL;
}
new_queue->size = size;
new_queue->head = new_queue->tail = 0;
new_queue->len = 0;
return new_queue;
}
long long BB_TryEnqueue(struct bounded_queue *queue,void *item)
{
long long returnValue = -1;
if (!BB_IsFull(queue)) {
returnValue = queue->head;
queue->buffer[RoundIDToBufferIndex(queue->size, queue->head)] = item;
queue->head++;
queue->len++;
}
return returnValue;
}
int BB_TryDequeue(struct bounded_queue *queue, long long id)
{
int returnValue = 0;
if (!BB_IsEmpty(queue)) {
if (BB_IsIdValid(queue, id)) {
// queue->buffer[RoundIDToBufferIndex(queue->size, queue->tail)] = NULL;
free(queue->buffer[RoundIDToBufferIndex(queue->size, queue->tail)]);
queue->tail++;
returnValue = 1;
queue->len--;
}
}
return returnValue;
}
long long BB_GetFront(struct bounded_queue *queue)
{
long long returnValue = -1;
if(!BB_IsEmpty(queue))
{
returnValue = queue->head;
}
return returnValue;
}
long long BB_GetBack(struct bounded_queue *queue)
{
long long returnValue = -1;
if (!BB_IsEmpty(queue)) {
return queue->tail;
}
return returnValue;
}
int BB_GetCount(struct bounded_queue *queue)
{
int returnValue = queue->len;
return (int)returnValue;
}
int BB_IsIdValid(struct bounded_queue *queue,long long id)
{
int returnValue = 0;
//long long id2idx = (long long) RoundIDToBufferIndex(queue->size, id);
//printf("head: %llu, tail: %llu, id: %llu\n", queue->head, queue->tail, id);
if (queue->head > id && id >= queue->tail) {
returnValue = 1;
}
return returnValue;
}
void *BB_GetItem(struct bounded_queue *queue,long long id)
{
void *returnValue = NULL;
if (BB_IsIdValid(queue, id)) {
int index = RoundIDToBufferIndex(queue->size, id);
return queue->buffer[index];
}
return returnValue;
}
int BB_IsFull(struct bounded_queue *queue)
{
int returnValue = 0;
if (queue->len == queue->size){
returnValue = 1;
}
return returnValue;
}
int BB_IsEmpty(struct bounded_queue *queue)
{
int returnValue = 0;
if (queue->len == 0) {
returnValue = 1;
}
return returnValue;
}
void BB_FreeBoundedQueue(struct bounded_queue *queue)
{
int n, i;
int tail = RoundIDToBufferIndex(queue->size, queue->tail);
for (i = tail, n = queue->len; n > 0; i = (i+1) % queue->size, n--) {
free(queue->buffer[i]);
}
free(queue->buffer);
free(queue);
}