summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2023-02-07 15:02:39 +0100
committerTyge Løvset <[email protected]>2023-02-07 15:02:39 +0100
commitff52ba11700f446904f7d4c28a367cc692d3481b (patch)
treec35b4d756d4335c8804d72ff14269678a05ddff9
parentca54204557669fb54f43959594ee92109fcc75b6 (diff)
downloadSTC-modified-ff52ba11700f446904f7d4c28a367cc692d3481b.tar.gz
STC-modified-ff52ba11700f446904f7d4c28a367cc692d3481b.zip
crange and docs update.
-rw-r--r--docs/ccommon_api.md20
-rw-r--r--include/stc/algo/crange.h4
-rw-r--r--include/stc/ccommon.h2
3 files changed, 15 insertions, 11 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index 0a361d46..b0bd18e4 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -261,9 +261,9 @@ int main() {
```
Note that `c_flt_take()` is given as an optional argument, which makes the loop stop when it becomes false (for efficiency). Chaining it after `isPrime()` instead will give same result, but the full input is processed.
-### c_make, c_new
+### c_make, c_new, c_delete
-**c_make**: Make a container from a literal initializer list. Example:
+- **c_make**: Make a container from a literal initializer list. Example:
```c
#define i_val_str // cstr value type
#include <stc/cset.h>
@@ -277,16 +277,20 @@ int x = 7, y = 8;
cmap_int mymap = c_make(cmap_int, { {1, 2}, {3, 4}, {5, 6}, {x, y} });
```
-**c_new(Type)**: Allocate and init a new object on the heap
+- **c_new(Type)**: Allocate *and init* a new object on the heap
+- **c_delete(Type, ptr)**: Drop *and free* an object allocated on the heap
```c
-struct Pnt { double x, y, z; };
-struct Pnt *pnt = c_new(struct Pnt, {1.2, 3.4, 5.6});
-c_free(pnt);
+#include <stc/cstr.h>
+
+cstr *stringptr = c_new(cstr, cstr_from("Hello"));
+printf("%s\n", cstr_str(stringptr));
+c_delete(cstr, stringptr);
```
### crange
-**crange** is a number sequence generator type. The **crange_value** type is `long long`. Below *start*, *stop*, and *step* are of type *crange_value*:
+- **crange** is a number sequence generator type, similar to [boost::irange](https://www.boost.org/doc/libs/release/libs/range/doc/html/range/reference/ranges/irange.html). The **crange_value** type is `long long`. Below *start*, *stop*, and *step* are of type *crange_value*:
```c
+crange& crange_obj(...) // create a compound literal crange object
crange crange_make(stop); // will generate 0, 1, ..., stop-1
crange crange_make(start, stop); // will generate start, start+1, ... stop-1
crange crange_make(start, stop, step); // will generate start, start+step, ... upto-not-including stop
@@ -305,7 +309,7 @@ c_FORFILTER (i, crange, r1
// 2. The 11 first primes:
printf("2");
-c_FORFILTER (i, crange, crange_literal(3, INT64_MAX, 2)
+c_FORFILTER (i, crange, crange_obj(3, INT64_MAX, 2)
, isPrime(*i.ref)
, c_flt_take(10))
printf(" %lld", *i.ref);
diff --git a/include/stc/algo/crange.h b/include/stc/algo/crange.h
index 39236ae0..3993e615 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_literal(a, b, 8)
+ c_FORFILTER (i, crange, crange_obj(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_literal(...) \
+#define crange_obj(...) \
(*(crange[]){crange_make(__VA_ARGS__)})
typedef long long crange_value;
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index ddfb41b9..d7d2d804 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -78,10 +78,10 @@
#define c_calloc(n, sz) calloc(c_i2u(n), c_i2u(sz))
#define c_realloc(p, sz) realloc(p, c_i2u(sz))
#define c_free(p) free(p)
+#define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); free(_tp); } while (0)
#define c_static_assert(b) ((int)(0*sizeof(int[(b) ? 1 : -1])))
#define c_container_of(p, T, m) ((T*)((char*)(p) + 0*sizeof((p) == &((T*)0)->m) - offsetof(T, m)))
-#define c_delete(T, ptr) do { T *_tp = ptr; T##_drop(_tp); i_free(_tp); } while (0)
#define c_swap(T, xp, yp) do { T *_xp = xp, *_yp = yp, \
_tv = *_xp; *_xp = *_yp; *_yp = _tv; } while (0)
#define c_sizeof (intptr_t)sizeof