Skip to content

Commit

Permalink
Removed emplace_key() functions in hmap and smap - not needed.
Browse files Browse the repository at this point in the history
Added a few more compiler expl. examples.
  • Loading branch information
tylov committed Oct 19, 2024
1 parent d3bd91f commit 13f2edb
Show file tree
Hide file tree
Showing 5 changed files with 14 additions and 78 deletions.
6 changes: 0 additions & 6 deletions docs/hmap_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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")));
Expand Down
69 changes: 14 additions & 55 deletions docs/random_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <stdio.h>
#include <time.h>
#define STC_IMPLEMENT
#include <math.h>
#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 *
```
1 change: 0 additions & 1 deletion docs/smap_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 0 additions & 9 deletions include/stc/hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
7 changes: 0 additions & 7 deletions include/stc/smap.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 13f2edb

Please sign in to comment.