summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-09-02 22:09:24 +0200
committerTyge Løvset <[email protected]>2022-09-02 22:09:24 +0200
commita2a18a52df4fc10ad453eba7cdfbe2d02a026f0b (patch)
tree40073f5fb52292a3965824e086eab379440386af
parent1fe32468be872bf99698bc899164d56d6b6c80c5 (diff)
downloadSTC-modified-a2a18a52df4fc10ad453eba7cdfbe2d02a026f0b.tar.gz
STC-modified-a2a18a52df4fc10ad453eba7cdfbe2d02a026f0b.zip
Removed cstack_X_push_back(), cstack_X_emplace_back(), cstack_X_pop_back().
Added a simple c_erase_if(). Removed deprecated c_apply() macro.
-rw-r--r--docs/ccommon_api.md20
-rw-r--r--docs/cstack_api.md3
-rw-r--r--examples/shape.c24
-rw-r--r--include/stc/ccommon.h17
-rw-r--r--include/stc/cstack.h6
5 files changed, 30 insertions, 40 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index d37d4d04..8a805cef 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -211,19 +211,21 @@ c_forrange (int, i, 30, 0, -5) printf(" %d", i);
// 30 25 20 15 10 5
```
-### c_find_if, c_find_in
-Search linearily in containers using a predicate
-```
-cvec_i_iter it, it1, it2;
-
+### c_find_if, c_find_in, c_erase_if
+Find or erase linearily in containers using a predicate
+```c
// Search vec for first value > 2:
-// NOTE: it.ref is NULL if not found
-c_find_if(it, cvec_i, vec, *it.ref > 2);
-if (it.ref) printf("%d\n", *it.ref);
+cvec_i_iter i;
+c_find_if(i, cvec_i, vec, *i.ref > 2);
+if (i.ref) printf("%d\n", *i.ref);
-// Search within a range:
+// Search map for a string containing "hello" and erase it:
+cmap_str_iter it, it1 = ..., it2 = ...;
c_find_in(it, csmap_str, it1, it2, cstr_contains(it.ref, "hello"));
if (it.ref) cmap_str_erase_at(&map, it);
+
+// Erase all numbers less than 100:
+c_erase_if(k, cvec_i, *k.ref < 100);
```
### c_new, c_alloc, c_alloc_n, c_drop, c_make
diff --git a/docs/cstack_api.md b/docs/cstack_api.md
index f148df38..2a61aecf 100644
--- a/docs/cstack_api.md
+++ b/docs/cstack_api.md
@@ -45,11 +45,8 @@ i_val* cstack_X_at_mut(cstack_X* self, size_t idx);
i_val* cstack_X_push(cstack_X* self, i_val value);
i_val* cstack_X_emplace(cstack_X* self, i_valraw raw);
-i_val* cstack_X_push_back(cstack_X* self, i_val value); // alias for push()
-i_val* cstack_X_emplace_back(cstack_X* self, i_valraw r); // alias for emplace()
void cstack_X_pop(cstack_X* self);
-void cstack_X_pop_back(cstack_X* self); // alias for pop()
cstack_X_iter cstack_X_begin(const cstack_X* self);
cstack_X_iter cstack_X_end(const cstack_X* self);
diff --git a/examples/shape.c b/examples/shape.c
index 4c2a7542..e72c8b7d 100644
--- a/examples/shape.c
+++ b/examples/shape.c
@@ -7,10 +7,6 @@
#define c_dyn_cast(T, s) \
(&T##_api == (s)->api ? (T*)(s) : (T*)0)
-#define c_vtable(Api, T, base) \
- c_static_assert(offsetof(T, base) == 0); \
- static Api T##_api
-
// Shape definition
// ============================================================
@@ -54,12 +50,13 @@ typedef struct {
Point p[3];
} Triangle;
-c_vtable(struct ShapeAPI, Triangle, shape);
+static struct ShapeAPI Triangle_api;
-Triangle* Triangle_new(Point a, Point b, Point c)
+Triangle Triangle_from(Point a, Point b, Point c)
{
- return c_new(Triangle, {{.api=&Triangle_api}, .p={a, b, c}});
+ Triangle t = {{.api=&Triangle_api}, .p={a, b, c}};
+ return t;
}
static void Triangle_draw(const Shape* shape)
@@ -88,12 +85,13 @@ typedef struct {
PointVec points;
} Polygon;
-c_vtable(struct ShapeAPI, Polygon, shape);
+static struct ShapeAPI Polygon_api;
-Polygon* Polygon_new(void)
+Polygon Polygon_init(void)
{
- return c_new(Polygon, {{.api=&Polygon_api}, .points=PointVec_init()});
+ Polygon p = {{.api=&Polygon_api}, .points=PointVec_init()};
+ return p;
}
void Polygon_addPoint(Polygon* self, Point p)
@@ -141,9 +139,9 @@ int main(void)
{
c_auto (Shapes, shapes)
{
- Triangle* tri1 = Triangle_new((Point){5, 7}, (Point){12, 7}, (Point){12, 20});
- Polygon* pol1 = Polygon_new();
- Polygon* pol2 = Polygon_new();
+ Triangle* tri1 = c_new(Triangle, Triangle_from((Point){5, 7}, (Point){12, 7}, (Point){12, 20}));
+ Polygon* pol1 = c_new(Polygon, Polygon_init());
+ Polygon* pol2 = c_new(Polygon, Polygon_init());
c_forarray (Point, p, {{50, 72}, {123, 73}, {127, 201}, {828, 333}})
Polygon_addPoint(pol1, *p);
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 58d74ddf..0c724d55 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -206,14 +206,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
*b = (n)*sizeof *b > (BYTES) ? c_alloc_n(type, n) : _c_b \
; b; b != _c_b ? c_free(b) : (void)0, b = NULL)
-// [deprecated] use c_forarray.
-#define c_apply(v, action, T, ...) do { \
- typedef T _T; \
- _T _arr[] = __VA_ARGS__, *v = _arr; \
- const _T *_end = v + c_arraylen(_arr); \
- while (v != _end) { action; ++v; } \
-} while (0)
-
#define c_forarray(T, v, ...) \
for (T _a[] = __VA_ARGS__, *v = _a; v != _a + c_arraylen(_a); ++v)
@@ -223,7 +215,6 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
#define c_pair(v) (v)->first, (v)->second
#define c_drop(C, ...) do { c_forarray_p(C*, _p, {__VA_ARGS__}) C##_drop(*_p); } while(0)
-// it.ref == NULL when not found:
#define c_find_if(it, C, cnt, pred) do { \
size_t index = 0; \
for (it = C##_begin(&cnt); it.ref && !(pred); C##_next(&it)) \
@@ -237,6 +228,14 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle,
++index; \
if (it.ref == _endref) it.ref = NULL; \
} while (0)
+
+#define c_erase_if(it, C, cnt, pred) do { \
+ C##_iter it = C##_begin(&cnt); \
+ while (it.ref) \
+ if (pred) it = C##_erase_at(&cnt, it); \
+ else C##_next(&it); \
+} while (0)
+
#endif // CCOMMON_H_INCLUDED
#undef STC_API
diff --git a/include/stc/cstack.h b/include/stc/cstack.h
index 9ed2beda..921e6618 100644
--- a/include/stc/cstack.h
+++ b/include/stc/cstack.h
@@ -133,13 +133,9 @@ STC_INLINE _cx_value* _cx_memb(_push)(_cx_self* self, _cx_value val) {
_cx_value* vp = self->data + self->size++;
*vp = val; return vp;
}
-STC_INLINE _cx_value* _cx_memb(_push_back)(_cx_self* self, _cx_value val)
- { return _cx_memb(_push)(self, val); }
STC_INLINE void _cx_memb(_pop)(_cx_self* self)
{ _cx_value* p = &self->data[--self->size]; i_keydrop(p); }
-STC_INLINE void _cx_memb(_pop_back)(_cx_self* self)
- { _cx_memb(_pop)(self); }
STC_INLINE const _cx_value* _cx_memb(_at)(const _cx_self* self, size_t idx)
{ assert(idx < self->size); return self->data + idx; }
@@ -149,8 +145,6 @@ STC_INLINE _cx_value* _cx_memb(_at_mut)(_cx_self* self, size_t idx)
#if !defined _i_no_emplace
STC_INLINE _cx_value* _cx_memb(_emplace)(_cx_self* self, _cx_raw raw)
{ return _cx_memb(_push)(self, i_keyfrom(raw)); }
-STC_INLINE _cx_value* _cx_memb(_emplace_back)(_cx_self* self, _cx_raw raw)
- { return _cx_memb(_push)(self, i_keyfrom(raw)); }
#endif // !_i_no_emplace
#if !defined _i_no_clone