summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--README.md1
-rw-r--r--docs/carray_api.md123
-rw-r--r--docs/ccommon_api.md2
-rw-r--r--docs/cspan_api.md120
-rw-r--r--include/c11/fmt.h90
-rw-r--r--include/stc/algo/crange.h4
-rw-r--r--include/stc/ccommon.h5
-rw-r--r--include/stc/cspan.h (renamed from include/stc/algo/cspan.h)84
-rw-r--r--misc/archived/carr2.h152
-rw-r--r--misc/archived/carr3.h157
-rw-r--r--misc/examples/forfilter.c2
-rw-r--r--misc/examples/multidim.c18
-rw-r--r--misc/examples/prime.c2
13 files changed, 240 insertions, 520 deletions
diff --git a/README.md b/README.md
index 1e9bfbea..f184e597 100644
--- a/README.md
+++ b/README.md
@@ -40,6 +40,7 @@ Containers
- [***cstack*** - **std::stack** alike type](docs/cstack_api.md)
- [***cstr*** - **std::string** alike type](docs/cstr_api.md)
- [***csview*** - **std::string_view** alike type](docs/csview_api.md)
+- [***cspan*** - **std::mdspan** alike type](docs/cspan_api.md)
- [***cdeq*** - **std::deque** alike type](docs/cdeq_api.md)
- [***cvec*** - **std::vector** alike type](docs/cvec_api.md)
diff --git a/docs/carray_api.md b/docs/carray_api.md
deleted file mode 100644
index 803a1610..00000000
--- a/docs/carray_api.md
+++ /dev/null
@@ -1,123 +0,0 @@
-# STC [carr2, carr3](../include/stc/carray.h): Dynamic Multi-dimensional Arrays
-![Array](pics/array.jpg)
-
-The **carr2** and **carr3** are templated 2D and 3D dynamic arrays. They are allocated on the heap as a single
-contiguous block of memory. The arrays can be indexed like regular constant size multi-dimensional arrays in C.
-
-See the c++ class [boost::multi_array](https://www.boost.org/doc/libs/release/libs/multi_array) for similar functionality.
-
-## Header file and declaration
-
-```c
-#define i_type // full typename of the container
-#define i_val // value: REQUIRED
-#define i_valdrop // destroy value func - defaults to empty destruct
-#define i_valclone // REQUIRED IF valdrop is defined.
-#include <stc/carr2.h> // or <stc/carr3.h>
-```
-`X` should be replaced by the value of `i_tag` in all of the following documentation.
-
-## Methods
-
-carr2_X:
-```c
-carr2_X carr2_X_with_size(size_t xdim, size_t ydim, i_val val);
-carr2_X carr2_X_with_data(size_t xdim, size_t ydim, i_val* array);
-carr2_X carr2_X_new_uninit(size_t xdim, size_t ydim);
-carr2_X carr2_X_clone(carr2_X arr);
-void carr2_X_copy(carr2_X* self, const carr2_X* other);
-i_val* carr2_X_release(carr2_X* self); // release storage (not freed)
-void carr2_X_drop(carr2_X* self);
-
-size_t carr2_X_size(const carr2_X* self);
-i_val* carr2_X_data(carr2_X* self); // access storage data
-const i_val* carr2_X_at(const carr2_X* self, size_t x, size_t y);
-size_t carr2_X_idx(const carr2_X* self, size_t x, size_t y);
-
-carr2_X_iter carr2_X_begin(const carr2_X* self);
-carr2_X_iter carr2_X_end(const carr2_X* self);
-void carr2_X_next(carr2_X_iter* it);
-```
-The **carr2** elements can be accessed with `arr.data[x][y];` or with `carr2_i_at(&arr, x, y)`.
-
-carr3:
-```c
-carr3_X carr3_X_with_size(size_t xdim, size_t ydim, size_t zdim, i_val val);
-carr3_X carr3_X_with_data(size_t xdim, size_t ydim, size_t zdim, i_val* array);
-carr3_X carr3_X_new_uninit(size_t xdim, size_t ydim, size_t zdim);
-carr3_X carr3_X_clone(carr3_X arr);
-void carr3_X_copy(carr3_X* self, const carr3_X* other);
-i_val* carr3_X_release(carr3_X* self); // release storage (not freed)
-void carr3_X_drop(carr3_X* self);
-
-size_t carr3_X_size(const carr3_X* self);
-i_val* carr3_X_data(carr3_X* self); // storage data
-const i_val* carr3_X_at(const carr3_X* self, size_t x, size_t y, size_t z);
-size_t carr3_X_idx(const carr3_X* self, size_t x, size_t y, size_t z);
-
-carr3_X_iter carr3_X_begin(const carr3_X* self);
-carr3_X_iter carr3_X_end(const carr3_X* self);
-void carr3_X_next(carr3_X_iter* it);
-```
-The **carr3** elements can be accessed with `arr.data[x][y][z];` or with `carr3_i_at(&arr, x, y, z)`.
-
-## Types
-
-| Type name | Type definition | Used to represent... |
-|:------------------|:-----------------------------------------------------|:---------------------|
-| `carr2_X` | `struct { i_val **data; size_t xdim, ydim; }` | The array 2D type |
-| `carr2_X_value` | `i_val` | The value type |
-| `carr2_X_iter` | `struct { i_val *ref; }` | Iterator type |
-| | | |
-| `carr3_X` | `struct { i_val ***data; size_t xdim, ydim, zdim; }` | The array 3D type |
-| `carr3_X_value` | `i_val` | The value type |
-| `carr3_X_iter` | `struct { i_val *ref; }` | Iterator type |
-
-## Example
-```c
-#include <stdio.h>
-
-#define i_val uint32_t
-#define i_tag i
-#include <stc/carr2.h>
-
-#define i_val float
-#define i_tag f
-#include <stc/carr3.h>
-
-int main()
-{
- // Ex1
- int xd = 30, yd = 20, zd = 10;
- // define arr3[30][20][10], initialized with zeros.
- c_WITH (carr3_f arr3 = carr3_f_with_size(xd, yd, zd, 0.0f), carr3_f_drop(&arr3)) {
- arr3.data[5][4][3] = 3.14f;
-
- float *arr1 = arr3.data[5][4];
- float **arr2 = arr3.data[5];
-
- printf("%f\n", arr1[3]); // 3.14
- printf("%f\n", arr2[4][3]); // 3.14
- printf("%f\n", arr3.data[5][4][3]); // 3.14
- }
-
- // Ex2
- int w = 256, h = 128;
- c_WITH (carr2_i image = carr2_i_new_uninit(w, h), carr2_i_drop(&image)) {
- int n = 0;
- c_FOREACH (i, carr2_i, image) {
- uint32_t t = n++ % 256;
- *i.ref = t | t << 8 | t << 16 | 255;
- }
-
- for (int y = 0; y < image.ydim; ++y)
- image.data[y][y] = 0xffffffff;
- }
-}
-```
-Output:
-```
-3.140000
-3.140000
-3.140000
-```
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index e17e98ca..b610ff04 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -282,7 +282,7 @@ c_FORFILTER (i, crange, r1
// 2. The 11 first primes:
printf("2");
-c_FORFILTER (i, crange, crange_object(3, INT64_MAX, 2)
+c_FORFILTER (i, crange, crange_literal(3, INT64_MAX, 2)
, isPrime(*i.ref)
, c_FLT_TAKE(10))
printf(" %lld", *i.ref);
diff --git a/docs/cspan_api.md b/docs/cspan_api.md
new file mode 100644
index 00000000..7af96c06
--- /dev/null
+++ b/docs/cspan_api.md
@@ -0,0 +1,120 @@
+# STC [cspan](../include/stc/cspan.h): Multi-dimensional Array View
+![Array](pics/array.jpg)
+
+The **cspan** is templated non-owning multi-dimensional view of an array.
+
+See the c++ class [std::mdspan](https://en.cppreference.com/w/cpp/container/mdspan) for a similar functionality.
+
+## Header file and declaration
+
+```c
+#include <stc/cspan.h>
+using_cspan(SpanType, ValueType, Rank); // define SpanType with ValueType elements.
+ // Rank is number of dimensions (max 4 atm.)
+// Shorthands:
+using_cspan2(S, ValueType); // define span types S1, S2 with Ranks 1, 2.
+using_cspan3(S, ValueType); // define span types S1, S2, S3 with Ranks 1, 2, 3.
+using_cspan4(S, ValueType); // define span types S1.., S4 with Ranks 1, 2, 3, 4.
+```
+## Methods
+
+```c
+SpanType& cspan_literal(SpanType, {val1, val2, ...}); // create a 1D cspan compound literal
+SpanType cspan_from_array(ValueType array[]); // create a 1D cspan from an array.
+SpanType cspan_from(STCContainer* cnt); // create a 1D cspan from a STC container;
+ // cstack, cvec, cdeq, cqueue, or cpque
+SpanType cspan_make(ValueType* data, size_t xdim, ...); // make N-dimensional cspan
+void cspan_reshape(const SpanType* self, size_t x, ...); // change the extent of each dimension
+
+size_t cspan_size(const SpanType* self); // return number of elements
+unsigned cspan_rank(const SpanType* self); // return number of dimensions
+ValueType* cspan_at(SpanType* self, size_t x, ...); // access element
+size_t cspan_index(const SpanType* self, size_t x, ...); // index of element
+
+SpanType cspan_slice1(SpanType1* self, size_t x0, size_t with); // get a slice of a 1D cspan
+SpanType cspan_slice2(SpanType2* self, size_t x0, size_t with); // get a slice of a 2D cspan
+SpanType cspan_slice3(SpanType3* self, size_t x0, size_t with); // get a slice of a 3D cspan
+SpanType cspan_slice4(SpanType4* self, size_t x0, size_t with); // get a slice of a 4D cspan
+
+SpanType3 cspan_4to3(SpanType4* self, size_t x); // return a 3D subspan
+SpanType2 cspan_4to2(SpanType4* self, size_t x, size_t y); // return a 2D subspan
+SpanType1 cspan_4to1(SpanType4* self, size_t x, size_t y, size_t z); // return a 1D subspan
+SpanType2 cspan_3to2(SpanType3* self, size_t x); // return a 2D subspan
+SpanType1 cspan_3to1(SpanType3* self, size_t x, size_t y); // return a 1D subspan
+SpanType1 cspan_2to1(SpanType2* self, size_t x); // return a 1D subspan
+
+SpanType_iter cspan_begin(const SpanType* self);
+SpanTyåe_iter cspan_end(const SpanType* self);
+void cspan_next(SpanType_iter* it);
+```
+## Types
+
+| Type name | Type definition | Used to represent... |
+|:-----------------|:-----------------------------------------------------|:---------------------|
+| SpanType | `struct { ValueType *data; uint32_t dim[RANK]; }` | The SpanType |
+| SpanType`_value` | `ValueType` | The ValueType |
+| SpanType`_iter` | `struct { ValueType *ref; ... }` | Iterator type |
+
+## Example
+```c
+#include <stdio.h>
+#define i_val float
+#include <stc/cstack.h>
+
+#include <stc/cspan.h>
+using_cspan3(FS, float); // define spans FS1, FS2, and FS3.
+
+int main()
+{
+ int xd = 6, yd = 4, zd = 3;
+ c_AUTO (cstack_float, vec) {
+ c_FORRANGE (i, xd*yd*zd)
+ cstack_float_push(&vec, i);
+
+ // define arr[xd][yd][zd] cspan
+ FS3 span3 = cspan_make(vec.data, xd, yd, zd);
+ *cspan_at(&span3, 4, 3, 2) = 3.14f;
+ printf("index: %d", (int)cspan_index(&span3, 4, 3, 2));
+
+ FS1 span1 = cspan_3to1(&span3, 4, 3);
+ printf("\niterate span1: ");
+ c_FOREACH (i, FS1, span1)
+ printf("%g ", *i.ref);
+
+ FS2 span2 = cspan_3to2(&span3, 4);
+ printf("\niterate span2: ");
+ c_FOREACH (i, FS2, span2)
+ printf("%g ", *i.ref);
+
+ puts("\niterate span3 by dimensions:");
+ c_FORRANGE (i, span3.dim[0]) {
+ c_FORRANGE (j, span3.dim[1]) {
+ c_FORRANGE (k, span3.dim[2])
+ printf(" %g", *cspan_at(&span3, i, j, k));
+ printf(" |");
+ }
+ puts("");
+ }
+
+ printf("%g\n", *cspan_at(&span3, 4, 3, 2));
+ printf("%g\n", *cspan_at(&span2, 3, 2));
+ printf("%g\n", *cspan_at(&span1, 2));
+ }
+}
+```
+Output:
+```
+index: 59
+iterate span1: 57 58 3.14
+iterate span2: 48 49 50 51 52 53 54 55 56 57 58 3.14
+iterate span3 by dimensions:
+ 0 1 2 | 3 4 5 | 6 7 8 | 9 10 11 |
+ 12 13 14 | 15 16 17 | 18 19 20 | 21 22 23 |
+ 24 25 26 | 27 28 29 | 30 31 32 | 33 34 35 |
+ 36 37 38 | 39 40 41 | 42 43 44 | 45 46 47 |
+ 48 49 50 | 51 52 53 | 54 55 56 | 57 58 3.14 |
+ 60 61 62 | 63 64 65 | 66 67 68 | 69 70 71 |
+3.14
+3.14
+3.14
+```
diff --git a/include/c11/fmt.h b/include/c11/fmt.h
index 6e2f8e55..b42bd9fc 100644
--- a/include/c11/fmt.h
+++ b/include/c11/fmt.h
@@ -1,16 +1,19 @@
#ifndef FMT_H_INCLUDED
#define FMT_H_INCLUDED
-/*
-void fmt_print(dst, fmt, ...);
-void fmt_drop_buffer(fmt_buffer* buf);
+/*
+VER 2.0: NEW API:
- dst - destination, one of:
- int fd 1=stdout, 2=stderr
+void fmt_print(fmt, ...);
+void fmt_println(fmt, ...); // see remark below
+void fmt_printd(dest, fmt, ...);
+void fmt_freebuffer(fmt_buffer* buf);
+
+ dest - destination, one of:
FILE* fp Write to a file
char* strbuf Write to a pre-allocated string buffer
fmt_buffer* buf Auto realloc the needed memory (safe).
Set buf->stream=1 for stream-mode.
- Call fmt_drop_buffer(buf) after usage.
+ Call fmt_freebuffer(buf) after usage.
fmt - format string
{} Auto-detected format. If :MOD is not specified,
@@ -22,10 +25,11 @@ void fmt_drop_buffer(fmt_buffer* buf);
* C11 or higher required.
* MAX 255 chars fmt string by default. MAX 12 arguments after fmt string.
+* fmt_println(): a) fmt must be a const char* literal. b) at least one argument after fmt required by ISO C99.
* Static linking by default, shared symbols by defining FMT_HEADER / FMT_IMPLEMENT.
* (c) operamint, 2022, MIT License.
-----------------------------------------------------------------------------------
-#include "fmt.h"
+#include "c11/fmt.h"
int main() {
const double pi = 3.141592653589793;
@@ -37,22 +41,22 @@ int main() {
unsigned char r = 123, g = 214, b = 90, w = 110;
char buffer[64];
- fmt_print(1, "Color: ({} {} {}), {}\n", r, g, b, flag);
- fmt_print(1, "Wide: {}, {}\n", wstr, L"wide world");
- fmt_print(1, "{:10} {:10} {:10.2f}\n", 42ull, 43, pi);
- fmt_print(stdout, "{:>10} {:>10} {:>10}\n", z, z, w);
- fmt_print(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World");
- fmt_print(stderr, "100%: {:<20} {:.*} {}\n", string, 4, pi, x);
- fmt_print(buffer, "Precision: {} {:.10} {}", string, pi, x);
- fmt_print(1, "{}\n", buffer);
- fmt_print(1, "Vector: ({}, {}, {})\n", 3.2, 3.3, pi);
+ fmt_print("Color: ({} {} {}), {}\n", r, g, b, flag);
+ fmt_println("Wide: {}, {}", wstr, L"wide world");
+ fmt_println("{:10} {:10} {:10.2f}", 42ull, 43, pi);
+ fmt_println("{:>10} {:>10} {:>10}", z, z, w);
+ fmt_printd(stdout, "{:10} {:10} {:10}\n", "Hello", "Mad", "World");
+ fmt_printd(stderr, "100%: {:<20} {:.*} {}\n", string, 4, pi, x);
+ fmt_printd(buffer, "Precision: {} {:.10} {}", string, pi, x);
+ fmt_println("{}", buffer);
+ fmt_println("Vector: ({}, {}, {})", 3.2, 3.3, pi);
fmt_buffer out[1] = {{.stream=1}};
- fmt_print(out, "{} {}", "Pi is:", pi);
- fmt_print(1, "{}, len={}, cap={}\n", out->data, out->len, out->cap);
- fmt_print(out, "{} {}", ", Pi squared is:", pi*pi);
- fmt_print(1, "{}, len={}, cap={}\n", out->data, out->len, out->cap);
- fmt_drop_buffer(out);
+ fmt_printd(out, "{} {}", "Pi is:", pi);
+ fmt_print("{}, len={}, cap={}\n", out->data, out->len, out->cap);
+ fmt_printd(out, "{} {}", ", Pi squared is:", pi*pi);
+ fmt_print("{}, len={}, cap={}\n", out->data, out->len, out->cap);
+ fmt_freebuffer(out);
}
*/
#include <stdio.h>
@@ -87,58 +91,60 @@ typedef struct {
_Bool stream;
} fmt_buffer;
-FMT_API void fmt_drop_buffer(fmt_buffer* buf);
+FMT_API void fmt_freebuffer(fmt_buffer* buf);
FMT_API int _fmt_parse(char* p, int nargs, const char *fmt, ...);
-FMT_API void _fmt_iprint(int fd, const char* fmt, ...);
FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...);
#ifndef FMT_MAX
#define FMT_MAX 256
#endif
+#define fmt_print(...) fmt_printd(stdout, __VA_ARGS__)
+#define fmt_println(fmt, ...) fmt_printd(stdout, fmt "\n", __VA_ARGS__)
+
/* Primary function. */
-#define fmt_print(...) fmt_OVERLOAD(fmt_print, __VA_ARGS__)
-#define fmt_print2(to, fmt) \
+#define fmt_printd(...) fmt_OVERLOAD(fmt_printd, __VA_ARGS__)
+#define fmt_printd2(to, fmt) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 0, fmt); \
fmt_OK(_n == 0); _fmt_fn(to)(to, fmt); } while (0)
-#define fmt_print3(to, fmt, c) \
+#define fmt_printd3(to, fmt, c) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 1, fmt, _fc(c)); \
fmt_OK(_n == 1); _fmt_fn(to)(to, _fs, c); } while (0)
-#define fmt_print4(to, fmt, c, d) \
+#define fmt_printd4(to, fmt, c, d) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 2, fmt, _fc(c), _fc(d)); \
fmt_OK(_n == 2); _fmt_fn(to)(to, _fs, c, d); } while (0)
-#define fmt_print5(to, fmt, c, d, e) \
+#define fmt_printd5(to, fmt, c, d, e) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 3, fmt, _fc(c), _fc(d), _fc(e)); \
fmt_OK(_n == 3); _fmt_fn(to)(to, _fs, c, d, e); } while (0)
-#define fmt_print6(to, fmt, c, d, e, f) \
+#define fmt_printd6(to, fmt, c, d, e, f) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 4, fmt, _fc(c), _fc(d), _fc(e), _fc(f)); \
fmt_OK(_n == 4); _fmt_fn(to)(to, _fs, c, d, e, f); } while (0)
-#define fmt_print7(to, fmt, c, d, e, f, g) \
+#define fmt_printd7(to, fmt, c, d, e, f, g) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 5, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g)); \
fmt_OK(_n == 5); _fmt_fn(to)(to, _fs, c, d, e, f, g); } while (0)
-#define fmt_print8(to, fmt, c, d, e, f, g, h) \
+#define fmt_printd8(to, fmt, c, d, e, f, g, h) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 6, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h)); \
fmt_OK(_n == 6); _fmt_fn(to)(to, _fs, c, d, e, f, g, h); } while (0)
-#define fmt_print9(to, fmt, c, d, e, f, g, h, i) \
+#define fmt_printd9(to, fmt, c, d, e, f, g, h, i) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 7, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), _fc(i)); \
fmt_OK(_n == 7); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i); } while (0)
-#define fmt_print10(to, fmt, c, d, e, f, g, h, i, j) \
+#define fmt_printd10(to, fmt, c, d, e, f, g, h, i, j) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 8, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j)); \
fmt_OK(_n == 8); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j); } while (0)
-#define fmt_print11(to, fmt, c, d, e, f, g, h, i, j, k) \
+#define fmt_printd11(to, fmt, c, d, e, f, g, h, i, j, k) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 9, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k)); \
fmt_OK(_n == 9); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k); } while (0)
-#define fmt_print12(to, fmt, c, d, e, f, g, h, i, j, k, m) \
+#define fmt_printd12(to, fmt, c, d, e, f, g, h, i, j, k, m) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 10, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m)); \
fmt_OK(_n == 10); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m); } while (0)
-#define fmt_print13(to, fmt, c, d, e, f, g, h, i, j, k, m, n) \
+#define fmt_printd13(to, fmt, c, d, e, f, g, h, i, j, k, m, n) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 11, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m), _fc(n)); \
fmt_OK(_n == 11); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n); } while (0)
-#define fmt_print14(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o) \
+#define fmt_printd14(to, fmt, c, d, e, f, g, h, i, j, k, m, n, o) \
do { char _fs[FMT_MAX]; int _n = _fmt_parse(_fs, 12, fmt, _fc(c), _fc(d), _fc(e), _fc(f), _fc(g), _fc(h), \
_fc(i), _fc(j), _fc(k), _fc(m), _fc(n), _fc(o)); \
fmt_OK(_n == 12); _fmt_fn(to)(to, _fs, c, d, e, f, g, h, i, j, k, m, n, o); } while (0)
@@ -146,7 +152,6 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...);
#define _fmt_fn(x) _Generic ((x), \
FILE*: fprintf, \
char*: sprintf, \
- int: _fmt_iprint, \
fmt_buffer*: _fmt_bprint)
#if defined(_MSC_VER) && !defined(__clang__)
@@ -184,17 +189,10 @@ FMT_API void _fmt_bprint(fmt_buffer*, const char* fmt, ...);
#include <stdarg.h>
#include <string.h>
-FMT_API void fmt_drop_buffer(fmt_buffer* buf) {
+FMT_API void fmt_freebuffer(fmt_buffer* buf) {
free(buf->data);
}
-FMT_API void _fmt_iprint(int fd, const char* fmt, ...) {
- va_list args;
- va_start(args, fmt);
- vfprintf(fd == 1 ? stdout : stderr, fmt, args);
- va_end(args);
-}
-
FMT_API void _fmt_bprint(fmt_buffer* buf, const char* fmt, ...) {
va_list args, args2;
va_start(args, fmt);
diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h
index 518320b5..63242a54 100644
--- a/include/stc/algo/crange.h
+++ b/include/stc/algo/crange.h
@@ -34,7 +34,7 @@ int main()
// use a temporary crange object.
int a = 100, b = INT32_MAX;
- c_FORFILTER (i, crange, crange_object(a, b, 8)
+ c_FORFILTER (i, crange, crange_literal(a, b, 8)
, i.index > 10
, c_FLT_TAKE(i, 3))
printf(" %lld", *i.ref);
@@ -46,7 +46,7 @@ int main()
#include <stc/ccommon.h>
-#define crange_object(...) \
+#define crange_literal(...) \
(*(crange[]){crange_make(__VA_ARGS__)})
typedef long long crange_value;
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index a2c529a7..edf4b2cb 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -38,6 +38,11 @@
#define c_ZU PRIu64
#define c_NPOS INT64_MAX
#endif
+#if defined STC_NDEBUG || defined NDEBUG
+ #define c_ASSERT(expr) (void)(expr)
+#else
+ #define c_ASSERT(expr) assert(expr)
+#endif
#if defined(_MSC_VER)
#pragma warning(disable: 4116 4996) // unnamed type definition in parentheses
diff --git a/include/stc/algo/cspan.h b/include/stc/cspan.h
index 03cb8622..f5114a11 100644
--- a/include/stc/algo/cspan.h
+++ b/include/stc/cspan.h
@@ -22,19 +22,21 @@
*/
/*
#include <stdio.h>
-#include <stc/algo/cspan.h>
-
-using_cspan(Span3f, float, 3);
+#include <stc/cspan.h>
+#include <stc/algo/filter.h>
+using_cspan(Span2f, float, 2);
+using_cspan(Intspan, int, 1);
int demo1() {
- float raw[3*4*5];
- Span3f span = cspan_make(raw, 3, 4, 5);
- *cspan_at(&span, 2, 3, 4) = 100;
+ float raw[4*5];
+ Span2f ms = cspan_make(raw, 4, 5);
- printf("%f\n", *cspan_at(&span, 2, 3, 4));
-}
+ for (size_t i=0; i<ms.dim[0]; i++)
+ for (size_t j=0; j<ms.dim[1]; j++)
+ *cspan_at(&ms, i, j) = i*1000 + j;
-using_cspan(Intspan, int, 1);
+ printf("%f\n", *cspan_at(&span, 3, 4));
+}
int demo2() {
int array[] = {1, 2, 3, 4, 5};
@@ -44,8 +46,8 @@ int demo2() {
printf(" %d", *i.ref);
puts("");
- // use a temporary IntSpan object.
- c_FORFILTER (i, Intspan, cspan_object(Intspan, {10, 20, 30, 23, 22, 21})
+ // use a temporary Intspan object.
+ c_FORFILTER (i, Intspan, cspan_literal(Intspan, {10, 20, 30, 23, 22, 21})
, c_FLT_SKIPWHILE(i, *i.ref < 25)
&& (*i.ref & 1) == 0 // even only
, c_FLT_TAKE(i, 2)) // break after 2
@@ -56,12 +58,12 @@ int demo2() {
#ifndef STC_CSPAN_H_INCLUDED
#define STC_CSPAN_H_INCLUDED
-#include "../ccommon.h"
+#include "ccommon.h"
#define using_cspan(Self, T, RANK) \
typedef T Self##_value, Self##_raw; \
- typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \
typedef struct { Self##_value *ref, *end; } Self##_iter; \
+ typedef struct { Self##_value *data; uint32_t dim[RANK]; } Self; \
\
STC_INLINE Self##_iter Self##_begin(const Self* self) { \
Self##_iter it = {self->data, self->data + cspan_size(self)}; \
@@ -75,26 +77,46 @@ int demo2() {
{ if (++it->ref == it->end) it->ref = NULL; } \
struct stc_nostruct
-#define cspan_assert(self, rank) c_STATIC_ASSERT(cspan_rank(self) == rank)
+#define using_cspan2(Self, T) using_cspan(Self##1, T, 1); using_cspan(Self##2, T, 2)
+#define using_cspan3(Self, T) using_cspan2(Self, T); using_cspan(Self##3, T, 3)
+#define using_cspan4(Self, T) using_cspan3(Self, T); using_cspan(Self##4, T, 4)
+
+#define cspan_check_rank(self, rank) c_STATIC_ASSERT(cspan_rank(self) == rank)
-#define cspan_object(S, ...) \
+#define cspan_literal(S, ...) \
((S){.data = (S##_value[])__VA_ARGS__, \
.dim = {sizeof((S##_value[])__VA_ARGS__)/sizeof(S##_value)}})
-#define cspan_make(data, ...) {data, {__VA_ARGS__}}
-#define cspan_from(data) \
- {data + c_STATIC_ASSERT(sizeof(data) != sizeof(void*)), {c_ARRAYLEN(data)}}
+#define cspan_make(data, ...) \
+ {data, {__VA_ARGS__}}
+
+/* create a cspan from a cvec, cstack, cdeq, cqueue, or cpque (heap) */
+#define cspan_from(container) \
+ {(container)->data, {(container)->_len}}
+
+#define cspan_from_array(array) \
+ {(array) + c_STATIC_ASSERT(sizeof(array) != sizeof(void*)), {c_ARRAYLEN(array)}}
#define cspan_size(self) _cspan_size((self)->dim, cspan_rank(self))
#define cspan_rank(self) c_ARRAYLEN((self)->dim)
+#define cspan_index(self, ...) \
+ c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, __VA_ARGS__) + \
+ cspan_check_rank(self, c_NUMARGS(__VA_ARGS__))
#define cspan_reshape(self, ...) \
- memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \
- sizeof((self)->dim) + cspan_assert(self, c_NUMARGS(__VA_ARGS__)))
+ (void)memcpy((self)->dim, (uint32_t[]){__VA_ARGS__}, \
+ sizeof((self)->dim) + cspan_check_rank(self, c_NUMARGS(__VA_ARGS__)))
+
+#define cspan_at(self, ...) ((self)->data + cspan_index(self, __VA_ARGS__))
-#define cspan_at(self, ...) \
- ((self)->data + c_PASTE(_cspan_i, c_NUMARGS(__VA_ARGS__))((self)->dim, __VA_ARGS__) \
- + cspan_assert(self, c_NUMARGS(__VA_ARGS__)))
+#define cspan_slice4(self, x0, width) \
+ {cspan_at(self, x0, 0, 0, 0), {width, (self)->dim[1], (self)->dim[2], (self)->dim[3]}}
+#define cspan_slice3(self, x0, width) \
+ {cspan_at(self, x0, 0, 0), {width, (self)->dim[1], (self)->dim[2]}}
+#define cspan_slice2(self, x0, width) \
+ {cspan_at(self, x0, 0), {width, (self)->dim[1]}}
+#define cspan_slice1(self, x0, width) \
+ {cspan_at(self, x0), {width}}
#define cspan_4to3(self, x) \
{cspan_at(self, x, 0, 0, 0), {(self)->dim[1], (self)->dim[2], (self)->dim[3]}}
@@ -109,18 +131,18 @@ int demo2() {
#define cspan_2to1(self, x) \
{cspan_at(self, x, 0), {(self)->dim[1]}}
-STC_INLINE uint32_t _cspan_i1(const uint32_t dim[1], uint32_t x)
- { assert(x < dim[0]); return x; }
+STC_INLINE size_t _cspan_i1(const uint32_t dim[1], uint32_t x)
+ { c_ASSERT(x < dim[0]); return x; }
-STC_INLINE uint32_t _cspan_i2(const uint32_t dim[2], uint32_t x, uint32_t y)
- { assert(x < dim[0] && y < dim[1]); return dim[1]*x + y; }
+STC_INLINE size_t _cspan_i2(const uint32_t dim[2], uint32_t x, uint32_t y)
+ { c_ASSERT(x < dim[0] && y < dim[1]); return dim[1]*x + y; }
-STC_INLINE uint32_t _cspan_i3(const uint32_t dim[3], uint32_t x, uint32_t y, uint32_t z) {
- assert(x < dim[0] && y < dim[1] && z < dim[2]);
+STC_INLINE size_t _cspan_i3(const uint32_t dim[3], uint32_t x, uint32_t y, uint32_t z) {
+ c_ASSERT(x < dim[0] && y < dim[1] && z < dim[2]);
return dim[2]*(dim[1]*x + y) + z;
}
-STC_INLINE uint32_t _cspan_i4(const uint32_t dim[4], uint32_t x, uint32_t y, uint32_t z, uint32_t w) {
- assert(x < dim[0] && y < dim[1] && z < dim[3] && w < dim[3]);
+STC_INLINE size_t _cspan_i4(const uint32_t dim[4], uint32_t x, uint32_t y, uint32_t z, uint32_t w) {
+ c_ASSERT(x < dim[0] && y < dim[1] && z < dim[3] && w < dim[3]);
return dim[3]*(dim[2]*(dim[1]*x + y) + z) + w;
}
STC_INLINE size_t _cspan_size(const uint32_t dim[], unsigned rank) {
diff --git a/misc/archived/carr2.h b/misc/archived/carr2.h
deleted file mode 100644
index c400b6d3..00000000
--- a/misc/archived/carr2.h
+++ /dev/null
@@ -1,152 +0,0 @@
-/* MIT License
- *
- * Copyright (c) 2023 Tyge Løvset
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#include <stc/ccommon.h>
-
-#ifndef CARR2_H_INCLUDED
-#define CARR2_H_INCLUDED
-#include <stc/forward.h>
-#include <stdlib.h>
-#endif
-/*
-// carr2- 2D dynamic array in one memory block with easy indexing.
-#define i_key int
-#include <stc/carr2.h>
-#include <stdio.h>
-
-int main() {
- int w = 7, h = 5;
- c_WITH (carr2_int image = carr2_int_new_uninit(w, h), carr2_int_drop(&image))
- {
- int *dat = carr2_int_data(&image);
- for (int i = 0; i < carr2_int_size(&image); ++i)
- dat[i] = i;
-
- for (int x = 0; x < image.xdim; ++x)
- for (int y = 0; y < image.ydim; ++y)
- printf(" %d", image.data[x][y]);
- puts("\n");
-
- c_FOREACH (i, carr2_int, image)
- printf(" %d", *i.ref);
- puts("");
- }
-}
-*/
-
-#ifndef _i_prefix
-#define _i_prefix carr2_
-#endif
-#include <stc/priv/template.h>
-#if !c_option(c_is_forward)
-_cx_deftypes(_c_carr2_types, _cx_self, i_key);
-#endif
-
-STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null);
-STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* storage);
-STC_API _cx_value* _cx_memb(_release)(_cx_self* self);
-STC_API void _cx_memb(_drop)(_cx_self* self);
-#if !defined i_no_clone
-STC_API _cx_self _cx_memb(_clone)(_cx_self src);
-STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other);
-#endif
-
-STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim) {
- return _cx_memb(_with_data)(xdim, ydim, c_ALLOC_N(_cx_value, xdim*ydim));
-}
-STC_INLINE size_t _cx_memb(_size)(const _cx_self* self)
- { return self->xdim*self->ydim; }
-
-STC_INLINE _cx_value *_cx_memb(_data)(_cx_self* self)
- { return *self->data; }
-
-STC_INLINE const _cx_value *_cx_memb(_at)(const _cx_self* self, size_t x, size_t y) {
- assert(x < self->xdim && y < self->ydim);
- return *self->data + self->ydim*x + y;
-}
-
-STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y) {
- return self->ydim*x + y;
-}
-
-
-STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) {
- size_t n = self->xdim*self->ydim;
- return c_INIT(_cx_iter){n ? *self->data : NULL, *self->data + n};
-}
-
-STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
- { return c_INIT(_cx_iter){NULL, *self->data + self->xdim*self->ydim}; }
-
-STC_INLINE void _cx_memb(_next)(_cx_iter* it)
- { if (++it->ref == it->end) it->ref = NULL; }
-
-/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement)
-
-STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, _cx_value* block) {
- _cx_self _arr = {c_ALLOC_N(_cx_value*, xdim), xdim, ydim};
- for (size_t x = 0; x < xdim; ++x, block += ydim)
- _arr.data[x] = block;
- return _arr;
-}
-
-STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, i_key null) {
- _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim);
- for (_cx_value* p = _arr.data[0], *e = p + xdim*ydim; p != e; ++p)
- *p = null;
- return _arr;
-}
-
-#if !defined i_no_clone
-
-STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) {
- _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim);
- for (_cx_value* p = _arr.data[0], *q = src.data[0], *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q)
- *p = i_keyclone((*q));
- return _arr;
-}
-
-STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) {
- if (self->data == other->data) return;
- _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other);
-}
-#endif
-
-STC_DEF _cx_value *_cx_memb(_release)(_cx_self* self) {
- _cx_value *values = self->data[0];
- c_FREE(self->data);
- self->data = NULL;
- return values;
-}
-
-STC_DEF void _cx_memb(_drop)(_cx_self* self) {
- if (!self->data) return;
- for (_cx_value* p = self->data[0], *q = p + _cx_memb(_size)(self); p != q; ) {
- --q; i_keydrop(q);
- }
- c_FREE(self->data[0]); /* values */
- c_FREE(self->data); /* pointers */
-}
-
-#endif
-#include <stc/priv/template.h>
diff --git a/misc/archived/carr3.h b/misc/archived/carr3.h
deleted file mode 100644
index 31ed2d9a..00000000
--- a/misc/archived/carr3.h
+++ /dev/null
@@ -1,157 +0,0 @@
-/* MIT License
- *
- * Copyright (c) 2023 Tyge Løvset
- *
- * Permission is hereby granted, free of charge, to any person obtaining a copy
- * of this software and associated documentation files (the "Software"), to deal
- * in the Software without restriction, including without limitation the rights
- * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
- * copies of the Software, and to permit persons to whom the Software is
- * furnished to do so, subject to the following conditions:
- *
- * The above copyright notice and this permission notice shall be included in all
- * copies or substantial portions of the Software.
- *
- * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
- * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
- * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
- * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
- * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
- * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
- * SOFTWARE.
- */
-#include <stc/ccommon.h>
-
-#ifndef CARR3_H_INCLUDED
-#define CARR3_H_INCLUDED
-#include <stc/forward.h>
-#include <stdlib.h>
-#endif
-/*
-// carr3 - 3D dynamic array in one memory block with easy indexing.
-#define i_key int
-#include <stc/carr3.h>
-#include <stdio.h>
-
-int main() {
- int w = 7, h = 5, d = 3;
- c_WITH (carr3_int image = carr3_int_new_uninit(w, h, d), carr3_int_drop(&image))
- {
- int *dat = carr3_int_data(&image);
- for (int i = 0; i < carr3_int_size(&image); ++i)
- dat[i] = i;
-
- for (int x = 0; x < image.xdim; ++x)
- for (int y = 0; y < image.ydim; ++y)
- for (int z = 0; z < image.zdim; ++z)
- printf(" %d", image.data[x][y][z]);
- puts("\n");
-
- c_FOREACH (i, carr3_int, image)
- printf(" %d", *i.ref);
- puts("");
- }
-}
-*/
-
-#ifndef _i_prefix
-#define _i_prefix carr3_
-#endif
-#include <stc/priv/template.h>
-
-#if !c_option(c_is_forward)
-_cx_deftypes(_c_carr3_types, _cx_self, i_key);
-#endif
-
-STC_API _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null);
-STC_API _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* storage);
-STC_API _cx_value* _cx_memb(_release)(_cx_self* self);
-STC_API void _cx_memb(_drop)(_cx_self* self);
-#if !defined i_no_clone
-STC_API _cx_self _cx_memb(_clone)(_cx_self src);
-STC_API void _cx_memb(_copy)(_cx_self *self, const _cx_self* other);
-#endif
-
-STC_INLINE _cx_self _cx_memb(_new_uninit)(size_t xdim, size_t ydim, size_t zdim) {
- return _cx_memb(_with_data)(xdim, ydim, zdim, c_ALLOC_N(_cx_value, xdim*ydim*zdim));
-}
-
-STC_INLINE size_t _cx_memb(_size)(const _cx_self* self)
- { return self->xdim*self->ydim*self->zdim; }
-
-STC_INLINE _cx_value* _cx_memb(_data)(_cx_self* self)
- { return **self->data; }
-
-STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t x, size_t y, size_t z) {
- assert(x < self->xdim && y < self->ydim && z < self->zdim);
- return **self->data + self->zdim*(self->ydim*x + y) + z;
-}
-
-STC_INLINE size_t _cx_memb(_idx)(const _cx_self* self, size_t x, size_t y, size_t z) {
- return self->zdim*(self->ydim*x + y) + z;
-}
-
-
-STC_INLINE _cx_iter _cx_memb(_begin)(const _cx_self* self) {
- size_t n = _cx_memb(_size)(self);
- return c_INIT(_cx_iter){n ? **self->data : NULL, **self->data + n};
-}
-
-STC_INLINE _cx_iter _cx_memb(_end)(const _cx_self* self)
- { return c_INIT(_cx_iter){NULL, **self->data + _cx_memb(_size)(self)}; }
-
-STC_INLINE void _cx_memb(_next)(_cx_iter* it)
- { if (++it->ref == it->end) it->ref = NULL; }
-
-/* -------------------------- IMPLEMENTATION ------------------------- */
-#if defined(i_implement)
-
-STC_DEF _cx_self _cx_memb(_with_data)(size_t xdim, size_t ydim, size_t zdim, _cx_value* block) {
- _cx_self _arr = {c_ALLOC_N(_cx_value**, xdim*(ydim + 1)), xdim, ydim, zdim};
- _cx_value** p = (_cx_value**) &_arr.data[xdim];
- for (size_t x = 0, y; x < xdim; ++x, p += ydim)
- for (y = 0, _arr.data[x] = p; y < ydim; ++y, block += zdim)
- p[y] = block;
- return _arr;
-}
-
-STC_DEF _cx_self _cx_memb(_with_size)(size_t xdim, size_t ydim, size_t zdim, i_key null) {
- _cx_self _arr = _cx_memb(_new_uninit)(xdim, ydim, zdim);
- for (_cx_value* p = **_arr.data, *e = p + xdim*ydim*zdim; p != e; ++p)
- *p = null;
- return _arr;
-}
-
-#if !defined i_no_clone
-
-STC_DEF _cx_self _cx_memb(_clone)(_cx_self src) {
- _cx_self _arr = _cx_memb(_new_uninit)(src.xdim, src.ydim, src.zdim);
- for (_cx_value* p = **_arr.data, *q = **src.data, *e = p + _cx_memb(_size)(&src); p != e; ++p, ++q)
- *p = i_keyclone((*q));
- return _arr;
-}
-
-STC_DEF void _cx_memb(_copy)(_cx_self *self, const _cx_self* other) {
- if (self->data == other->data) return;
- _cx_memb(_drop)(self); *self = _cx_memb(_clone)(*other);
-}
-#endif
-
-STC_DEF _cx_value* _cx_memb(_release)(_cx_self* self) {
- _cx_value *values = self->data[0][0];
- c_FREE(self->data);
- self->data = NULL;
- return values;
-}
-
-STC_DEF void _cx_memb(_drop)(_cx_self* self) {
- if (!self->data) return;
- for (_cx_value* p = **self->data, *q = p + _cx_memb(_size)(self); p != q; ) {
- --q; i_keydrop(q);
- }
- c_FREE(self->data[0][0]); /* data */
- c_FREE(self->data); /* pointers */
-}
-
-#endif
-#include <stc/priv/template.h>
diff --git a/misc/examples/forfilter.c b/misc/examples/forfilter.c
index bd3d346d..05d0bc28 100644
--- a/misc/examples/forfilter.c
+++ b/misc/examples/forfilter.c
@@ -65,7 +65,7 @@ void demo2(void)
c_AUTO (IVec, vector) {
puts("demo2:");
- c_FORFILTER (x, crange, crange_object(INT64_MAX)
+ c_FORFILTER (x, crange, crange_literal(INT64_MAX)
, c_FLT_SKIPWHILE(x, *x.ref != 11)
&& *x.ref % 2 != 0
, c_FLT_TAKE(x, 5))
diff --git a/misc/examples/multidim.c b/misc/examples/multidim.c
index 2cf16b7f..99310847 100644
--- a/misc/examples/multidim.c
+++ b/misc/examples/multidim.c
@@ -1,13 +1,15 @@
-// Example from https://en.cppreference.com/w/cpp/container/mdspan
+// Example based on https://en.cppreference.com/w/cpp/container/mdspan
#define i_val int
#include <stc/cstack.h>
-#include <stc/algo/cspan.h>
+#include <stc/cspan.h>
#include <stdio.h>
+using_cspan3(ispan, int);
+/*
using_cspan(ispan1, int, 1);
using_cspan(ispan2, int, 2);
using_cspan(ispan3, int, 3);
-
+*/
int main()
{
@@ -16,7 +18,7 @@ int main()
cstack_int_push(&v, *i.ref);
// View data as contiguous memory representing 12 ints
- ispan1 ms1 = cspan_make(v.data, 12);
+ ispan1 ms1 = cspan_from(&v);
// View data as contiguous memory representing 2 rows of 6 ints each
ispan2 ms2 = cspan_make(v.data, 2, 6);
// View the same data as a 3D array 2 x 3 x 2
@@ -27,12 +29,16 @@ int main()
for (unsigned j=0; j != ms2.dim[1]; j++)
*cspan_at(&ms2, i, j) = i*1000 + j;
- // print data using 1D view
+ // print all items using 1D view
+ printf("all: ");
for (unsigned i=0; i != ms1.dim[0]; i++)
printf(" %d", *cspan_at(&ms1, i));
puts("");
- c_FOREACH (i, ispan1, ms1)
+ // or iterate a subspan...
+ ispan2 sub = cspan_3to2(&ms3, 1);
+ printf("sub: ");
+ c_FOREACH (i, ispan2, sub)
printf(" %d", *i.ref);
puts("");
diff --git a/misc/examples/prime.c b/misc/examples/prime.c
index b4d81868..40ccc299 100644
--- a/misc/examples/prime.c
+++ b/misc/examples/prime.c
@@ -43,7 +43,7 @@ int main(void)
puts("");
puts("Show the last 50 primes using a temporary crange generator:");
- c_FORFILTER (i, crange, crange_object(n - 1, 0, -2)
+ c_FORFILTER (i, crange, crange_literal(n - 1, 0, -2)
, cbits_test(&primes, *i.ref>>1)
, c_FLT_TAKE(i, 50)) {
printf("%lld ", *i.ref);