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
vec.h
Go to the documentation of this file.
1
5
6#ifndef LIBA_VEC_H
7#define LIBA_VEC_H
8
9#include "a.h"
10
16
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_of(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_OF(T, ctx, idx) a_cast_s(T *, a_vec_of(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
176A_EXTERN void a_vec_swap(a_vec *lhs, a_vec *rhs);
177
185A_EXTERN int a_vec_setm(a_vec *ctx, a_size mem);
186
195A_EXTERN int a_vec_setn(a_vec *ctx, a_size num, void (*dtor)(void *));
196
203A_EXTERN void a_vec_setz(a_vec *ctx, a_size siz, void (*dtor)(void *));
204
213A_EXTERN void a_vec_sort(a_vec const *ctx, int (*cmp)(void const *, void const *));
214
232A_EXTERN void a_vec_sort_fore(a_vec const *ctx, int (*cmp)(void const *, void const *));
233
251A_EXTERN void a_vec_sort_back(a_vec const *ctx, int (*cmp)(void const *, void const *));
252
264A_EXTERN void *a_vec_push_sort(a_vec *ctx, void const *key, int (*cmp)(void const *, void const *));
265#define A_VEC_PUSH_SORT(T, ctx, key, cmp) a_cast_s(T *, a_vec_push_sort(ctx, key, cmp))
266
278A_EXTERN void *a_vec_search(a_vec const *ctx, void const *obj, int (*cmp)(void const *, void const *));
279#define A_VEC_SEARCH(T, ctx, obj, cmp) a_cast_s(T *, a_vec_search(ctx, obj, cmp))
280
290A_EXTERN void *a_vec_insert(a_vec *ctx, a_size idx);
291#define A_VEC_INSERT(T, ctx, idx) a_cast_s(T *, a_vec_insert(ctx, idx))
292
302A_EXTERN void *a_vec_remove(a_vec *ctx, a_size idx);
303#define A_VEC_REMOVE(T, ctx, idx) a_cast_s(T *, a_vec_remove(ctx, idx))
304
311A_EXTERN void *a_vec_push_fore(a_vec *ctx);
312#define A_VEC_PUSH_FORE(T, ctx) a_cast_s(T *, a_vec_push_fore(ctx))
313
320A_EXTERN void *a_vec_push_back(a_vec *ctx);
321#define A_VEC_PUSH_BACK(T, ctx) a_cast_s(T *, a_vec_push_back(ctx))
322
329A_EXTERN void *a_vec_pull_fore(a_vec *ctx);
330#define A_VEC_PULL_FORE(T, ctx) a_cast_s(T *, a_vec_pull_fore(ctx))
331
338A_EXTERN void *a_vec_pull_back(a_vec *ctx);
339#define A_VEC_PULL_BACK(T, ctx) a_cast_s(T *, a_vec_pull_back(ctx))
340
352A_EXTERN int a_vec_store(a_vec *ctx, a_size idx, void *ptr, a_size num, int (*copy)(void *, void const *));
353
364A_EXTERN int a_vec_erase(a_vec *ctx, a_size idx, a_size num, void (*dtor)(void *));
365
366#if defined(__cplusplus)
367} /* extern "C" */
368#endif /* __cplusplus */
369
376A_INTERN void *a_vec_push(a_vec *ctx) { return a_vec_push_back(ctx); }
377#define A_VEC_PUSH(T, ctx) a_cast_s(T *, a_vec_push(ctx))
378
385A_INTERN void *a_vec_pull(a_vec *ctx) { return a_vec_pull_back(ctx); }
386#define A_VEC_PULL(T, ctx) a_cast_s(T *, a_vec_pull(ctx))
387
400#define a_vec_forenum(i, ctx) a_forenum(a_size, i, (ctx)->num_)
401#define A_VEC_FORENUM(I, i, ctx) A_FORENUM(I, i, (ctx)->num_)
402
415#define a_vec_forenum_reverse(i, ctx) a_forenum_reverse(a_size, i, (ctx)->num_)
416#define A_VEC_FORENUM_REVERSE(I, i, ctx) A_FORENUM_REVERSE(I, i, (ctx)->num_)
417
431#define a_vec_foreach(T, S, it, ctx) a_forsafe(T, S, it, (ctx)->ptr_, (ctx)->num_)
432#define A_VEC_FOREACH(T, it, at, ctx) A_FORSAFE(T, it, at, (ctx)->ptr_, (ctx)->num_)
433
447#define a_vec_foreach_reverse(T, S, it, ctx) a_forsafe_reverse(T, S, it, (ctx)->ptr_, (ctx)->num_)
448#define A_VEC_FOREACH_REVERSE(T, it, at, ctx) A_FORSAFE_REVERSE(T, it, at, (ctx)->ptr_, (ctx)->num_)
449
451
452#endif /* a/vec.h */
algorithm library
int a_vec_setn(a_vec *ctx, a_size num, void(*dtor)(void *))
set number of element for a pointer to vector structure
void * a_vec_push_fore(a_vec *ctx)
push an element into the vector forward
a_vec * a_vec_new(a_size size)
allocate a pointer to vector structure from memory
void * a_vec_end_(a_vec const *ctx)
access end pointer for a pointer to vector structure
Definition vec.h:122
void * a_vec_pull(a_vec *ctx)
pull an element from the vector
Definition vec.h:385
void a_vec_die(a_vec *ctx, void(*dtor)(void *))
deallocate a pointer to vector structure
void a_vec_swap(a_vec *lhs, a_vec *rhs)
swap the contents of two pointers to vector structure
int a_vec_erase(a_vec *ctx, a_size idx, a_size num, void(*dtor)(void *))
erase elements from the vector
void * a_vec_at_(a_vec const *ctx, a_size idx)
access specified element for a pointer to vector structure
Definition vec.h:60
void * a_vec_push(a_vec *ctx)
push an element into the vector
Definition vec.h:376
void * a_vec_push_back(a_vec *ctx)
push an element into the vector backward
int a_vec_setm(a_vec *ctx, a_size mem)
set memory of element 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_setz(a_vec *ctx, a_size siz, void(*dtor)(void *))
set size of a element for a pointer to vector structure
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_at(a_vec const *ctx, a_size idx)
access specified element for a pointer to vector structure
Definition vec.h:73
void * a_vec_remove(a_vec *ctx, a_size idx)
remove an element from the vector
a_size a_vec_num(a_vec const *ctx)
access number of element for a pointer to vector structure
Definition vec.h:45
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
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_top(a_vec const *ctx)
access top element for a pointer to vector structure
Definition vec.h:111
void a_vec_ctor(a_vec *ctx, a_size size)
constructor for vector structure
void * a_vec_ptr(a_vec const *ctx)
access address of vector for a pointer to vector structure
Definition vec.h:32
a_size a_vec_mem(a_vec const *ctx)
access memory of element for a pointer to vector structure
Definition vec.h:51
int a_vec_store(a_vec *ctx, a_size idx, void *ptr, a_size num, int(*copy)(void *, void const *))
store elements into the vector
void a_vec_dtor(a_vec *ctx, void(*dtor)(void *))
destructor for vector structure
void * a_vec_insert(a_vec *ctx, a_size idx)
insert an element into the vector
void * a_vec_pull_back(a_vec *ctx)
pull an element from the vector backward
a_size a_vec_siz(a_vec const *ctx)
access size of a element for a pointer to vector structure
Definition vec.h:39
void * a_vec_of(a_vec const *ctx, a_diff idx)
access specified element for a pointer to vector structure
Definition vec.h:86
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
ptrdiff_t a_diff
signed integer type returned when subtracting two pointers
Definition a.h:780
#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 vector
Definition vec.h:21
a_size mem_
Definition vec.h:25
a_size num_
Definition vec.h:24
a_size siz_
Definition vec.h:23
void * ptr_
Definition vec.h:22