diff options
| -rw-r--r-- | benchmarks/others/old/carray_v1.h (renamed from benchmarks/others/carray_v1.h) | 0 | ||||
| -rw-r--r-- | benchmarks/others/old/sstr.h (renamed from benchmarks/others/sstr.h) | 0 | ||||
| -rw-r--r-- | docs/cdeq_api.md | 1 | ||||
| -rw-r--r-- | docs/cstack_api.md | 4 | ||||
| -rw-r--r-- | docs/cvec_api.md | 1 | ||||
| -rw-r--r-- | examples/runall.sh | 2 | ||||
| -rw-r--r-- | examples/sptr_pthread.c | 61 | ||||
| -rw-r--r-- | examples/unordered_map.c | 64 | ||||
| -rw-r--r-- | examples/unordered_set.c | 41 | ||||
| -rw-r--r-- | include/stc/cdeq.h | 4 | ||||
| -rw-r--r-- | include/stc/cpque.h | 2 | ||||
| -rw-r--r-- | include/stc/cvec.h | 5 |
12 files changed, 173 insertions, 12 deletions
diff --git a/benchmarks/others/carray_v1.h b/benchmarks/others/old/carray_v1.h index 65777416..65777416 100644 --- a/benchmarks/others/carray_v1.h +++ b/benchmarks/others/old/carray_v1.h diff --git a/benchmarks/others/sstr.h b/benchmarks/others/old/sstr.h index 5301978a..5301978a 100644 --- a/benchmarks/others/sstr.h +++ b/benchmarks/others/old/sstr.h diff --git a/docs/cdeq_api.md b/docs/cdeq_api.md index 90a44293..4f188143 100644 --- a/docs/cdeq_api.md +++ b/docs/cdeq_api.md @@ -62,7 +62,6 @@ cdeq_X_iter_t cdeq_X_emplace_range(cdeq_X* self, cdeq_X_iter_t it, cdeq_X_iter_t cdeq_X_emplace_range_p(cdeq_X* self, i_val* pos, const i_val* p1, const i_val* p2); -cdeq_X_iter_t cdeq_X_erase(cdeq_X* self, size_t idx); cdeq_X_iter_t cdeq_X_erase_n(cdeq_X* self, size_t idx, size_t n); cdeq_X_iter_t cdeq_X_erase_at(cdeq_X* self, cdeq_X_iter_t it); cdeq_X_iter_t cdeq_X_erase_range(cdeq_X* self, cdeq_X_iter_t it1, cdeq_X_iter_t it2); diff --git a/docs/cstack_api.md b/docs/cstack_api.md index 5b878ba4..c4a806e1 100644 --- a/docs/cstack_api.md +++ b/docs/cstack_api.md @@ -23,6 +23,8 @@ See the c++ class [std::stack](https://en.cppreference.com/w/cpp/container/stack ```c cstack_X cstack_X_init(void); +cstack_X cstack_with_capacity(size_t cap); +cstack_X cstack_with_size(size_t size, i_val fill); cstack_X cstack_X_clone(cstack_X st); void cstack_X_clear(cstack_X* self); @@ -32,8 +34,10 @@ void cstack_X_copy(cstack_X* self, cstack_X other); void cstack_X_del(cstack_X* self); // destructor size_t cstack_X_size(cstack_X st); +size_t cstack_X_capacity(cstack_X st); bool cstack_X_empty(cstack_X st); cstack_X_value_t* cstack_X_top(const cstack_X* self); +cstack_X_value_t* cstack_X_at(const cstack_X* self, size_t idx); cstack_X_value_t* cstack_X_push(cstack_X* self, i_val value); cstack_X_value_t* cstack_X_emplace(cstack_X* self, i_valraw raw); diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 3a78bf58..a14f6952 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -65,7 +65,6 @@ cvec_X_iter_t cvec_X_emplace_range(cvec_X* self, cvec_X_iter_t it, cvec_X_iter_t cvec_X_emplace_range_p(cvec_X* self, i_val* pos, const i_val* p1, const i_val* p2); -cvec_X_iter_t cvec_X_erase(cvec_X* self, size_t idx); cvec_X_iter_t cvec_X_erase_n(cvec_X* self, size_t idx, size_t n); cvec_X_iter_t cvec_X_erase_at(cvec_X* self, cvec_X_iter_t it); cvec_X_iter_t cvec_X_erase_range(cvec_X* self, cvec_X_iter_t it1, cvec_X_iter_t it2); diff --git a/examples/runall.sh b/examples/runall.sh index cec626a6..12c72684 100644 --- a/examples/runall.sh +++ b/examples/runall.sh @@ -8,7 +8,7 @@ cc='gcc -std=c99 -pedantic' libs='' run=0 if [ -z "$OS" ]; then - libs='-lm' + libs='-lm -pthread' fi if [ "$1" == '-h' -o "$1" == '--help' ]; then echo usage: runall.sh [-run] [compiler + options] diff --git a/examples/sptr_pthread.c b/examples/sptr_pthread.c new file mode 100644 index 00000000..8957a118 --- /dev/null +++ b/examples/sptr_pthread.c @@ -0,0 +1,61 @@ +// example based on https://en.cppreference.com/w/cpp/memory/shared_ptr
+#if defined __GNUC__ || defined __linux__
+
+#include <stdio.h>
+#include <unistd.h>
+#include <pthread.h>
+#include <time.h>
+
+struct Base
+{
+ int value;
+} typedef Base;
+
+void Base_del(Base* b) { printf("Base::~Base()\n"); }
+
+#define i_val Base
+#define i_cmp c_no_compare
+#define i_del Base_del
+#define i_tag base
+#include <stc/csptr.h>
+
+void* thr(csptr_base* lp)
+{
+ static pthread_mutex_t mtx = PTHREAD_MUTEX_INITIALIZER;
+ sleep(1);
+ c_autoscope (pthread_mutex_lock(&mtx), pthread_mutex_unlock(&mtx))
+ {
+ printf("local pointer in a thread:\n"
+ " p.get() = %p, p.use_count() = %zu\n", lp->get, *lp->use_count);
+ }
+ /* atomically decrease ref. */
+ csptr_base_del(lp);
+ return NULL;
+}
+
+int main()
+{
+ csptr_base p = csptr_base_make((Base){42});
+
+ printf("Created a Base\n"
+ " p.get() = %p, p.use_count() = %zu\n", p.get, *p.use_count);
+ enum {N = 3};
+ pthread_t t[N];
+ csptr_base c[N];
+ c_forrange (i, N) {
+ c[i] = csptr_base_clone(p);
+ pthread_create(&t[i], NULL, (void*(*)(void*))thr, &c[i]);
+ }
+
+ printf("Shared ownership between %d threads and released\n"
+ "ownership from main:\n"
+ " p.get() = %p, p.use_count() = %zu\n", N, p.get, *p.use_count);
+ csptr_base_reset(&p);
+
+ c_forrange (i, N) pthread_join(t[i], NULL);
+ printf("All threads completed, the last one deleted Base\n");
+}
+
+#else
+int main() {}
+#endif
\ No newline at end of file diff --git a/examples/unordered_map.c b/examples/unordered_map.c new file mode 100644 index 00000000..7af6fa0a --- /dev/null +++ b/examples/unordered_map.c @@ -0,0 +1,64 @@ +// https://iq.opengenus.org/containers-cpp-stl/
+
+#define i_key int
+#define i_val int
+#include <stc/csmap.h>
+#include <stdio.h>
+
+int main()
+{
+
+ // empty map containers
+ c_auto (csmap_int, gquiz1, gquiz2)
+ {
+ // insert elements in random order
+ csmap_int_insert(&gquiz1, 2, 30);
+ csmap_int_insert(&gquiz1, 4, 20);
+ csmap_int_insert(&gquiz1, 7, 10);
+ csmap_int_insert(&gquiz1, 5, 50);
+ csmap_int_insert(&gquiz1, 3, 60);
+ csmap_int_insert(&gquiz1, 1, 40);
+ csmap_int_insert(&gquiz1, 6, 50);
+
+ // printing map gquiz1
+ printf("\nThe map gquiz1 is :\n\tKEY\tELEMENT\n");
+ c_foreach (itr, csmap_int, gquiz1)
+ printf("\t%d\t%d\n", itr.ref->first, itr.ref->second);
+ printf("\n");
+
+ // assigning the elements from gquiz1 to gquiz2
+ c_foreach (i, csmap_int, gquiz1)
+ csmap_int_insert(&gquiz2, i.ref->first, i.ref->second);
+
+ // print all elements of the map gquiz2
+ printf("\nThe map gquiz2 is :\n\tKEY\tELEMENT\n");
+ c_foreach (itr, csmap_int, gquiz2)
+ printf("\t%d\t%d\n", itr.ref->first, itr.ref->second);
+ printf("\n");
+
+ // remove all elements up to element with key=3 in gquiz2
+ printf("\ngquiz2 after removal of elements less than key=3 :\n");
+ printf("\tKEY\tELEMENT\n");
+ csmap_int_erase_range(&gquiz2, csmap_int_begin(&gquiz2),
+ csmap_int_find(&gquiz2, 3));
+ c_foreach (itr, csmap_int, gquiz2)
+ printf("\t%d\t%d\n", itr.ref->first, itr.ref->second);
+ printf("\n");
+
+ // remove all elements with key = 4
+ int num = csmap_int_erase(&gquiz2, 4);
+ printf("\ngquiz2.erase(4) : %d removed\n", num);
+ printf("\tKEY\tELEMENT\n");
+ c_foreach (itr, csmap_int, gquiz2)
+ printf("\t%d\t%d\n", itr.ref->first, itr.ref->second);
+ printf("\n");
+
+ // lower bound and upper bound for map gquiz1 key = 5
+ printf("gquiz1.lower_bound(5) : ");
+ printf("\tKEY = %d\t", csmap_int_lower_bound(&gquiz1, 5).ref->first);
+ printf("\tELEMENT = %d\n", csmap_int_lower_bound(&gquiz1, 5).ref->second);
+ printf("gquiz1.upper_bound(5) : ");
+ printf("\tKEY = %d\t", csmap_int_lower_bound(&gquiz1, 5+1).ref->first);
+ printf("\tELEMENT = %d\n", csmap_int_lower_bound(&gquiz1, 5+1).ref->second);
+ }
+}
diff --git a/examples/unordered_set.c b/examples/unordered_set.c new file mode 100644 index 00000000..f4e37cf8 --- /dev/null +++ b/examples/unordered_set.c @@ -0,0 +1,41 @@ +// https://iq.opengenus.org/containers-cpp-stl/
+// C program to demonstrate various function of stc cset
+#define i_key_str
+#include <stc/cset.h>
+
+int main()
+{
+ // declaring set for storing string data-type
+ c_auto (cset_str, stringSet)
+ {
+ // inserting various string, same string will be stored
+ // once in set
+ cset_str_emplace(&stringSet, "code");
+ cset_str_emplace(&stringSet, "in");
+ cset_str_emplace(&stringSet, "C");
+ cset_str_emplace(&stringSet, "is");
+ cset_str_emplace(&stringSet, "fast");
+
+ const char* key = "slow";
+
+ // find returns end iterator if key is not found,
+ // else it returns iterator to that key
+
+ if (cset_str_find(&stringSet, key).ref == cset_str_end(&stringSet).ref)
+ printf("\"%s\" not found\n", key);
+ else
+ printf("Found \"%s\"\n", key);
+
+ key = "C";
+ if (!cset_str_contains(&stringSet, key))
+ printf("\"%s\" not found\n", key);
+ else
+ printf("Found \"%s\"\n", key);
+
+ // now iterating over whole set and printing its
+ // content
+ printf("All elements :\n");
+ c_foreach (itr, cset_str, stringSet)
+ printf("%s\n", itr.ref->str);
+ }
+}
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 264761e0..160c7601 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -158,10 +158,6 @@ cx_memb(_emplace_range)(Self* self, cx_iter_t it, cx_iter_t it1, cx_iter_t it2) }
STC_INLINE cx_iter_t
-cx_memb(_erase)(Self* self, size_t idx) {
- return cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + 1);
-}
-STC_INLINE cx_iter_t
cx_memb(_erase_n)(Self* self, size_t idx, size_t n) {
return cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n);
}
diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 0a06fc40..3fc8ebea 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -53,7 +53,7 @@ STC_INLINE Self cx_memb(_with_capacity)(size_t cap) { 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(_clear)(Self* self) {
diff --git a/include/stc/cvec.h b/include/stc/cvec.h index ff5b4399..3810e678 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -172,10 +172,7 @@ STC_INLINE cx_iter_t cx_memb(_emplace_range)(Self* self, cx_iter_t it, cx_iter_t it1, cx_iter_t it2) {
return cx_memb(_insert_range_p)(self, it.ref, it1.ref, it2.ref, true);
}
-STC_INLINE cx_iter_t
-cx_memb(_erase)(Self* self, size_t idx) {
- return cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + 1);
-}
+
STC_INLINE cx_iter_t
cx_memb(_erase_n)(Self* self, size_t idx, size_t n) {
return cx_memb(_erase_range_p)(self, self->data + idx, self->data + idx + n);
|
