summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-01-18 19:31:17 +0100
committerTyge Løvset <[email protected]>2021-01-18 19:31:17 +0100
commit21769ca6769b5e0e6bd45f29a8676aa4f8c02af2 (patch)
tree7682d8c72cefa3c55932a4bbfedb4c62d333e0bb
parentc44df963a425d054f3b86cbb4aca353be5f919ba (diff)
downloadSTC-modified-21769ca6769b5e0e6bd45f29a8676aa4f8c02af2.tar.gz
STC-modified-21769ca6769b5e0e6bd45f29a8676aa4f8c02af2.zip
Some improvements.
-rw-r--r--docs/csmap_api.md1
-rw-r--r--docs/csset_api.md3
-rw-r--r--examples/convert.c8
-rw-r--r--examples/csmap_ex.c4
-rw-r--r--stc/ccommon.h26
-rw-r--r--stc/cpque.h1
-rw-r--r--stc/csmap.h22
7 files changed, 36 insertions, 29 deletions
diff --git a/docs/csmap_api.md b/docs/csmap_api.md
index c4f6c0c4..b34bff37 100644
--- a/docs/csmap_api.md
+++ b/docs/csmap_api.md
@@ -82,6 +82,7 @@ size_t csmap_X_erase(csmap_X* self, RawKey rkey);
csmap_X_iter_t csmap_X_erase_at(csmap_X* self, csmap_X_iter_t pos);
csmap_X_value_t* csmap_X_find(const csmap_X* self, RawKey rkey);
+csmap_X_value_t* csmap_X_find_it(const csmap_X* self, RawKey rkey, csmap_X_iter_t* out);
bool csmap_X_contains(const csmap_X* self, RawKey rkey);
csmap_X_iter_t csmap_X_begin(csmap_X* self);
diff --git a/docs/csset_api.md b/docs/csset_api.md
index bbcdb65d..88b44168 100644
--- a/docs/csset_api.md
+++ b/docs/csset_api.md
@@ -60,7 +60,8 @@ csset_X_result_t csset_X_insert(csset_X* self, RawKey rkey);
size_t csset_X_erase(csset_X* self, RawKey rkey);
csset_X_iter_t csset_X_erase_at(csset_X* self, csset_X_iter_t pos);
-csset_X_value_t* csset_X_find(const csset_X* self, RawKey rkey, csset_X_iter_t* it);
+csset_X_value_t* csset_X_find(const csset_X* self, RawKey rkey);
+csset_X_value_t* csset_X_find_it(const csset_X* self, RawKey rkey, csset_X_iter_t* out);
bool csset_X_contains(const csset_X* self, RawKey rkey);
csset_X_iter_t csset_X_begin(csset_X* self);
diff --git a/examples/convert.c b/examples/convert.c
index c5724b54..55f7098f 100644
--- a/examples/convert.c
+++ b/examples/convert.c
@@ -27,18 +27,18 @@ int main()
printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
cvec_ss vec = cvec_ss_init();
- c_convert(cmap_str, map, cvec_ss, push_back, &vec);
+ c_convert(cmap_str, map, cvec_ss, &vec, push_back);
puts("\nvec_ss:");
c_foreach (i, cvec_ss, vec)
- printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
+ printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
clist_ss list = clist_ss_init();
- c_convert(cmap_str, map, clist_ss, push_back, &list);
+ c_convert(cmap_str, map, clist_ss, &list, push_back);
puts("\nclist_ss:");
c_foreach (i, clist_ss, list)
- printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
+ printf(" %s: %s\n", i.ref->first.str, i.ref->second.str);
c_del(cmap_str, &map, &clone);
cvec_ss_del(&vec);
diff --git a/examples/csmap_ex.c b/examples/csmap_ex.c
index 1fd14a09..aa78028b 100644
--- a/examples/csmap_ex.c
+++ b/examples/csmap_ex.c
@@ -21,7 +21,7 @@ int main(int argc, char **argv)
c_forrange (i, n) {
uint64_t val = stc64_random() & mask;
csmap_i_emplace(&map, val, i);
- if (!csmap_i_find(&map, val, &it)) {
+ if (!csmap_i_contains(&map, val)) {
printf("Not found: %zu, %zu: ", i, val);
}
}
@@ -37,7 +37,7 @@ int main(int argc, char **argv)
c_foreach (i, csmap_i, map)
printf("%2d %d: %zu\n", ++k, i.ref->first, i.ref->second);
- csmap_i_find(&map, val, &it);
+ csmap_i_find_it(&map, val, &it);
printf("\nmin/max: %d -- %d: found: %d. size: %zu\n", csmap_i_front(&map)->first,
csmap_i_back(&map)->first,
it.ref->first,
diff --git a/stc/ccommon.h b/stc/ccommon.h
index d9689875..b2a98d7e 100644
--- a/stc/ccommon.h
+++ b/stc/ccommon.h
@@ -76,6 +76,7 @@
#endif
#define c_swap(T, x, y) do { T __t = x; x = y; y = __t; } while (0)
+#define c_arraylen(a) (sizeof (a)/sizeof (a)[0])
#define c_default_compare(x, y) c_less_compare(c_default_less, x, y)
#define c_default_less(x, y) (*(x) < *(y))
@@ -107,37 +108,38 @@
#define c_forrange_5(i, type, start, stop, step) \
for (type i=start, i##_inc_=step, i##_end_=(stop) - (0 < i##_inc_); (i <= i##_end_) == (0 < i##_inc_); i += i##_inc_)
-#define c_break_with continue
#define c_withfile(f, open) for (FILE *f = open; f; fclose(f), f = NULL)
#define c_withbuffer(b, type, n) c_withbuffer_x(b, type, n, 256)
#define c_withbuffer_x(b, type, n, BYTES) \
for (type __b[((BYTES) - 1) / sizeof(type) + 1], \
*b = (n) * sizeof *b > (BYTES) ? c_new_2(type, n) : __b; \
b; b != __b ? c_free(b) : (void)0, b = NULL)
+#define c_breakwith continue
+
+#define c_init(ctype, c, ...) \
+ ctype c = ctype##_init(); c_push_items(&c, ctype, __VA_ARGS__)
#define c_push_items(self, ctype, ...) do { \
const ctype##_rawvalue_t __arr[] = __VA_ARGS__; \
ctype##_push_n(self, __arr, sizeof __arr/sizeof *__arr); \
} while (0)
-#define c_init(ctype, c, ...) \
- ctype c = ctype##_init(); c_push_items(&c, ctype, __VA_ARGS__)
-
-#define c_convert(ctype1, c1, ctype2, put, c2ref) do { \
- ctype2* __c2 = c2ref; \
+#define c_convert(ctype1, c1, ctype2, c2ptr, push) do { \
+ ctype2* __c2 = c2ptr; \
c_foreach_3 (__i, ctype1, c1) \
- ctype2##_##put(__c2, ctype2##_value_clone(*__i.ref)); \
+ ctype2##_##push(__c2, ctype2##_value_clone(*__i.ref)); \
} while (0)
-#define c_del(ctype, ...) do { \
- ctype##_t* __arr[] = {__VA_ARGS__}; \
- for (size_t __i=0; __i<sizeof __arr/sizeof *__arr; ++__i) \
- ctype##_del(__arr[__i]); \
-} while (0)
/* For cmap_X and csmap_X only: */
#define c_try_emplace(self, ctype, rkey, mapped) do { \
ctype##_result_t __r = ctype##_insert_key(self, rkey); \
if (__r.second) __r.first->second = mapped; \
} while (0)
+#define c_del(ctype, ...) do { \
+ ctype##_t* __arr[] = {__VA_ARGS__}; \
+ for (size_t __i=0; __i<sizeof __arr/sizeof *__arr; ++__i) \
+ ctype##_del(__arr[__i]); \
+} while (0)
+
#endif
diff --git a/stc/cpque.h b/stc/cpque.h
index fdee6b29..41113b7a 100644
--- a/stc/cpque.h
+++ b/stc/cpque.h
@@ -130,7 +130,6 @@
} \
STC_API void \
cpque_##X##_push_n(cpque_##X *self, const cpque_##X##_rawvalue_t arr[], size_t size) { \
- ctype##_reserve(self, cpque_##X##_size(*self) + size); \
for (size_t i=0; i<size; ++i) cpque_##X##_push(self, arr[i]); \
} \
\
diff --git a/stc/csmap.h b/stc/csmap.h
index 6279a30a..0d43eea1 100644
--- a/stc/csmap.h
+++ b/stc/csmap.h
@@ -203,12 +203,16 @@ int main(void) {
} \
\
STC_API C##_##X##_value_t* \
- C##_##X##_find(const C##_##X* self, RawKey rkey, C##_##X##_iter_t* it); \
+ C##_##X##_find_it(const C##_##X* self, RawKey rkey, C##_##X##_iter_t* out); \
\
+ STC_INLINE C##_##X##_value_t* \
+ C##_##X##_find(const C##_##X* self, RawKey rkey) { \
+ C##_##X##_iter_t it; \
+ return C##_##X##_find_it(self, rkey, &it); \
+ } \
STC_INLINE bool \
C##_##X##_contains(const C##_##X* self, RawKey rkey) { \
- C##_##X##_iter_t it; \
- return C##_##X##_find(self, rkey, &it) != NULL; \
+ return C##_##X##_find(self, rkey) != NULL; \
} \
\
STC_API C##_##X##_result_t \
@@ -250,7 +254,7 @@ int main(void) {
STC_INLINE C##_##X##_mapped_t* \
C##_##X##_at(const C##_##X* self, RawKey rkey) { \
C##_##X##_iter_t it; \
- return &C##_##X##_find(self, rkey, &it)->second; \
+ return &C##_##X##_find_it(self, rkey, &it)->second; \
}) \
\
STC_INLINE C##_##X##_value_t* \
@@ -310,18 +314,18 @@ static csmap___node_t cbst_nil = {&cbst_nil, &cbst_nil, 0};
keyFromRaw, keyToRaw, RawKey, mappedFromRaw, mappedToRaw, RawMapped) \
\
STC_DEF C##_##X##_value_t* \
- C##_##X##_find(const C##_##X* self, C##_##X##_rawkey_t rkey, C##_##X##_iter_t* it) { \
+ C##_##X##_find_it(const C##_##X* self, C##_##X##_rawkey_t rkey, C##_##X##_iter_t* out) { \
C##_##X##_node_t *tn = self->root; \
- it->_top = 0; \
+ out->_top = 0; \
while (tn->level) { \
C##_##X##_rawkey_t rx = keyToRaw(KEY_REF_##C(&tn->value)); \
switch (keyCompareRaw(&rx, &rkey)) { \
case -1: tn = tn->link[1]; break; \
- case 1: it->_st[it->_top++] = tn; tn = tn->link[0]; break; \
- case 0: it->ref = &tn->value; it->_tn = tn->link[1]; return it->ref; \
+ case 1: out->_st[out->_top++] = tn; tn = tn->link[0]; break; \
+ case 0: out->ref = &tn->value; out->_tn = tn->link[1]; return out->ref; \
} \
} \
- return (it->ref = NULL); \
+ return (out->ref = NULL); \
} \
\
STC_DEF void \