10 #undef a_que_push_fore
11 #undef a_que_push_back
12 #undef a_que_pull_fore
13 #undef a_que_pull_back
15 static a_que_node
*a_que_node_new(a_que
*ctx
)
17 a_que_node
*node
= A_NULL
;
20 node
= ctx
->_ptr
[--ctx
->_cur
];
24 node
= (a_que_node
*)a_alloc(A_NULL
, sizeof(a_que_node
));
25 if (a_unlikely(!node
)) { return node
; }
30 node
->_data
= a_alloc(A_NULL
, ctx
->_siz
);
31 if (a_unlikely(!node
->_data
))
41 static int a_que_node_die(a_que
*ctx
, a_que_node
*obj
)
43 if (!obj
) { return A_INVALID
; }
44 if (ctx
->_mem
<= ctx
->_cur
)
46 a_size
const mem
= ctx
->_mem
+ (ctx
->_mem
>> 1) + 1;
47 a_que_node
**const ptr
= (a_que_node
**)a_alloc(ctx
->_ptr
, sizeof(void *) * mem
);
48 if (a_unlikely(!ptr
)) { return A_FAILURE
; }
52 ctx
->_ptr
[ctx
->_cur
++] = obj
;
57 static void a_que_drop_(a_que
*ctx
)
59 while (ctx
->_head
.next
!= &ctx
->_head
)
61 a_que_node
*const node
= (a_que_node
*)ctx
->_head
.next
;
62 if (a_unlikely(a_que_node_die(ctx
, node
))) { break; }
63 a_list_del_node(&node
->_node
);
64 a_list_dtor(&node
->_node
);
68 a_que
*a_que_new(a_size size
)
70 a_que
*const ctx
= (a_que
*)a_alloc(A_NULL
, sizeof(a_que
));
71 if (ctx
) { a_que_ctor(ctx
, size
); }
75 void a_que_die(a_que
*ctx
, void (*dtor
)(void *))
79 a_que_dtor(ctx
, dtor
);
84 void a_que_ctor(a_que
*ctx
, a_size size
)
86 a_list_ctor(&ctx
->_head
);
87 ctx
->_siz
= size
? size
: sizeof(a_cast
);
94 void a_que_dtor(a_que
*ctx
, void (*dtor
)(void *))
101 for (; ctx
->_cur
; ++node
, --ctx
->_cur
)
103 dtor((**node
)._data
);
104 a_alloc((**node
)._data
, 0);
110 for (; ctx
->_cur
; ++node
, --ctx
->_cur
)
112 a_alloc((**node
)._data
, 0);
116 a_alloc(ctx
->_ptr
, 0);
122 void a_que_move(a_que
*ctx
, a_que
*obj
)
124 a_copy(ctx
, obj
, sizeof(*obj
));
125 a_zero(obj
, sizeof(*obj
));
128 void *a_que_at(a_que
const *ctx
, a_imax idx
)
134 a_list_foreach_prev(it
, &ctx
->_head
)
138 ptr
= ((a_que_node
*)it
)->_data
;
145 a_list_foreach_next(it
, &ctx
->_head
)
149 ptr
= ((a_que_node
*)it
)->_data
;
157 void a_que_drop(a_que
*ctx
, void (*dtor
)(void *))
164 for (a_size cur
= ctx
->_cur
; cur
; ++node
, --cur
)
166 dtor((**node
)._data
);
167 a_alloc((**node
)._data
, 0);
168 (**node
)._data
= A_NULL
;
173 for (a_size cur
= ctx
->_cur
; cur
; ++node
, --cur
)
175 a_alloc((**node
)._data
, 0);
176 (**node
)._data
= A_NULL
;
181 void a_que_edit(a_que
*ctx
, a_size size
, void (*dtor
)(void *))
183 size
= size
? size
: sizeof(a_cast
);
184 a_que_drop(ctx
, dtor
);
188 int a_que_swap_(a_que
const *ctx
, void *lhs
, void *rhs
)
191 a_que_node
*l
= A_NULL
;
192 a_que_node
*r
= A_NULL
;
193 if (lhs
== rhs
) { return A_SUCCESS
; }
194 a_list_foreach_next(it
, &ctx
->_head
)
196 a_que_node
*const node
= (a_que_node
*)it
;
197 if (node
->_data
== lhs
|| node
->_data
== rhs
)
210 a_list_swap_node(&l
->_node
, &r
->_node
);
218 void a_que_swap(a_que
const *ctx
, a_size lhs
, a_size rhs
)
221 a_size
const num
= ctx
->_num
- 1;
222 lhs
= lhs
< ctx
->_num
? lhs
: num
;
223 rhs
= rhs
< ctx
->_num
? rhs
: num
;
226 a_que_node
*l
= A_NULL
;
227 a_que_node
*r
= A_NULL
;
228 a_list_foreach_next(it
, &ctx
->_head
)
230 if (cur
== lhs
|| cur
== rhs
)
232 // because lhs less than num
233 // it's never a null pointer
236 l
= (a_que_node
*)it
;
240 r
= (a_que_node
*)it
;
246 a_list_swap_node(&l
->_node
, &r
->_node
);
254 void a_que_sort_fore(a_que
const *ctx
, int (*cmp
)(void const *, void const *))
259 a_list
*const it
= ctx
->_head
.next
;
260 for (a_list
*at
= it
->next
; at
!= &ctx
->_head
; at
= at
->next
)
262 void *const lhs
= ((a_que_node
*)it
)->_data
;
263 void *const rhs
= ((a_que_node
*)at
)->_data
;
264 if (cmp(lhs
, rhs
) > 0) { pt
= at
; }
269 /* a<->it<->b<->pt<->a */
270 a_list_link(it
->prev
, it
->next
); // a<->b
271 a_list_link(it
, pt
->next
); // it<->a
272 a_list_link(pt
, it
); // pt<->it
273 /* a<->b<->pt<->it<->a */
278 void a_que_sort_back(a_que
const *ctx
, int (*cmp
)(void const *, void const *))
283 a_list
*const it
= ctx
->_head
.prev
;
284 for (a_list
*at
= it
->prev
; at
!= &ctx
->_head
; at
= at
->prev
)
286 void *const lhs
= ((a_que_node
*)at
)->_data
;
287 void *const rhs
= ((a_que_node
*)it
)->_data
;
288 if (cmp(lhs
, rhs
) > 0) { pt
= at
; }
293 /* a<->pt<->b<->it<->a */
294 a_list_link(it
->prev
, it
->next
); // b<->a
295 a_list_link(pt
->prev
, it
); // a<->it
296 a_list_link(it
, pt
); // it<->pt
297 /* a<->it<->pt<->b<->a */
302 void *a_que_push_fore(a_que
*ctx
)
304 a_que_node
*const node
= a_que_node_new(ctx
);
305 if (a_unlikely(!node
)) { return node
; }
306 a_list_add_next(&ctx
->_head
, &node
->_node
);
310 void *a_que_push_back(a_que
*ctx
)
312 a_que_node
*const node
= a_que_node_new(ctx
);
313 if (a_unlikely(!node
)) { return node
; }
314 a_list_add_prev(&ctx
->_head
, &node
->_node
);
318 void *a_que_pull_fore(a_que
*ctx
)
321 if (ctx
->_head
.next
!= &ctx
->_head
)
323 a_que_node
*const node
= (a_que_node
*)ctx
->_head
.next
;
324 if (a_unlikely(a_que_node_die(ctx
, node
))) { goto fail
; }
325 a_list_del_node(&node
->_node
);
326 a_list_dtor(&node
->_node
);
333 void *a_que_pull_back(a_que
*ctx
)
336 if (ctx
->_head
.prev
!= &ctx
->_head
)
338 a_que_node
*const node
= (a_que_node
*)ctx
->_head
.prev
;
339 if (a_unlikely(a_que_node_die(ctx
, node
))) { goto fail
; }
340 a_list_del_node(&node
->_node
);
341 a_list_dtor(&node
->_node
);
348 void *a_que_insert(a_que
*ctx
, a_size idx
)
353 a_que_node
*const node
= a_que_node_new(ctx
);
354 if (a_unlikely(!node
)) { return node
; }
355 a_list_foreach_next(it
, &ctx
->_head
)
359 a_list_add_prev(it
, &node
->_node
);
365 return a_que_push_back(ctx
);
368 void *a_que_remove(a_que
*ctx
, a_size idx
)
373 a_que_node
*node
= A_NULL
;
374 a_list_foreach_next(it
, &ctx
->_head
)
378 // because idx less than num
379 // it's never a null pointer
380 node
= (a_que_node
*)it
;
384 if (a_unlikely(a_que_node_die(ctx
, node
))) { return A_NULL
; }
385 a_list_del_node(&node
->_node
);
386 a_list_dtor(&node
->_node
);
389 return a_que_pull_back(ctx
);