From 47de9987a623e8374915af40d67d72dcb56e4283 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Fri, 28 Aug 2020 16:08:08 +0200 Subject: Some smaller fixes and cleanup. --- examples/demos.c | 3 +-- examples/priority.c | 11 +++++++++-- examples/words.c | 12 ++++++++---- stc/cmap.h | 14 +++++++------- stc/cpqueue.h | 12 ++++++++---- stc/cvec.h | 4 +++- 6 files changed, 36 insertions(+), 20 deletions(-) diff --git a/examples/demos.c b/examples/demos.c index 4f5c2356..731197e3 100644 --- a/examples/demos.c +++ b/examples/demos.c @@ -55,8 +55,7 @@ void vectordemo1() -//declare_cvec(cs, cstr_t, cstr_destroy, cstr_compare); // supply inline destructor of values -declare_cvec_str(); // supply inline destructor of values +declare_cvec_str(); void vectordemo2() { diff --git a/examples/priority.c b/examples/priority.c index 780e5809..2d5c23bd 100644 --- a/examples/priority.c +++ b/examples/priority.c @@ -5,7 +5,7 @@ #include #include -declare_cvec(i, uint32_t); +declare_cvec(i, int64_t); declare_cvec_pqueue(i, >); // min-heap (increasing values) int main() { @@ -17,9 +17,16 @@ int main() { for (int i=0; i<10000000; ++i) cvec_i_pqueue_push(&heap, crandom_uniform_i32(&pcg, dist)); + // push some negative numbers too. + c_push(&heap, cvec_i_pqueue, c_items(-231, -32, -873, -4, -343)); + + for (int i=0; i<10000000; ++i) + cvec_i_pqueue_push(&heap, crandom_uniform_i32(&pcg, dist)); + + // Extract the hundred smallest. for (int i=0; i<100; ++i) { - printf("%u ", cvec_i_pqueue_top(&heap)); + printf("%d ", cvec_i_pqueue_top(&heap)); cvec_i_pqueue_pop(&heap); } cvec_i_destroy(&heap); diff --git a/examples/words.c b/examples/words.c index f8e86329..98134cf6 100644 --- a/examples/words.c +++ b/examples/words.c @@ -8,6 +8,7 @@ declare_cvec_str(); declare_clist_str(); declare_cmap_strkey(si, int); + int main1() { clist_str lwords = clist_init; @@ -15,6 +16,10 @@ int main1() "this", "sentence", "is", "not", "a", "sentence", "this", "sentence", "is", "a", "hoax" )); + clist_str_push_back_v(&lwords, cstr_from("%f", 123897.0 / 23.0)); + c_foreach (w, clist_str, lwords) + printf("%s\n", w.item->value.str); + puts(""); cvec_str words = cvec_init; c_push(&words, cvec_str, c_items( @@ -23,9 +28,8 @@ int main1() )); cmap_si word_map = cmap_init; - c_foreach (w, cvec_str, words) { + c_foreach (w, cvec_str, words) ++cmap_si_insert(&word_map, w.item->str, 0)->value; - } c_foreach (pair, cmap_si, word_map) { printf("%d occurrences of word '%s'\n", @@ -43,7 +47,7 @@ int main1() #include #include #include - + int main2() { std::vector words = { @@ -59,7 +63,7 @@ int main2() for (const auto &pair : word_map) { std::cout << pair.second << " occurrences of word '" - << pair.first << "'\n"; + << pair.first << "'\n"; } return 0; } diff --git a/stc/cmap.h b/stc/cmap.h index a7488edf..76edc94b 100644 --- a/stc/cmap.h +++ b/stc/cmap.h @@ -204,13 +204,6 @@ struct ctype##_##tag##_result {ctype##_##tag##_entry_t *entry; bool inserted;}; STC_API struct ctype##_##tag##_result \ ctype##_##tag##_insert_key(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey); \ \ -STC_INLINE ctype##_##tag##_entry_t* /* like c++ std::map.insert_or_assign(): */ \ -ctype##_##tag##_put(ctype##_##tag* self, CMAP_BOTH_##ctype(ctype##_##tag##_rawkey_t rawKey, RawValue rawValue)) { \ - struct ctype##_##tag##_result res = ctype##_##tag##_insert_key(self, rawKey); \ - CMAP_ONLY_##ctype( if (!res.inserted) valueDestroy(&res.entry->value); \ - res.entry->value = valueFromRaw(rawValue); ) \ - return res.entry; \ -} \ STC_INLINE ctype##_##tag##_entry_t* /* like c++ std::map.insert(): */ \ ctype##_##tag##_insert(ctype##_##tag* self, CMAP_BOTH_##ctype(ctype##_##tag##_rawkey_t rawKey, RawValue rawValue)) { \ struct ctype##_##tag##_result res = ctype##_##tag##_insert_key(self, rawKey); \ @@ -218,6 +211,13 @@ ctype##_##tag##_insert(ctype##_##tag* self, CMAP_BOTH_##ctype(ctype##_##tag##_ra return res.entry; \ } \ \ +STC_INLINE ctype##_##tag##_entry_t* /* like c++ std::map.insert_or_assign(): */ \ +ctype##_##tag##_put(ctype##_##tag* self, CMAP_BOTH_##ctype(ctype##_##tag##_rawkey_t rawKey, RawValue rawValue)) { \ + struct ctype##_##tag##_result res = ctype##_##tag##_insert_key(self, rawKey); \ + CMAP_ONLY_##ctype( if (!res.inserted) valueDestroy(&res.entry->value); \ + res.entry->value = valueFromRaw(rawValue); ) \ + return res.entry; \ +} \ CMAP_ONLY_##ctype( \ STC_INLINE ctype##_##tag##_entry_t* /* cmap_put_v(key, move(value)) */ \ ctype##_##tag##_put_v(ctype##_##tag* self, ctype##_##tag##_rawkey_t rawKey, Value value) { \ diff --git a/stc/cpqueue.h b/stc/cpqueue.h index b0dab7ed..ed66f3cb 100644 --- a/stc/cpqueue.h +++ b/stc/cpqueue.h @@ -61,9 +61,13 @@ cvec_##tag##_pqueue_top(cvec_##tag* self) {return self->data[0];} \ STC_INLINE void \ cvec_##tag##_pqueue_pop(cvec_##tag* self) {cvec_##tag##_pqueue_erase(self, 0);} \ STC_API void \ -cvec_##tag##_pqueue_push(cvec_##tag* self, cvec_##tag##_value_t value); \ +cvec_##tag##_pqueue_push_v(cvec_##tag* self, cvec_##tag##_value_t value); \ +STC_INLINE void \ +cvec_##tag##_pqueue_push(cvec_##tag* self, cvec_##tag##_rawvalue_t rawValue) { \ + cvec_##tag##_pqueue_push_v(self, cvec_##tag##_value_from_raw(rawValue)); \ +} \ STC_API void \ -cvec_##tag##_pqueue_push_n(cvec_##tag *self, const cvec_##tag##_value_t in[], size_t size); \ +cvec_##tag##_pqueue_push_n(cvec_##tag *self, const cvec_##tag##_input_t in[], size_t size); \ \ implement_cvec_pqueue(tag, cmpOpr) \ typedef cvec_##tag##_value_t cvec_##tag##_pqueue_input_t @@ -104,7 +108,7 @@ cvec_##tag##_pqueue_erase(cvec_##tag* self, size_t i) { \ } \ \ STC_API void \ -cvec_##tag##_pqueue_push(cvec_##tag* self, cvec_##tag##_value_t value) { \ +cvec_##tag##_pqueue_push_v(cvec_##tag* self, cvec_##tag##_value_t value) { \ cvec_##tag##_push_back(self, value); /* sift-up the value */ \ size_t n = cvec_size(*self), c = n; \ cvec_##tag##_value_t *arr = self->data - 1; \ @@ -113,7 +117,7 @@ cvec_##tag##_pqueue_push(cvec_##tag* self, cvec_##tag##_value_t value) { \ if (c != n) arr[c] = value; \ } \ STC_API void \ -cvec_##tag##_pqueue_push_n(cvec_##tag *self, const cvec_##tag##_value_t in[], size_t size) { \ +cvec_##tag##_pqueue_push_n(cvec_##tag *self, const cvec_##tag##_input_t in[], size_t size) { \ cvec_##tag##_reserve(self, cvec_size(*self) + size); \ for (size_t i=0; i