-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathboundeddeque.h
233 lines (212 loc) · 6.15 KB
/
boundeddeque.h
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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
#ifndef _T_BOUNDED_DEQUE_
#define _T_BOUNDED_DEQUE_
#include "error.h"
//*****************************************************************************************
// BOUNDEDDEQUE STRUCTURE
/*
Structure that simulates a deque (https://en.wikipedia.org/wiki/Double-ended_queue) using an array.
Since the Deque is stored in an array, the maximum number of elements that the Deque can store is fixed,
until a new maximum size is requested via 'BD_AllocateMemory' (this resets the Deque to empty).
*/
typedef struct
{
// The maximum length that the deque.
int max_length;
// The size of the deque.
int length;
// The index of the first element of the deque in the container.
int begin;
// The index of the last element of the deque in the container.
int end;
// The array.
int *container;
} BoundedDeque;
//***********************************
// BOUNDEDDEQUE METHODS
// Constructor: initializes the members to safe default values.
// This function always needs to be called on a 'BoundedDeque' instance before any other move.
void BD_Defaults(BoundedDeque *BL)
{
BL->max_length = 0;
BL->length = 0;
BL->begin = 0;
BL->end = 0;
BL->container = NULL;
}
// Allocate memory for the maximum capacity of the Deque.
// In case of error, an Error instance will be returned.
Error BD_AllocateMemory(BoundedDeque *BL, int size)
{
Error error = {1, "OK"};
//Free the memory, if necessary.
if(BL->container != NULL)
{
free((char*) BL->container);
BL->container = 0;
}
BL->max_length = 0;
BL->length = 0;
BL->begin = 0;
BL->end = 0;
//If the size is zero or less, return with the arrays destroyed.
if(size > 0)
{
BL->container = (int*) malloc(size * sizeof(int));
//If memory allocation failed, return the respective error.
if(BL->container != NULL) BL->max_length = size;
else
{
Error_Set(&error, -1, "Error: no memory for the allocation in 'BoundedDeque::allocateMemory'");
return error;
}
}
else if (size < 0)
{
Error_Set(&error, -1, "Error: can not allocate a negative amount of memory in BoundedDeque::allocateMemory");
return error;
}
return error;
}
// Insert an element at the front of the Deque.
// If the Deque is already at max size, the element will not be inserted.
void BD_Push_Front(BoundedDeque *BL, int data)
{
//If we have space for another element.
if(BL->max_length > 0 && BL->length < BL->max_length)
{
//Consider the rotation in the array.
if(BL->begin == 0) BL->begin = BL->max_length - 1;
else --BL->begin;
//Consider the case where this is the first element.
if(BL->length == 0) BL->end = BL->begin;
BL->container[BL->begin] = data;
++BL->length;
}
}
// Insert an element at the back of the Deque.
// If the Deque is already at max size, the element will not be inserted.
void BD_Push_Back(BoundedDeque *BL, int data)
{
//If we have space for another element.
if(BL->max_length > 0 && BL->length < BL->max_length)
{
//Consider the rotation in the array.
BL->end = (BL->end == BL->max_length - 1 ? 0 : BL->end + 1);
//Consider the case where this is the first element.
if (BL->length == 0) BL->begin = BL->end;
BL->container[BL->end] = data;
++BL->length;
}
}
// Remove the front element of the Deque, returning such element in the reference argument 'data'.
// In case of error, an Error instance will be returned.
Error BD_Pop_Front(BoundedDeque *BL, int *data)
{
//If the list contains elements.
if(BL->max_length > 0 && BL->length > 0)
{
Error error = {1, "OK"};
//Save the position of the first element.
int aux_index = BL->begin;
//Consider the rotation in the array.
BL->begin = (BL->begin == BL->max_length - 1 ? 0 : BL->begin + 1);
//Consider the case where this is the only element.
if(BL->length == 1)
{
BL->begin = 0;
BL->end = 0;
}
--BL->length;
*data = BL->container[aux_index];
return error;
}
else
{
Error error = {-1, "Error: The list is empty, in BoundedDeque::BD_Pop_Front"};
return error;
}
}
// Remove the back element of the Deque, returning such element in the reference argument 'data'.
// In case of error, an Error instance will be returned.
Error BD_Pop_Back(BoundedDeque *BL, int *data)
{
//If the list contains elements.
if(BL->max_length > 0 && BL->length > 0)
{
Error error = {1, "OK"};
//Save the position of the first element.
int aux_index = BL->end;
//Consider the case where this is the only element.
if(BL->length == 1)
{
BL->begin = 0;
BL->end = 0;
}
else BL->end = (BL->end == 0 ? BL->max_length - 1 : BL->end - 1);
--BL->length;
*data = BL->container[aux_index];
return error;
}
else
{
Error error = {-1, "Error: The list is empty, in BoundedDeque::BD_Pop_Back"};
return error;
}
}
// Remove the front element of the Deque.
void BD_Delete_Front(BoundedDeque *BL)
{
//If the list contains elements.
if(BL->max_length > 0 && BL->length > 0)
{
//Consider the case where this is the only element.
if(BL->length == 1)
{
BL->begin = 0;
BL->end = 0;
}
//Take in account the rotation in the array.
else BL->begin = (BL->begin == BL->max_length - 1 ? 0 : BL->begin + 1);
--BL->length;
}
}
// Remove the back element of the Deque
void BD_Delete_Back(BoundedDeque *BL)
{
//If the list contains elements.
if(BL->max_length > 0 && BL->length > 0)
{
//Consider the case where this is the only element.
if(BL->length == 1)
{
BL->begin = 0;
BL->end = 0;
}
else
//Take in account the rotation in the array.
BL->end = (BL->end == 0 ? BL->max_length - 1 : BL->end - 1);
--BL->length;
}
}
// Empty the Deque.
void BD_Reset(BoundedDeque *BL)
{
BL->length = 0;
BL->begin = 0;
BL->end = 0;
}
// Safe destructor: free the memory and set everything to default values.
void BD_Clear(BoundedDeque *BL)
{
if(BL->container != NULL)
{
free((char*) BL->container);
BL->container = NULL;
}
BL->max_length = 0;
BL->length = 0;
BL->begin = 0;
BL->end = 0;
}
//*****************************************************************************************
#endif