liba 0.1.15
An algorithm library based on C/C++
 
All Data Structures Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
Loading...
Searching...
No Matches
buf.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_BUF_H
7#define LIBA_BUF_H
8
9#include "a.h"
10
16
17#define A_BUF_DEF a_size num_, mem_, siz_
21typedef struct a_buf
22{
23 A_BUF_DEF;
25
32#define a_buf_(_, x) a_cast_s(a_buf _, a_cast_s(void _, x))
33
38A_INTERN a_size a_buf_num(void const *ctx) { return a_buf_(const *, ctx)->num_; }
39
44A_INTERN a_size a_buf_mem(void const *ctx) { return a_buf_(const *, ctx)->mem_; }
45
50A_INTERN a_size a_buf_siz(void const *ctx) { return a_buf_(const *, ctx)->siz_; }
51
56A_INTERN void *a_buf_ptr(void const *ctx)
57{
58 return a_cast_r(a_buf *, a_cast_r(a_uptr, ctx)) + 1;
59}
60#define A_BUF_PTR(T, ctx) a_cast_s(T *, a_buf_ptr(ctx))
61
69A_INTERN void *a_buf_at_(void const *ctx, a_size idx)
70{
71 return a_byte_(*, a_buf_ptr(ctx)) + a_buf_siz(ctx) * idx;
72}
73#define A_BUF_AT_(T, ctx, idx) a_cast_s(T *, a_buf_at_(ctx, idx))
74
82A_INTERN void *a_buf_at(void const *ctx, a_size idx)
83{
84 return idx < a_buf_mem(ctx) ? a_buf_at_(ctx, idx) : A_NULL;
85}
86#define A_BUF_AT(T, ctx, idx) a_cast_s(T *, a_buf_at(ctx, idx))
87
95A_INTERN void *a_buf_of(void const *ctx, a_diff idx)
96{
97 a_size const num = idx >= 0 ? a_size_c(idx) : a_size_c(idx) + a_buf_num(ctx);
98 return num < a_buf_mem(ctx) ? a_buf_at_(ctx, num) : A_NULL;
99}
100#define A_BUF_OF(T, ctx, idx) a_cast_s(T *, a_buf_of(ctx, idx))
101
108A_INTERN void *a_buf_top_(void const *ctx)
109{
110 return a_byte_(*, a_buf_ptr(ctx)) + a_buf_siz(ctx) * (a_buf_num(ctx) - 1);
111}
112#define A_BUF_TOP_(T, ctx) a_cast_s(T *, a_buf_top_(ctx))
113
120A_INTERN void *a_buf_top(void const *ctx)
121{
122 return a_buf_num(ctx) ? a_buf_top_(ctx) : A_NULL;
123}
124#define A_BUF_TOP(T, ctx) a_cast_s(T *, a_buf_top(ctx))
125
131A_INTERN void *a_buf_end(void const *ctx)
132{
133 return a_byte_(*, a_buf_ptr(ctx)) + a_buf_siz(ctx) * a_buf_num(ctx);
134}
135#define A_BUF_END(T, ctx) a_cast_s(T *, a_buf_end(ctx))
136
137#if defined(__cplusplus)
138extern "C" {
139#endif /* __cplusplus */
140
146A_EXTERN a_buf *a_buf_new(a_size siz, a_size num);
147
153A_EXTERN void a_buf_die(a_buf *ctx, void (*dtor)(void *));
154
161A_EXTERN void a_buf_ctor(void *ctx, a_size siz, a_size num);
162
168A_EXTERN void a_buf_dtor(void *ctx, void (*dtor)(void *));
169
177A_EXTERN a_buf *a_buf_setm(a_buf *ctx, a_size mem);
178
185A_EXTERN void a_buf_setn(void *ctx, a_size num, void (*dtor)(void *));
186
193A_EXTERN void a_buf_setz(void *ctx, a_size siz, void (*dtor)(void *));
194
203A_EXTERN void a_buf_sort(void const *ctx, int (*cmp)(void const *, void const *));
204
222A_EXTERN void a_buf_sort_fore(void const *ctx, int (*cmp)(void const *, void const *));
223
241A_EXTERN void a_buf_sort_back(void const *ctx, int (*cmp)(void const *, void const *));
242
254A_EXTERN void *a_buf_push_sort(void *ctx, void const *key, int (*cmp)(void const *, void const *));
255#define A_BUF_PUSH_SORT(T, ctx, key, cmp) a_cast_s(T *, a_buf_push_sort(ctx, key, cmp))
256
268A_EXTERN void *a_buf_search(void const *ctx, void const *obj, int (*cmp)(void const *, void const *));
269#define A_BUF_SEARCH(T, ctx, obj, cmp) a_cast_s(T *, a_buf_search(ctx, obj, cmp))
270
280A_EXTERN void *a_buf_insert(void *ctx, a_size idx);
281#define A_BUF_INSERT(T, ctx, idx) a_cast_s(T *, a_buf_insert(ctx, idx))
282
292A_EXTERN void *a_buf_remove(void *ctx, a_size idx);
293#define A_BUF_REMOVE(T, ctx, idx) a_cast_s(T *, a_buf_remove(ctx, idx))
294
301A_EXTERN void *a_buf_push_fore(void *ctx);
302#define A_BUF_PUSH_FORE(T, ctx) a_cast_s(T *, a_buf_push_fore(ctx))
303
310A_EXTERN void *a_buf_push_back(void *ctx);
311#define A_BUF_PUSH_BACK(T, ctx) a_cast_s(T *, a_buf_push_back(ctx))
312
319A_EXTERN void *a_buf_pull_fore(void *ctx);
320#define A_BUF_PULL_FORE(T, ctx) a_cast_s(T *, a_buf_pull_fore(ctx))
321
328A_EXTERN void *a_buf_pull_back(void *ctx);
329#define A_BUF_PULL_BACK(T, ctx) a_cast_s(T *, a_buf_pull_back(ctx))
330
342A_EXTERN int a_buf_store(void *ctx, a_size idx, void *ptr, a_size num, int (*copy)(void *, void const *));
343
354A_EXTERN int a_buf_erase(void *ctx, a_size idx, a_size num, void (*dtor)(void *));
355
356#if defined(__cplusplus)
357} /* extern "C" */
358#endif /* __cplusplus */
359
366A_INTERN void *a_buf_push(void *ctx) { return a_buf_push_back(ctx); }
367#define A_BUF_PUSH(T, ctx) a_cast_s(T *, a_buf_push(ctx))
368
375A_INTERN void *a_buf_pull(void *ctx) { return a_buf_pull_back(ctx); }
376#define A_BUF_PULL(T, ctx) a_cast_s(T *, a_buf_pull(ctx))
377
390#define a_buf_forenum(i, ctx) a_forenum(a_size, i, a_buf_num(ctx))
391#define A_BUF_FORENUM(I, i, ctx) A_FORENUM(I, i, a_buf_num(ctx))
392
405#define a_buf_forenum_reverse(i, ctx) a_forenum_reverse(a_size, i, a_buf_num(ctx))
406#define A_BUF_FORENUM_REVERSE(I, i, ctx) A_FORENUM_REVERSE(I, i, a_buf_num(ctx))
407
421#define a_buf_foreach(T, S, it, ctx) a_foreach(T, S, it, a_buf_ptr(ctx), a_buf_num(ctx))
422#define A_BUF_FOREACH(T, it, at, ctx) A_FOREACH(T, it, at, a_buf_ptr(ctx), a_buf_num(ctx))
423
437#define a_buf_foreach_reverse(T, S, it, ctx) a_foreach_reverse(T, S, it, a_buf_ptr(ctx), a_buf_num(ctx))
438#define A_BUF_FOREACH_REVERSE(T, it, at, ctx) A_FOREACH_REVERSE(T, it, at, a_buf_ptr(ctx), a_buf_num(ctx))
439
441
442#endif /* a/buf.h */
algorithm library
void * a_buf_top_(void const *ctx)
access top element for a pointer to buffer structure
Definition buf.h:108
void * a_buf_insert(void *ctx, a_size idx)
insert an element into the buffer
a_size a_buf_num(void const *ctx)
access number of element for a pointer to buffer structure
Definition buf.h:38
void * a_buf_at(void const *ctx, a_size idx)
access specified element for a pointer to buffer structure
Definition buf.h:82
void * a_buf_ptr(void const *ctx)
access address of buffer for a pointer to buffer structure
Definition buf.h:56
#define a_buf_(_, x)
cast a buffer pointer from another type pointer
Definition buf.h:32
void a_buf_sort(void const *ctx, int(*cmp)(void const *, void const *))
sort all elements for a pointer to buffer structure
void a_buf_dtor(void *ctx, void(*dtor)(void *))
destructor for buffer structure
void * a_buf_search(void const *ctx, void const *obj, int(*cmp)(void const *, void const *))
search the given element in this buffer
void a_buf_setz(void *ctx, a_size siz, void(*dtor)(void *))
set size of a element for a pointer to buffer structure
void * a_buf_push(void *ctx)
push an element into the buffer
Definition buf.h:366
void * a_buf_push_sort(void *ctx, void const *key, int(*cmp)(void const *, void const *))
push an element into the buffer and sort it
void * a_buf_remove(void *ctx, a_size idx)
remove an element from the buffer
void * a_buf_pull_back(void *ctx)
pull an element from the buffer backward
void * a_buf_of(void const *ctx, a_diff idx)
access specified element for a pointer to buffer structure
Definition buf.h:95
void * a_buf_pull_fore(void *ctx)
pull an element from the buffer forward
a_buf * a_buf_setm(a_buf *ctx, a_size mem)
set memory of element for a pointer to buffer structure
int a_buf_store(void *ctx, a_size idx, void *ptr, a_size num, int(*copy)(void *, void const *))
store elements into the buffer
a_size a_buf_siz(void const *ctx)
access size of a element for a pointer to buffer structure
Definition buf.h:50
void a_buf_setn(void *ctx, a_size num, void(*dtor)(void *))
set number of element for a pointer to buffer structure
void * a_buf_pull(void *ctx)
pull an element from the buffer
Definition buf.h:375
void a_buf_sort_back(void const *ctx, int(*cmp)(void const *, void const *))
insert sort backmost element for a pointer to buffer structure
void * a_buf_push_back(void *ctx)
push an element into the buffer backward
void a_buf_sort_fore(void const *ctx, int(*cmp)(void const *, void const *))
insert sort foremost element for a pointer to buffer structure
void * a_buf_end(void const *ctx)
access end pointer for a pointer to buffer structure
Definition buf.h:131
void * a_buf_push_fore(void *ctx)
push an element into the buffer forward
void * a_buf_at_(void const *ctx, a_size idx)
access specified element for a pointer to buffer structure
Definition buf.h:69
int a_buf_erase(void *ctx, a_size idx, a_size num, void(*dtor)(void *))
erase elements from the buffer
void * a_buf_top(void const *ctx)
access top element for a pointer to buffer structure
Definition buf.h:120
a_size a_buf_mem(void const *ctx)
access memory of element for a pointer to buffer structure
Definition buf.h:44
void a_buf_ctor(void *ctx, a_size siz, a_size num)
constructor for buffer structure
a_buf * a_buf_new(a_size siz, a_size num)
allocate a pointer to buffer structure from memory
void a_buf_die(a_buf *ctx, void(*dtor)(void *))
deallocate a pointer to buffer structure
ptrdiff_t a_diff
signed integer type returned when subtracting two pointers
Definition a.h:780
unsigned long a_uptr
unsigned integer type capable of holding a pointer to void
Definition a.h:738
#define a_size_c(x)
static cast to a_size
Definition a.h:820
size_t a_size
unsigned integer type returned by the sizeof operator
Definition a.h:823
instance structure for basic buffer
Definition buf.h:22