liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
buf.h
Go to the documentation of this file.
1
6#ifndef LIBA_BUF_H
7#define LIBA_BUF_H
8
9#include "a.h"
10
27
32A_INTERN void *a_buf_ptr(a_buf const *ctx) { return ctx->ptr_; }
33#define A_BUF_PTR(T, ctx) a_cast_s(T *, a_buf_ptr(ctx))
34
39A_INTERN a_size a_buf_siz(a_buf const *ctx) { return ctx->siz_; }
40
45A_INTERN a_size a_buf_num(a_buf const *ctx) { return ctx->num_; }
46
51A_INTERN a_size a_buf_mem(a_buf const *ctx) { return ctx->mem_; }
52
60A_INTERN void *a_buf_at_(a_buf const *ctx, a_size idx)
61{
62 return a_byte_(*, ctx->ptr_) + ctx->siz_ * idx;
63}
64#define A_BUF_AT_(T, ctx, idx) a_cast_s(T *, a_buf_at_(ctx, idx))
65
73A_INTERN void *a_buf_at(a_buf const *ctx, a_size idx)
74{
75 return idx < ctx->mem_ ? a_buf_at_(ctx, idx) : A_NULL;
76}
77#define A_BUF_AT(T, ctx, idx) a_cast_s(T *, a_buf_at(ctx, idx))
78
86A_INTERN void *a_buf_idx(a_buf const *ctx, a_diff idx)
87{
88 a_size const num = idx >= 0 ? a_size_c(idx) : a_size_c(idx) + ctx->num_;
89 return num < ctx->mem_ ? a_buf_at_(ctx, num) : A_NULL;
90}
91#define A_BUF_IDX(T, ctx, idx) a_cast_s(T *, a_buf_idx(ctx, idx))
92
99A_INTERN void *a_buf_top_(a_buf const *ctx)
100{
101 return a_byte_(*, ctx->ptr_) + ctx->siz_ * (ctx->num_ - 1);
102}
103#define A_BUF_TOP_(T, ctx) a_cast_s(T *, a_buf_top_(ctx))
104
111A_INTERN void *a_buf_top(a_buf const *ctx)
112{
113 return ctx->num_ ? a_buf_top_(ctx) : A_NULL;
114}
115#define A_BUF_TOP(T, ctx) a_cast_s(T *, a_buf_top(ctx))
116
122A_INTERN void *a_buf_end(a_buf const *ctx)
123{
124 return a_byte_(*, ctx->ptr_) + ctx->siz_ * ctx->num_;
125}
126#define A_BUF_END(T, ctx) a_cast_s(T *, a_buf_end(ctx))
127
128#if defined(__cplusplus)
129extern "C" {
130#endif /* __cplusplus */
131
139A_EXTERN void a_buf_ctor(a_buf *ctx, void *ptr, a_size siz, a_size num);
140
146A_EXTERN void a_buf_dtor(a_buf *ctx, void (*dtor)(void *));
147
153A_EXTERN void a_buf_move(a_buf *ctx, a_buf *obj);
154
160A_EXTERN void a_buf_drop(a_buf *ctx, void (*dtor)(void *));
161
168A_EXTERN void a_buf_swap(a_buf const *ctx, a_size lhs, a_size rhs);
169
178A_EXTERN void a_buf_sort(a_buf const *ctx, int (*cmp)(void const *, void const *));
179
197A_EXTERN void a_buf_sort_fore(a_buf const *ctx, int (*cmp)(void const *, void const *));
198
216A_EXTERN void a_buf_sort_back(a_buf const *ctx, int (*cmp)(void const *, void const *));
217
229A_EXTERN void *a_buf_push_sort(a_buf *ctx, void const *key, int (*cmp)(void const *, void const *));
230#define A_BUF_PUSH_SORT(T, ctx, key, cmp) a_cast_s(T *, a_buf_push_sort(ctx, key, cmp))
231
243A_EXTERN void *a_buf_search(a_buf const *ctx, void const *obj, int (*cmp)(void const *, void const *));
244#define A_BUF_SEARCH(T, ctx, obj, cmp) a_cast_s(T *, a_buf_search(ctx, obj, cmp))
245
255A_EXTERN void *a_buf_insert(a_buf *ctx, a_size idx);
256#define A_BUF_INSERT(T, ctx, idx) a_cast_s(T *, a_buf_insert(ctx, idx))
257
267A_EXTERN void *a_buf_remove(a_buf *ctx, a_size idx);
268#define A_BUF_REMOVE(T, ctx, idx) a_cast_s(T *, a_buf_remove(ctx, idx))
269
276A_EXTERN void *a_buf_push_fore(a_buf *ctx);
277#define A_BUF_PUSH_FORE(T, ctx) a_cast_s(T *, a_buf_push_fore(ctx))
278
285A_EXTERN void *a_buf_push_back(a_buf *ctx);
286#define A_BUF_PUSH_BACK(T, ctx) a_cast_s(T *, a_buf_push_back(ctx))
287
294A_EXTERN void *a_buf_pull_fore(a_buf *ctx);
295#define A_BUF_PULL_FORE(T, ctx) a_cast_s(T *, a_buf_pull_fore(ctx))
296
303A_EXTERN void *a_buf_pull_back(a_buf *ctx);
304#define A_BUF_PULL_BACK(T, ctx) a_cast_s(T *, a_buf_pull_back(ctx))
305
306#if defined(__cplusplus)
307} /* extern "C" */
308#endif /* __cplusplus */
309
316A_INTERN void *a_buf_push(a_buf *ctx) { return a_buf_push_back(ctx); }
317#define A_BUF_PUSH(T, ctx) a_cast_s(T *, a_buf_push(ctx))
318
325A_INTERN void *a_buf_pull(a_buf *ctx) { return a_buf_pull_back(ctx); }
326#define A_BUF_PULL(T, ctx) a_cast_s(T *, a_buf_pull(ctx))
327
340#define a_buf_forenum(i, ctx) a_forenum(a_size, i, (ctx)->num_)
341
354#define a_buf_forenum_reverse(i, ctx) a_forenum_reverse(a_size, i, (ctx)->num_)
355
369#define a_buf_foreach(T, P, it, ctx) a_foreach(T, P, it, (ctx)->ptr_, (ctx)->num_)
370
384#define a_buf_foreach_reverse(T, P, it, ctx) a_foreach_reverse(T, P, it, (ctx)->ptr_, (ctx)->num_)
385
388#endif /* a/buf.h */
algorithm library
void * a_buf_at_(a_buf const *ctx, size_t idx)
access specified element for a pointer to buffer structure
Definition buf.h:60
void * a_buf_pull_back(a_buf *ctx)
pull an element from the buffer backward
void a_buf_move(a_buf *ctx, a_buf *obj)
initialize a pointer to buffer structure by moving
void a_buf_sort_fore(a_buf const *ctx, int(*cmp)(void const *, void const *))
insert sort foremost element for a pointer to buffer structure
void a_buf_sort(a_buf const *ctx, int(*cmp)(void const *, void const *))
sort all elements for a pointer to buffer structure
void * a_buf_insert(a_buf *ctx, size_t idx)
insert an element into the buffer
void * a_buf_at(a_buf const *ctx, size_t idx)
access specified element for a pointer to buffer structure
Definition buf.h:73
void a_buf_drop(a_buf *ctx, void(*dtor)(void *))
drop all the elements for a pointer to buffer structure
void a_buf_sort_back(a_buf const *ctx, int(*cmp)(void const *, void const *))
insert sort backmost element for a pointer to buffer structure
size_t a_buf_mem(a_buf const *ctx)
access memory of element for a pointer to buffer structure
Definition buf.h:51
void * a_buf_ptr(a_buf const *ctx)
access address of buffer for a pointer to buffer structure
Definition buf.h:32
void * a_buf_end(a_buf const *ctx)
access end pointer for a pointer to buffer structure
Definition buf.h:122
void a_buf_dtor(a_buf *ctx, void(*dtor)(void *))
destructor for buffer structure
void a_buf_ctor(a_buf *ctx, void *ptr, size_t siz, size_t num)
constructor for buffer structure
void * a_buf_push(a_buf *ctx)
push an element into the buffer
Definition buf.h:316
void a_buf_swap(a_buf const *ctx, size_t lhs, size_t rhs)
swap elements lhs and rhs for a pointer to buffer structure
void * a_buf_pull_fore(a_buf *ctx)
pull an element from the buffer forward
size_t a_buf_num(a_buf const *ctx)
access number of element for a pointer to buffer structure
Definition buf.h:45
void * a_buf_remove(a_buf *ctx, size_t idx)
remove an element from the buffer
void * a_buf_top(a_buf const *ctx)
access top element for a pointer to buffer structure
Definition buf.h:111
void * a_buf_idx(a_buf const *ctx, ptrdiff_t idx)
access specified element for a pointer to buffer structure
Definition buf.h:86
size_t a_buf_siz(a_buf const *ctx)
access size of a element for a pointer to buffer structure
Definition buf.h:39
void * a_buf_pull(a_buf *ctx)
pull an element from the buffer
Definition buf.h:325
void * a_buf_top_(a_buf const *ctx)
access top element for a pointer to buffer structure
Definition buf.h:99
void * a_buf_push_back(a_buf *ctx)
push an element into the buffer backward
void * a_buf_search(a_buf const *ctx, void const *obj, int(*cmp)(void const *, void const *))
search the given element in this buffer
void * a_buf_push_sort(a_buf *ctx, void const *key, int(*cmp)(void const *, void const *))
push an element into the buffer and sort it
void * a_buf_push_fore(a_buf *ctx)
push an element into the buffer forward
struct a_buf a_buf
instance structure for basic buffer
#define a_size
Definition a.h:610
#define a_diff
Definition a.h:598
#define a_size_c(x)
Definition a.h:607
instance structure for basic buffer
Definition buf.h:21
size_t siz_
Definition buf.h:23
void * ptr_
Definition buf.h:22
size_t num_
Definition buf.h:24
size_t mem_
Definition buf.h:25