Skip to content

Commit

Permalink
- Added move and take functions to containers which missed it.
Browse files Browse the repository at this point in the history
- Added more tests.
- Renamed cco_timer_from() => cco_timer_make()
  • Loading branch information
tylov committed Oct 26, 2024
1 parent b42d762 commit fb6b2f1
Show file tree
Hide file tree
Showing 32 changed files with 253 additions and 75 deletions.
8 changes: 4 additions & 4 deletions docs/algorithm_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

STC contains many generic algorithms and loop abstactions. Raw loops are one of the most prominent
sources for errors in C and C++ code. By using the loop abstractions below, your code becomes more
descriptive and reduces chances of making mistakes. It is often easier to read and write too.
descriptive and reduces chances of making mistakes. It is generally easier to read and write too.

## Ranged for-loops
<details>
Expand Down Expand Up @@ -240,16 +240,16 @@ c_func (get_data1,(void), ->, Vec) {
// return two Vec types "on-the-fly".
c_func (get_data2,(void), ->, struct {Vec v1, v2;}) {
return (get_data2_result){
.v1=c_init(Vec, {1, 2, 3, 4, 5, 6}),
.v2=c_init(Vec, {7, 8, 9, 10, 11})
.v1 = c_init(Vec, {1, 2, 3, 4, 5, 6}),
.v2 = c_init(Vec, {7, 8, 9, 10, 11})
};
}
// return a Vec, and an err code which is 0 if OK.
c_func (load_data,(const char* fname), ->, struct {Vec vec; int err;}) {
FILE* fp = fopen(fname, "rb");
if (fp == 0)
return (load_data_result){.err=1};
return (load_data_result){.err = 1};
load_data_result out = {Vec_with_size(1024, '\0')};
fread(out.vec.data, sizeof(out.vec.data[0]), 1024, fp);
Expand Down
7 changes: 3 additions & 4 deletions docs/arc_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,13 +58,12 @@ arc_X arc_X_from_ptr(i_key* p); // create an arc
arc_X arc_X_make(i_key key); // create an arc from constructed key object. Faster than from_ptr().
arc_X arc_X_clone(arc_X other); // return other with increased use count
arc_X arc_X_move(arc_X* self); // transfer ownership to receiver; self becomes NULL
void arc_X_take(arc_X* self, arc_X unowned); // take ownership of unowned.
void arc_X_assign(arc_X* self, arc_X other); // shared assign (increases use count)
void arc_X_take(arc_X* self, arc_X unowned); // take ownership of unowned.
arc_X arc_X_move(arc_X* self); // transfer ownership to receiver; self becomes NULL
void arc_X_drop(arc_X* self); // destruct (decrease use count, free at 0)
long arc_X_use_count(const arc_X* self);
long arc_X_use_count(const arc_X* self);
void arc_X_reset_to(arc_X* self, i_key* p); // assign new arc from ptr. Takes ownership of p.
size_t arc_X_hash(const arc_X* x); // hash value
Expand Down
4 changes: 2 additions & 2 deletions docs/box_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,9 @@ box_X box_X_from_ptr(i_key* ptr); // create a box
box_X box_X_make(i_key val); // create a box from unowned val object.
box_X box_X_clone(box_X other); // return deep copied clone
box_X box_X_move(box_X* self); // transfer ownership to receiving box returned. self becomes NULL.
void box_X_take(box_X* self, box_X unowned); // take ownership of unowned box object.
void box_X_assign(box_X* self, box_X* moved); // transfer ownership from moved to self; moved becomes NULL.
void box_X_take(box_X* self, box_X unowned); // take ownership of unowned box object.
box_X box_X_move(box_X* self); // transfer ownership to receiving box returned. self becomes NULL.
void box_X_drop(box_X* self); // destruct the contained object and free its heap memory.
void box_X_reset_to(box_X* self, i_key* p); // assign new box from ptr. Takes ownership of p.
Expand Down
2 changes: 1 addition & 1 deletion docs/cbits_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ cbits cbits_clone(cbits other);

void cbits_clear(cbits* self);
cbits* cbits_copy(cbits* self, const cbits* other);
void cbits_resize(cbits* self, isize size, bool value); // NB! only for dynamic bitsets!
bool cbits_resize(cbits* self, isize size, bool value); // NB! only for dynamic bitsets!
void cbits_drop(cbits* self);

cbits* cbits_take(cbits* self, const cbits* other); // give other to self
Expand Down
7 changes: 5 additions & 2 deletions docs/deque_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,13 +41,16 @@ See the c++ class [std::deque](https://en.cppreference.com/w/cpp/container/deque
```c
deque_X deque_X_init(void);
deque_X deque_X_with_capacity(isize size);

deque_X deque_X_clone(deque_X deque);
void deque_X_copy(deque_X* self, const deque_X* other);
void deque_X_take(deque_X* self, deque_X unowned); // take ownership of unowned
deque_X deque_X_move(deque_X* self); // move
void deque_X_drop(deque_X* self); // destructor

void deque_X_clear(deque_X* self);
void deque_X_copy(deque_X* self, const deque_X* other);
bool deque_X_reserve(deque_X* self, isize cap);
void deque_X_shrink_to_fit(deque_X* self);
void deque_X_drop(deque_X* self); // destructor

bool deque_X_is_empty(const deque_X* self);
isize deque_X_size(const deque_X* self);
Expand Down
7 changes: 5 additions & 2 deletions docs/hmap_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ See the c++ class [std::unordered_map](https://en.cppreference.com/w/cpp/contain
```c
hmap_X hmap_X_init(void);
hmap_X hmap_X_with_capacity(isize cap);

hmap_X hmap_X_clone(hmap_x map);
void hmap_X_copy(hmap_X* self, const hmap_X* other);
void hmap_X_take(hmap_X* self, hmap_X unowned); // take ownership of unowned
hmap_X hmap_X_move(hmap_X* self); // move
void hmap_X_drop(hmap_X* self); // destructor

void hmap_X_clear(hmap_X* self);
void hmap_X_copy(hmap_X* self, const hmap_X* other);
float hmap_X_max_load_factor(const hmap_X* self); // default: 0.85f
bool hmap_X_reserve(hmap_X* self, isize size);
void hmap_X_shrink_to_fit(hmap_X* self);
void hmap_X_drop(hmap_X* self); // destructor

bool hmap_X_is_empty(const hmap_X* self );
isize hmap_X_size(const hmap_X* self);
Expand Down
7 changes: 5 additions & 2 deletions docs/hset_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,14 +34,17 @@ A **hset** is an associative container that contains a set of unique objects of
```c
hset_X hset_X_init(void);
hset_X hset_X_with_capacity(isize cap);

hset_X hset_X_clone(hset_x set);
void hset_X_copy(hset_X* self, const hset_X* other);
void hset_X_take(hset_X* self, hset_X unowned); // take ownership of unowned
hset_X hset_X_move(hset_X* self); // move
void hset_X_drop(hset_X* self); // destructor

void hset_X_clear(hset_X* self);
void hset_X_copy(hset_X* self, const hset_X* other);
float hset_X_max_load_factor(const hset_X* self); // default: 0.85
bool hset_X_reserve(hset_X* self, isize size);
void hset_X_shrink_to_fit(hset_X* self);
void hset_X_drop(hset_X* self); // destructor

bool hset_X_is_empty(const hset_X* self);
isize hset_X_size(const hset_X* self); // num. of allocated buckets
Expand Down
7 changes: 5 additions & 2 deletions docs/list_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,12 +51,15 @@ See the c++ class [std::list](https://en.cppreference.com/w/cpp/container/list)
```c
list_X list_X_init(void);
list_X list_X_clone(list_X list);
void list_X_clear(list_X* self);
list_X list_X_clone(list_X list);
void list_X_copy(list_X* self, const list_X* other);
void list_X_take(list_X* self, list_X unowned); // take ownership of unowned
list_X list_X_move(list_X* self); // move
void list_X_drop(list_X* self); // destructor
void list_X_clear(list_X* self);
bool list_X_is_empty(const list_X* list);
isize list_X_count(const list_X* list); // size() in O(n) time
Expand Down
13 changes: 8 additions & 5 deletions docs/pqueue_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,24 +34,27 @@ In the following, `X` is the value of `i_key` unless `i_type` is defined.
## Methods

```c
pqueue_X pqueue_X_init(void); // create empty pri-queue.
pqueue_X pqueue_X_init(void); // create empty pri-queue.
pqueue_X pqueue_X_with_capacity(isize cap);
pqueue_X pqueue_X_with_size(isize size, i_key null);

pqueue_X pqueue_X_clone(pqueue_X pq);
void pqueue_X_copy(pqueue_X* self, const pqueue_X* other);
void pqueue_X_take(pqueue_X* self, pqueue_X unowned); // take ownership of unowned
pqueue_X pqueue_X_move(pqueue_X* self); // move
void pqueue_X_drop(pqueue_X* self); // destructor

void pqueue_X_clear(pqueue_X* self);
bool pqueue_X_reserve(pqueue_X* self, isize n);
void pqueue_X_shrink_to_fit(pqueue_X* self);
void pqueue_X_copy(pqueue_X* self, const pqueue_X* other);
void pqueue_X_drop(pqueue_X* self); // destructor

isize pqueue_X_size(const pqueue_X* self);
bool pqueue_X_is_empty(const pqueue_X* self);
const i_key* pqueue_X_top(const pqueue_X* self);

void pqueue_X_make_heap(pqueue_X* self); // heapify the vector.
void pqueue_X_make_heap(pqueue_X* self); // heapify the vector.
void pqueue_X_push(pqueue_X* self, i_key value);
void pqueue_X_emplace(pqueue_X* self, i_keyraw raw); // converts from raw
void pqueue_X_emplace(pqueue_X* self, i_keyraw raw); // converts from raw

void pqueue_X_pop(pqueue_X* self);
i_key pqueue_X_pull(const pqueue_X* self);
Expand Down
9 changes: 6 additions & 3 deletions docs/queue_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,16 @@ In the following, `X` is the value of `i_key` unless `i_type` is defined.
```c
queue_X queue_X_init(void);
queue_X queue_X_with_capacity(isize size);

queue_X queue_X_clone(queue_X q);
void queue_X_copy(queue_X* self, const queue_X* other);
void queue_X_take(queue_X* self, queue_X unowned); // take ownership of unowned
queue_X queue_X_move(queue_X* self); // move
void queue_X_drop(queue_X* self); // destructor

void queue_X_clear(queue_X* self);
void queue_X_copy(queue_X* self, const queue_X* other);
bool queue_X_reserve(queue_X* self, isize cap);
void queue_X_shrink_to_fit(queue_X* self);
void queue_X_drop(queue_X* self); // destructor

isize queue_X_size(const queue_X* self);
isize queue_X_capacity(const queue_X* self);
Expand All @@ -57,7 +60,7 @@ queue_X_iter queue_X_end(const queue_X* self);
void queue_X_next(queue_X_iter* it);
queue_X_iter queue_X_advance(queue_X_iter it, isize n);

bool queue_X_eq(const queue_X* c1, const queue_X* c2); // require i_eq/i_cmp/i_less.
bool queue_X_eq(const queue_X* c1, const queue_X* c2); // require i_eq/i_cmp/i_less.
i_key queue_X_value_clone(i_key value);
queue_X_raw queue_X_value_toraw(const i_key* pval);
void queue_X_value_drop(i_key* pval);
Expand Down
10 changes: 7 additions & 3 deletions docs/random_api.md
Original file line number Diff line number Diff line change
@@ -1,8 +1,11 @@
# STC [crand64 / crand32](../include/stc/random.h): Pseudo Random Number Generator
![Random](pics/random.jpg)

This features an excellent 32- and 64-bit Pseudo Random Number Geneator (PRNG).
See [random](https://en.cppreference.com/w/cpp/header/random) for similar c++ functionality.
A high quality, very fast 32- and 64-bit Pseudo Random Number Geneator (PRNG). It features
uniform and normal distributed random numbers and float/doubles conversions.

<details>
<summary>A comparison with xoshiro256**</summary>

Several programming languages uses xoshiro256\*\* as the default PRNG. Let's compare.

Expand All @@ -16,7 +19,7 @@ from currently available random test-suites. **SFC64** has a minimum period leng
- Trivially predictable and invertible: previous outputs along with all future ones can trivially be computed from four
output samples.
- Requires *jump-functions*, which the user must call in order to split up the output ranges before parallel execution.
- Overkill: Even to create "as few as" 2^64 random numbers in one thread at 1ns per number takes 584 years.
- Overkill: Even to create "as few as" 2^64 random numbers in one thread at 1ns per number takes 584 years.
- The generator may end up in "zeroland" or "oneland" states (nearly all bits 0s or 1s for multiple outputs in a row), and will
generate low quality output. See [A Quick Look at Xoshiro256\*\*](https://www.pcg-random.org/posts/a-quick-look-at-xoshiro256.html).
- **crand64** does not need jump-functions. Instead one can simply pass a unique odd id/number to each stream/thread as argument.
Expand All @@ -27,6 +30,7 @@ from currently available random test-suites. **SFC64** has a minimum period leng
- **xoshiro256\*\***'s output is not fed back into its state, instead every possible bit-state is iterated over by applying XOR and
SHIFT bit-operations exclusively. Like with Mersenne Twister, the extreme period length has a cost: Because of the highly regulated
state changes, a relative expensive output function with two multiplications is needed to achieve high quality output.
</details>

## Header file
```c
Expand Down
11 changes: 7 additions & 4 deletions docs/smap_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -54,14 +54,17 @@ See the c++ class [std::map](https://en.cppreference.com/w/cpp/container/map) fo
```c
smap_X smap_X_init(void);
sset_X smap_X_with_capacity(isize cap);
bool smap_X_reserve(smap_X* self, isize cap);
void smap_X_shrink_to_fit(smap_X* self);
smap_X smap_X_clone(smap_x map);

void smap_X_clear(smap_X* self);
smap_X smap_X_clone(smap_x map);
void smap_X_copy(smap_X* self, const smap_X* other);
void smap_X_take(smap_X* self, smap_X unowned); // take ownership of unowned
smap_X smap_X_move(smap_X* self); // move
void smap_X_drop(smap_X* self); // destructor

void smap_X_clear(smap_X* self);
bool smap_X_reserve(smap_X* self, isize cap);
void smap_X_shrink_to_fit(smap_X* self);

bool smap_X_is_empty(const smap_X* self);
isize smap_X_size(const smap_X* self);
isize smap_X_capacity(const smap_X* self);
Expand Down
11 changes: 7 additions & 4 deletions docs/sset_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,17 @@ See the c++ class [std::set](https://en.cppreference.com/w/cpp/container/set) fo
```c
sset_X sset_X_init(void);
sset_X sset_X_with_capacity(isize cap);
bool sset_X_reserve(sset_X* self, isize cap);
void sset_X_shrink_to_fit(sset_X* self);
sset_X sset_X_clone(sset_x set);

void sset_X_clear(sset_X* self);
sset_X sset_X_clone(sset_x set);
void sset_X_copy(sset_X* self, const sset_X* other);
void sset_X_take(sset_X* self, sset_X unowned); // take ownership of unowned
sset_X sset_X_move(sset_X* self); // move
void sset_X_drop(sset_X* self); // destructor

void sset_X_clear(sset_X* self);
bool sset_X_reserve(sset_X* self, isize cap);
void sset_X_shrink_to_fit(sset_X* self);

bool sset_X_is_empty(const sset_X* self);
isize sset_X_size(const sset_X* self);
isize sset_X_capacity(const sset_X* self);
Expand Down
9 changes: 6 additions & 3 deletions docs/stack_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,14 +41,16 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack
stack_X stack_X_init(void);
stack_X stack_X_with_capacity(isize cap);
stack_X stack_X_with_size(isize size, i_key fill);

stack_X stack_X_clone(stack_X st);
void stack_X_copy(stack_X* self, const stack_X* other);
stack_X stack_X_move(stack_X* self); // move
void stack_X_take(stack_X* self, stack_X unowned); // take ownership of unowned
void stack_X_drop(stack_X* self); // destructor

void stack_X_clear(stack_X* self);
bool stack_X_reserve(stack_X* self, isize n);
void stack_X_shrink_to_fit(stack_X* self);
i_key* stack_X_append_uninit(stack_X* self, isize n);
void stack_X_copy(stack_X* self, const stack_X* other);
void stack_X_drop(stack_X* self); // destructor

isize stack_X_size(const stack_X* self);
isize stack_X_capacity(const stack_X* self);
Expand All @@ -66,6 +68,7 @@ i_key* stack_X_back_mut(stack_X* self);

i_key* stack_X_push(stack_X* self, i_key value);
i_key* stack_X_emplace(stack_X* self, i_keyraw raw);
i_key* stack_X_append_uninit(stack_X* self, isize n);

void stack_X_pop(stack_X* self); // destroy last element
i_key stack_X_pull(stack_X* self); // move out last element
Expand Down
7 changes: 5 additions & 2 deletions docs/vec_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,16 @@ vec_X vec_X_with_size(isize size, i_key null);
vec_X vec_X_with_capacity(isize size);
vec_X vec_X_clone(vec_X vec);

void vec_X_clear(vec_X* self);
void vec_X_copy(vec_X* self, const vec_X* other);
vec_X_iter vec_X_copy_n(vec_X* self, isize idx, const i_key* arr, isize n);
vec_X vec_X_move(vec_X* self); // move
void vec_X_take(vec_X* self, vec_X unowned); // take ownership of unowned
void vec_X_drop(vec_X* self); // destructor

void vec_X_clear(vec_X* self);
bool vec_X_reserve(vec_X* self, isize cap);
bool vec_X_resize(vec_X* self, isize size, i_key null);
void vec_X_shrink_to_fit(vec_X* self);
void vec_X_drop(vec_X* self); // destructor

bool vec_X_is_empty(const vec_X* self);
isize vec_X_size(const vec_X* self);
Expand Down
8 changes: 5 additions & 3 deletions include/stc/cbits.h
Original file line number Diff line number Diff line change
Expand Up @@ -181,10 +181,11 @@ STC_INLINE cbits* cbits_copy(cbits* self, const cbits* other) {
return self;
}

STC_INLINE void cbits_resize(cbits* self, const isize size, const bool value) {
STC_INLINE bool cbits_resize(cbits* self, const isize size, const bool value) {
const isize new_w = _cbits_words(size), osize = self->_size, old_w = _cbits_words(osize);
self->buffer = (uintptr_t *)i_realloc(self->buffer, old_w*_cbits_WS, new_w*_cbits_WS);
self->_size = size;
uintptr_t* b = (uintptr_t *)i_realloc(self->buffer, old_w*_cbits_WS, new_w*_cbits_WS);
if (b == NULL) return false;
self->buffer = b; self->_size = size;
if (size > osize) {
c_memset(self->buffer + old_w, -(int)value, (new_w - old_w)*_cbits_WS);
if (osize & (_cbits_WB - 1)) {
Expand All @@ -193,6 +194,7 @@ STC_INLINE void cbits_resize(cbits* self, const isize size, const bool value) {
else self->buffer[old_w - 1] &= mask;
}
}
return true;
}

STC_INLINE void cbits_set_all(cbits *self, const bool value);
Expand Down
Loading

0 comments on commit fb6b2f1

Please sign in to comment.