diff options
| author | Tyge Løvset <[email protected]> | 2021-10-23 12:29:43 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-10-23 12:29:43 +0200 |
| commit | 0f95c005891b0d4b3b2610fdba241cf78c372d81 (patch) | |
| tree | ebf3cebe3ef6dc66f820ab063b1cc3a3b7861a38 | |
| parent | 63a09f6492b022eccd1583fd9b69c450ae3a8935 (diff) | |
| download | STC-modified-0f95c005891b0d4b3b2610fdba241cf78c372d81.tar.gz STC-modified-0f95c005891b0d4b3b2610fdba241cf78c372d81.zip | |
Fixed bug in c_realloc() call ion cstack. Added assert in cmap_X_at(). Added a c++ example.
| -rw-r--r-- | examples/sidebyside.cpp | 46 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 6 | ||||
| -rw-r--r-- | include/stc/cmap.h | 1 | ||||
| -rw-r--r-- | include/stc/cstack.h | 5 |
4 files changed, 55 insertions, 3 deletions
diff --git a/examples/sidebyside.cpp b/examples/sidebyside.cpp new file mode 100644 index 00000000..0f645991 --- /dev/null +++ b/examples/sidebyside.cpp @@ -0,0 +1,46 @@ +#include <iostream>
+#include <map>
+#include <string>
+
+#define i_key_str
+#define i_val int
+#include <stc/cmap.h>
+
+#define i_key int
+#define i_val int
+#include <stc/cmap.h>
+
+int main() {
+ {
+ std::map<std::string, int> food = {{"burger", 5}, {"pizza", 12}, {"steak", 15}};
+ for (auto i: food)
+ std::cout << i.first << ", " << i.second << std::endl;
+ std::cout << std::endl;
+ }
+ c_auto (cmap_str, food)
+ {
+ c_apply_pair(cmap_str, emplace, &food, {{"burger", 5}, {"pizza", 12}, {"steak", 15}});
+ c_foreach (i, cmap_str, food)
+ printf("%s, %d\n", i.ref->first.str, i.ref->second);
+ puts("");
+ }
+
+ {
+ std::map<int, int> hist;
+ ++ hist.emplace(12, 100).first->second;
+ ++ hist.emplace(13, 100).first->second;
+ ++ hist.emplace(12, 100).first->second;
+ for (auto i: hist)
+ std::cout << i.first << ", " << i.second << std::endl;
+ std::cout << std::endl;
+ }
+ c_auto (cmap_int, hist)
+ {
+ ++ cmap_int_emplace(&hist, 12, 100).ref->second;
+ ++ cmap_int_emplace(&hist, 13, 100).ref->second;
+ ++ cmap_int_emplace(&hist, 12, 100).ref->second;
+ c_foreach (i, cmap_int, hist)
+ printf("%d, %d\n", i.ref->first, i.ref->second);
+ puts("");
+ }
+}
\ No newline at end of file diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 0157f967..264761e0 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -108,11 +108,13 @@ STC_INLINE cx_value_t* cx_memb(_emplace_front)(Self* self, i_valraw raw) { }
STC_INLINE void cx_memb(_pop_back)(Self* self) {
- cx_value_t* p = &self->data[--cdeq_rep_(self)->size]; i_valdel(p);
+ cx_value_t* p = &self->data[--cdeq_rep_(self)->size];
+ i_valdel(p);
}
STC_INLINE cx_value_t* cx_memb(_at)(const Self* self, size_t idx) {
- assert(idx < cdeq_rep_(self)->size); return self->data + idx;
+ assert(idx < cdeq_rep_(self)->size);
+ return self->data + idx;
}
STC_INLINE size_t cx_memb(_index)(Self cx, cx_iter_t it) {
diff --git a/include/stc/cmap.h b/include/stc/cmap.h index 5bfee69a..7e4e64eb 100644 --- a/include/stc/cmap.h +++ b/include/stc/cmap.h @@ -120,6 +120,7 @@ cx_MAP_ONLY( STC_INLINE cx_mapped_t*
cx_memb(_at)(const Self* self, i_keyraw rkey) {
chash_bucket_t b = cx_memb(_bucket_)(self, &rkey);
+ assert(self->_hashx[b.idx]);
return &self->table[b.idx].second;
}
)
diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 3283eaf0..e461ca4b 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -78,7 +78,7 @@ STC_INLINE void cx_memb(_pop)(Self* self) STC_INLINE void cx_memb(_reserve)(Self* self, size_t n) {
if (n >= self->size)
- self->data = (cx_value_t *)c_realloc(self->data, (self->capacity = n)*sizeof(cx_rawvalue_t));
+ self->data = (cx_value_t *)c_realloc(self->data, (self->capacity = n)*sizeof(cx_value_t));
}
STC_INLINE void cx_memb(_shrink_to_fit)(Self* self)
@@ -93,6 +93,9 @@ STC_INLINE cx_value_t* cx_memb(_push)(Self* self, cx_value_t val) { STC_INLINE cx_value_t* cx_memb(_emplace)(Self* self, cx_rawvalue_t raw)
{ return cx_memb(_push)(self, i_valfrom(raw)); }
+STC_INLINE cx_value_t* cx_memb(_at)(const Self* self, size_t idx)
+ { assert(idx < self->size); return self->data + idx; }
+
STC_INLINE Self cx_memb(_clone)(Self v) {
Self out = {(cx_value_t *) c_malloc(v.size*sizeof(cx_value_t)), v.size, v.size};
for (size_t i = 0; i < v.size; ++i, ++v.data) out.data[i] = i_valfrom(i_valto(v.data));
|
