diff options
| author | Tyge Lovset <[email protected]> | 2022-08-11 23:33:14 +0200 |
|---|---|---|
| committer | Tyge Lovset <[email protected]> | 2022-08-11 23:33:14 +0200 |
| commit | f534db7ac4a993a05074868b8840a3a674ac76b4 (patch) | |
| tree | 7b5d6d011f2ec3896ef97b50a05adf797551b1cb | |
| parent | 2526ae23267a5381f0564100b2aba73dfc367b58 (diff) | |
| download | STC-modified-f534db7ac4a993a05074868b8840a3a674ac76b4.tar.gz STC-modified-f534db7ac4a993a05074868b8840a3a674ac76b4.zip | |
find_if, find_in changed.
| -rw-r--r-- | docs/ccommon_api.md | 9 | ||||
| -rw-r--r-- | examples/arc_containers.c | 2 | ||||
| -rw-r--r-- | examples/arcvec_erase.c | 7 | ||||
| -rw-r--r-- | examples/city.c | 9 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 17 |
5 files changed, 24 insertions, 20 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md index b4a90036..179e7398 100644 --- a/docs/ccommon_api.md +++ b/docs/ccommon_api.md @@ -209,16 +209,15 @@ c_forrange (int, i, 30, 0, -5) printf(" %d", i); ### c_find_if, c_find_in Search linearily in containers using a predicate ``` -// NOTE: it.ref is NULL if not found, not cvec_i_end(&vec).ref -// This makes it easier to test. -cvec_i_iter it; +cvec_i_iter it, it1, it2; // Search vec for first value > 2: -c_find_if(cvec_i, vec, it, *it.ref > 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); // Search within a range: -c_find_in(csmap_str, it1, it2, it, cstr_contains(*it.ref, "hello")); +c_find_in(it, csmap_str, it1, it2, cstr_contains(*it.ref, "hello")); if (it.ref) cmap_str_erase_at(&map, it); ``` diff --git a/examples/arc_containers.c b/examples/arc_containers.c index e8716129..debc6617 100644 --- a/examples/arc_containers.c +++ b/examples/arc_containers.c @@ -53,7 +53,7 @@ int main() // Clone (deep copy) a Map from the stack to the list // List will contain two shared and two unshared maps. - map = List_push_back(&list, Arc_make(Map_clone(*stack.data[1].get)))->get; + map = List_push_back(&list, Arc_from(Map_clone(*stack.data[1].get)))->get; // Add one more element to the cloned map: Map_emplace_or_assign(map, "CLONED", 2021); diff --git a/examples/arcvec_erase.c b/examples/arcvec_erase.c index eba77f51..b96f4278 100644 --- a/examples/arcvec_erase.c +++ b/examples/arcvec_erase.c @@ -20,13 +20,12 @@ int main() { c_auto (Vec, vec) { - const int v[] = {2012, 1990, 2012, 2019, 2015}; - c_forrange (i, c_arraylen(v)) - Vec_push_back(&vec, Arc_make(v[i])); + c_forarray (int, v, {2012, 1990, 2012, 2019, 2015}) + Vec_emplace(&vec, *v); // clone the second 2012 and push it back. // note: cloning make sure that vec.data[2] has ref count 2. - Vec_push_back(&vec, Arc_clone(vec.data[2])); + Vec_push(&vec, Arc_clone(vec.data[2])); printf("vec before erase :"); c_foreach (i, Vec, vec) diff --git a/examples/city.c b/examples/city.c index c22693f9..0fcee341 100644 --- a/examples/city.c +++ b/examples/city.c @@ -58,8 +58,9 @@ int main(void) {"Berlin", "Germany", 4.3, 23.2, 9000000}, {"London", "UK", 4.3, 23.2, 9000000}, }) { - Cities_push(&cities, CityArc_make((City){cstr_from(c->name), cstr_from(c->country), - c->lat, c->lon, c->pop})); + Cities_emplace(&cities, (City){cstr_from(c->name), + cstr_from(c->country), + c->lat, c->lon, c->pop}); } copy = Cities_clone(cities); // share each element! @@ -73,7 +74,9 @@ int main(void) printf("Vec:\n"); c_foreach (c, Cities, cities) - printf("city:%s, %d, use:%ld\n", cstr_str(&c.ref->get->name), c.ref->get->population, CityArc_use_count(*c.ref)); + printf("city:%s, %d, use:%ld\n", cstr_str(&c.ref->get->name), + c.ref->get->population, + CityArc_use_count(*c.ref)); printf("\nMap:\n"); c_forpair (id, city, CityMap, map) diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index 8ea58314..a4ae28aa 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -219,16 +219,19 @@ 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) -#define c_find_if(C, cnt, it, pred) \ - c_find_in(C, C##_begin(&cnt), C##_end(&cnt), it, pred) +// 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)) \ + ++index; \ +} while (0) -// NB: it.ref == NULL when not found, not end.ref: -#define c_find_in(C, start, end, it, pred) do { \ +#define c_find_in(it, C, start, end, pred) do { \ size_t index = 0; \ - C##_iter _end = end; \ - for (it = start; it.ref != _end.ref && !(pred); C##_next(&it)) \ + const C##_value* _endref = (end).ref; \ + for (it = start; it.ref != _endref && !(pred); C##_next(&it)) \ ++index; \ - if (it.ref == _end.ref) it.ref = NULL; \ + if (it.ref == _endref) it.ref = NULL; \ } while (0) #endif // CCOMMON_H_INCLUDED |
