summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-08-11 17:58:15 +0200
committerTyge Løvset <[email protected]>2022-08-11 17:58:15 +0200
commit5cf6b762012168be51b32a1a85ab2bc33504f020 (patch)
tree9fd59787a0d33d23196b77cecbd31ef5d44e103a
parent9831e8d6ee6772a4f9899cf9e3d36e3de47bbaf5 (diff)
downloadSTC-modified-5cf6b762012168be51b32a1a85ab2bc33504f020.tar.gz
STC-modified-5cf6b762012168be51b32a1a85ab2bc33504f020.zip
Fixed issue with cbox / carc. Minor update some examples.
-rw-r--r--docs/carc_api.md12
-rw-r--r--docs/cbox_api.md8
-rw-r--r--examples/music_arc.c37
-rw-r--r--examples/prime.c7
-rw-r--r--examples/rawptr_elements.c82
-rw-r--r--include/stc/carc.h17
-rw-r--r--include/stc/cbox.h11
-rw-r--r--include/stc/template.h5
8 files changed, 92 insertions, 87 deletions
diff --git a/docs/carc_api.md b/docs/carc_api.md
index 687e9547..243349a8 100644
--- a/docs/carc_api.md
+++ b/docs/carc_api.md
@@ -32,8 +32,8 @@ See similar c++ class [std::shared_ptr](https://en.cppreference.com/w/cpp/memory
## Methods
```c
carc_X carc_X_init(); // empty shared pointer
-carc_X carc_X_from(i_valraw raw); // construct a new value in an carc from raw type.
-carc_X carc_X_make(i_val val); // make a carc from constructed val object. Faster than from_ptr().
+carc_X carc_X_new(i_valraw raw); // construct a new value in an carc from raw type.
+carc_X carc_X_from(i_val val); // create a carc from constructed val object. Faster than from_ptr().
carc_X carc_X_from_ptr(i_val* p); // create a carc from raw pointer. Takes ownership of p.
carc_X carc_X_clone(carc_X other); // return other with increased use count
@@ -96,18 +96,18 @@ int main()
// POPULATE s1 with shared pointers to Map:
Map *map;
- map = Stack_push(&s1, Arc_make(Map_init()))->get; // push empty map to s1.
+ map = Stack_push(&s1, Arc_from(Map_init()))->get; // push empty map to s1.
c_forarray (Map_raw, v, { {"Joey", 1990}, {"Mary", 1995}, {"Joanna", 1992}}) {
Map_emplace(map, v->first, v->second); // populate it.
}
- map = Stack_push(&s1, Arc_make(Map_init()))->get;
+ map = Stack_push(&s1, Arc_from(Map_init()))->get;
c_forarray (Map_raw, v, { {"Rosanna", 2001}, {"Brad", 1999}, {"Jack", 1980} }) {
Map_emplace(map, v->first, v->second);
}
// POPULATE s2:
- map = Stack_push(&s2, Arc_make(Map_init()))->get;
+ map = Stack_push(&s2, Arc_from(Map_init()))->get;
c_forarray (Map_raw, v, { {"Steve", 1979}, {"Rick", 1974}, {"Tracy", 2003} }) {
Map_emplace(map, v->first, v->second);
}
@@ -118,7 +118,7 @@ int main()
// Deep-copy (does not share) a Map from s1 to s2.
// s2 will contain two shared and two unshared maps.
- map = Stack_push(&s2, Arc_make(Map_clone(*s1.data[1].get)))->get;
+ map = Stack_push(&s2, Arc_from(Map_clone(*s1.data[1].get)))->get;
// Add one more element to the cloned map:
Map_emplace_or_assign(map, "Cloned", 2022);
diff --git a/docs/cbox_api.md b/docs/cbox_api.md
index 1119d930..9801ad92 100644
--- a/docs/cbox_api.md
+++ b/docs/cbox_api.md
@@ -68,7 +68,7 @@ void int_drop(int* x) {
#define i_type IBox
#define i_val int
#define i_valdrop int_drop // optional func, just to display elements destroyed
-#define i_valclone(x) x // must specify because i_valdrop was defined.
+#define i_valclone(x) x // must specified when i_valdrop is defined.
#include <stc/cbox.h>
#define i_type ISet
@@ -84,10 +84,8 @@ int main()
c_auto (IVec, vec) // declare and init vec, call drop at scope exit
c_auto (ISet, set) // similar
{
- c_forarray (IBox, v, {
- IBox_make(2021), IBox_make(2012),
- IBox_make(2022), IBox_make(2015),
- }) IVec_push(&vec, *v);
+ c_forarray (int, v, {2021, 2012, 2022, 2015})
+ IVec_emplace(&vec, *v); // same as: IVec_push(&vec, IBox_from(*v));
printf("vec:");
c_foreach (i, IVec, vec)
diff --git a/examples/music_arc.c b/examples/music_arc.c
index ac730bc3..8cbcbb59 100644
--- a/examples/music_arc.c
+++ b/examples/music_arc.c
@@ -8,7 +8,7 @@ struct Song
cstr title;
} typedef Song;
-Song Song_new(const char* artist, const char* title)
+Song Song_from(const char* artist, const char* title)
{ return (Song){cstr_from(artist), cstr_from(title)}; }
void Song_drop(Song* s) {
@@ -16,39 +16,40 @@ void Song_drop(Song* s) {
c_drop(cstr, &s->artist, &s->title);
}
-#define i_type SongPtr
+#define i_type SongArc
#define i_val Song
#define i_valdrop Song_drop
#define i_opt c_no_cmp
#include <stc/carc.h>
#define i_type SongVec
-#define i_val_arcbox SongPtr
+#define i_val_arcbox SongArc
#include <stc/cvec.h>
void example3()
{
c_auto (SongVec, vec, vec2)
{
- c_forarray (SongPtr, v, {
- SongPtr_make(Song_new("Bob Dylan", "The Times They Are A Changing")),
- SongPtr_make(Song_new("Aretha Franklin", "Bridge Over Troubled Water")),
- SongPtr_make(Song_new("Thalia", "Entre El Mar y Una Estrella"))
+ c_forarray (SongArc, v, {
+ SongArc_make(Song_from("Bob Dylan", "The Times They Are A Changing")),
+ SongArc_make(Song_from("Aretha Franklin", "Bridge Over Troubled Water")),
+ SongArc_make(Song_from("Thalia", "Entre El Mar y Una Estrella"))
}) SongVec_push_back(&vec, *v);
c_foreach (s, SongVec, vec)
if (!cstr_equals(s.ref->get->artist, "Bob Dylan"))
- SongVec_push_back(&vec2, SongPtr_clone(*s.ref));
-
- c_forarray (SongPtr, v, {
- SongPtr_make(Song_new("Michael Jackson", "Billie Jean")),
- SongPtr_make(Song_new("Rihanna", "Stay")),
- }) SongVec_push_back(&vec2, *v);
-
- c_foreach (s, SongVec, vec2)
- printf("%s - %s: refs %lu\n", cstr_str(&s.ref->get->artist),
- cstr_str(&s.ref->get->title),
- *s.ref->use_count);
+ SongVec_push_back(&vec2, SongArc_clone(*s.ref));
+
+ SongVec_push_back(&vec2, SongArc_make(Song_from("Michael Jackson", "Billie Jean")));
+ SongVec_push_back(&vec2, SongArc_make(Song_from("Rihanna", "Stay")));
+
+ c_forarray (SongVec, v, {vec, vec2}) {
+ puts("VEC:");
+ c_foreach (s, SongVec, *v)
+ printf(" %s - %s, REFS: %lu\n", cstr_str(&s.ref->get->artist),
+ cstr_str(&s.ref->get->title),
+ *s.ref->use_count);
+ }
}
}
diff --git a/examples/prime.c b/examples/prime.c
index 01a6800b..85a66ee5 100644
--- a/examples/prime.c
+++ b/examples/prime.c
@@ -37,5 +37,12 @@ int main(void)
for (size_t i = 3; i < 1000; i += 2)
if (cbits_test(&primes, i>>1)) printf(" %" PRIuMAX "", i);
puts("");
+
+ int k = 20;
+ c_forrange (intptr_t, i, n-1, 1, -2) {
+ if (k == 0) break;
+ else if (cbits_test(&primes, i>>1)) printf("%" PRIdMAX "\n", i), k--;
+ }
+ puts("");
}
}
diff --git a/examples/rawptr_elements.c b/examples/rawptr_elements.c
index c3e3188d..bae314fd 100644
--- a/examples/rawptr_elements.c
+++ b/examples/rawptr_elements.c
@@ -1,67 +1,55 @@
#include <stc/ccommon.h>
#include <stdio.h>
-struct { double x, y; } typedef Point;
-
-// Set of Point pointers: define all template parameters "in-line"
-// Note it may be simpler to use a cbox for this.
-#define i_key Point*
-#define i_keydrop(x) c_free(*(x))
-#define i_keyclone(x) c_new(Point, *(x))
-#define i_hash(x) c_default_hash(*(x))
-#define i_cmp(x, y) memcmp(*(x), *(y), sizeof **(x)) // not good!
-#define i_tag pnt
-#include <stc/cset.h>
-
#include <stc/cstr.h>
-// Map of int64 pointers: Define i_valraw as int64_t for easy emplace calls!
+// Map of cstr => int64 pointers
typedef int64_t inttype;
+
+// Do it without cbox:
+#define i_type SIPtrMap
#define i_key_str
#define i_val inttype*
#define i_valraw inttype
-#define i_valfrom(raw) (puts("from"), c_new(inttype, raw))
-#define i_valto(x) (puts("to"), **(x))
-#define i_valclone c_derived_valclone // enables clone via valto+valfrom
-#define i_valdrop(x) c_free(*(x))
+#define i_valfrom(raw) c_new(inttype, raw)
+#define i_valto(x) **x
+#define i_valclone(x) c_new(inttype, *x)
+#define i_valdrop(x) c_free(*x)
#include <stc/cmap.h>
-int main()
-{
- c_auto (cset_pnt, set, cpy)
- {
- printf("Set with pointer elements:\n");
- // c++: set.insert(new Point{1.2, 3.4});
- cset_pnt_insert(&set, c_new(Point, {1.2, 3.4}));
- Point* q = *cset_pnt_insert(&set, c_new(Point, {6.1, 4.7})).ref;
- cset_pnt_insert(&set, c_new(Point, {5.7, 2.3}));
-
- cpy = cset_pnt_clone(set);
- cset_pnt_erase(&cpy, q);
+// With cbox:
+#define i_type IBox
+#define i_val int
+#include <stc/cbox.h> //<stc/carc.h>
- printf("set:");
- c_foreach (i, cset_pnt, set)
- printf(" (%g %g)", i.ref[0]->x, i.ref[0]->y);
-
- printf("\ncpy:");
- c_foreach (i, cset_pnt, cpy)
- printf(" (%g %g)", i.ref[0]->x, i.ref[0]->y);
- puts("");
- }
+#define i_type SIBoxMap
+#define i_key_str
+#define i_val_arcbox IBox
+#include <stc/cmap.h>
- c_auto (cmap_str, map, m2)
+int main()
+{
+ c_auto (SIPtrMap, map, m1)
+ c_auto (SIBoxMap, m2)
{
printf("\nMap with pointer elements:\n");
- cmap_str_insert(&map, cstr_new("testing"), c_new(inttype, 999));
- cmap_str_insert(&map, cstr_new("done"), c_new(inttype, 111));
+ SIPtrMap_insert(&map, cstr_from("testing"), c_new(inttype, 1));
+ SIPtrMap_insert(&map, cstr_from("done"), c_new(inttype, 2));
- // Emplace: implicit key, val construction using i_keyfrom/i_valfrom:
- cmap_str_emplace(&map, "hello", 200);
- cmap_str_emplace(&map, "goodbye", 400);
+ // Emplace: implicit key, val construction:
+ SIPtrMap_emplace(&map, "hello", 3);
+ SIPtrMap_emplace(&map, "goodbye", 4);
- // default uses i_valfrom+i_valto when no i_valclone defined:
- m2 = cmap_str_clone(map);
+ m1 = SIPtrMap_clone(map);
- c_forpair (name, number, cmap_str, m2)
+ c_forpair (name, number, SIPtrMap, m1)
printf("%s: %" PRIdMAX "\n", cstr_str(_.name), **_.number);
+
+
+ puts("\nIBox map:");
+ SIBoxMap_insert(&m2, cstr_from("Hello"), IBox_from(123));
+ SIBoxMap_emplace(&m2, "World", 999);
+ c_forpair (name, number, SIBoxMap, m2)
+ printf("%s: %d\n", cstr_str(_.name), *_.number->get);
+ puts("");
}
}
diff --git a/include/stc/carc.h b/include/stc/carc.h
index 57f00899..62b33bed 100644
--- a/include/stc/carc.h
+++ b/include/stc/carc.h
@@ -34,18 +34,18 @@ void Person_drop(Person* p) {
c_drop(cstr, &p->name, &p->last);
}
-#define i_tag person
+#define i_type ArcPers
#define i_key Person
#define i_keydrop Person_drop
#define i_opt c_no_cmp
#include <stc/carc.h>
int main() {
- carc_person p = carc_person_make(Person_new("John", "Smiths"));
- carc_person q = carc_person_clone(p); // share the pointer
+ ArcPers p = ArcPers_from(Person_new("John", "Smiths"));
+ ArcPers q = ArcPers_clone(p); // share the pointer
printf("%s %s. uses: %" PRIuMAX "\n", cstr_str(&q.get->name), cstr_str(&q.get->last), *q.use_count);
- c_drop(carc_person, &p, &q);
+ c_drop(ArcPers, &p, &q);
}
*/
#include "ccommon.h"
@@ -104,13 +104,16 @@ STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p) {
}
// c++: std::make_shared<_cx_value>(val)
-STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) {
+STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) {
_cx_self ptr;
_cx_carc_rep *rep = c_alloc(_cx_carc_rep);
*(ptr.use_count = &rep->counter) = 1;
*(ptr.get = &rep->value) = val;
return ptr;
}
+// [deprecated]
+STC_INLINE _cx_self _cx_memb(_make)(_cx_value val)
+ { return _cx_memb(_from)(val); }
STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
{ return i_keyto(self->get); }
@@ -144,8 +147,8 @@ STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) {
}
#if !defined _i_no_clone && !defined _i_no_emplace
- STC_INLINE _cx_self _cx_memb(_from)(_cx_raw raw)
- { return _cx_memb(_make)(i_keyfrom(raw)); }
+ STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw)
+ { return _cx_memb(_from)(i_keyfrom(raw)); }
#endif // !_i_no_clone
// does not use i_keyclone, so OK to always define.
diff --git a/include/stc/cbox.h b/include/stc/cbox.h
index c3a9dd02..467dd645 100644
--- a/include/stc/cbox.h
+++ b/include/stc/cbox.h
@@ -48,7 +48,7 @@ void Person_drop(Person* p) {
int main() {
c_auto (PBox, p, q)
{
- p = PBox_make(Person_from("John Smiths", "[email protected]"));
+ p = PBox_from(Person_from("John Smiths", "[email protected]"));
q = PBox_clone(p);
cstr_assign(&q.get->name, "Joe Smiths");
@@ -89,10 +89,13 @@ STC_INLINE _cx_self _cx_memb(_from_ptr)(_cx_value* p)
{ return c_make(_cx_self){p}; }
// c++: std::make_unique<i_key>(val)
-STC_INLINE _cx_self _cx_memb(_make)(_cx_value val) {
+STC_INLINE _cx_self _cx_memb(_from)(_cx_value val) {
_cx_self ptr = {c_alloc(_cx_value)};
*ptr.get = val; return ptr;
}
+// [deprecated]
+STC_INLINE _cx_self _cx_memb(_make)(_cx_value val)
+ { return _cx_memb(_from)(val); }
STC_INLINE _cx_raw _cx_memb(_toraw)(const _cx_self* self)
{ return i_keyto(self->get); }
@@ -128,8 +131,8 @@ STC_INLINE void _cx_memb(_reset_to)(_cx_self* self, _cx_value* p) {
#if !defined _i_no_clone
#if !defined _i_no_emplace
- STC_INLINE _cx_self _cx_memb(_from)(_cx_raw raw)
- { return _cx_memb(_make)(i_keyfrom(raw)); }
+ STC_INLINE _cx_self _cx_memb(_new)(_cx_raw raw)
+ { return _cx_memb(_from)(i_keyfrom(raw)); }
#endif
STC_INLINE _cx_self _cx_memb(_clone)(_cx_self other) {
if (!other.get)
diff --git a/include/stc/template.h b/include/stc/template.h
index ac9d5b5e..eeccc39e 100644
--- a/include/stc/template.h
+++ b/include/stc/template.h
@@ -109,6 +109,7 @@
#endif
#elif defined i_key_arcbox
#define i_key_bind i_key_arcbox
+ #define i_keyfrom c_paste(i_key_arcbox, _from)
#define i_keyraw c_paste(i_key_arcbox, _value)
#define i_keyto c_paste(i_key_arcbox, _toval)
#define i_eq c_paste(i_key_arcbox, _value_eq)
@@ -199,6 +200,7 @@
#define i_valto cstr_sv
#elif defined i_val_arcbox
#define i_val_bind i_val_arcbox
+ #define i_valfrom c_paste(i_val_arcbox, _from)
#define i_valraw c_paste(i_val_arcbox, _value)
#define i_valto c_paste(i_val_arcbox, _toval)
#endif
@@ -216,6 +218,9 @@
#endif
#endif
+#ifndef i_val
+ #error "i_val* must be defined for maps"
+#endif
#if defined i_valraw ^ defined i_valto
#error "both i_valto and i_valraw must be defined, if any"
#elif defined i_valfrom && !defined i_valraw