Skip to content

Commit ef322ae

Browse files
committed
Added c_reverse / c_reverse_array.
1 parent af36f6c commit ef322ae

4 files changed

Lines changed: 52 additions & 23 deletions

File tree

docs/algorithm_api.md

Lines changed: 27 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -458,29 +458,34 @@ int main(void) {
458458
<!--{%endraw%}-->
459459
</details>
460460
<details>
461-
<summary><b>c_find, c_append, c_erase</b> - Container operations with custom predicate</summary>
461+
<summary><b>c_find, c_reverse, c_append, c_erase</b> - Container/array operations</summary>
462462

463463
### c_find_if, c_find_reverse_if
464464
Find linearily in containers using a predicate. `value` is a pointer to each element in predicate.
465465
***outiter_ptr*** must be defined prior to call.
466-
- `c_find_if(CntType, cnt, outiter_ptr, pred)`.
467-
- `c_find_if(CntType, startiter, enditer, outiter_ptr, pred)`
468-
- `c_find_reverse_if(CntType, cnt, outiter_ptr, pred)`
469-
- `c_find_reverse_if(CntType, startiter, enditer, outiter_ptr, pred)`
466+
- void `c_find_if`(**CntType**, cnt, outiter_ptr, pred).
467+
- void `c_find_if`(**CntType**, startiter, enditer, outiter_ptr, pred)
468+
- void `c_find_reverse_if`(**CntType**, cnt, outiter_ptr, pred)
469+
- void `c_find_reverse_if`(**CntType**, startiter, enditer, outiter_ptr, pred)
470+
471+
### c_reverse, c_reverse_array
472+
473+
- void `c_reverse`(**CntType**, cnt); // reverse a cspan, vec, stack, queue or deque type.
474+
- void `c_reverse_array`(array, len); // reverse an array of elements.
470475

471476
### c_append, c_append_if
472477
Clones any container onto an arbitrary container type, optionally using a predicate to filter out elements.
473478
Requires only that the element types are equal for the two containers.
474479
`value` is the pointer to each element in predicate. See example below for usage.
475-
- `c_append(CntType, outcnt_ptr, cnt)`
476-
- `c_append(OutCnt, outcnt_ptr, CntType, cnt)`
477-
- `c_append_if(CntType, outcnt_ptr, cnt, pred)`
478-
- `c_append_if(OutCnt, outcnt_ptr, CntType, cnt, pred)`
480+
- void `c_append`(**CntType**, outcnt_ptr, cnt)
481+
- void `c_append`(**OutCnt**, outcnt_ptr, **CntType**, cnt)
482+
- void `c_append_if`(**CntType**, outcnt_ptr, cnt, pred)
483+
- void `c_append_if`(**OutCnt**, outcnt_ptr, **CntType**, cnt, pred)
479484

480485
### c_erase_if, c_eraseremove_if
481486
Erase linearily in containers using a predicate. `value` is a pointer to each element in predicate.
482-
- `c_erase_if(CntType, cnt_ptr, pred)`. Use with **list**, **hmap**, **hset**, **smap**, and **sset**.
483-
- `c_eraseremove_if(CntType, cnt_ptr, pred)`. Use with **stack**, **vec**, **deque**, and **queue** only.
487+
- void `c_erase_if`(**CntType**, cnt_ptr, pred)`. Use with ***list**, ***hmap***, ***hset***, ***smap***, and ***sset***.
488+
- void `c_eraseremove_if`(**CntType**, cnt_ptr, pred)`. Use with ***stack***, ***vec***, ***deque***, and ***queue*** only.
484489

485490
[ [Run this code](https://godbolt.org/z/n7c641WhE) ]
486491
<!--{%raw%}-->
@@ -552,9 +557,9 @@ int main(void)
552557
553558
### c_all_of, c_any_of, c_none_of
554559
Test a container/range using a predicate. ***result*** is output and must be declared prior to call.
555-
- `void c_all_of(CntType, cnt, bool* result, pred)`
556-
- `void c_any_of(CntType, cnt, bool* result, pred)`
557-
- `void c_none_of(CntType, cnt, bool* result, pred)`
560+
- void `c_all_of`(**CntType**, cnt, bool* result, pred)
561+
- void `c_any_of`(**CntType**, cnt, bool* result, pred)
562+
- void `c_none_of`(**CntType**, cnt, bool* result, pred)
558563
```c++
559564
#define DivisibleBy(n) (*value % (n) == 0) // `value` refers to the current element
560565
@@ -636,10 +641,10 @@ int main(void) {
636641
637642
### c_new, c_delete
638643
639-
- `c_new(Type, value)` - Allocate *and initialize* a new object on the heap with *value*.
640-
- `c_new_n(Type, n)` - Allocate an array of ***n*** new objects on the heap, initialized to zero.
641-
- `c_delete(Type, ptr)` - *Type_drop(ptr)* and *c_free(ptr, ..)* allocated on the heap. NULL is OK.
642-
- `c_delete_n(Type, arr, n)` - *Type_drop(&arr[i])* and *c_free(arr, ..)* of ***n*** objects allocated on the heap. (NULL, 0) is OK.
644+
- Type\* `c_new`(**Type**, value) - Allocate *and initialize* a new object on the heap with *value*.
645+
- Type\* `c_new_n`(**Type**, n) - Allocate an array of ***n*** new objects on the heap, initialized to zero.
646+
- void `c_delete`(**Type**, ptr) - *Type_drop(ptr)* and *c_free(ptr, ..)* allocated on the heap. NULL is OK.
647+
- void `c_delete_n`(**Type**, arr, n) - *Type_drop(&arr[i])* and *c_free(arr, ..)* of ***n*** objects allocated on the heap. (NULL, 0) is OK.
643648
```c++
644649
#include "stc/cstr.h"
645650
@@ -653,10 +658,10 @@ Memory allocator wrappers which uses signed sizes. Note that the signatures for
653658
*c_realloc()* and *c_free()* have an extra size parameter. These will be used as
654659
default in containers unless `i_malloc`, `i_calloc`, `i_realloc`, and `i_free` are user defined. See
655660
[Per container-instance customization](../README.md#per-container-instance-customization)
656-
- `void* c_malloc(isize sz)`
657-
- `void* c_calloc(isize n, isize sz)`
658-
- `void* c_realloc(void* old_p, isize old_sz, isize new_sz)`
659-
- `void c_free(void* p, isize sz)`
661+
- void* `c_malloc`(isize sz)
662+
- void* `c_calloc`(isize n, isize sz)
663+
- void* `c_realloc`(void* old_p, isize old_sz, isize new_sz)
664+
- void `c_free`(void* p, isize sz)
660665

661666
</details>
662667
<details>

docs/random_api.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,8 +61,8 @@ double crand64_normal_r(crand64* rng, uint64_t strm, crand64_normal_dis
6161
```c++
6262
// Generic algorithms (uses 64 or 32 bit depending on word size):
6363
void c_shuffle_seed(size_t seed); // calls crand64_seed() or crand32_seed()
64+
void c_shuffle(TYPE CntType, CntType* cnt); // shuffle a cspan, vec, stack, queue or deque type.
6465
void c_shuffle_array(T* array, isize n); // shuffle an array of elements.
65-
void c_shuffle(TYPE CntType, CntType* cnt); // shuffle a vec, stack or deque type.
6666
```
6767
Note that `strm` must be an odd number.
6868
## Types

include/stc/cspan.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,10 @@ typedef isize _isize_triple[3];
7070
using_cspan_3(Self, T, 1); \
7171
STC_INLINE Self Self##_with_n(Self##_value* values, isize n) \
7272
{ return (Self)cspan_with_n(values, n); } \
73+
STC_INLINE const Self##_value* Self##_at(const Self* self, isize idx) \
74+
{ return cspan_at(self, idx); } \
75+
STC_INLINE Self##_value* Self##_at_mut(Self* self, isize idx) \
76+
{ return cspan_at(self, idx); } \
7377
struct stc_nostruct
7478

7579
#define using_cspan_with_eq(...) c_MACRO_OVERLOAD(using_cspan_with_eq, __VA_ARGS__)
@@ -113,6 +117,8 @@ typedef isize _isize_triple[3];
113117
it->ref += _cspan_next##RANK(it->pos, it->_s->shape, it->_s->stride.d, RANK, &done); \
114118
if (done) it->ref = NULL; \
115119
} \
120+
STC_INLINE isize Self##_size(const Self* self) \
121+
{ return cspan_size(self); } \
116122
STC_INLINE Self Self##_transpose(Self sp) \
117123
{ _cspan_transpose(sp.shape, sp.stride.d, cspan_rank(&sp)); return sp; } \
118124
struct stc_nostruct

include/stc/sys/utility.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,24 @@
5151
_out->ref = NULL; c_JOIN(findif_, __LINE__):; \
5252
} while (0)
5353

54+
// --------------------------------
55+
// c_reverse
56+
// --------------------------------
57+
58+
#define c_reverse_array(array, n) do { \
59+
typedef struct { char d[sizeof 0[array]]; } _etype; \
60+
_etype* _arr = (_etype *)(array); \
61+
for (isize _i = 0, _j = (n) - 1; _i < _j; ++_i, --_j) \
62+
c_swap(_arr + _i, _arr + _j); \
63+
} while (0)
64+
65+
// Compiles with vec, stack, and deque, and cspan container types:
66+
#define c_reverse(CntType, self) do { \
67+
CntType* _self = self; \
68+
for (isize _i = 0, _j = CntType##_size(_self) - 1; _i < _j; ++_i, --_j) \
69+
c_swap(CntType##_at_mut(_self, _i), CntType##_at_mut(_self, _j)); \
70+
} while (0)
71+
5472
// --------------------------------
5573
// c_erase_if
5674
// --------------------------------

0 commit comments

Comments
 (0)