-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathSPBPriorityQueue.h
240 lines (217 loc) · 6.41 KB
/
SPBPriorityQueue.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
234
235
236
237
238
239
240
#ifndef SPBPRIORITYQUEUE_H_
#define SPBPRIORITYQUEUE_H_
#include <stdbool.h>
/**
* SP Bounded Priority Queue summary
*
* A bounded priority queue with element type of BPQueueElement
*
* pop the element with the lowest value in O(1) complexity
*/
/** type used to define Bounded priority queue **/
typedef struct sp_bp_queue_t SPBPQueue;
typedef struct sp_bpq_element_t {
int index;
double value;
} BPQueueElement;
/** type for error reporting **/
typedef enum sp_bp_queue_msg_t {
SP_BPQUEUE_OUT_OF_MEMORY,
SP_BPQUEUE_FULL,
SP_BPQUEUE_EMPTY,
SP_BPQUEUE_INVALID_ARGUMENT,
SP_BPQUEUE_SUCCESS
} SP_BPQUEUE_MSG;
/**
* Allocates a new SPBPQueue in the memory.
* Given maxSize the bound of the queue
*
* Initialize every queue element to EMPTY_BPQUEUE_ELEMENT_INDEX & EMPTY_BPQUEUE_ELEMENT_VALUE
*
* O(maxSize) - complexity
*
* @param maxSize is the maximum Capacity of the queue
* @assert(maxSize>0)
* @return
* NULL in case allocation failure occurred
* Otherwise, the new point is returned
*/
SPBPQueue* spBPQueueCreate(int maxSize);
/**
* Allocates a copy of the given Queue.
*
* Given the SPBQueue source, the functions returns a
* new SPBQueue such that:
*
* - copy_maxSize = source_maxSize (The maximum capacity is the same)
* - size(copy) = size(source) (copy and source have the same current size)
* - for every BPQueueElement:
* - index(copy) = index(source) (copy and source have the same index)
* - value(copy) = value(source) (copy and source have the same value)
*
* @param source - The source queue
* @assert (source != NULL && source->queue !=NULL)
*
* @return
* NULL in case memory allocation occurs
* Others a copy of source is returned.
*/
SPBPQueue* spBPQueueCopy(SPBPQueue* source);
/**
* Free all memory allocation associated with point,
* if source is NULL nothing happens.
*/
void spBPQueueDestroy(SPBPQueue* source);
/**
* Given the SPBQueue source,the function initialize the queue
* the functions returns source such that:
*
* - source_maxSize (no change)
* - size(source)=0 (initialized the size to empty queue)
* Initialize every queue element to EMPTY_BPQUEUE_ELEMENT_VALUE & EMPTY_BPQUEUE_ELEMENT_VALUE
*
* @param source - The source queue
* @assert (source != NUlL)
*/
void spBPQueueClear(SPBPQueue* source);
/**
* returns the number of elements in the queue source.
*
* Given the SPBQueue source
* @param source - The source queue
* @assert(NULL != source)
*
* @return
* The numbers of elements in the queue
*/
int spBPQueueSize(SPBPQueue* source);
/**
* returns the maximum capacity of elements in the queue source.
*
* Given the SPBQueue source
* @param source - The source queue
* @assert (source != NUlL)
*
* @return
* The maximum capacity of elements in the queue
*/
int spBPQueueGetMaxSize(SPBPQueue* source);
/**
* Inserts an element to the queue
*
* @param source pointer to the queue that we will insert the element to
* @param index is the index of the element that we insert
* @param value is the value of the element that we insert
*
* case1 : the queue isn't full
* - inserts the value and the index after all the values
* that with higher value than the new value
*
* case2 : the queue is full & the value is higher than every value in the queue
* - nothing happened
* case3 : the queue is full & the value is not higher than every value in the queue
* - follow case1 and remove the element with the highest value
*
* * O(size) - complexity
*
* @return
* SP_BPQUEUE_INVALID_ARGUMENT -for NULL pointer or value is negative
* SP_BPQUEUE_FULL - the insertion was successful and the queue is full capacity
* SP_BPQUEUE_SUCCESS - the insertion was successful
*
*/
SP_BPQUEUE_MSG spBPQueueEnqueue(SPBPQueue* source, int index, double value);
/**
* removes the element with the lowest value
*
* @param source pointer to the queue
*
* @return
* SP_BPQUEUE_INVALID_ARGUMENT -for NULL pointer
* SP_BPQUEUE_EMPTY - for am empty queue and do nothing
* SP_BPQUEUE_SUCCESS - switch the lowest element with empty value and index
* and decrement the size of the queue
*/
SP_BPQUEUE_MSG spBPQueueDequeue(SPBPQueue* source);
/**
* Returns a copy of the element with the lowest value
*
* @param source pointer to the queue
* @param res pointer to BPQueueElement that a copy to the last element will insert to
*
* @return
* SP_BPQUEUE_INVALID_ARGUMENT -for NULL pointer
* SP_BPQUEUE_EMPTY - for am empty queue and return empty index and value in res
* SP_BPQUEUE_SUCCESS - the copy of the lowest element was successful
*
*/
SP_BPQUEUE_MSG spBPQueuePeek(SPBPQueue* source, BPQueueElement* res);
/**
* Returns a copy of the element with the highest value
*
* @param source pointer to the queue
* @param res pointer to BPQueueElement that a copy to the last element will insert to
*
* @return
* SP_BPQUEUE_INVALID_ARGUMENT -for NULL pointer
* SP_BPQUEUE_EMPTY - for am empty queue and return NULL in res
* SP_BPQUEUE_SUCCESS - the copy of the highest element was successful
*/
SP_BPQUEUE_MSG spBPQueuePeekLast(SPBPQueue* source, BPQueueElement* res);
/**
* returns the minimum value in the queue
*
* Given the SPBQueue source
*
* @param source - The source queue
* @assert(NULL != source)
* @assert(source->queue != NULL);
*
* @return
* double - the minimum value in the queue or -1 if the queue is empty
*
*/
double spBPQueueMinValue(SPBPQueue* source);
/**
* returns the maximum value in the queue
*
* Given the SPBQueue source
*
* @param source - The source queue
* @assert(NULL != source)
* @assert(source->queue != NULL);
*
* @return
* double - the maximum value in the queue or -1 if the queue is empty
*
*/
double spBPQueueMaxValue(SPBPQueue* source);
/**
* checks if the queue is empty
*
* Given the SPBQueue source
*
* @param source - The source queue
* @assert(NULL != source)
*
* @return
* TRUE - the queue is empty
* FALSE - the queue is not empty
*
*/
bool spBPQueueIsEmpty(SPBPQueue* source);
/**
* checks if the queue is in full capacity
*
* Given the SPBQueue source
*
* @param source - The source queue
* @assert(NULL != source)
*
* @return
* TRUE - the queue is in full capacity
* FALSE - the queue is not full capacity
*
*/
bool spBPQueueIsFull(SPBPQueue* source);
#endif