diff options
| author | Tyge Løvset <[email protected]> | 2020-12-02 09:17:05 +0100 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2020-12-02 09:17:05 +0100 |
| commit | e60e9f398f43ba09a7f0027ac93d4f8f61a9f254 (patch) | |
| tree | 7ddd7067fbe0290562ef3f4756663125fa270d6c /docs | |
| parent | a754aabd160fc02cf44b4a02deaffff9b233387f (diff) | |
| download | STC-modified-e60e9f398f43ba09a7f0027ac93d4f8f61a9f254.tar.gz STC-modified-e60e9f398f43ba09a7f0027ac93d4f8f61a9f254.zip | |
Added better crandom and cvec example.
Diffstat (limited to 'docs')
| -rw-r--r-- | docs/cbitset_api.md | 5 | ||||
| -rw-r--r-- | docs/clist_api.md | 25 | ||||
| -rw-r--r-- | docs/cmap_api.md | 7 | ||||
| -rw-r--r-- | docs/crandom_api.md | 81 | ||||
| -rw-r--r-- | docs/cset_api.md | 8 | ||||
| -rw-r--r-- | docs/cvec_api.md | 37 |
6 files changed, 77 insertions, 86 deletions
diff --git a/docs/cbitset_api.md b/docs/cbitset_api.md index 41e4d976..a22d6f2f 100644 --- a/docs/cbitset_api.md +++ b/docs/cbitset_api.md @@ -18,9 +18,6 @@ All cstr definitions and prototypes may be included in your C source file by inc ``` ## Methods -### Construction - -The interfaces to create a cbitset object: ```c cbitset_t cbitset_init(void); cbitset_t cbitset_with_size(size_t size, bool value); @@ -68,7 +65,7 @@ bool cbitset_itval(cbitset_iter_t it); ## Example ```c #include <stdio.h> -#include <stc/cbitset.h> +#include "stc/cbitset.h" static inline cbitset_t sieveOfEratosthenes(size_t n) { diff --git a/docs/clist_api.md b/docs/clist_api.md index c3958e52..d899e6ad 100644 --- a/docs/clist_api.md +++ b/docs/clist_api.md @@ -54,9 +54,6 @@ All clist definitions and prototypes may be included in your C source file by in ``` ## Methods -### Construction - -The interfaces to create a clist_X object: ```c clist_X clist_X_init(void); @@ -108,31 +105,31 @@ clist_X_value_t* clist_X_itval(clist_X_iter_t it); ```c #include <stdio.h> #include "stc/clist.h" - -using_clist(fx, double); +using_clist(d, double); int main() { - clist_fx list = clist_fx_init(); - c_push_items(&list, clist_fx, { + clist_d list = clist_inits; + c_push_items(&list, clist_d, { 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0 }); - // interleave push_front / push_back: + + // Interleave push_front / push_back: c_forrange (i, int, 1, 10) { - if (i & 1) clist_fx_push_front(&list, (float) i); - else clist_fx_push_back(&list, (float) i); + if (i & 1) clist_d_push_front(&list, (float) i); + else clist_d_push_back(&list, (float) i); } printf("initial: "); - c_foreach (i, clist_fx, list) + c_foreach (i, clist_d, list) printf(" %g", *i.val); - clist_fx_sort(&list); // mergesort O(n*log n) + clist_d_sort(&list); // mergesort O(n*log n) printf("\nsorted: "); - c_foreach (i, clist_fx, list) + c_foreach (i, clist_d, list) printf(" %g", *i.val); - clist_fx_del(&list); + clist_d_del(&list); } ``` Output: diff --git a/docs/cmap_api.md b/docs/cmap_api.md index 1fb5994c..3541c9ab 100644 --- a/docs/cmap_api.md +++ b/docs/cmap_api.md @@ -80,9 +80,6 @@ All cmap definitions and prototypes may be included in your C source file by inc ``` ## Methods -### Construction - -The interface for cmap_X: ```c cmap_X cmap_X_init(void); cmap_X cmap_X_with_capacity(size_t cap); @@ -129,8 +126,8 @@ uint32_t c_default_hash32(const void* data, size_t len); ## Example ```c #include <stdio.h> -#include <stc/cstr.h> -#include <stc/cmap.h> +#include "stc/cstr.h" +#include "stc/cmap.h" using_cmap_str(); diff --git a/docs/crandom_api.md b/docs/crandom_api.md index 5d0a8a98..0d0f415d 100644 --- a/docs/crandom_api.md +++ b/docs/crandom_api.md @@ -61,25 +61,26 @@ Integer generator has range \[low, high]. (9-10) Initializer and generator for n #include <stdio.h> #include <time.h> #include <math.h> -#include <stc/crandom.h> -#include <stc/cstr.h> -#include <stc/cmap.h> -#include <stc/cvec.h> +#include "stc/crandom.h" +#include "stc/cstr.h" +#include "stc/cmap.h" +#include "stc/cvec.h" -// Declare int -> int hashmap with typetag 'i'. +// Declare unordered map: int -> int with typetag 'i'. using_cmap(i, int, size_t); // Comparison of map keys. static int compare(cmap_i_entry_t *a, cmap_i_entry_t *b) { return c_default_compare(&a->first, &b->first); } -// Vector of map entries, with comparison function. +// Declare vector of map entries, with comparison function. using_cvec(e, cmap_i_entry_t, c_default_del, compare); + int main() { enum {N = 10000000}; - const double Mean = -12.0, StdDev = 6.0, Mag = 8000.0 / StdDev; + const double Mean = -12.0, StdDev = 6.0, Scale = 74; printf("Demo of gaussian / normal distribution of %d random samples\n", N); @@ -95,7 +96,7 @@ int main() cmap_i_emplace(&mhist, index, 0).first->second += 1; } - // Transfer map to vec and sort it by map keys. + // Transfer map to vec and sort it by map entry keys. cvec_e vhist = cvec_e_init(); c_foreach (i, cmap_i, mhist) cvec_e_push_back(&vhist, *i.val); @@ -104,13 +105,14 @@ int main() // Print the gaussian bar chart cstr_t bar = cstr_init(); c_foreach (i, cvec_e, vhist) { - size_t n = (size_t) (i.val->second * Mag / N); + size_t n = (size_t) (i.val->second * StdDev * Scale * 2.5 / N); if (n > 0) { - // bar string: take ownership in new str after freeing current. + // Bar string: move new str after freeing current cstr_take(&bar, cstr_with_size(n, '*')); printf("%4d %s\n", i.val->first, bar.str); } } + // Cleanup cstr_del(&bar); cmap_i_del(&mhist); @@ -123,37 +125,36 @@ Demo of gaussian / normal distribution of 10000000 random samples -29 * -28 ** -27 *** - -26 ***** - -25 ******** - -24 ************ - -23 **************** - -22 ********************** - -21 **************************** - -20 ************************************ - -19 ******************************************** - -18 ***************************************************** - -17 ************************************************************** - -16 ********************************************************************** - -15 ****************************************************************************** - -14 *********************************************************************************** - -13 *************************************************************************************** - -12 **************************************************************************************** - -11 *************************************************************************************** - -10 *********************************************************************************** - -9 ****************************************************************************** - -8 ********************************************************************** - -7 ************************************************************** - -6 ***************************************************** - -5 ******************************************** - -4 ************************************ - -3 **************************** - -2 ********************** - -1 **************** - 0 ************ - 1 ******** - 2 ***** + -26 **** + -25 ******* + -24 ********* + -23 ************* + -22 ****************** + -21 *********************** + -20 ****************************** + -19 ************************************* + -18 ******************************************** + -17 **************************************************** + -16 *********************************************************** + -15 ***************************************************************** + -14 ********************************************************************* + -13 ************************************************************************ + -12 ************************************************************************* + -11 ************************************************************************ + -10 ********************************************************************* + -9 ***************************************************************** + -8 *********************************************************** + -7 **************************************************** + -6 ******************************************** + -5 ************************************* + -4 ****************************** + -3 *********************** + -2 ****************** + -1 ************* + 0 ********* + 1 ******* + 2 **** 3 *** 4 ** 5 * - 6 * ```
\ No newline at end of file diff --git a/docs/cset_api.md b/docs/cset_api.md index ddf1012d..d705ab02 100644 --- a/docs/cset_api.md +++ b/docs/cset_api.md @@ -62,9 +62,6 @@ All cset definitions and prototypes may be included in your C source file by inc ```
## Methods
-### Construction
-
-The interface for cset_X:
```c
cset_X cset_X_init(void);
cset_X cset_X_with_capacity(size_t cap);
@@ -107,9 +104,8 @@ uint32_t c_default_hash32(const void* data, size_t len); ## Example
```c
#include <stdio.h>
-#include <stc/cstr.h>
-#include <stc/cset.h>
-
+#include "stc/cstr.h"
+#include "stc/cset.h"
using_cset_str();
int main ()
diff --git a/docs/cvec_api.md b/docs/cvec_api.md index 1a51470e..7a60c110 100644 --- a/docs/cvec_api.md +++ b/docs/cvec_api.md @@ -49,9 +49,6 @@ All cvec definitions and prototypes may be included in your C source file by inc ``` ## Methods -### Construction - -The interface for cvec: ```c cvec_X cvec_X_init(void); cvec_X cvec_X_with_size(size_t size, Value fill_val); @@ -111,31 +108,37 @@ cvec_X_value_t* cvec_X_itval(cvec_X_iter_t it); ## Example ```c #include <stdio.h> -#include <stc/cvec.h> +#include "stc/cvec.h" using_cvec(i, int); int main() { // Create a vector containing integers - cvec_i v = cvec_inits; - c_push_items(&v, cvec_i, {7, 5, 16, 8}); + cvec_i vec = cvec_inits; + c_push_items(&vec, cvec_i, {7, 5, 16, 8}); // Add two more integers to vector - cvec_i_push_back(&v, 25); - cvec_i_push_back(&v, 13); + cvec_i_push_back(&vec, 25); + cvec_i_push_back(&vec, 13); + + printf("initial: "); + c_foreach (n, cvec_i, vec) { + printf(" %d", *n.val); + } + + // Sort the vector + cvec_i_sort(&vec); - // Iterate and print values of vector - c_foreach (n, cvec_i, v) { - printf("%d\n", *n.val); + printf("\nsorted: "); + c_foreach (n, cvec_i, vec) { + printf(" %d", *n.val); } + + cvec_i_del(&vec); } ``` Output: ``` -7 -5 -16 -8 -25 -13 +initial: 7 5 16 8 25 13 +sorted: 5 7 8 13 16 25 ```
\ No newline at end of file |
