summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2020-08-28 16:08:08 +0200
committerTyge Løvset <[email protected]>2020-08-28 16:08:08 +0200
commit47de9987a623e8374915af40d67d72dcb56e4283 (patch)
tree44ad1af184a3121d87a3281ff04546452b927819
parentb29771837b5e37fc9175dc43ab7108d01aaaeba1 (diff)
downloadSTC-modified-47de9987a623e8374915af40d67d72dcb56e4283.tar.gz
STC-modified-47de9987a623e8374915af40d67d72dcb56e4283.zip
Some smaller fixes and cleanup.
-rw-r--r--examples/demos.c3
-rw-r--r--examples/priority.c11
-rw-r--r--examples/words.c12
-rw-r--r--stc/cmap.h14
-rw-r--r--stc/cpqueue.h12
-rw-r--r--stc/cvec.h4
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 <stc/cmap.h>
#include <stc/crandom.h>
-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 <iostream>
#include <vector>
#include <unordered_map>
-
+
int main2()
{
std::vector<std::string> 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<size; ++i) cvec_##tag##_pqueue_push(self, in[i]); \
}
diff --git a/stc/cvec.h b/stc/cvec.h
index c4d8796d..782354aa 100644
--- a/stc/cvec.h
+++ b/stc/cvec.h
@@ -53,12 +53,14 @@ typedef struct cvec_##tag { \
typedef RawValue cvec_##tag##_rawvalue_t; \
typedef cvec_##tag##_rawvalue_t cvec_##tag##_input_t; \
\
+STC_INLINE Value \
+cvec_##tag##_value_from_raw(cvec_##tag##_rawvalue_t rawValue) {return valueFromRaw(rawValue);} \
STC_API void \
cvec_##tag##_destroy(cvec_##tag* self); \
STC_API void \
cvec_##tag##_reserve(cvec_##tag* self, size_t cap); \
STC_API void \
-cvec_##tag##_resize(cvec_##tag* self, size_t size, Value null_val); \
+cvec_##tag##_resize(cvec_##tag* self, size_t size, Value fill_val); \
STC_API void \
cvec_##tag##_push_n(cvec_##tag *self, const cvec_##tag##_input_t in[], size_t size); \
STC_API void \