From 13f2edb78e45b526a5488dffcfa35a102c1634e4 Mon Sep 17 00:00:00 2001 From: tylov Date: Sat, 19 Oct 2024 23:23:50 +0200 Subject: [PATCH] Removed emplace_key() functions in hmap and smap - not needed. Added a few more compiler expl. examples. --- docs/hmap_api.md | 6 ---- docs/random_api.md | 69 ++++++++++------------------------------------ docs/smap_api.md | 1 - include/stc/hmap.h | 9 ------ include/stc/smap.h | 7 ----- 5 files changed, 14 insertions(+), 78 deletions(-) diff --git a/docs/hmap_api.md b/docs/hmap_api.md index 7a68c899..2fa9b7f5 100644 --- a/docs/hmap_api.md +++ b/docs/hmap_api.md @@ -82,7 +82,6 @@ hmap_X_result hmap_X_put(hmap_X* self, i_keyraw rkey, i_valraw rmapped); hmap_X_result hmap_X_emplace(hmap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map hmap_X_result hmap_X_emplace_or_assign(hmap_X* self, i_keyraw rkey, i_valraw rmapped); // always update mapped -hmap_X_result hmap_X_emplace_key(hmap_X* self, i_keyraw rkey); // see example 1. int hmap_X_erase(hmap_X* self, i_keyraw rkey); // return 0 or 1 hmap_X_iter hmap_X_erase_at(hmap_X* self, hmap_X_iter it); // return iter after it @@ -151,11 +150,6 @@ int main(void) hmap_cstr_emplace(&umap, "BLACK", "#000000"); hmap_cstr_emplace(&umap, "WHITE", "#FFFFFF"); - // Insert only if "CYAN" is not in the map: create mapped value when needed only. - hmap_cstr_result res = hmap_cstr_emplace_key(&umap, "CYAN"); - if (res.inserted) - res.ref->second = cstr_lit("#00FFFF"); // must assign second if key was inserted. - // Output values by key printf("The HEX of color RED is:[%s]\n", cstr_str(hmap_cstr_at(&umap, "RED"))); printf("The HEX of color BLACK is:[%s]\n", cstr_str(hmap_cstr_at(&umap, "BLACK"))); diff --git a/docs/random_api.md b/docs/random_api.md index aa54651c..343e7805 100644 --- a/docs/random_api.md +++ b/docs/random_api.md @@ -87,84 +87,43 @@ int32_t crand32_uniform_r(crand32* rng, uint32_t strm, crand32_unif | `crand32_uniform_dist` | `struct {...}` | Uniform int distribution struct | ## Example + +[ [Run this code](https://godbolt.org/z/sWa3ea1cr) ] ```c +#include #include -#define STC_IMPLEMENT +#include #include "stc/random.h" -#include "stc/cstr.h" -// Declare int => int sorted map. #define i_type SortedMap, int, long -#include "stc/smap.h" +#include "stc/smap.h" // sorted map. int main(void) { - enum {N = 10000000}; + enum {N = 5000000}; printf("Demo of gaussian / normal distribution of %d random samples\n", N); // Setup a reentrant random number engine with normal distribution. - uint64_t seed = time(NULL); - crand64 rng = crand64_from(seed); - crand64_normal_dist dist = {.mean=-12.0, .stddev=6.0}; + crand64_seed((uint64_t)time(NULL)); + crand64_normal_dist dist = {.mean=-12.0, .stddev=4.0}; // Create histogram map SortedMap mhist = {0}; c_forrange (N) { - int index = (int)round(crand64_normal_r(&rng, 1, &dist)); + int index = (int)round(crand64_normal(&dist)); SortedMap_insert(&mhist, index, 0).ref->second += 1; } // Print the gaussian bar chart - cstr bar = {0}; - const double scale = 74; + const double scale = 50; c_foreach (i, SortedMap, mhist) { - int n = (int)(i.ref->second * dist.stddev * scale * 2.5 / N); + int n = (int)((double)i.ref->second * dist.stddev * scale * 2.5 / N); if (n > 0) { - cstr_resize(&bar, n, '*'); - printf("%4d %s\n", i.ref->first, cstr_str(&bar)); + printf("%4d ", i.ref->first); + c_forrange(n) printf("*"); + puts(""); } } - // Cleanup - cstr_drop(&bar); SortedMap_drop(&mhist); } ``` -Output: -``` -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 **** - 3 *** - 4 ** - 5 * -``` diff --git a/docs/smap_api.md b/docs/smap_api.md index 9ec4cef5..38240ef2 100644 --- a/docs/smap_api.md +++ b/docs/smap_api.md @@ -85,7 +85,6 @@ smap_X_result smap_X_put(smap_X* self, i_keyraw rkey, i_valraw rmapped); smap_X_result smap_X_emplace(smap_X* self, i_keyraw rkey, i_valraw rmapped); // no change if rkey in map smap_X_result smap_X_emplace_or_assign(smap_X* self, i_keyraw rkey, i_valraw rmapped); // always update rmapped -smap_X_result smap_X_emplace_key(smap_X* self, i_keyraw rkey); // if key not in map, mapped is left unassigned int smap_X_erase(smap_X* self, i_keyraw rkey); smap_X_iter smap_X_erase_at(smap_X* self, smap_X_iter it); // returns iter after it diff --git a/include/stc/hmap.h b/include/stc/hmap.h index c5b319e4..8c8c7321 100644 --- a/include/stc/hmap.h +++ b/include/stc/hmap.h @@ -165,15 +165,6 @@ _c_MEMB(_insert_entry_)(Self* self, _m_keyraw rkey) { } return _res; } - - #ifdef _i_is_map - STC_INLINE _m_result _c_MEMB(_emplace_key)(Self* self, _m_keyraw rkey) { - _m_result _res = _c_MEMB(_insert_entry_)(self, rkey); - if (_res.inserted) - _res.ref->first = i_keyfrom(rkey); - return _res; - } - #endif // _i_is_map #endif // !i_no_emplace STC_INLINE _m_raw _c_MEMB(_value_toraw)(const _m_value* val) { diff --git a/include/stc/smap.h b/include/stc/smap.h index 85dd9ff7..3ea28b2b 100644 --- a/include/stc/smap.h +++ b/include/stc/smap.h @@ -170,13 +170,6 @@ STC_API _m_result _c_MEMB(_insert_entry_)(Self* self, _m_keyraw rkey); STC_API _m_result _c_MEMB(_insert_or_assign)(Self* self, _m_key key, _m_mapped mapped); #ifndef i_no_emplace STC_API _m_result _c_MEMB(_emplace_or_assign)(Self* self, _m_keyraw rkey, _m_rmapped rmapped); - - STC_INLINE _m_result _c_MEMB(_emplace_key)(Self* self, _m_keyraw rkey) { - _m_result res = _c_MEMB(_insert_entry_)(self, rkey); - if (res.inserted) - res.ref->first = i_keyfrom(rkey); - return res; - } #endif STC_INLINE const _m_mapped* _c_MEMB(_at)(const Self* self, _m_keyraw rkey)