liba 0.1.15
An algorithm library based on C/C++
Loading...
Searching...
No Matches
vec.h
Go to the documentation of this file.
1
6#ifndef LIBA_VEC_H
7#define LIBA_VEC_H
8
9#include "a.h"
10
27
32A_INTERN void *a_vec_ptr(a_vec const *ctx) { return ctx->ptr_; }
33#define A_VEC_PTR(T, ctx) a_cast_s(T *, a_vec_ptr(ctx))
34
39A_INTERN a_size a_vec_siz(a_vec const *ctx) { return ctx->siz_; }
40
45A_INTERN a_size a_vec_num(a_vec const *ctx) { return ctx->num_; }
46
51A_INTERN a_size a_vec_mem(a_vec const *ctx) { return ctx->mem_; }
52
60A_INTERN void *a_vec_at_(a_vec const *ctx, a_size idx)
61{
62 return a_byte_(*, ctx->ptr_) + ctx->siz_ * idx;
63}
64#define A_VEC_AT_(T, ctx, idx) a_cast_s(T *, a_vec_at_(ctx, idx))
65
73A_INTERN void *a_vec_at(a_vec const *ctx, a_size idx)
74{
75 return idx < ctx->mem_ ? a_vec_at_(ctx, idx) : A_NULL;
76}
77#define A_VEC_AT(T, ctx, idx) a_cast_s(T *, a_vec_at(ctx, idx))
78
86A_INTERN void *a_vec_idx(a_vec 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_vec_at_(ctx, num) : A_NULL;
90}
91#define A_VEC_IDX(T, ctx, idx) a_cast_s(T *, a_vec_idx(ctx, idx))
92
99A_INTERN void *a_vec_top_(a_vec const *ctx)
100{
101 return a_byte_(*, ctx->ptr_) + ctx->siz_ * (ctx->num_ - 1);
102}
103#define A_VEC_TOP_(T, ctx) a_cast_s(T *, a_vec_top_(ctx))
104
111A_INTERN void *a_vec_top(a_vec const *ctx)
112{
113 return ctx->num_ ? a_vec_top_(ctx) : A_NULL;
114}
115#define A_VEC_TOP(T, ctx) a_cast_s(T *, a_vec_top(ctx))
116
122A_INTERN void *a_vec_end_(a_vec const *ctx)
123{
124 return a_byte_(*, ctx->ptr_) + ctx->siz_ * ctx->num_;
125}
126#define A_VEC_END_(T, ctx) a_cast_s(T *, a_vec_end_(ctx))
127
134A_INTERN void *a_vec_end(a_vec const *ctx)
135{
136 return ctx->ptr_ ? a_vec_end_(ctx) : ctx->ptr_;
137}
138#define A_VEC_END(T, ctx) a_cast_s(T *, a_vec_end(ctx))
139
140#if defined(__cplusplus)
141extern "C" {
142#endif /* __cplusplus */
143
148A_EXTERN a_vec *a_vec_new(a_size size);
149
155A_EXTERN void a_vec_die(a_vec *ctx, void (*dtor)(void *));
156
162A_EXTERN void a_vec_ctor(a_vec *ctx, a_size size);
163
169A_EXTERN void a_vec_dtor(a_vec *ctx, void (*dtor)(void *));
170
181A_EXTERN int a_vec_copy(a_vec *ctx, a_vec const *obj, int (*dup)(void *, void const *));
182
188A_EXTERN void a_vec_move(a_vec *ctx, a_vec *obj);
189
196A_EXTERN void a_vec_edit(a_vec *ctx, a_size size, void (*dtor)(void *));
197
207A_EXTERN int a_vec_make(a_vec *ctx, a_size num, void (*dtor)(void *));
208
214A_EXTERN void a_vec_drop(a_vec *ctx, void (*dtor)(void *));
215
222A_EXTERN void a_vec_swap(a_vec const *ctx, a_size lhs, a_size rhs);
223
232A_EXTERN void a_vec_sort(a_vec const *ctx, int (*cmp)(void const *, void const *));
233
251A_EXTERN void a_vec_sort_fore(a_vec const *ctx, int (*cmp)(void const *, void const *));
252
270A_EXTERN void a_vec_sort_back(a_vec const *ctx, int (*cmp)(void const *, void const *));
271
283A_EXTERN void *a_vec_push_sort(a_vec *ctx, void const *key, int (*cmp)(void const *, void const *));
284#define A_VEC_PUSH_SORT(T, ctx, key, cmp) a_cast_s(T *, a_vec_push_sort(ctx, key, cmp))
285
297A_EXTERN void *a_vec_search(a_vec const *ctx, void const *obj, int (*cmp)(void const *, void const *));
298#define A_VEC_SEARCH(T, ctx, obj, cmp) a_cast_s(T *, a_vec_search(ctx, obj, cmp))
299
309A_EXTERN void *a_vec_insert(a_vec *ctx, a_size idx);
310#define A_VEC_INSERT(T, ctx, idx) a_cast_s(T *, a_vec_insert(ctx, idx))
311
321A_EXTERN void *a_vec_remove(a_vec *ctx, a_size idx);
322#define A_VEC_REMOVE(T, ctx, idx) a_cast_s(T *, a_vec_remove(ctx, idx))
323
330A_EXTERN void *a_vec_push_fore(a_vec *ctx);
331#define A_VEC_PUSH_FORE(T, ctx) a_cast_s(T *, a_vec_push_fore(ctx))
332
339A_EXTERN void *a_vec_push_back(a_vec *ctx);
340#define A_VEC_PUSH_BACK(T, ctx) a_cast_s(T *, a_vec_push_back(ctx))
341
348A_EXTERN void *a_vec_pull_fore(a_vec *ctx);
349#define A_VEC_PULL_FORE(T, ctx) a_cast_s(T *, a_vec_pull_fore(ctx))
350
357A_EXTERN void *a_vec_pull_back(a_vec *ctx);
358#define A_VEC_PULL_BACK(T, ctx) a_cast_s(T *, a_vec_pull_back(ctx))
359
360#if defined(__cplusplus)
361} /* extern "C" */
362#endif /* __cplusplus */
363
370A_INTERN void *a_vec_push(a_vec *ctx) { return a_vec_push_back(ctx); }
371#define A_VEC_PUSH(T, ctx) a_cast_s(T *, a_vec_push(ctx))
372
379A_INTERN void *a_vec_pull(a_vec *ctx) { return a_vec_pull_back(ctx); }
380#define A_VEC_PULL(T, ctx) a_cast_s(T *, a_vec_pull(ctx))
381
394#define a_vec_forenum(i, ctx) a_forenum(a_size, i, (ctx)->num_)
395
408#define a_vec_forenum_reverse(i, ctx) a_forenum_reverse(a_size, i, (ctx)->num_)
409
423#define a_vec_foreach(T, P, it, ctx) a_forsafe(T, P, it, (ctx)->ptr_, (ctx)->num_)
424
438#define a_vec_foreach_reverse(T, P, it, ctx) a_forsafe_reverse(T, P, it, (ctx)->ptr_, (ctx)->num_)
439
442#endif /* a/vec.h */
algorithm library
void * a_vec_push_fore(a_vec *ctx)
push an element into the vector forward
struct a_vec a_vec
instance structure for basic vector
void * a_vec_end_(a_vec const *ctx)
access end pointer for a pointer to vector structure
Definition vec.h:122
void * a_vec_idx(a_vec const *ctx, ptrdiff_t idx)
access specified element for a pointer to vector structure
Definition vec.h:86
void * a_vec_pull(a_vec *ctx)
pull an element from the vector
Definition vec.h:379
void a_vec_die(a_vec *ctx, void(*dtor)(void *))
deallocate a pointer to vector structure
void a_vec_ctor(a_vec *ctx, size_t size)
constructor for vector structure
void a_vec_edit(a_vec *ctx, size_t size, void(*dtor)(void *))
edit size of a element for a pointer to vector structure
void a_vec_move(a_vec *ctx, a_vec *obj)
initialize a pointer to vector structure by moving
int a_vec_copy(a_vec *ctx, a_vec const *obj, int(*dup)(void *, void const *))
initialize a pointer to vector structure by copying
int a_vec_make(a_vec *ctx, size_t num, void(*dtor)(void *))
modify element number for a pointer to string structure
void * a_vec_push(a_vec *ctx)
push an element into the vector
Definition vec.h:370
void * a_vec_push_back(a_vec *ctx)
push an element into the vector backward
void a_vec_drop(a_vec *ctx, void(*dtor)(void *))
drop all the elements for a pointer to vector structure
void * a_vec_search(a_vec const *ctx, void const *obj, int(*cmp)(void const *, void const *))
search the given element in this vector
void * a_vec_push_sort(a_vec *ctx, void const *key, int(*cmp)(void const *, void const *))
push an element into the vector and sort it
void * a_vec_remove(a_vec *ctx, size_t idx)
remove an element from the vector
void a_vec_sort_back(a_vec const *ctx, int(*cmp)(void const *, void const *))
insert sort backmost element for a pointer to vector structure
void * a_vec_top_(a_vec const *ctx)
access top element for a pointer to vector structure
Definition vec.h:99
a_vec * a_vec_new(size_t size)
allocate a pointer to vector structure from memory
void a_vec_sort_fore(a_vec const *ctx, int(*cmp)(void const *, void const *))
insert sort foremost element for a pointer to vector structure
void * a_vec_at(a_vec const *ctx, size_t idx)
access specified element for a pointer to vector structure
Definition vec.h:73
void * a_vec_insert(a_vec *ctx, size_t idx)
insert an element into the vector
void * a_vec_top(a_vec const *ctx)
access top element for a pointer to vector structure
Definition vec.h:111
size_t a_vec_siz(a_vec const *ctx)
access size of a element for a pointer to vector structure
Definition vec.h:39
size_t a_vec_mem(a_vec const *ctx)
access memory of element for a pointer to vector structure
Definition vec.h:51
void * a_vec_at_(a_vec const *ctx, size_t idx)
access specified element for a pointer to vector structure
Definition vec.h:60
void * a_vec_ptr(a_vec const *ctx)
access address of vector for a pointer to vector structure
Definition vec.h:32
void a_vec_swap(a_vec const *ctx, size_t lhs, size_t rhs)
swap elements lhs and rhs for a pointer to vector structure
void a_vec_dtor(a_vec *ctx, void(*dtor)(void *))
destructor for vector structure
size_t a_vec_num(a_vec const *ctx)
access number of element for a pointer to vector structure
Definition vec.h:45
void * a_vec_pull_back(a_vec *ctx)
pull an element from the vector backward
void * a_vec_pull_fore(a_vec *ctx)
pull an element from the vector forward
void * a_vec_end(a_vec const *ctx)
access end pointer for a pointer to vector structure
Definition vec.h:134
void a_vec_sort(a_vec const *ctx, int(*cmp)(void const *, void const *))
sort all elements for a pointer to vector structure
#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 vector
Definition vec.h:21
size_t num_
Definition vec.h:24
void * ptr_
Definition vec.h:22
size_t siz_
Definition vec.h:23
size_t mem_
Definition vec.h:25