summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2022-05-06 17:42:58 +0200
committerTyge Løvset <[email protected]>2022-05-06 17:42:58 +0200
commitd9dc93a90e1ac0a42e946be52b169e1175377c4f (patch)
tree255b12b7313d1a85d0a2147f27404d59e40ae43c
parent9df37980d5801d4251db29fb33d8f8fa94570a2b (diff)
downloadSTC-modified-d9dc93a90e1ac0a42e946be52b169e1175377c4f.tar.gz
STC-modified-d9dc93a90e1ac0a42e946be52b169e1175377c4f.zip
Changed find_if() again. Now 3 iterator variants only.
-rw-r--r--docs/ccommon_api.md25
-rw-r--r--include/stc/ccommon.h22
2 files changed, 24 insertions, 23 deletions
diff --git a/docs/ccommon_api.md b/docs/ccommon_api.md
index ef0ae621..95f1c6bb 100644
--- a/docs/ccommon_api.md
+++ b/docs/ccommon_api.md
@@ -172,25 +172,30 @@ c_forrange (i, int, 30, 0, -5) printf(" %d", i);
```c
// apply multiple push_backs
c_apply(v, cvec_i_push_back(&vec, v), int, {1, 2, 3});
+
// inserts to existing map
c_apply(v, cmap_i_insert(&map, c_pair(v)), cmap_i_raw, { {4, 5}, {6, 7} });
int arr[] = {1, 2, 3};
c_apply_arr(v, cvec_i_push_back(&vec, v), int, arr, c_arraylen(arr));
```
-**c_find_if**, **c_find_it** searches linearily in containers using a predicate
+**c_find_if**, **c_find_in** searches linearily in containers using a predicate
```
-int* v;
-c_find_if (cvec_i, vec, v, *v == 2);
-if (v) printf("%d\n", *v);
+// NOTE: it.ref is NULL if not found, not cvec_i_end(&vec).ref
+// This makes it easier to test.
+cvec_i_iter it;
-c_find_if (cvec_i, vec, v, index == 2); // index is internal in find_if.
-if (v) printf("%d\n", *v); // 3
+// Search the the whole vec
+c_find_if(cvec_i, vec, it, *it.ref == 2);
+if (it.ref) printf("%d\n", *it.ref);
-// use iterator:
-cvec_i_iter it;
-c_find_it (cvec_i, vec, it, *it.ref == 2);
-cvec_i_erase_at(&vec, it); // assume found
+// Search from iter's current position
+c_find_from(cvec_i, vec, it, index == 2); // index is internal in find_if.
+if (it.ref) printf("%d\n", *it.ref); // 3
+
+// Search in the range
+c_find_in(csmap_str, it1, it2, it, cstr_contains(*it.ref, "hello"));
+cmap_str_erase_at(&map, it); // assume found
```
### c_new, c_alloc, c_alloc_n, c_drop, c_make
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h
index c00023ce..b0342928 100644
--- a/include/stc/ccommon.h
+++ b/include/stc/ccommon.h
@@ -149,11 +149,9 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, size_t slen, cons
}
#define c_foreach(...) c_MACRO_OVERLOAD(c_foreach, __VA_ARGS__)
-
#define c_foreach3(it, C, cnt) \
for (C##_iter it = C##_begin(&cnt), it##_end_ = C##_end(&cnt) \
; it.ref != it##_end_.ref; C##_next(&it))
-
#define c_foreach4(it, C, start, finish) \
for (C##_iter it = start, it##_end_ = finish \
; it.ref != it##_end_.ref; C##_next(&it))
@@ -213,19 +211,17 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, size_t slen, cons
} while (0)
#define c_pair(v) (v).first, (v).second
-#define c_find_it(C, cnt, it, pred) do { \
- size_t index = 0; \
- C##_iter _end = C##_end(&cnt); \
- for (it = C##_begin(&cnt); it.ref != _end.ref && !(pred); C##_next(&it)) \
- ++index; \
-} while (0)
-
-#define c_find_if(C, cnt, vp, pred) do { \
+#define c_find_if(C, cnt, it, pred) \
+ c_find_in(C, C##_begin(&cnt), C##_end(&cnt), it, pred)
+#define c_find_from(C, cnt, it, pred) \
+ c_find_in(C, it, C##_end(&cnt), it, pred)
+// NB: it.ref == NULL when not found, not end.ref:
+#define c_find_in(C, start, end, it, pred) do { \
size_t index = 0; \
- C##_iter _it = C##_begin(&cnt), _end = C##_end(&cnt); \
- for (; vp = _it.ref, vp != _end.ref && !(pred); C##_next(&_it)) \
+ C##_iter _end = end; \
+ for (it = start; it.ref != _end.ref && !(pred); C##_next(&it)) \
++index; \
- if (vp == _end.ref) vp = NULL; \
+ if (it.ref == _end.ref) it.ref = NULL; \
} while (0)
#define c_drop(C, ...) do { \