diff options
| author | Tyge Løvset <[email protected]> | 2022-12-30 12:36:13 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-12-30 14:38:39 +0100 |
| commit | 4f0f45422fb58e9b134445ad6a4ea96d806214e8 (patch) | |
| tree | ea0bb95c1f82fa5ec71218ce1f6d7ce9bfcc4e29 /misc/examples | |
| parent | 0761c13f316cc98ae7756f3527931aa86bed5016 (diff) | |
| download | STC-modified-4f0f45422fb58e9b134445ad6a4ea96d806214e8.tar.gz STC-modified-4f0f45422fb58e9b134445ad6a4ea96d806214e8.zip | |
More restructuring of files and cleanup. Moved carr2.h and carr3.h to misc/include/old/ as it is not among classic containers.
Removed stctest.h: Recommending https://github.com/bvdberg/ctest instead.
Diffstat (limited to 'misc/examples')
| -rw-r--r-- | misc/examples/demos.c | 30 | ||||
| -rw-r--r-- | misc/examples/mapmap.c | 18 | ||||
| -rw-r--r-- | misc/examples/multimap.c | 31 | ||||
| -rw-r--r-- | misc/examples/new_arr.c | 57 | ||||
| -rw-r--r-- | misc/examples/sort.c | 48 | ||||
| -rw-r--r-- | misc/examples/utf8replace_c.c | 23 | ||||
| -rw-r--r-- | misc/examples/utf8replace_rs.rs | 11 |
7 files changed, 97 insertions, 121 deletions
diff --git a/misc/examples/demos.c b/misc/examples/demos.c index 4a9fac89..4455b840 100644 --- a/misc/examples/demos.c +++ b/misc/examples/demos.c @@ -185,35 +185,6 @@ void mapdemo3() cmap_str_drop(&table); // frees key and value cstrs, and hash table. } -#define i_val float -#define i_tag f -#include <stc/carr3.h> - -void arraydemo1() -{ - printf("\nARRAYDEMO1\n"); - c_WITH (carr3_f arr3 = carr3_f_with_size(30, 20, 10, 0.0f), - carr3_f_drop(&arr3)) - { - arr3.data[5][4][3] = 10.2f; - float **arr2 = arr3.data[5]; - float *arr1 = arr3.data[5][4]; - - printf("arr3: %" c_ZU ": (%" c_ZU ", %" c_ZU ", %" c_ZU ") = %" c_ZU "\n", sizeof(arr3), - arr3.xdim, arr3.ydim, arr3.zdim, carr3_f_size(&arr3)); - - printf("%g\n", arr1[3]); // = 10.2 - printf("%g\n", arr2[4][3]); // = 10.2 - printf("%g\n", arr3.data[5][4][3]); // = 10.2 - - float x = 0.0; - c_FOREACH (i, carr3_f, arr3) - *i.ref = ++x; - printf("%g\n", arr3.data[29][19][9]); // = 6000 - } -} - - int main() { stringdemo1(); @@ -224,5 +195,4 @@ int main() mapdemo1(); mapdemo2(); mapdemo3(); - arraydemo1(); } diff --git a/misc/examples/mapmap.c b/misc/examples/mapmap.c index c361233b..d5fe9c81 100644 --- a/misc/examples/mapmap.c +++ b/misc/examples/mapmap.c @@ -4,18 +4,24 @@ // People: std::map<std::string, std::string> #define i_type People -#define i_key_str -#define i_val_str +#define i_key_str // name +#define i_val_str // email #define i_keydrop(p) (printf("kdrop: %s\n", cstr_str(p)), cstr_drop(p)) // override #include <stc/csmap.h> // Departments: std::map<std::string, People> #define i_type Departments -#define i_key_str +#define i_key_str // dep. name #define i_valclass People -// Shorthand for: +// i_key_str implies: +// #define i_tag str +// #define i_key cstr +// #define i_keyclone cstr_clone +// #define i_keydrop cstr_drop +// #define i_cmp cstr_cmp +// #define i_hash cstr_hash +// i_valclass implies: // #define i_val People -// #define i_cmp People_cmp // #define i_valclone People_clone // #define i_valdrop People_drop #include <stc/csmap.h> @@ -23,7 +29,7 @@ void add(Departments* deps, const char* name, const char* email, const char* dep) { - People *people = &Departments_insert(deps, cstr_from(dep), People_init()).ref->second; + People *people = &Departments_emplace(deps, dep, People_init()).ref->second; People_emplace_or_assign(people, name, email); } diff --git a/misc/examples/multimap.c b/misc/examples/multimap.c index 8797c24e..ba0bf71b 100644 --- a/misc/examples/multimap.c +++ b/misc/examples/multimap.c @@ -32,14 +32,14 @@ struct OlympicsData { int year; const char *city, *country, *date; } ol_data[] = {1924, "Chamonix", "France", "January 25 - February 5"}, }; -typedef struct { int year; cstr city, date; } OlympicLocation; +typedef struct { int year; cstr city, date; } OlympicLoc; -int OlympicLocation_cmp(const OlympicLocation* a, const OlympicLocation* b); -OlympicLocation OlympicLocation_clone(OlympicLocation loc); -void OlympicLocation_drop(OlympicLocation* self); +int OlympicLoc_cmp(const OlympicLoc* a, const OlympicLoc* b); +OlympicLoc OlympicLoc_clone(OlympicLoc loc); +void OlympicLoc_drop(OlympicLoc* self); -// Create a clist<OlympicLocation>, can be sorted by year. -#define i_valclass OlympicLocation // binds _cmp, _clone and _drop. +// Create a clist<OlympicLoc>, can be sorted by year. +#define i_valclass OlympicLoc // binds _cmp, _clone and _drop. #define i_tag OL #define i_extern // define _clist_mergesort() #include <stc/clist.h> @@ -50,19 +50,22 @@ void OlympicLocation_drop(OlympicLocation* self); #define i_tag OL #include <stc/csmap.h> -int OlympicLocation_cmp(const OlympicLocation* a, const OlympicLocation* b) { +int OlympicLoc_cmp(const OlympicLoc* a, const OlympicLoc* b) { return a->year - b->year; } -OlympicLocation OlympicLocation_clone(OlympicLocation loc) { +OlympicLoc OlympicLoc_clone(OlympicLoc loc) { loc.city = cstr_clone(loc.city); loc.date = cstr_clone(loc.date); return loc; } -void OlympicLocation_drop(OlympicLocation* self) { - c_DROP(cstr, &self->city, &self->date); + +void OlympicLoc_drop(OlympicLoc* self) { + cstr_drop(&self->city); + cstr_drop(&self->date); } + int main() { // Define the multimap with destructor defered to when block is completed. @@ -73,12 +76,12 @@ int main() for (size_t i = 0; i < c_ARRAYLEN(ol_data); ++i) { struct OlympicsData* d = &ol_data[i]; - OlympicLocation loc = {.year = d->year, - .city = cstr_from(d->city), - .date = cstr_from(d->date)}; + OlympicLoc loc = {.year = d->year, + .city = cstr_from(d->city), + .date = cstr_from(d->date)}; // Insert an empty list for each new country, and append the entry to the list. // If country already exist in map, its list is returned from the insert function. - clist_OL* list = &csmap_OL_insert(&multimap, cstr_from(d->country), empty).ref->second; + clist_OL* list = &csmap_OL_emplace(&multimap, d->country, empty).ref->second; clist_OL_push_back(list, loc); } // Sort locations by year for each country. diff --git a/misc/examples/new_arr.c b/misc/examples/new_arr.c deleted file mode 100644 index 7170d14a..00000000 --- a/misc/examples/new_arr.c +++ /dev/null @@ -1,57 +0,0 @@ -#include <stc/cstr.h> - -#define i_val int -#include <stc/carr2.h> - -#define i_val int -#include <stc/carr3.h> - -#define i_val_str -#include <stc/carr2.h> - -int main() -{ - int w = 7, h = 5, d = 3; - - c_WITH (carr2_int volume = carr2_int_new_uninit(w, h), carr2_int_drop(&volume)) - { - int *dat = carr2_int_data(&volume); - for (size_t i = 0; i < carr2_int_size(&volume); ++i) - dat[i] = i; - - for (size_t x = 0; x < volume.xdim; ++x) - for (size_t y = 0; y < volume.ydim; ++y) - printf(" %d", volume.data[x][y]); - puts(""); - - c_FOREACH (i, carr2_int, volume) - printf(" %d", *i.ref); - puts("\n"); - } - - c_WITH (carr3_int volume = carr3_int_new_uninit(w, h, d), carr3_int_drop(&volume)) - { - int *dat = carr3_int_data(&volume); - for (size_t i = 0; i < carr3_int_size(&volume); ++i) - dat[i] = i; - - for (size_t x = 0; x < volume.xdim; ++x) - for (size_t y = 0; y < volume.ydim; ++y) - for (size_t z = 0; z < volume.zdim; ++z) - printf(" %d", volume.data[x][y][z]); - puts(""); - - c_FOREACH (i, carr3_int, volume) - printf(" %d", *i.ref); - puts(""); - } - - c_WITH (carr2_str text2d = carr2_str_with_size(h, d, cstr_NULL), carr2_str_drop(&text2d)) - { - cstr_assign(&text2d.data[2][1], "hello"); - cstr_assign(&text2d.data[4][0], "world"); - - c_FOREACH (i, carr2_str, text2d) - printf("line: %s\n", cstr_str(i.ref)); - } -} diff --git a/misc/examples/sort.c b/misc/examples/sort.c new file mode 100644 index 00000000..65ae7359 --- /dev/null +++ b/misc/examples/sort.c @@ -0,0 +1,48 @@ +#include <stdio.h> +#include <time.h> +#include <stdlib.h> +#define i_val int +#include <stc/crandom.h> +#include <stc/algo/csort.h> +#ifdef __cplusplus +#include <algorithm> +#endif + + +int testsort(csortval_int *a, size_t size, const char *desc) { + clock_t t = clock(); +#ifdef __cplusplus + printf("std::sort: "); + std::sort(a, a + size); +#else + printf("csort: "); + csort_int(a, size); +#endif + t = clock() - t; + + printf("%s: %d elements sorted in %.3fms\n", + desc, (int)size, t*1000.0/CLOCKS_PER_SEC); + return 0; +} + +int main(int argc, char *argv[]) { + size_t i, size = argc > 1 ? strtoull(argv[1], NULL, 0) : 10000000; + csortval_int *a = (csortval_int*)malloc(sizeof(*a) * size); + if (a == NULL) return -1; + + for (i = 0; i < size; i++) + a[i] = crandom() & ((1U << 28) - 1); + testsort(a, size, "random"); + for (i = 0; i < 20; i++) printf(" %d", a[i]); + puts(""); + + testsort(a, size, "sorted"); + + for (i = 0; i < size; i++) a[i] = size - i; + testsort(a, size, "reverse sorted"); + + for (i = 0; i < size; i++) a[i] = 126735; + testsort(a, size, "constant"); + + free(a); +} diff --git a/misc/examples/utf8replace_c.c b/misc/examples/utf8replace_c.c index 3645bd0a..2d8d1921 100644 --- a/misc/examples/utf8replace_c.c +++ b/misc/examples/utf8replace_c.c @@ -1,23 +1,26 @@ -#define i_extern // add utf8 dependencies #include <stc/cstr.h> -#include <stc/csview.h> int main() { - c_AUTO (cstr, hello, upper) { + c_AUTO (cstr, hello, str) { hello = cstr_lit("hell😀 w😀rld"); printf("%s\n", cstr_str(&hello)); /* replace second smiley at utf8 codepoint pos 7 */ - cstr_u8_replace_at(&hello, cstr_u8_to_pos(&hello, 7), 1, c_SV("🐨")); + cstr_u8_replace_at(&hello, + cstr_u8_to_pos(&hello, 7), + 1, + c_SV("🐨") + ); printf("%s\n", cstr_str(&hello)); - cstr_replace_ex(&hello, "🐨", "ø", 1); - printf("%s\n", cstr_str(&hello)); - - upper = cstr_toupper_sv(cstr_sv(&hello)); - c_FOREACH (c, cstr, hello) printf("%.*s,", c_ARGSV(c.u8.chr)); - puts(""); + + //csview sv = c_SV("If you find the time, you will find the winner"); + //str = cstr_replace_sv(sv, c_SV("find"), c_SV("match"), 0); + + str = cstr_lit("If you find the time, you will find the winner"); + cstr_replace(&str, "find", "match"); + printf("\n%s\n", cstr_str(&str)); } } diff --git a/misc/examples/utf8replace_rs.rs b/misc/examples/utf8replace_rs.rs index 717978aa..8b163b4e 100644 --- a/misc/examples/utf8replace_rs.rs +++ b/misc/examples/utf8replace_rs.rs @@ -1,12 +1,12 @@ - pub fn main() { - let mut hello = String::from("hell😀 world"); + let mut hello = String::from("hell😀 w😀rld"); println!("{}", hello); - + + /* replace second smiley at utf8 codepoint pos 7 */ hello.replace_range( hello .char_indices() - .nth(4) + .nth(7) .map(|(pos, ch)| (pos..pos + ch.len_utf8())) .unwrap(), "🐨", @@ -16,4 +16,7 @@ pub fn main() { for c in hello.chars() { print!("{},", c); } + + let str = "If you find the time, you will find the winner"; + println!("\n{}", str.replace("find", "match")); } |
