summaryrefslogtreecommitdiffhomepage
path: root/examples
diff options
context:
space:
mode:
authorTyge Løvset <[email protected]>2021-01-20 23:08:47 +0100
committerTyge Løvset <[email protected]>2021-01-20 23:08:47 +0100
commitb0716d34433af4714f9a0b68278579fa1dd2b24e (patch)
tree162330d3fb25cd1b8de095e68fa40bebc6886919 /examples
parent13c60c38e84e66585038d7509cb485ae33900db5 (diff)
downloadSTC-modified-b0716d34433af4714f9a0b68278579fa1dd2b24e.tar.gz
STC-modified-b0716d34433af4714f9a0b68278579fa1dd2b24e.zip
Improved README.md
Diffstat (limited to 'examples')
-rw-r--r--examples/phonebook.c72
-rw-r--r--examples/share_ptr.c75
-rw-r--r--examples/tmap.cpp53
3 files changed, 0 insertions, 200 deletions
diff --git a/examples/phonebook.c b/examples/phonebook.c
deleted file mode 100644
index 91d01072..00000000
--- a/examples/phonebook.c
+++ /dev/null
@@ -1,72 +0,0 @@
-// The MIT License (MIT)
-// Copyright (c) 2018 Maksim Andrianov
-//
-// Permission is hereby granted, free of charge, to any person obtaining a copy
-// of this software and associated documentation files (the "Software"), to
-// deal in the Software without restriction, including without limitation the
-// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
-// sell copies of the Software, and to permit persons to whom the Software is
-// furnished to do so, subject to the following conditions:
-//
-// The above copyright notice and this permission notice shall be included in
-// all copies or substantial portions of the Software.
-//
-// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
-// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
-// IN THE SOFTWARE.
-
-// Program to emulates the phone book.
-
-#include <stdio.h>
-#include <stc/cmap.h>
-#include <stc/cstr.h>
-
-using_cmap_str();
-
-void print_phone_book(cmap_str phone_book)
-{
- c_foreach (i, cmap_str, phone_book)
- printf("%s\t- %s\n", i.ref->first.str, i.ref->second.str);
-}
-
-int main(int argc, char **argv)
-{
- bool erased;
- cmap_str phone_book = cmap_inits;
- c_push_items(&phone_book, cmap_str, {
- {"Lilia Friedman", "(892) 670-4739"},
- {"Tariq Beltran", "(489) 600-7575"},
- {"Laiba Juarez", "(303) 885-5692"},
- {"Elliott Mooney", "(945) 616-4482"},
- });
-
- printf("Phone book:\n");
- print_phone_book(phone_book);
-
- c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1880"));
- c_try_emplace(&phone_book, cmap_str, "Zak Byers", cstr_from("(551) 396-1990"));
-
- printf("\nPhone book after adding Zak Byers:\n");
- print_phone_book(phone_book);
-
- if (cmap_str_find(&phone_book, "Tariq Beltran") != NULL)
- printf("\nTariq Beltran is in phone book\n");
-
- erased = cmap_str_erase(&phone_book, "Tariq Beltran");
- erased = cmap_str_erase(&phone_book, "Elliott Mooney");
-
- printf("\nPhone book after erasing Tariq and Elliott:\n");
- print_phone_book(phone_book);
-
- cmap_str_insert_or_assign(&phone_book, "Zak Byers", "(555) 396-188");
-
- printf("\nPhone book after update phone of Zak Byers:\n");
- print_phone_book(phone_book);
-
- cmap_str_del(&phone_book);
- puts("done");
-} \ No newline at end of file
diff --git a/examples/share_ptr.c b/examples/share_ptr.c
deleted file mode 100644
index 9387bd61..00000000
--- a/examples/share_ptr.c
+++ /dev/null
@@ -1,75 +0,0 @@
-#include <stc/cptr.h>
-#include <stc/clist.h>
-#include <stc/cvec.h>
-#include <stc/cstr.h>
-#include <stdio.h>
-
-typedef struct { cstr_t name, last; } Person;
-
-Person* Person_make(Person* p, const char* name, const char* last) {
- p->name = cstr_from(name), p->last = cstr_from(last);
- return p;
-}
-void Person_del(Person* p) {
- printf("del: %s\n", p->name.str);
- c_del(cstr, &p->name, &p->last);
-}
-int Person_compare(const Person* p, const Person* q) {
- int cmp = strcmp(p->name.str, q->name.str);
- return cmp == 0 ? strcmp(p->last.str, q->last.str) : cmp;
-}
-
-using_csptr(pe, Person, Person_compare, Person_del);
-using_clist(pe, csptr_pe, csptr_pe_compare, csptr_pe_del, csptr_pe_clone);
-using_cvec(pe, csptr_pe, csptr_pe_compare, csptr_pe_del, csptr_pe_clone);
-
-int main() {
- clist_pe queue = clist_pe_init();
- cvec_pe vec = cvec_pe_init();
-
- Person tmp = {cstr_from("Joe"), cstr_from("Jordan")};
- csptr_pe joe = csptr_pe_make(tmp);
- clist_pe_push_back(&queue, csptr_pe_clone(joe));
- cvec_pe_push_back(&vec, csptr_pe_clone(joe));
-
- puts("Push 10:");
- c_forrange (i, 10) {
- csptr_pe p = csptr_pe_from(c_new(Person));
- p.get->name = cstr_from_fmt("Name %d", (i * 7) % 10);
- p.get->last = cstr_from_fmt("Last %d", (i * 7) % 10);
- clist_pe_push_back(&queue, p);
- cvec_pe_push_back(&vec, csptr_pe_clone(p)); // Don't forget to share!
- }
- c_foreach (i, clist_pe, queue)
- printf(" %s\n", i.ref->get->name.str);
-
- puts("Sort and pop 3:");
- clist_pe_sort(&queue);
- cvec_pe_sort(&vec);
- c_forrange (3) {
- clist_pe_pop_front(&queue);
- cvec_pe_pop_back(&vec);
- }
-
- puts("Sorted queue:");
- c_foreach (i, clist_pe, queue)
- printf(" %s\n", i.ref->get->name.str);
- puts("Sorted vec:");
- c_foreach (i, cvec_pe, vec)
- printf(" %s\n", i.ref->get->name.str);
-
- Person lost; Person_make(&lost, "Name 5", "Last 5");
- csptr_pe ptmp = {&lost, NULL}; // share pointer without counter - OK.
- clist_pe_iter_t lit = clist_pe_find(&queue, ptmp);
- Person_del(&lost);
- if (lit.ref) printf("Found: %s\n", lit.ref->get->name.str);
-
- printf("use %ld\n", *joe.use_count);
- csptr_pe_del(&joe);
-
- puts("Destroy queue:");
- clist_pe_del(&queue);
-
- puts("Destroy vec:");
- cvec_pe_del(&vec);
-} \ No newline at end of file
diff --git a/examples/tmap.cpp b/examples/tmap.cpp
deleted file mode 100644
index 793ce6cc..00000000
--- a/examples/tmap.cpp
+++ /dev/null
@@ -1,53 +0,0 @@
-#include <stc/csmap.h>
-#include <stc/cstr.h>
-#include <stc/crand.h>
-#include <stdio.h>
-#include <map>
-
-using_csmap(i, int, size_t);
-//using_csset_str();
-
-#include <time.h>
-
-int main(int argc, char **argv)
-{
- std::map<int, size_t> tmap;
- csmap_i map = csmap_i_init();
- csmap_i_iter_t it;
- time_t seed = time(NULL);
-
- size_t n = 2000000;
- uint64_t mask = (1ull << 22) - 1;
-
- clock_t t1 = clock();
- stc64_srandom(seed);
- for (size_t i = 0; i < n; ++i) {
- uint64_t x = stc64_random() & mask;
- tmap.emplace(x, i);
- }
- size_t s1 = tmap.size();
- stc64_srandom(seed);
- for (size_t i = 0; i < n - 50; ++i) {
- uint64_t x = stc64_random() & mask;
- tmap.erase(x);
- }
- clock_t t2 = clock();
-
- stc64_srandom(seed);
- for (size_t i = 0; i < n; ++i) {
- uint64_t x = stc64_random() & mask;
- csmap_i_emplace(&map, x, i);
- }
- size_t s2 = csmap_i_size(map);
- stc64_srandom(seed);
- for (size_t i = 0; i < n - 50; ++i) {
- uint64_t x = stc64_random() & mask;
- csmap_i_erase(&map, x);
- }
- clock_t t3 = clock();
-
- printf("%zu %zu\n", s1, s2);
- printf("%zu %zu\n", tmap.size(), csmap_i_size(map));
-
- printf("time: %f %f\n", ((float)t2-t1) / CLOCKS_PER_SEC, ((float)t3-t2) / CLOCKS_PER_SEC);
-}