From c7253fa4c12adf0639c1ed3a0cd2b2ea28ee194f Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Mon, 30 Nov 2020 14:00:19 +0100 Subject: Added clist docs, updated cbitset.h and docs. --- docs/cbitset_api.md | 4 ++ docs/clist_api.md | 104 ++++++++++++++++++++++++++++++++++++++++++++++++++++ examples/bits.c | 4 +- stc/cbitset.h | 26 ++++++++++--- 4 files changed, 131 insertions(+), 7 deletions(-) create mode 100644 docs/clist_api.md diff --git a/docs/cbitset_api.md b/docs/cbitset_api.md index 0fd0e699..1fa28fe8 100644 --- a/docs/cbitset_api.md +++ b/docs/cbitset_api.md @@ -28,12 +28,16 @@ cbitset_t cbitset_init(void); cbitset_t cbitset_with_size(size_t size, bool value); cbitset_t cbitset_from_str(const char* str); cbitset_t cbitset_clone(cbitset_t other); +cbitset_t cbitset_move(cbitset_t* self); cbitset_t cbitset_intersect(cbitset_t s1, cbitset_t s2); cbitset_t cbitset_union(cbitset_t s1, cbitset_t s2); cbitset_t cbitset_xor(cbitset_t s1, cbitset_t s2); cbitset_t cbitset_not(cbitset_t s1); +cbitset_t* cbitset_take(cbitset_t* self, cbitset_t other); +cbitset_t* cbitset_assign(cbitset_t* self, cbitset_t other); + void cbitset_resize(cbitset_t* self, size_t size, bool value); void cbitset_del(cbitset_t* self); diff --git a/docs/clist_api.md b/docs/clist_api.md new file mode 100644 index 00000000..d30c5338 --- /dev/null +++ b/docs/clist_api.md @@ -0,0 +1,104 @@ +# Introduction + +UNDER CONSTRUCTION! + +This describes the API of string type **clist_xx**. + +## Instantiation + +```c +#define using_clist(xx, Value, ...) + +#define using_clist_str() + +#define using_clist(xx, Value, valueDestroy=c_default_del, + valueCompareRaw=c_default_compare, + RawValue=Value, + valueToRaw=c_default_to_raw, + valueFromRaw=c_default_from_raw) +``` +The macro `using_clist()` can be instantiated with 2, 3, 4, or 7 arguments. Defaults are given above for args not specified. +Note that xx can be any name, it's a tag and will affect the names of all clist types and methods, +e.g. for `using_clist(my, int);` 'xx' should be replaced by 'my' in all the following. +`using_clist_str()` is a predefined definition of clist_using(str, cstr_t, ...). + +## Types + +| cstr | Type definition | Used to represent... | +|:----------------------|:---------------------------------------|:------------------------------------| +| `clist_xx` | `struct { clist_xx_node_t* last; }` | The clist type | +| `clist_xx_node_t` | `struct {` | | +| | ` struct clist_xx_node* next;` | | +| | ` clist_xx_value_t value;` | | +| | `}` | clist node | +| `clist_xx_value_t` | `Value` | The clist element type | +| `clist_xx_iter_t` | `struct {` | | +| | ` clist_xx_node_t* const* _last;` | | +| | ` clist_xx_value_t* val;` | | +| | ` int _state;` | | +| | `}` | clist iterator | + +## Constants + +| cstr constant name | Numerical values | +|:---------------------------|:-----------------| +| `clist_inits` | `{NULL}` | + +## Header file + +All clist definitions and prototypes may be included in your C source file by including a single header file. + +```c +#include "stc/clist.h" +``` +## Methods + +### Construction + +The interfaces to create a clist_xx object: +```c +clist_xx clist_xx_init(void); +bool clist_xx_empty(clist_xx ls); +size_t clist_xx_size(clist_xx ls); + +void clist_xx_clear(clist_xx* self); +void clist_xx_del(clist_xx* self); + +Value clist_xx_value_from_raw(RawValue rawValue); + +void clist_xx_push_n(clist_xx *self, const clist_xx_input_t in[], size_t size); +void clist_xx_push_back(clist_xx* self, Value value); +void clist_xx_emplace_back(clist_xx* self, RawValue rawValue); + +void clist_xx_push_front(clist_xx* self, Value value); +void clist_xx_emplace_front(clist_xx* self, RawValue rawValue); + +void clist_xx_pop_front(clist_xx* self); + +clist_xx_iter_t clist_xx_insert_after(clist_xx* self, clist_xx_iter_t pos, Value value); +clist_xx_iter_t clist_xx_emplace_after(clist_xx* self, clist_xx_iter_t pos, RawValue rawValue); + +clist_xx_iter_t clist_xx_erase_after(clist_xx* self, clist_xx_iter_t pos); +clist_xx_iter_t clist_xx_erase_range_after(clist_xx* self, clist_xx_iter_t pos, clist_xx_iter_t finish); + +void clist_xx_splice_front(clist_xx* self, clist_xx* other); +void clist_xx_splice_back(clist_xx* self, clist_xx* other); +void clist_xx_splice_after(clist_xx* self, clist_xx_iter_t pos, clist_xx* other); + +clist_xx_iter_t clist_xx_find(const clist_xx* self, RawValue val); +clist_xx_iter_t clist_xx_find_before(const clist_xx* self, clist_xx_iter_t first, clist_xx_iter_t finish, RawValue val); + +size_t clist_xx_remove(clist_xx* self, RawValue val); + +void clist_xx_sort(clist_xx* self); + +Value* clist_xx_front(clist_xx* self); +Value* clist_xx_back(clist_xx* self); + +clist_xx_iter_t clist_xx_before_begin(const clist_xx* self); +clist_xx_iter_t clist_xx_begin(const clist_xx* self); +clist_xx_iter_t clist_xx_last(const clist_xx* self); +clist_xx_iter_t clist_xx_end(const clist_xx* self); +void clist_xx_next(clist_xx_iter_t* it); +clist_xx_value_t* clist_xx_itval(clist_xx_iter_t it); +``` diff --git a/examples/bits.c b/examples/bits.c index 1b12f238..a720ae64 100644 --- a/examples/bits.c +++ b/examples/bits.c @@ -28,13 +28,13 @@ int main() { printf("%4zu: ", set.size); c_foreach (i, cbitset, set) printf("%d", cbitset_itval(i)); - puts(""); + puts(""); cbitset_t s2 = cbitset_clone(set); cbitset_flip_all(&s2); cbitset_set(&s2, 16); cbitset_set(&s2, 17); - cbitset_set(&s2, 18); + cbitset_set(&s2, 18); printf(" new: "); c_forrange (i, int, s2.size) printf("%d", cbitset_test(s2, i)); diff --git a/stc/cbitset.h b/stc/cbitset.h index 946f9545..715eddb4 100644 --- a/stc/cbitset.h +++ b/stc/cbitset.h @@ -45,7 +45,9 @@ int main() { cbitset_del(&bset); } */ -#include "cstr.h" +#include +#include +#include "ccommon.h" typedef struct cbitset { uint64_t* _arr; size_t size; } cbitset_t; @@ -64,6 +66,23 @@ STC_INLINE size_t cbitset_size(cbitset_t set) {return set.size;} STC_INLINE cbitset_t cbitset_init() { cbitset_t bs = {NULL, 0}; return bs; } +STC_INLINE void cbitset_del(cbitset_t* self) { + c_free(self->_arr); +} + +STC_DEF cbitset_t* cbitset_take(cbitset_t* self, cbitset_t other) { + if (self->_arr != other._arr) cbitset_del(self), *self = other; + return self; +} +STC_DEF cbitset_t* cbitset_assign(cbitset_t* self, cbitset_t other) { + if (self->_arr != other._arr) cbitset_del(self), *self = cbitset_clone(other); + return self; +} +STC_INLINE cbitset_t cbitset_move(cbitset_t* self) { + cbitset_t tmp = *self; self->_arr = NULL, self->size = 0; + return tmp; +} + STC_INLINE void cbitset_set(cbitset_t *self, size_t i) { self->_arr[i >> 6] |= 1ull << (i & 63); } @@ -90,9 +109,6 @@ STC_INLINE void cbitset_flip_all(cbitset_t *self) { size_t n = (self->size + 63) >> 6; for (size_t i=0; i_arr[i] ^= ~0ull; } -STC_INLINE void cbitset_del(cbitset_t* self) { - c_free(self->_arr); -} /* Intersection */ STC_INLINE void cbitset_intersect_with(cbitset_t *self, cbitset_t other) { @@ -192,7 +208,7 @@ STC_DEF cbitset_t cbitset_from_str(const char* str) { } STC_DEF char* cbitset_to_str(cbitset_t set, char* out, size_t start, intptr_t stop) { size_t end = stop < 0 ? set.size : stop; - for (size_t i=start; i