summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-04-26 20:42:11 +0200
committerTyge Løvset <[email protected]>2021-04-26 20:42:11 +0200
commit17f052c6d09f2453923937c5703e90f0d9b6ae18 (patch)
treed7957d7978d7b959bcb92d891c14f6dc4879b7f0
parente91e6bcd73c1e1ceb2d0b99ea0676c801b104e1d (diff)
downloadSTC-modified-17f052c6d09f2453923937c5703e90f0d9b6ae18.tar.gz
STC-modified-17f052c6d09f2453923937c5703e90f0d9b6ae18.zip
Doc update. Renamed c_new_2() to c_new_n()
-rw-r--r--benchmarks/others/carray_v1.h8
-rw-r--r--benchmarks/others/clist_v1.h2
-rw-r--r--benchmarks/others/csmap_v1.h4
-rw-r--r--docs/carray_api.md2
-rw-r--r--docs/ccommon_api.md23
-rw-r--r--stc/carray.h12
-rw-r--r--stc/ccommon.h35
-rw-r--r--stc/clist.h2
-rw-r--r--stc/cmap.h7
-rw-r--r--stc/csptr.h4
10 files changed, 53 insertions, 46 deletions
diff --git a/benchmarks/others/carray_v1.h b/benchmarks/others/carray_v1.h
index fdd69013..4b1c71db 100644
--- a/benchmarks/others/carray_v1.h
+++ b/benchmarks/others/carray_v1.h
@@ -77,7 +77,7 @@ int main()
\
STC_INLINE carray1##X \
carray1##X##_init(size_t xdim, Value val) { \
- carray1##X##_value_t* m = c_new_2(carray1##X##_value_t, xdim); \
+ carray1##X##_value_t* m = c_new_n(carray1##X##_value_t, xdim); \
for (size_t i=0; i<xdim; ++i) m[i] = val; \
carray1##X a = {m, xdim | _carray_OWN}; \
return a; \
@@ -85,7 +85,7 @@ int main()
STC_INLINE carray2##X \
carray2##X##_init(size_t ydim, size_t xdim, Value val) { \
const size_t n = ydim * xdim; \
- carray2##X##_value_t* m = c_new_2(carray2##X##_value_t, n); \
+ carray2##X##_value_t* m = c_new_n(carray2##X##_value_t, n); \
for (size_t i=0; i<n; ++i) m[i] = val; \
carray2##X a = {m, xdim | _carray_OWN, ydim}; \
return a; \
@@ -144,7 +144,7 @@ int main()
STC_INLINE carray3##X \
carray3##X##_init(size_t zdim, size_t ydim, size_t xdim, Value val) { \
const size_t n = zdim * ydim * xdim; \
- carray3##X##_value_t* m = c_new_2(carray3##X##_value_t, n); \
+ carray3##X##_value_t* m = c_new_n(carray3##X##_value_t, n); \
for (size_t i=0; i<n; ++i) m[i] = val; \
carray3##X a = {m, xdim | _carray_OWN, ydim, zdim}; \
return a; \
@@ -204,7 +204,7 @@ int main()
STC_INLINE carray##D##X \
carray##D##X##_clone(carray##D##X arr) { \
carray##D##X cp = arr; size_t k = 0; \
- cp.data = c_new_2(carray1##X##_value_t, carray##D##X##_size(arr)); \
+ cp.data = c_new_n(carray1##X##_value_t, carray##D##X##_size(arr)); \
c_foreach_3 (i, carray##D##X, arr) \
cp.data[k++] = valueClone(*i.ref); \
return cp; \
diff --git a/benchmarks/others/clist_v1.h b/benchmarks/others/clist_v1.h
index 42532612..9d832434 100644
--- a/benchmarks/others/clist_v1.h
+++ b/benchmarks/others/clist_v1.h
@@ -328,7 +328,7 @@ STC_API size_t _clist_count(const clist_VOID* self);
#define _c_clist_insert_after(self, CX, node, val) \
- CX##_node_t *entry = c_new_1 (CX##_node_t); \
+ CX##_node_t *entry = c_new (CX##_node_t); \
if (node) entry->next = node->next, node->next = entry; \
else entry->next = entry; \
entry->value = val
diff --git a/benchmarks/others/csmap_v1.h b/benchmarks/others/csmap_v1.h
index b81d7554..081886c7 100644
--- a/benchmarks/others/csmap_v1.h
+++ b/benchmarks/others/csmap_v1.h
@@ -388,7 +388,7 @@ int main(void) {
if ((c = keyCompareRaw(&r, rkey)) == 0) {res->ref = &it->value; return tn;} \
it = it->link[(dir = (c == -1))]; \
} \
- tn = c_new_1(C##X##_node_t); \
+ tn = c_new(C##X##_node_t); \
res->ref = &tn->value, res->inserted = true; \
tn->link[0] = tn->link[1] = (C##X##_node_t*) &aatree_nil, tn->level = 1; \
if (top == 0) return tn; \
@@ -448,7 +448,7 @@ int main(void) {
STC_DEF C##X##_node_t* \
C##X##_clone_r_(C##X##_node_t *tn) { \
if (! tn->level) return tn; \
- C##X##_node_t *cn = c_new_1(C##X##_node_t); \
+ C##X##_node_t *cn = c_new(C##X##_node_t); \
cn->link[0] = C##X##_clone_r_(tn->link[0]); \
cn->link[1] = C##X##_clone_r_(tn->link[1]); \
cn->level = tn->level; \
diff --git a/docs/carray_api.md b/docs/carray_api.md
index 5d260129..182ba22f 100644
--- a/docs/carray_api.md
+++ b/docs/carray_api.md
@@ -95,7 +95,7 @@ int main()
// Ex2
int w = 256, h = 128;
- carray2i image = carray2i_from(c_new(uint32_t, w*h), w, h); // no value init
+ carray2i image = carray2i_from(c_new_n(uint32_t, w*h), w, h); // no value init
int n = 0;
c_foreach (i, carray2i, image) {
uint32_t t = n++ % 256;
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index 891d0ec6..cabd2711 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -5,8 +5,8 @@ The following handy macros are safe to use, i.e. have no side-effects.
### c_init, c_emplace
**c_init** declares and initializes any container with an array of elements. **c_emplace** adds elements to any existing container:
```c
-c_init (cvec_i, vec, {1, 2, 3}); // declare and emplace
-c_emplace(cvec_i, vec, {4, 5, 6}); // adds to existing vec
+c_init(cvec_i, vec, {1, 2, 3}); // declare and emplace
+c_emplace(cvec_i, vec, {4, 5, 6}); // adds to existing vec
```
### c_forrange
@@ -15,7 +15,7 @@ Declare an iterator and specify a range to iterate with a for loop. Like python'
| Usage | Python equivalent |
|:----------------------------------------------|:-------------------------------------|
| `c_forrange (stop)` | `for _ in range(stop):` |
-| `c_forrange (i, stop) // IterType=size_t` | `for i in range(stop):` |
+| `c_forrange (i, stop) // IterType = size_t` | `for i in range(stop):` |
| `c_forrange (i, IterType, stop)` | `for i in range(stop):` |
| `c_forrange (i, IterType, start, stop)` | `for i in range(start, stop):` |
| `c_forrange (i, IterType, start, stop, step)` | `for i in range(start, stop, step):` |
@@ -49,8 +49,15 @@ c_foreach (i, csset_x, it, csset_x_end(&set)) printf(" %d", *i.ref);
// 7 12 23
```
-### c_withfile, c_breakwith
-Simplifies reading a file. Use only **c_breakwith** to break out of the block if needed. Example:
+### c_withfile, c_withbuffer, c_breakwith
+Simplifies reading a file. Use only **c_breakwith** to break out of the block if needed.
+**c_withbuffer** uses stack memory if buf is up to 256 bytes, and heap memory otherwise:
+
+| Usage | Description |
+|:-------------------------------|:----------------------------------|
+| `c_withfile (fp, fileopen)` | `Declare, open and close file` |
+| `c_withbuffer (buf, type, n)` | `Declare, allocate and free buf` |
+
```c
// Load each line of a text file into a vector of strings
#include <errno.h>
@@ -73,15 +80,15 @@ cvec_str readFile(const char* name) {
}
```
-### c_new, c_del
+### c_new, c_new_n, c_del
| Usage | Meaning |
|:-------------------------------|:----------------------------------------|
| `c_new (type)` | `(type *) c_malloc(sizeof(type))` |
-| `c_new (type, N)` | `(type *) c_malloc((N) * sizeof(type))` |
+| `c_new_n (type, N)` | `(type *) c_malloc((N)*sizeof(type))` |
| `c_del (ctype, c1, ..., cN)` | `ctype_del(c1); ... ctype_del(cN)` |
```c
-int* array = c_new (int, 100);
+int* array = c_new_n (int, 100);
c_free(array);
cstr a = cstr_from("Hello"), b = cstr_from("World");
diff --git a/stc/carray.h b/stc/carray.h
index 9c5b488e..b2409155 100644
--- a/stc/carray.h
+++ b/stc/carray.h
@@ -140,7 +140,7 @@ int main() {
\
STC_DEF CX CX##_from(CX##_value_t* block, size_t xdim, size_t ydim) { \
size_t n = xdim * ydim; \
- CX _arr = {c_new_2(CX##_value_t*, xdim), xdim, ydim}; \
+ CX _arr = {c_new_n(CX##_value_t*, xdim), xdim, ydim}; \
for (size_t x = 0; x < xdim; ++x, block += ydim) \
_arr.at[x] = block; \
return _arr; \
@@ -148,7 +148,7 @@ int main() {
\
STC_DEF CX CX##_init(size_t xdim, size_t ydim, Value value) { \
size_t n = xdim*ydim; \
- CX _arr = CX##_from(c_new_2(CX##_value_t, n), xdim, ydim); \
+ CX _arr = CX##_from(c_new_n(CX##_value_t, n), xdim, ydim); \
for (CX##_value_t* p = _arr.at[0], *e = p + n; p != e; ++p) \
*p = value; \
return _arr; \
@@ -156,7 +156,7 @@ int main() {
\
STC_DEF CX CX##_clone(CX src) { \
size_t n = src.xdim*src.ydim; \
- CX _arr = CX##_from(c_new_2(CX##_value_t, n), src.xdim, src.ydim); \
+ CX _arr = CX##_from(c_new_n(CX##_value_t, n), src.xdim, src.ydim); \
for (CX##_value_t* p = _arr.at[0], *q = src.at[0], *e = p + n; p != e; ++p, ++q) \
*p = valueClone(*q); \
return _arr; \
@@ -175,7 +175,7 @@ int main() {
#define _c_implement_carray3(CX, Value, valueDel, valueClone) \
\
STC_DEF CX CX##_from(CX##_value_t* block, size_t xdim, size_t ydim, size_t zdim) { \
- CX _arr = {c_new_2(CX##_value_t**, xdim*(ydim + 1)), xdim, ydim, zdim}; \
+ CX _arr = {c_new_n(CX##_value_t**, xdim*(ydim + 1)), xdim, ydim, zdim}; \
CX##_value_t** p = (CX##_value_t**) &_arr.at[xdim]; \
for (size_t x = 0, y; x < xdim; ++x, p += ydim) \
for (_arr.at[x] = p, y = 0; y < ydim; ++y, block += zdim) \
@@ -185,7 +185,7 @@ int main() {
\
STC_DEF CX CX##_init(size_t xdim, size_t ydim, size_t zdim, Value value) { \
size_t n = xdim*ydim*zdim; \
- CX _arr = CX##_from(c_new_2(CX##_value_t, n), xdim, ydim, zdim); \
+ CX _arr = CX##_from(c_new_n(CX##_value_t, n), xdim, ydim, zdim); \
for (CX##_value_t* p = **_arr.at, *e = p + n; p != e; ++p) \
*p = value; \
return _arr; \
@@ -193,7 +193,7 @@ int main() {
\
STC_DEF CX CX##_clone(CX src) { \
size_t n = CX##_size(src); \
- CX _arr = CX##_from(c_new_2(CX##_value_t, n), src.xdim, src.ydim, src.zdim); \
+ CX _arr = CX##_from(c_new_n(CX##_value_t, n), src.xdim, src.ydim, src.zdim); \
for (CX##_value_t* p = **_arr.at, *q = **src.at, *e = p + n; p != e; ++p, ++q) \
*p = valueClone(*q); \
return _arr; \
diff --git a/stc/ccommon.h b/stc/ccommon.h
index 684c0f90..49b74a92 100644
--- a/stc/ccommon.h
+++ b/stc/ccommon.h
@@ -66,9 +66,8 @@
((type *)((char *)(ptr) - offsetof(type, member)))
#define c_struct(S) typedef struct S S; struct S
-#define c_new(...) c_MACRO_OVERLOAD(c_new, __VA_ARGS__)
-#define c_new_1(T) ((T *) c_malloc(sizeof(T)))
-#define c_new_2(T, n) ((T *) c_malloc(sizeof(T)*(n)))
+#define c_new(T) ((T *) c_malloc(sizeof(T)))
+#define c_new_n(T, n) ((T *) c_malloc(sizeof(T)*(n)))
#ifndef c_malloc
#define c_malloc(sz) malloc(sz)
#define c_calloc(n, sz) calloc(n, sz)
@@ -96,12 +95,12 @@
/* Generic algorithms */
#define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__)
-#define c_foreach_3(it, ctype, cnt) \
- for (ctype##_iter_t it = ctype##_begin(&cnt), it##_end_ = ctype##_end(&cnt) \
- ; it.ref != it##_end_.ref; ctype##_next(&it))
-#define c_foreach_4(it, ctype, start, finish) \
- for (ctype##_iter_t it = start, it##_end_ = finish \
- ; it.ref != it##_end_.ref; ctype##_next(&it))
+#define c_foreach_3(it, CX, cnt) \
+ for (CX##_iter_t it = CX##_begin(&cnt), it##_end_ = CX##_end(&cnt) \
+ ; it.ref != it##_end_.ref; CX##_next(&it))
+#define c_foreach_4(it, CX, start, finish) \
+ for (CX##_iter_t it = start, it##_end_ = finish \
+ ; it.ref != it##_end_.ref; CX##_next(&it))
#define c_forrange(...) c_MACRO_OVERLOAD(c_forrange, __VA_ARGS__)
#define c_forrange_1(stop) for (size_t _c_i=0, _c_end_=stop; _c_i < _c_end_; ++_c_i)
@@ -116,22 +115,22 @@
#define c_withbuffer(b, type, n) c_withbuffer_x(b, type, n, 256)
#define c_withbuffer_x(b, type, n, BYTES) \
for (type _c_b[((BYTES) - 1) / sizeof(type) + 1], \
- *b = (n)*sizeof *b > (BYTES) ? c_new_2(type, n) : _c_b \
+ *b = (n)*sizeof *b > (BYTES) ? c_new_n(type, n) : _c_b \
; b; b != _c_b ? c_free(b) : (void)0, b = NULL)
#define c_breakwith continue
-#define c_init(ctype, c, ...) \
- ctype c = ctype##_init(); c_emplace(ctype, c, __VA_ARGS__)
+#define c_init(CX, c, ...) \
+ CX c = CX##_init(); c_emplace(CX, c, __VA_ARGS__)
-#define c_emplace(ctype, c, ...) do { \
- const ctype##_rawvalue_t _c_arr[] = __VA_ARGS__; \
- ctype##_emplace_n(&(c), _c_arr, c_arraylen(_c_arr)); \
+#define c_emplace(CX, c, ...) do { \
+ const CX##_rawvalue_t _c_arr[] = __VA_ARGS__; \
+ CX##_emplace_n(&(c), _c_arr, c_arraylen(_c_arr)); \
} while (0)
-#define c_del(ctype, ...) do { \
- ctype* _c_arr[] = {__VA_ARGS__}; \
+#define c_del(CX, ...) do { \
+ CX* _c_arr[] = {__VA_ARGS__}; \
for (size_t _c_i = 0; _c_i < c_arraylen(_c_arr); ++_c_i) \
- ctype##_del(_c_arr[_c_i]); \
+ CX##_del(_c_arr[_c_i]); \
} while (0)
#if defined(__SIZEOF_INT128__)
diff --git a/stc/clist.h b/stc/clist.h
index a3b836fb..1d10c807 100644
--- a/stc/clist.h
+++ b/stc/clist.h
@@ -315,7 +315,7 @@ STC_API size_t _clist_count(const clist_VOID* self);
#define _c_clist_insert_after(self, CX, node, val) \
- CX##_node_t *entry = c_new_1 (CX##_node_t); \
+ CX##_node_t *entry = c_new (CX##_node_t); \
if (node) entry->next = node->next, node->next = entry; \
else entry->next = entry; \
entry->value = val
diff --git a/stc/cmap.h b/stc/cmap.h
index 77311da6..1e1863eb 100644
--- a/stc/cmap.h
+++ b/stc/cmap.h
@@ -381,9 +381,10 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) \
STC_DEF CX \
CX##_clone(CX m) { \
CX clone = { \
- c_new_2(CX##_value_t, m.bucket_count), \
+ c_new_n(CX##_value_t, m.bucket_count), \
(uint8_t *) memcpy(c_malloc(m.bucket_count + 1), m._hashx, m.bucket_count + 1), \
- m.size, m.bucket_count, m.max_load_factor \
+ m.size, m.bucket_count, \
+ m.max_load_factor \
}; \
CX##_value_t *e = m.table, *end = e + m.bucket_count, *dst = clone.table; \
for (uint8_t *hx = m._hashx; e != end; ++hx, ++e, ++dst) \
@@ -397,7 +398,7 @@ STC_INLINE size_t fastrange_uint64_t(uint64_t x, uint64_t n) \
size_t oldcap = self->bucket_count; \
newcap = (size_t) (2 + newcap / self->max_load_factor) | 1; \
CX tmp = { \
- c_new_2 (CX##_value_t, newcap), \
+ c_new_n(CX##_value_t, newcap), \
(uint8_t *) c_calloc(newcap + 1, sizeof(uint8_t)), \
self->size, (CX##_size_t) newcap, \
self->max_load_factor \
diff --git a/stc/csptr.h b/stc/csptr.h
index d4f81e5d..435f8a35 100644
--- a/stc/csptr.h
+++ b/stc/csptr.h
@@ -105,13 +105,13 @@ typedef long atomic_count_t;
STC_INLINE CX \
CX##_from(CX##_value_t* p) { \
CX ptr = {p}; \
- if (p) *(ptr.use_count = c_new_1(atomic_count_t)) = 1; \
+ if (p) *(ptr.use_count = c_new(atomic_count_t)) = 1; \
return ptr; \
} \
\
STC_INLINE CX \
CX##_make(CX##_value_t val) { \
- CX ptr = {c_new_1(CX##_value_t), c_new_1(atomic_count_t)}; \
+ CX ptr = {c_new(CX##_value_t), c_new(atomic_count_t)}; \
*ptr.get = val, *ptr.use_count = 1; return ptr; \
} \
\