/src/igraph/src/core/dqueue.pmt
Line | Count | Source |
1 | | /* |
2 | | igraph library. |
3 | | Copyright (C) 2003-2012 Gabor Csardi <csardi.gabor@gmail.com> |
4 | | 334 Harvard street, Cambridge, MA 02139 USA |
5 | | |
6 | | This program is free software; you can redistribute it and/or modify |
7 | | it under the terms of the GNU General Public License as published by |
8 | | the Free Software Foundation; either version 2 of the License, or |
9 | | (at your option) any later version. |
10 | | |
11 | | This program is distributed in the hope that it will be useful, |
12 | | but WITHOUT ANY WARRANTY; without even the implied warranty of |
13 | | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
14 | | GNU General Public License for more details. |
15 | | |
16 | | You should have received a copy of the GNU General Public License |
17 | | along with this program; if not, write to the Free Software |
18 | | Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA |
19 | | 02110-1301 USA |
20 | | |
21 | | */ |
22 | | |
23 | | #include "igraph_memory.h" |
24 | | #include "igraph_error.h" |
25 | | |
26 | | #include <string.h> /* memcpy & co. */ |
27 | | #include <stdlib.h> |
28 | | |
29 | | /* Notes on the internal representation of dqueue: |
30 | | * |
31 | | * 'stor_begin' points at the beginning of the allocated storage. |
32 | | * 'stor_end' points one past the allocated storage. |
33 | | * |
34 | | * 'begin' points at the first element of the queue contents. |
35 | | * 'end' points one past the last element. |
36 | | * |
37 | | * The queue elements are stored "cyclically" within the allocated |
38 | | * buffer, and arithmetic on 'begin' and 'end' is done modulo |
39 | | * 'size = stor_end - stor_begin'. Thus the smallest valid value of |
40 | | * 'begin' and 'end' is 'stor_begin'. Their largest valid value is |
41 | | * 'stor_end - 1'. |
42 | | * |
43 | | * This means that 'begin == end' would be true both when the queue |
44 | | * is full and when it is empty. To distinguish between these |
45 | | * two situations, 'end' is set to NULL when the queue is empty. |
46 | | */ |
47 | | |
48 | | /** |
49 | | * \section igraph_dqueue |
50 | | * <para> |
51 | | * This is the classic data type of the double ended queue. Most of |
52 | | * the time it is used if a First-In-First-Out (FIFO) behavior is |
53 | | * needed. See the operations below. |
54 | | * </para> |
55 | | * |
56 | | * <para> |
57 | | * \example examples/simple/dqueue.c |
58 | | * </para> |
59 | | */ |
60 | | |
61 | | /** |
62 | | * \ingroup dqueue |
63 | | * \function igraph_dqueue_init |
64 | | * \brief Initialize a double ended queue (deque). |
65 | | * |
66 | | * The queue will be always empty. |
67 | | * |
68 | | * \param q Pointer to an uninitialized deque. |
69 | | * \param capacity How many elements to allocate memory for. |
70 | | * \return Error code. |
71 | | * |
72 | | * Time complexity: O(\p capacity). |
73 | | */ |
74 | | |
75 | 1.42M | igraph_error_t FUNCTION(igraph_dqueue, init)(TYPE(igraph_dqueue)* q, igraph_int_t capacity) { |
76 | 1.42M | IGRAPH_ASSERT(q != NULL); |
77 | 1.42M | IGRAPH_ASSERT(capacity >= 0); |
78 | | |
79 | 1.42M | if (capacity == 0) capacity = 1; |
80 | | |
81 | 1.42M | q->stor_begin = IGRAPH_CALLOC(capacity, BASE); |
82 | 1.42M | IGRAPH_CHECK_OOM(q->stor_begin, "Cannot initialize dqueue."); |
83 | 1.42M | q->stor_end = q->stor_begin + capacity; |
84 | 1.42M | q->begin = q->stor_begin; |
85 | 1.42M | q->end = NULL; |
86 | | |
87 | 1.42M | return IGRAPH_SUCCESS; |
88 | 1.42M | } Unexecuted instantiation: igraph_dqueue_init Line | Count | Source | 75 | 1.42M | igraph_error_t FUNCTION(igraph_dqueue, init)(TYPE(igraph_dqueue)* q, igraph_int_t capacity) { | 76 | 1.42M | IGRAPH_ASSERT(q != NULL); | 77 | 1.42M | IGRAPH_ASSERT(capacity >= 0); | 78 | | | 79 | 1.42M | if (capacity == 0) capacity = 1; | 80 | | | 81 | 1.42M | q->stor_begin = IGRAPH_CALLOC(capacity, BASE); | 82 | 1.42M | IGRAPH_CHECK_OOM(q->stor_begin, "Cannot initialize dqueue."); | 83 | 1.42M | q->stor_end = q->stor_begin + capacity; | 84 | 1.42M | q->begin = q->stor_begin; | 85 | 1.42M | q->end = NULL; | 86 | | | 87 | 1.42M | return IGRAPH_SUCCESS; | 88 | 1.42M | } |
Unexecuted instantiation: igraph_dqueue_char_init Unexecuted instantiation: igraph_dqueue_bool_init |
89 | | |
90 | | /** |
91 | | * \ingroup dqueue |
92 | | * \function igraph_dqueue_destroy |
93 | | * \brief Destroy a double ended queue. |
94 | | * |
95 | | * \param q The queue to destroy. |
96 | | * |
97 | | * Time complexity: O(1). |
98 | | */ |
99 | | |
100 | 1.42M | void FUNCTION(igraph_dqueue, destroy)(TYPE(igraph_dqueue)* q) { |
101 | 1.42M | IGRAPH_ASSERT(q != NULL); |
102 | 1.42M | IGRAPH_FREE(q->stor_begin); /* sets to NULL */ |
103 | 1.42M | } Unexecuted instantiation: igraph_dqueue_destroy igraph_dqueue_int_destroy Line | Count | Source | 100 | 1.42M | void FUNCTION(igraph_dqueue, destroy)(TYPE(igraph_dqueue)* q) { | 101 | 1.42M | IGRAPH_ASSERT(q != NULL); | 102 | 1.42M | IGRAPH_FREE(q->stor_begin); /* sets to NULL */ | 103 | 1.42M | } |
Unexecuted instantiation: igraph_dqueue_char_destroy Unexecuted instantiation: igraph_dqueue_bool_destroy |
104 | | |
105 | | /** |
106 | | * \ingroup dqueue |
107 | | * \function igraph_dqueue_empty |
108 | | * \brief Decide whether the queue is empty. |
109 | | * |
110 | | * \param q The queue. |
111 | | * \return Boolean, true if \p q contains at least one element, |
112 | | * false otherwise. |
113 | | * |
114 | | * Time complexity: O(1). |
115 | | */ |
116 | | |
117 | 104M | igraph_bool_t FUNCTION(igraph_dqueue, empty)(const TYPE(igraph_dqueue)* q) { |
118 | 104M | IGRAPH_ASSERT(q != NULL); |
119 | 104M | IGRAPH_ASSERT(q->stor_begin != NULL); |
120 | 104M | return q->end == NULL; |
121 | 104M | } Unexecuted instantiation: igraph_dqueue_empty Line | Count | Source | 117 | 104M | igraph_bool_t FUNCTION(igraph_dqueue, empty)(const TYPE(igraph_dqueue)* q) { | 118 | 104M | IGRAPH_ASSERT(q != NULL); | 119 | 104M | IGRAPH_ASSERT(q->stor_begin != NULL); | 120 | 104M | return q->end == NULL; | 121 | 104M | } |
Unexecuted instantiation: igraph_dqueue_char_empty Unexecuted instantiation: igraph_dqueue_bool_empty |
122 | | |
123 | | /** |
124 | | * \ingroup dqueue |
125 | | * \function igraph_dqueue_clear |
126 | | * \brief Remove all elements from the queue. |
127 | | * |
128 | | * \param q The queue. |
129 | | * |
130 | | * Time complexity: O(1). |
131 | | */ |
132 | | |
133 | 733k | void FUNCTION(igraph_dqueue, clear)(TYPE(igraph_dqueue)* q) { |
134 | 733k | IGRAPH_ASSERT(q != NULL); |
135 | 733k | IGRAPH_ASSERT(q->stor_begin != NULL); |
136 | 733k | q->begin = q->stor_begin; |
137 | 733k | q->end = NULL; |
138 | 733k | } Unexecuted instantiation: igraph_dqueue_clear Line | Count | Source | 133 | 733k | void FUNCTION(igraph_dqueue, clear)(TYPE(igraph_dqueue)* q) { | 134 | 733k | IGRAPH_ASSERT(q != NULL); | 135 | 733k | IGRAPH_ASSERT(q->stor_begin != NULL); | 136 | 733k | q->begin = q->stor_begin; | 137 | | q->end = NULL; | 138 | 733k | } |
Unexecuted instantiation: igraph_dqueue_char_clear Unexecuted instantiation: igraph_dqueue_bool_clear |
139 | | |
140 | | /** |
141 | | * \ingroup dqueue |
142 | | * \function igraph_dqueue_full |
143 | | * \brief Check whether the queue is full. |
144 | | * |
145 | | * If a queue is full the next \ref igraph_dqueue_push() operation will allocate |
146 | | * more memory. |
147 | | * |
148 | | * \param q The queue. |
149 | | * \return \c true if \p q is full, \c false otherwise. |
150 | | * |
151 | | * Time complecity: O(1). |
152 | | */ |
153 | | |
154 | 0 | igraph_bool_t FUNCTION(igraph_dqueue, full)(TYPE(igraph_dqueue)* q) { |
155 | 0 | IGRAPH_ASSERT(q != NULL); |
156 | 0 | IGRAPH_ASSERT(q->stor_begin != NULL); |
157 | 0 | return q->begin == q->end; |
158 | 0 | } Unexecuted instantiation: igraph_dqueue_full Unexecuted instantiation: igraph_dqueue_int_full Unexecuted instantiation: igraph_dqueue_char_full Unexecuted instantiation: igraph_dqueue_bool_full |
159 | | |
160 | | /** |
161 | | * \ingroup dqueue |
162 | | * \function igraph_dqueue_size |
163 | | * \brief Number of elements in the queue. |
164 | | * |
165 | | * \param q The queue. |
166 | | * \return Integer, the number of elements currently in the queue. |
167 | | * |
168 | | * Time complexity: O(1). |
169 | | */ |
170 | | |
171 | 207k | igraph_int_t FUNCTION(igraph_dqueue, size)(const TYPE(igraph_dqueue)* q) { |
172 | 207k | IGRAPH_ASSERT(q != NULL); |
173 | 207k | IGRAPH_ASSERT(q->stor_begin != NULL); |
174 | 207k | if (q->end == NULL) { |
175 | 0 | return 0; |
176 | 207k | } else if (q->begin < q->end) { |
177 | 134k | return q->end - q->begin; |
178 | 134k | } else { |
179 | 72.4k | return q->stor_end - q->begin + q->end - q->stor_begin; |
180 | 72.4k | } |
181 | 207k | } Unexecuted instantiation: igraph_dqueue_size Line | Count | Source | 171 | 207k | igraph_int_t FUNCTION(igraph_dqueue, size)(const TYPE(igraph_dqueue)* q) { | 172 | 207k | IGRAPH_ASSERT(q != NULL); | 173 | 207k | IGRAPH_ASSERT(q->stor_begin != NULL); | 174 | 207k | if (q->end == NULL) { | 175 | 0 | return 0; | 176 | 207k | } else if (q->begin < q->end) { | 177 | 134k | return q->end - q->begin; | 178 | 134k | } else { | 179 | 72.4k | return q->stor_end - q->begin + q->end - q->stor_begin; | 180 | 72.4k | } | 181 | 207k | } |
Unexecuted instantiation: igraph_dqueue_char_size Unexecuted instantiation: igraph_dqueue_bool_size |
182 | | |
183 | | /** |
184 | | * \ingroup dqueue |
185 | | * \function igraph_dqueue_head |
186 | | * \brief Head of the queue. |
187 | | * |
188 | | * The queue must contain at least one element. |
189 | | * |
190 | | * \param q The queue. |
191 | | * \return The first element in the queue. |
192 | | * |
193 | | * Time complexity: O(1). |
194 | | */ |
195 | | |
196 | 75.5k | BASE FUNCTION(igraph_dqueue, head)(const TYPE(igraph_dqueue)* q) { |
197 | 75.5k | IGRAPH_ASSERT(q != NULL); |
198 | 75.5k | IGRAPH_ASSERT(q->stor_begin != NULL); |
199 | 75.5k | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ |
200 | 75.5k | return *(q->begin); |
201 | 75.5k | } Unexecuted instantiation: igraph_dqueue_head Line | Count | Source | 196 | 75.5k | BASE FUNCTION(igraph_dqueue, head)(const TYPE(igraph_dqueue)* q) { | 197 | 75.5k | IGRAPH_ASSERT(q != NULL); | 198 | 75.5k | IGRAPH_ASSERT(q->stor_begin != NULL); | 199 | 75.5k | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ | 200 | 75.5k | return *(q->begin); | 201 | 75.5k | } |
Unexecuted instantiation: igraph_dqueue_char_head Unexecuted instantiation: igraph_dqueue_bool_head |
202 | | |
203 | | /** |
204 | | * \ingroup dqueue |
205 | | * \function igraph_dqueue_back |
206 | | * \brief Tail of the queue. |
207 | | * |
208 | | * The queue must contain at least one element. |
209 | | * |
210 | | * \param q The queue. |
211 | | * \return The last element in the queue. |
212 | | * |
213 | | * Time complexity: O(1). |
214 | | */ |
215 | | |
216 | 6.46M | BASE FUNCTION(igraph_dqueue, back)(const TYPE(igraph_dqueue)* q) { |
217 | 6.46M | IGRAPH_ASSERT(q != NULL); |
218 | 6.46M | IGRAPH_ASSERT(q->stor_begin != NULL); |
219 | 6.46M | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ |
220 | 6.46M | if (q->end == q->stor_begin) { |
221 | 4.62k | return *(q->stor_end - 1); |
222 | 4.62k | } |
223 | 6.46M | return *(q->end - 1); |
224 | 6.46M | } Unexecuted instantiation: igraph_dqueue_back Line | Count | Source | 216 | 6.46M | BASE FUNCTION(igraph_dqueue, back)(const TYPE(igraph_dqueue)* q) { | 217 | 6.46M | IGRAPH_ASSERT(q != NULL); | 218 | 6.46M | IGRAPH_ASSERT(q->stor_begin != NULL); | 219 | 6.46M | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ | 220 | 6.46M | if (q->end == q->stor_begin) { | 221 | 4.62k | return *(q->stor_end - 1); | 222 | 4.62k | } | 223 | 6.46M | return *(q->end - 1); | 224 | 6.46M | } |
Unexecuted instantiation: igraph_dqueue_char_back Unexecuted instantiation: igraph_dqueue_bool_back |
225 | | |
226 | | /** |
227 | | * \ingroup dqueue |
228 | | * \function igraph_dqueue_pop |
229 | | * \brief Remove the head. |
230 | | * |
231 | | * Removes and returns the first element in the queue. The queue must |
232 | | * be non-empty. |
233 | | * |
234 | | * \param q The input queue. |
235 | | * \return The first element in the queue. |
236 | | * |
237 | | * Time complexity: O(1). |
238 | | */ |
239 | | |
240 | 82.2M | BASE FUNCTION(igraph_dqueue, pop)(TYPE(igraph_dqueue)* q) { |
241 | 82.2M | IGRAPH_ASSERT(q != NULL); |
242 | 82.2M | IGRAPH_ASSERT(q->stor_begin != NULL); |
243 | 82.2M | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ |
244 | 82.2M | BASE tmp = *(q->begin); |
245 | 82.2M | (q->begin)++; |
246 | 82.2M | if (q->begin == q->stor_end) { |
247 | 1.19M | q->begin = q->stor_begin; |
248 | 1.19M | } |
249 | 82.2M | if (q->begin == q->end) { |
250 | 24.0M | q->end = NULL; |
251 | 24.0M | } |
252 | | |
253 | 82.2M | return tmp; |
254 | 82.2M | } Unexecuted instantiation: igraph_dqueue_pop Line | Count | Source | 240 | 82.2M | BASE FUNCTION(igraph_dqueue, pop)(TYPE(igraph_dqueue)* q) { | 241 | 82.2M | IGRAPH_ASSERT(q != NULL); | 242 | 82.2M | IGRAPH_ASSERT(q->stor_begin != NULL); | 243 | 82.2M | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ | 244 | 82.2M | BASE tmp = *(q->begin); | 245 | 82.2M | (q->begin)++; | 246 | 82.2M | if (q->begin == q->stor_end) { | 247 | 1.19M | q->begin = q->stor_begin; | 248 | 1.19M | } | 249 | 82.2M | if (q->begin == q->end) { | 250 | 24.0M | q->end = NULL; | 251 | 24.0M | } | 252 | | | 253 | 82.2M | return tmp; | 254 | 82.2M | } |
Unexecuted instantiation: igraph_dqueue_char_pop Unexecuted instantiation: igraph_dqueue_bool_pop |
255 | | |
256 | | /** |
257 | | * \ingroup dqueue |
258 | | * \function igraph_dqueue_pop_back |
259 | | * \brief Removes the tail. |
260 | | * |
261 | | * Removes and returns the last element in the queue. The queue must |
262 | | * be non-empty. |
263 | | * |
264 | | * \param q The queue. |
265 | | * \return The last element in the queue. |
266 | | * |
267 | | * Time complexity: O(1). |
268 | | */ |
269 | | |
270 | 3.67M | BASE FUNCTION(igraph_dqueue, pop_back)(TYPE(igraph_dqueue)* q) { |
271 | 3.67M | BASE tmp; |
272 | 3.67M | IGRAPH_ASSERT(q != NULL); |
273 | 3.67M | IGRAPH_ASSERT(q->stor_begin != NULL); |
274 | 3.67M | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ |
275 | 3.67M | if (q->end != q->stor_begin) { |
276 | 3.65M | tmp = *((q->end) - 1); |
277 | 3.65M | q->end = (q->end) - 1; |
278 | 3.65M | } else { |
279 | 16.6k | tmp = *((q->stor_end) - 1); |
280 | 16.6k | q->end = (q->stor_end) - 1; |
281 | 16.6k | } |
282 | 3.67M | if (q->begin == q->end) { |
283 | 2.78M | q->end = NULL; |
284 | 2.78M | } |
285 | | |
286 | 3.67M | return tmp; |
287 | 3.67M | } Unexecuted instantiation: igraph_dqueue_pop_back igraph_dqueue_int_pop_back Line | Count | Source | 270 | 3.67M | BASE FUNCTION(igraph_dqueue, pop_back)(TYPE(igraph_dqueue)* q) { | 271 | 3.67M | BASE tmp; | 272 | 3.67M | IGRAPH_ASSERT(q != NULL); | 273 | 3.67M | IGRAPH_ASSERT(q->stor_begin != NULL); | 274 | 3.67M | IGRAPH_ASSERT(q->stor_end != NULL); /* queue is not empty */ | 275 | 3.67M | if (q->end != q->stor_begin) { | 276 | 3.65M | tmp = *((q->end) - 1); | 277 | 3.65M | q->end = (q->end) - 1; | 278 | 3.65M | } else { | 279 | 16.6k | tmp = *((q->stor_end) - 1); | 280 | 16.6k | q->end = (q->stor_end) - 1; | 281 | 16.6k | } | 282 | 3.67M | if (q->begin == q->end) { | 283 | 2.78M | q->end = NULL; | 284 | 2.78M | } | 285 | | | 286 | 3.67M | return tmp; | 287 | 3.67M | } |
Unexecuted instantiation: igraph_dqueue_char_pop_back Unexecuted instantiation: igraph_dqueue_bool_pop_back |
288 | | |
289 | | /** |
290 | | * \ingroup dqueue |
291 | | * \function igraph_dqueue_push |
292 | | * \brief Appends an element. |
293 | | * |
294 | | * Append an element to the end of the queue. |
295 | | * |
296 | | * \param q The queue. |
297 | | * \param elem The element to append. |
298 | | * \return Error code. |
299 | | * |
300 | | * Time complexity: O(1) if no memory allocation is needed, O(n), the |
301 | | * number of elements in the queue otherwise. But note that by |
302 | | * allocating always twice as much memory as the current size of the |
303 | | * queue we ensure that n push operations can always be done in at |
304 | | * most O(n) time. (Assuming memory allocation is at most linear.) |
305 | | */ |
306 | | |
307 | 86.1M | igraph_error_t FUNCTION(igraph_dqueue, push)(TYPE(igraph_dqueue)* q, BASE elem) { |
308 | 86.1M | IGRAPH_ASSERT(q != NULL); |
309 | 86.1M | IGRAPH_ASSERT(q->stor_begin != NULL); |
310 | 86.1M | if (q->begin != q->end) { |
311 | | /* not full */ |
312 | 85.5M | if (q->end == NULL) { |
313 | 26.8M | q->end = q->begin; |
314 | 26.8M | } |
315 | 85.5M | *(q->end) = elem; |
316 | 85.5M | (q->end)++; |
317 | 85.5M | if (q->end == q->stor_end) { |
318 | 1.39M | q->end = q->stor_begin; |
319 | 1.39M | } |
320 | 85.5M | } else { |
321 | | /* full, allocate more storage */ |
322 | | |
323 | 597k | BASE *bigger = NULL, *old = q->stor_begin; |
324 | 597k | igraph_int_t old_size = q->stor_end - q->stor_begin; |
325 | 597k | igraph_int_t new_capacity = old_size < IGRAPH_INTEGER_MAX/2 ? old_size * 2 : IGRAPH_INTEGER_MAX; |
326 | | |
327 | 597k | if (old_size == IGRAPH_INTEGER_MAX) { |
328 | 0 | IGRAPH_ERROR("Cannot push to dqueue, already at maximum size.", IGRAPH_EOVERFLOW); |
329 | 0 | } |
330 | 597k | if (new_capacity == 0) { |
331 | 0 | new_capacity = 1; |
332 | 0 | } |
333 | | |
334 | 597k | bigger = IGRAPH_CALLOC(new_capacity, BASE); |
335 | 597k | IGRAPH_CHECK_OOM(bigger, "Cannot push to dqueue."); |
336 | | |
337 | 597k | if (q->stor_end - q->begin > 0) { |
338 | 597k | memcpy(bigger, q->begin, |
339 | 597k | (size_t)(q->stor_end - q->begin) * sizeof(BASE)); |
340 | 597k | } |
341 | 597k | if (q->end - q->stor_begin > 0) { |
342 | 36.5k | memcpy(bigger + (q->stor_end - q->begin), q->stor_begin, |
343 | 36.5k | (size_t)(q->end - q->stor_begin) * sizeof(BASE)); |
344 | 36.5k | } |
345 | | |
346 | 597k | q->end = bigger + old_size; |
347 | 597k | q->stor_end = bigger + new_capacity; |
348 | 597k | q->stor_begin = bigger; |
349 | 597k | q->begin = bigger; |
350 | | |
351 | 597k | *(q->end) = elem; |
352 | 597k | (q->end)++; |
353 | 597k | if (q->end == q->stor_end) { |
354 | 415k | q->end = q->stor_begin; |
355 | 415k | } |
356 | | |
357 | 597k | IGRAPH_FREE(old); |
358 | 597k | } |
359 | | |
360 | 86.1M | return IGRAPH_SUCCESS; |
361 | 86.1M | } Unexecuted instantiation: igraph_dqueue_push Line | Count | Source | 307 | 86.1M | igraph_error_t FUNCTION(igraph_dqueue, push)(TYPE(igraph_dqueue)* q, BASE elem) { | 308 | 86.1M | IGRAPH_ASSERT(q != NULL); | 309 | 86.1M | IGRAPH_ASSERT(q->stor_begin != NULL); | 310 | 86.1M | if (q->begin != q->end) { | 311 | | /* not full */ | 312 | 85.5M | if (q->end == NULL) { | 313 | 26.8M | q->end = q->begin; | 314 | 26.8M | } | 315 | 85.5M | *(q->end) = elem; | 316 | 85.5M | (q->end)++; | 317 | 85.5M | if (q->end == q->stor_end) { | 318 | 1.39M | q->end = q->stor_begin; | 319 | 1.39M | } | 320 | 85.5M | } else { | 321 | | /* full, allocate more storage */ | 322 | | | 323 | 597k | BASE *bigger = NULL, *old = q->stor_begin; | 324 | 597k | igraph_int_t old_size = q->stor_end - q->stor_begin; | 325 | 597k | igraph_int_t new_capacity = old_size < IGRAPH_INTEGER_MAX/2 ? old_size * 2 : IGRAPH_INTEGER_MAX; | 326 | | | 327 | 597k | if (old_size == IGRAPH_INTEGER_MAX) { | 328 | 0 | IGRAPH_ERROR("Cannot push to dqueue, already at maximum size.", IGRAPH_EOVERFLOW); | 329 | 0 | } | 330 | 597k | if (new_capacity == 0) { | 331 | 0 | new_capacity = 1; | 332 | 0 | } | 333 | | | 334 | 597k | bigger = IGRAPH_CALLOC(new_capacity, BASE); | 335 | 597k | IGRAPH_CHECK_OOM(bigger, "Cannot push to dqueue."); | 336 | | | 337 | 597k | if (q->stor_end - q->begin > 0) { | 338 | 597k | memcpy(bigger, q->begin, | 339 | 597k | (size_t)(q->stor_end - q->begin) * sizeof(BASE)); | 340 | 597k | } | 341 | 597k | if (q->end - q->stor_begin > 0) { | 342 | 36.5k | memcpy(bigger + (q->stor_end - q->begin), q->stor_begin, | 343 | 36.5k | (size_t)(q->end - q->stor_begin) * sizeof(BASE)); | 344 | 36.5k | } | 345 | | | 346 | 597k | q->end = bigger + old_size; | 347 | 597k | q->stor_end = bigger + new_capacity; | 348 | 597k | q->stor_begin = bigger; | 349 | 597k | q->begin = bigger; | 350 | | | 351 | 597k | *(q->end) = elem; | 352 | 597k | (q->end)++; | 353 | 597k | if (q->end == q->stor_end) { | 354 | 415k | q->end = q->stor_begin; | 355 | 415k | } | 356 | | | 357 | 597k | IGRAPH_FREE(old); | 358 | 597k | } | 359 | | | 360 | 86.1M | return IGRAPH_SUCCESS; | 361 | 86.1M | } |
Unexecuted instantiation: igraph_dqueue_char_push Unexecuted instantiation: igraph_dqueue_bool_push |
362 | | |
363 | | #if defined (OUT_FORMAT) |
364 | | |
365 | | #ifndef USING_R |
366 | 0 | igraph_error_t FUNCTION(igraph_dqueue, print)(const TYPE(igraph_dqueue)* q) { |
367 | 0 | return FUNCTION(igraph_dqueue, fprint)(q, stdout); |
368 | 0 | } Unexecuted instantiation: igraph_dqueue_print Unexecuted instantiation: igraph_dqueue_int_print Unexecuted instantiation: igraph_dqueue_char_print Unexecuted instantiation: igraph_dqueue_bool_print |
369 | | #endif |
370 | | |
371 | 0 | igraph_error_t FUNCTION(igraph_dqueue, fprint)(const TYPE(igraph_dqueue)* q, FILE *file) { |
372 | 0 | if (q->end != NULL) { |
373 | | /* There is one element at least */ |
374 | 0 | BASE *p = q->begin; |
375 | 0 | fprintf(file, OUT_FORMAT, *p); |
376 | 0 | p++; |
377 | 0 | if (q->end > q->begin) { |
378 | | /* Q is in one piece */ |
379 | 0 | while (p != q->end) { |
380 | 0 | fprintf(file, " " OUT_FORMAT, *p); |
381 | 0 | p++; |
382 | 0 | } |
383 | 0 | } else { |
384 | | /* Q is in two pieces */ |
385 | 0 | while (p != q->stor_end) { |
386 | 0 | fprintf(file, " " OUT_FORMAT, *p); |
387 | 0 | p++; |
388 | 0 | } |
389 | 0 | p = q->stor_begin; |
390 | 0 | while (p != q->end) { |
391 | 0 | fprintf(file, " " OUT_FORMAT, *p); |
392 | 0 | p++; |
393 | 0 | } |
394 | 0 | } |
395 | 0 | } |
396 | |
|
397 | 0 | fprintf(file, "\n"); |
398 | |
|
399 | 0 | return IGRAPH_SUCCESS; |
400 | 0 | } Unexecuted instantiation: igraph_dqueue_fprint Unexecuted instantiation: igraph_dqueue_int_fprint Unexecuted instantiation: igraph_dqueue_char_fprint Unexecuted instantiation: igraph_dqueue_bool_fprint |
401 | | |
402 | | #endif |
403 | | |
404 | | /** |
405 | | * \ingroup dqueue |
406 | | * \function igraph_dqueue_get |
407 | | * \brief Access an element in a queue. |
408 | | * |
409 | | * \param q The queue. |
410 | | * \param idx The index of the element within the queue. |
411 | | * \return The desired element. |
412 | | * |
413 | | * Time complexity: O(1). |
414 | | */ |
415 | | |
416 | 142k | BASE FUNCTION(igraph_dqueue, get)(const TYPE(igraph_dqueue) *q, igraph_int_t idx) { |
417 | 142k | IGRAPH_ASSERT(idx >= 0); |
418 | 142k | IGRAPH_ASSERT(idx < FUNCTION(igraph_dqueue, size)(q)); |
419 | 142k | if ((q->begin + idx < q->end) || |
420 | 142k | (q->begin >= q->end && q->begin + idx < q->stor_end)) { |
421 | 142k | return q->begin[idx]; |
422 | 142k | } else if (q->begin >= q->end && q->stor_begin + idx < q->end) { |
423 | 0 | idx = idx - (q->stor_end - q->begin); |
424 | 0 | return q->stor_begin[idx]; |
425 | 0 | } else { |
426 | | /* The assertions at the top make it impossible to reach here, |
427 | | but omitting this branch would cause compiler warnings. */ |
428 | 0 | IGRAPH_FATAL("Out of bounds access in dqueue."); |
429 | 0 | } |
430 | 142k | } Unexecuted instantiation: igraph_dqueue_get Line | Count | Source | 416 | 142k | BASE FUNCTION(igraph_dqueue, get)(const TYPE(igraph_dqueue) *q, igraph_int_t idx) { | 417 | 142k | IGRAPH_ASSERT(idx >= 0); | 418 | 142k | IGRAPH_ASSERT(idx < FUNCTION(igraph_dqueue, size)(q)); | 419 | 142k | if ((q->begin + idx < q->end) || | 420 | 142k | (q->begin >= q->end && q->begin + idx < q->stor_end)) { | 421 | 142k | return q->begin[idx]; | 422 | 142k | } else if (q->begin >= q->end && q->stor_begin + idx < q->end) { | 423 | 0 | idx = idx - (q->stor_end - q->begin); | 424 | 0 | return q->stor_begin[idx]; | 425 | 0 | } else { | 426 | | /* The assertions at the top make it impossible to reach here, | 427 | | but omitting this branch would cause compiler warnings. */ | 428 | 0 | IGRAPH_FATAL("Out of bounds access in dqueue."); | 429 | 0 | } | 430 | 142k | } |
Unexecuted instantiation: igraph_dqueue_char_get Unexecuted instantiation: igraph_dqueue_bool_get |