vec.h
Go to the documentation of this file.
1 #pragma once
2 
11 
12 #include <stdint.h>
13 #include <stdlib.h>
14 #include <stdbool.h>
15 #include <unistd.h>
16 
17 #include <crater/container.h>
18 #include <crater/prand.h>
19 
23 typedef struct{
25  void *buf;
27  uint64_t len;
29  uint64_t cap;
30 } cr8r_vec;
31 
39 typedef struct cr8r_vec_ft cr8r_vec_ft;
40 struct cr8r_vec_ft{
46  uint64_t (*new_size)(cr8r_base_ft*, uint64_t cap);
53  void *(*resize)(cr8r_base_ft*, void *p, uint64_t cap);
57  void (*del)(cr8r_base_ft*, void *p);
60  void (*copy)(cr8r_base_ft*, void *dest, const void *src);
63  int (*cmp)(const cr8r_base_ft*, const void *a, const void *b);
66  void (*swap)(cr8r_base_ft*, void *a, void *b);
67 };
68 
72 typedef bool (*cr8r_vec_pred)(const cr8r_vec_ft*, const void *ent, void *data);
73 
77 typedef void (*cr8r_vec_mapper)(const cr8r_vec_ft *src_ft, const cr8r_vec_ft *dest_ft, void *o, const void *e, void *data);
78 
82 typedef void *(*cr8r_vec_accumulator)(const cr8r_vec_ft*, void *acc, const void *e);
83 
104  void *data, uint64_t size,
105  uint64_t (*new_size)(cr8r_base_ft*, uint64_t cap),
106  void *(*resize)(cr8r_base_ft*, void *p, uint64_t cap),
107  void (*del)(cr8r_base_ft*, void *p),
108  void (*copy)(cr8r_base_ft*, void *dest, const void *src),
109  int (*cmp)(const cr8r_base_ft*, const void *a, const void *b),
110  void (*swap)(cr8r_base_ft*, void *a, void *b)
111 );
112 
113 
118 bool cr8r_vec_init(cr8r_vec*, cr8r_vec_ft*, uint64_t cap);
119 
125 
126 
135 bool cr8r_vec_copy(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft*);
136 
147 bool cr8r_vec_sub(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft*, uint64_t a, uint64_t b);
148 
149 
155 bool cr8r_vec_resize(cr8r_vec*, cr8r_vec_ft*, uint64_t cap);
156 
162 
163 
168 
169 
177 
178 
185 void *cr8r_vec_get(cr8r_vec*, const cr8r_vec_ft*, uint64_t i);
186 
192 void *cr8r_vec_getx(cr8r_vec*, const cr8r_vec_ft*, int64_t i);
193 
199 
200 
201 
207 bool cr8r_vec_pushr(cr8r_vec*, cr8r_vec_ft*, const void *e);
208 
215 
221 bool cr8r_vec_pushl(cr8r_vec*, cr8r_vec_ft*, const void *e);
222 
229 
230 
238 
249 bool cr8r_vec_filtered(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft*, cr8r_vec_pred pred, void *data);
250 
265 bool cr8r_vec_map(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *src_ft, cr8r_vec_ft *dest_ft, cr8r_vec_mapper f, void *data);
266 
275 int cr8r_vec_forEachPermutation(cr8r_vec*, cr8r_vec_ft*, void (*f)(const cr8r_vec*, const cr8r_vec_ft*, void *data), void *data);
276 
277 
286 bool cr8r_vec_combine(cr8r_vec *dest, const cr8r_vec *src_a, const cr8r_vec *src_b, cr8r_vec_ft*);
287 
295 bool cr8r_vec_augment(cr8r_vec *self, const cr8r_vec *other, cr8r_vec_ft*);
296 
301 bool cr8r_vec_ensure_cap(cr8r_vec *self, cr8r_vec_ft *ft, uint64_t cap);
302 
309 bool cr8r_vec_all(const cr8r_vec*, const cr8r_vec_ft*, cr8r_vec_pred pred, void *data);
310 
319 bool cr8r_vec_any(const cr8r_vec*, const cr8r_vec_ft*, cr8r_vec_pred pred, void *data);
320 
327 bool cr8r_vec_contains(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
328 
336 int64_t cr8r_vec_index(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
337 
338 
343 void *cr8r_vec_exm(cr8r_vec*, const cr8r_vec_ft*, int ord);
344 
345 
361 void *cr8r_vec_foldr(const cr8r_vec*, const cr8r_vec_ft*, cr8r_vec_accumulator f, void *init);
362 
370 void *cr8r_vec_foldl(const cr8r_vec*, const cr8r_vec_ft*, cr8r_vec_accumulator f, void *init);
371 
372 
377 
384 bool cr8r_vec_sorted(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft*);
385 
388 
396 bool cr8r_vec_reversed(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft*);
397 
406 bool cr8r_vec_containss(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
407 
417 int64_t cr8r_vec_indexs(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
418 
427 int64_t cr8r_vec_first_gts(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
428 
437 int64_t cr8r_vec_first_ges(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
438 
447 int64_t cr8r_vec_last_lts(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
448 
457 int64_t cr8r_vec_last_les(const cr8r_vec*, const cr8r_vec_ft*, const void *e);
458 
463 int cr8r_vec_cmp(const cr8r_vec *a, const cr8r_vec *b, const cr8r_vec_ft*);
464 
478 void *cr8r_vec_pivot_m3(const cr8r_vec*, cr8r_vec_ft*, uint64_t a, uint64_t b);
479 
501 void *cr8r_vec_pivot_mm(cr8r_vec*, cr8r_vec_ft*, uint64_t a, uint64_t b);
502 
513 void *cr8r_vec_partition(cr8r_vec*, cr8r_vec_ft*, uint64_t a, uint64_t b, void *piv);
514 
527 void *cr8r_vec_partition_with_median(cr8r_vec*, cr8r_vec_ft*, uint64_t a, uint64_t b, void *piv);
528 
539 void *cr8r_vec_ith(cr8r_vec*, cr8r_vec_ft*, uint64_t a, uint64_t b, uint64_t i);
540 
545 void *cr8r_default_acc_sum_u64(const cr8r_vec_ft*, void *acc, const void *ent);
546 
552 void *cr8r_default_acc_sumpowmod_u64(const cr8r_vec_ft*, void *acc, const void *ent);
553 
560 
569 
576 
578 #define CR8R_VEC_ISORT_BOUND 16
579 
Base function table for all Crater containers.
Definition: container.h:22
A PseudoRandom Number Generator.
Definition: prand.h:105
Function table for vector.
Definition: vec.h:40
cr8r_base_ft base
Base function table values (data and size)
Definition: vec.h:42
void(* swap)(cr8r_base_ft *, void *a, void *b)
swap two elements.
Definition: vec.h:66
void(* del)(cr8r_base_ft *, void *p)
delete an element of the vector.
Definition: vec.h:57
int(* cmp)(const cr8r_base_ft *, const void *a, const void *b)
compare two elements of the vector.
Definition: vec.h:63
uint64_t(* new_size)(cr8r_base_ft *, uint64_t cap)
determines the capacity to grow to if the vector is full but needs to have additional elements added.
Definition: vec.h:46
void(* copy)(cr8r_base_ft *, void *dest, const void *src)
copy an element of the vector.
Definition: vec.h:60
A vector Take care if manipulating these fields directly, this should only be done if the functions i...
Definition: vec.h:23
void * buf
underlying storage for vector. Managed by ft->resize.
Definition: vec.h:25
uint64_t len
number of elements in the vector.
Definition: vec.h:27
uint64_t cap
capacity of buf (in number of elements)
Definition: vec.h:29
bool cr8r_vec_any(const cr8r_vec *, const cr8r_vec_ft *, cr8r_vec_pred pred, void *data)
Test if a predicate holds for any element in a vector.
void cr8r_vec_reverse(cr8r_vec *, cr8r_vec_ft *)
Reverse a vector in place.
void * cr8r_vec_exm(cr8r_vec *, const cr8r_vec_ft *, int ord)
Find the extremal (minimum or maximum) element of a vector.
bool cr8r_vec_pushr(cr8r_vec *, cr8r_vec_ft *, const void *e)
Add an element to the right hand end of a vector.
bool cr8r_vec_resize(cr8r_vec *, cr8r_vec_ft *, uint64_t cap)
Resize a vector's reserved buffer.
bool cr8r_vec_combine(cr8r_vec *dest, const cr8r_vec *src_a, const cr8r_vec *src_b, cr8r_vec_ft *)
Create a new vector by concatenating copies of two given vectors.
void * cr8r_vec_pivot_m3(const cr8r_vec *, cr8r_vec_ft *, uint64_t a, uint64_t b)
Pick a pivot for cr8r_vec_partition using the median of 3 approach.
bool cr8r_vec_filter(cr8r_vec *, cr8r_vec_ft *, cr8r_vec_pred pred, void *data)
Filter a vector in-place.
int64_t cr8r_vec_index(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Get the first index of an element in a vector.
void * cr8r_vec_pivot_mm(cr8r_vec *, cr8r_vec_ft *, uint64_t a, uint64_t b)
Pick a pivot for cr8r_vec_partition using the median of medians approach.
bool cr8r_vec_filtered(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *, cr8r_vec_pred pred, void *data)
Create a new vector as a subsequence matching a predicate.
bool cr8r_vec_popr(cr8r_vec *, cr8r_vec_ft *, void *o)
Remove an element from the right hand end of a vector.
bool cr8r_vec_augment(cr8r_vec *self, const cr8r_vec *other, cr8r_vec_ft *)
Add a copy of another vector to a given vector.
bool cr8r_vec_init(cr8r_vec *, cr8r_vec_ft *, uint64_t cap)
Initialize a vector with an empty buffer of a given capacity.
int64_t cr8r_vec_first_ges(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Get the index of the first element greater than or equal to a given key in a sorted vector.
cr8r_vec_ft cr8r_vecft_u8
Function table for vectors of uint8_t's, which can be used as smart strings.
bool cr8r_vec_contains(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Check if a vector contains a given element.
void(* cr8r_vec_mapper)(const cr8r_vec_ft *src_ft, const cr8r_vec_ft *dest_ft, void *o, const void *e, void *data)
Callback type for functions mapping from one vector element type to another.
Definition: vec.h:77
bool cr8r_vec_ensure_cap(cr8r_vec *self, cr8r_vec_ft *ft, uint64_t cap)
Expand a vector's internal storage if needed to fit a given capacity Will not reallocate if self->cap...
bool cr8r_vec_map(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *src_ft, cr8r_vec_ft *dest_ft, cr8r_vec_mapper f, void *data)
Create a new vector by applying a transformation function to each element.
int cr8r_vec_forEachPermutation(cr8r_vec *, cr8r_vec_ft *, void(*f)(const cr8r_vec *, const cr8r_vec_ft *, void *data), void *data)
Execute a function on every permutation of a vector.
void *(* cr8r_vec_accumulator)(const cr8r_vec_ft *, void *acc, const void *e)
Callback type for accumulator functions on vectors (cr8r_vec_foldl)
Definition: vec.h:82
bool(* cr8r_vec_pred)(const cr8r_vec_ft *, const void *ent, void *data)
Callback type for predicates on vector elements.
Definition: vec.h:72
void * cr8r_default_acc_sum_u64(const cr8r_vec_ft *, void *acc, const void *ent)
Callback for cr8r_vec_foldr to sum up elements in vector.
void cr8r_vec_sort(cr8r_vec *, cr8r_vec_ft *)
Sort a vector in-place according to ft->cmp.
int64_t cr8r_vec_last_les(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Get the index of the last element greater than a given key in a sorted vector.
bool cr8r_vec_all(const cr8r_vec *, const cr8r_vec_ft *, cr8r_vec_pred pred, void *data)
Test if a predicate holds for all elements in a vector.
uint64_t cr8r_vec_len(cr8r_vec *)
Get the length of a vector.
void cr8r_vec_shuffle(cr8r_vec *, cr8r_vec_ft *, cr8r_prng *)
Shuffle the vector into a random permutation.
bool cr8r_vec_ft_init(cr8r_vec_ft *, void *data, uint64_t size, uint64_t(*new_size)(cr8r_base_ft *, uint64_t cap), void *(*resize)(cr8r_base_ft *, void *p, uint64_t cap), void(*del)(cr8r_base_ft *, void *p), void(*copy)(cr8r_base_ft *, void *dest, const void *src), int(*cmp)(const cr8r_base_ft *, const void *a, const void *b), void(*swap)(cr8r_base_ft *, void *a, void *b))
Convenience function to initialize a cr8r_vec_ft.
void * cr8r_vec_getx(cr8r_vec *, const cr8r_vec_ft *, int64_t i)
Get the element at a given index, with support for negative indices.
void cr8r_vec_delete(cr8r_vec *, cr8r_vec_ft *)
Delete all entries in a vector and free its buffer.
int cr8r_vec_cmp(const cr8r_vec *a, const cr8r_vec *b, const cr8r_vec_ft *)
Lexicographically compare two vectors.
cr8r_vec_ft cr8r_vecft_u64
Function table for vectors of uint64_t's.
void * cr8r_vec_partition(cr8r_vec *, cr8r_vec_ft *, uint64_t a, uint64_t b, void *piv)
Partition a subrange of a vector into elements < a pivot and elements >= a pivot.
bool cr8r_vec_pushl(cr8r_vec *, cr8r_vec_ft *, const void *e)
Add an element to the left hand end of a vector.
void * cr8r_vec_foldr(const cr8r_vec *, const cr8r_vec_ft *, cr8r_vec_accumulator f, void *init)
Perform a right fold on a vector.
void cr8r_vec_clear(cr8r_vec *, cr8r_vec_ft *)
Remove all elements from a vector.
void * cr8r_vec_get(cr8r_vec *, const cr8r_vec_ft *, uint64_t i)
Get the element at a given index.
bool cr8r_vec_popl(cr8r_vec *, cr8r_vec_ft *, void *o)
Remove an element from the left hand end of a vector.
void * cr8r_vec_partition_with_median(cr8r_vec *, cr8r_vec_ft *, uint64_t a, uint64_t b, void *piv)
Partition a subrange of a vector into elements <, ==, and > a pivot.
bool cr8r_vec_sub(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *, uint64_t a, uint64_t b)
Create a copy of a slice of a vector.
bool cr8r_vec_trim(cr8r_vec *, cr8r_vec_ft *)
Resize a vector's reserved buffer to its length.
bool cr8r_vec_copy(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *)
Create a copy of a vector.
bool cr8r_vec_sorted(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *)
Create a sorted copy of a vector.
bool cr8r_vec_reversed(cr8r_vec *dest, const cr8r_vec *src, cr8r_vec_ft *)
Create a reversed copy of a vector.
int64_t cr8r_vec_last_lts(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Get the index of the last element less than a given key in a sorted vector.
int64_t cr8r_vec_first_gts(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Get the index of the first element greater than a given key in a sorted vector.
int64_t cr8r_vec_indexs(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Get the index of an element in a sorted vector.
bool cr8r_vec_containss(const cr8r_vec *, const cr8r_vec_ft *, const void *e)
Check if a sorted vector contains an element.
cr8r_vec_ft cr8r_vecft_cstr
Function table for vectors of strings.
void * cr8r_default_acc_sumpowmod_u64(const cr8r_vec_ft *, void *acc, const void *ent)
Callback for cr8r_vec_foldr to sum up powers of elements in a vector mod a number.
void * cr8r_vec_foldl(const cr8r_vec *, const cr8r_vec_ft *, cr8r_vec_accumulator f, void *init)
Perform a left fold on a vector.
void * cr8r_vec_ith(cr8r_vec *, cr8r_vec_ft *, uint64_t a, uint64_t b, uint64_t i)
Find the ith element of a subrange of a vector without completely sorting it.