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_list
*a_que_new_(a_que
*ctx
)
17 a_list
*node
= A_NULL
;
20 node
= ctx
->ptr_
[--ctx
->cur_
];
24 node
= (a_list
*)a_alloc(A_NULL
, sizeof(a_list
) + ctx
->siz_
);
25 if (a_unlikely(!node
)) { return node
; }
31 static int a_que_die_(a_que
*ctx
, a_list
*node
)
33 if (a_unlikely(!node
)) { return A_INVALID
; }
34 if (ctx
->mem_
<= ctx
->cur_
)
36 a_size
const mem
= ctx
->mem_
+ (ctx
->mem_
>> 1) + 1;
37 a_list
**const ptr
= (a_list
**)a_alloc(ctx
->ptr_
, sizeof(void *) * mem
);
38 if (a_unlikely(!ptr
)) { return A_FAILURE
; }
42 ctx
->ptr_
[ctx
->cur_
++] = node
;
47 a_que
*a_que_new(a_size size
)
49 a_que
*const ctx
= (a_que
*)a_alloc(A_NULL
, sizeof(a_que
));
50 if (ctx
) { a_que_ctor(ctx
, size
); }
54 void a_que_die(a_que
*ctx
, void (*dtor
)(void *))
58 a_que_dtor(ctx
, dtor
);
63 void a_que_ctor(a_que
*ctx
, a_size size
)
65 if (!size
) { size
= sizeof(a_cast
); }
66 a_list_ctor(&ctx
->head_
);
74 void a_que_dtor(a_que
*ctx
, void (*dtor
)(void *))
78 for (a_list
**node
= ctx
->ptr_
; ctx
->cur_
; --ctx
->cur_
)
83 a_list_forsafe_next(node
, next
, &ctx
->head_
)
91 for (a_list
**node
= ctx
->ptr_
; ctx
->cur_
; --ctx
->cur_
)
95 a_list_forsafe_next(node
, next
, &ctx
->head_
)
100 a_alloc(ctx
->ptr_
, 0);
106 void a_que_move(a_que
*ctx
, a_que
*obj
)
108 a_copy(ctx
, obj
, sizeof(*obj
));
109 a_zero(obj
, sizeof(*obj
));
112 void *a_que_at(a_que
const *ctx
, a_imax idx
)
117 a_list_foreach_next(it
, &ctx
->head_
)
119 if (cur
++ == idx
) { return it
+ 1; }
124 a_list_foreach_prev(it
, &ctx
->head_
)
126 if (--cur
== idx
) { return it
+ 1; }
132 int a_que_drop(a_que
*ctx
, void (*dtor
)(void *))
134 a_list
*const head
= &ctx
->head_
;
135 for (a_list
*node
= head
->next
; node
!= head
; node
= head
->next
)
137 if (a_que_die_(ctx
, node
) == A_SUCCESS
)
139 a_list_del_node(node
);
142 else { return A_FAILURE
; }
146 a_size cur
= ctx
->cur_
;
147 a_list
**node
= ctx
->ptr_
;
148 for (; cur
; ++node
, --cur
)
156 int a_que_edit(a_que
*ctx
, a_size size
, void (*dtor
)(void *))
158 int ok
= a_que_drop(ctx
, dtor
);
161 if (!size
) { size
= sizeof(a_cast
); }
162 if (size
> ctx
->siz_
)
164 a_size cur
= ctx
->cur_
;
165 a_list
**node
= ctx
->ptr_
;
166 for (; cur
; ++node
, --cur
)
168 void *const ptr
= a_alloc(*node
, sizeof(a_list
) + size
);
169 if (a_unlikely(!ptr
)) { return A_FAILURE
; }
170 *node
= (a_list
*)ptr
;
178 int a_que_swap(a_que
const *ctx
, a_size lhs
, a_size rhs
)
183 a_size
const num
= ctx
->num_
- 1;
184 lhs
= lhs
< ctx
->num_
? lhs
: num
;
185 rhs
= rhs
< ctx
->num_
? rhs
: num
;
186 if (lhs
== rhs
) { return A_SUCCESS
; }
187 a_list_foreach_next(it
, &ctx
->head_
)
189 if (cur
== lhs
|| cur
== rhs
)
193 a_list_swap_node(it
, at
);
204 void a_que_sort_fore(a_que
const *ctx
, int (*cmp
)(void const *, void const *))
209 a_list
*const it
= ctx
->head_
.next
;
210 for (a_list
*at
= it
->next
; at
!= &ctx
->head_
; at
= at
->next
)
212 if (cmp(it
+ 1, at
+ 1) > 0) { ok
= at
; }
217 /* a-it-b-at-a => a-b-at-it-a */
218 a_list_link(it
->prev
, it
->next
); // a-b
219 a_list_link(it
, ok
->next
); // it-a
220 a_list_link(ok
, it
); // at-it
225 void a_que_sort_back(a_que
const *ctx
, int (*cmp
)(void const *, void const *))
230 a_list
*const it
= ctx
->head_
.prev
;
231 for (a_list
*at
= it
->prev
; at
!= &ctx
->head_
; at
= at
->prev
)
233 if (cmp(at
+ 1, it
+ 1) > 0) { ok
= at
; }
238 /* a-at-b-it-a => a-it-at-b-a */
239 a_list_link(it
->prev
, it
->next
); // b-a
240 a_list_link(ok
->prev
, it
); // a-it
241 a_list_link(it
, ok
); // it-at
246 void *a_que_push_fore(a_que
*ctx
)
248 a_list
*const node
= a_que_new_(ctx
);
249 if (a_unlikely(!node
)) { return node
; }
250 a_list_add_next(&ctx
->head_
, node
);
254 void *a_que_push_back(a_que
*ctx
)
256 a_list
*const node
= a_que_new_(ctx
);
257 if (a_unlikely(!node
)) { return node
; }
258 a_list_add_prev(&ctx
->head_
, node
);
262 void *a_que_pull_fore(a_que
*ctx
)
264 if (ctx
->head_
.next
!= &ctx
->head_
)
266 a_list
*const node
= (a_list
*)ctx
->head_
.next
;
267 if (a_que_die_(ctx
, node
) == A_SUCCESS
)
269 a_list_del_node(node
);
277 void *a_que_pull_back(a_que
*ctx
)
279 if (ctx
->head_
.prev
!= &ctx
->head_
)
281 a_list
*const node
= (a_list
*)ctx
->head_
.prev
;
282 if (a_que_die_(ctx
, node
) == A_SUCCESS
)
284 a_list_del_node(node
);
292 void *a_que_insert(a_que
*ctx
, a_size idx
)
297 a_list
*const node
= a_que_new_(ctx
);
300 a_list_foreach_next(it
, &ctx
->head_
)
304 a_list_add_prev(it
, node
);
312 return a_que_push_back(ctx
);
315 void *a_que_remove(a_que
*ctx
, a_size idx
)
320 a_list
*node
= A_NULL
;
321 a_list_foreach_next(it
, &ctx
->head_
)
329 if (a_que_die_(ctx
, node
) == A_SUCCESS
)
331 a_list_del_node(node
);
337 return a_que_pull_back(ctx
);