summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--include/stc/ccommon.h10
-rw-r--r--include/stc/clist.h19
-rw-r--r--misc/examples/triples.c35
3 files changed, 42 insertions, 22 deletions
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index 33446982..ad6063e4 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -254,10 +254,12 @@ STC_INLINE char* cstrnstrn(const char *str, const char *needle,
#define c_eraseremove_if(it, C, cnt, pred) do { \
C* _cnt = &cnt; \
intptr_t _n = 0; \
- C##_iter _first = C##_begin(_cnt), it = _first; \
- for (; it.ref; C##_next(&it)) \
- if (pred) ++_n; \
- else C##_value_drop(_first.ref), *_first.ref = *it.ref, C##_next(&_first); \
+ C##_iter it = C##_begin(_cnt), _i; \
+ while (it.ref && !(pred)) \
+ C##_next(&it); \
+ for (_i = it; it.ref; C##_next(&it)) \
+ if (pred) C##_value_drop(it.ref), ++_n; \
+ else *_i.ref = *it.ref, C##_next(&_i); \
_cnt->_len -= _n; \
} while (0)
diff --git a/include/stc/clist.h b/include/stc/clist.h
index 3bab94de..eb72b888 100644
--- a/include/stc/clist.h
+++ b/include/stc/clist.h
@@ -99,10 +99,10 @@ STC_API _cx_iter _cx_memb(_find_in)(_cx_iter it1, _cx_iter it2, _cx_raw v
STC_API intptr_t _cx_memb(_remove)(_cx_self* self, _cx_raw val);
#endif
#ifndef i_no_cmp
-STC_API void _cx_memb(_sort_with)(_cx_self* self, int(*cmp)(const _cx_value*, const _cx_value*));
+STC_API bool _cx_memb(_sort_with)(_cx_self* self, int(*cmp)(const _cx_value*, const _cx_value*));
STC_API int _cx_memb(_sort_cmp_)(const _cx_value*, const _cx_value*);
-STC_INLINE void _cx_memb(_sort)(_cx_self* self)
- { _cx_memb(_sort_with)(self, _cx_memb(_sort_cmp_)); }
+STC_INLINE bool _cx_memb(_sort)(_cx_self* self)
+ { return _cx_memb(_sort_with)(self, _cx_memb(_sort_cmp_)); }
#endif
STC_API void _cx_memb(_reverse)(_cx_self* self);
STC_API _cx_iter _cx_memb(_splice)(_cx_self* self, _cx_iter it, _cx_self* other);
@@ -385,18 +385,21 @@ STC_DEF int _cx_memb(_sort_cmp_)(const _cx_value* x, const _cx_value* y) {
return i_cmp((&a), (&b));
}
-STC_DEF void _cx_memb(_sort_with)(_cx_self* self, int(*cmp)(const _cx_value*, const _cx_value*)) {
+STC_DEF bool _cx_memb(_sort_with)(_cx_self* self, int(*cmp)(const _cx_value*, const _cx_value*)) {
size_t len = 0, cap = 0;
- _cx_value *a = NULL, *p;
+ _cx_value *a = NULL, *p = NULL;
_cx_iter i;
for (i = _cx_memb(_begin)(self); i.ref; _cx_memb(_next)(&i)) {
- if (len == cap) a = (_cx_value *)i_realloc(a, (cap += cap/2 + 4)*sizeof *a);
+ if (len == cap) {
+ if ((p = (_cx_value *)i_realloc(a, (cap += cap/2 + 4)*sizeof *a))) a = p;
+ else { i_free(a); return false; }
+ }
a[len++] = *i.ref;
}
qsort(a, len, sizeof *a, (int(*)(const void*, const void*))cmp);
- for (i = _cx_memb(_begin)(self), p = a; i.ref; _cx_memb(_next)(&i), ++p)
+ for (i = _cx_memb(_begin)(self); i.ref; _cx_memb(_next)(&i), ++p)
*i.ref = *p;
- i_free(a);
+ i_free(a); return true;
}
#endif // !c_no_cmp
#endif // i_implement
diff --git a/misc/examples/triples.c b/misc/examples/triples.c
index 4783d603..aac7967a 100644
--- a/misc/examples/triples.c
+++ b/misc/examples/triples.c
@@ -4,12 +4,12 @@
#include <stdio.h>
void triples_vanilla(int n) {
- for (int i = 1, c = 1;; ++c) {
+ for (int i = 5, c = 1;; ++c) {
for (int a = 1; a < c; ++a) {
- for (int b = a; b < c; ++b) {
- if (a*a + b*b == c*c) {
- printf("{%d, %d, %d},\n", a, b, c);
- if (++i > n) goto done;
+ for (int b = a + 1; b < c; ++b) {
+ if ((int64_t)a*a + (int64_t)b*b == (int64_t)c*c) {
+ if (i++ == n) goto done;
+ printf("{%d, %d, %d}\n", a, b, c);
}
}
}
@@ -25,10 +25,10 @@ struct triples {
bool triples_next(struct triples* I) {
cco_begin(I);
- for (I->c = 1;; ++I->c) {
+ for (I->c = 5;; ++I->c) {
for (I->a = 1; I->a < I->c; ++I->a) {
- for (I->b = I->a; I->b < I->c; ++I->b) {
- if (I->a*I->a + I->b*I->b == I->c*I->c) {
+ for (I->b = I->a + 1; I->b < I->c; ++I->b) {
+ if ((int64_t)I->a*I->a + (int64_t)I->b*I->b == (int64_t)I->c*I->c) {
if (I->n-- == 0) cco_return;
cco_yield(true);
}
@@ -36,9 +36,18 @@ bool triples_next(struct triples* I) {
}
}
cco_final:
+ puts("done");
cco_end(false);
}
+int gcd(int a, int b) {
+ while (b) {
+ int t = a % b;
+ a = b;
+ b = t;
+ }
+ return a;
+}
int main()
{
@@ -47,8 +56,14 @@ int main()
puts("\nCoroutine triples:");
struct triples t = {INT32_MAX};
+ int n = 0;
+
while (triples_next(&t)) {
- if (t.c < 100) printf("{%d, %d, %d},\n", t.a, t.b, t.c);
- else cco_stop(&t);
+ if (gcd(t.a, t.b) > 1)
+ continue;
+ if (t.c < 1000)
+ printf("%d: {%d, %d, %d}\n", ++n, t.a, t.b, t.c);
+ else
+ cco_stop(&t);
}
}