diff options
| author | Tyge Løvset <[email protected]> | 2021-09-21 09:57:13 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-09-21 09:57:13 +0200 |
| commit | 32a3454eb463f86d7ace7b29c2e1574530499afc (patch) | |
| tree | fe2fd5565113e9b20c10b138086b3e40b8962726 | |
| parent | b31765540d40675665d8a58f5fa4d1e23dd5ba64 (diff) | |
| parent | bcd6e966bd9702e0c315213917d445c2370dd1ec (diff) | |
| download | STC-modified-32a3454eb463f86d7ace7b29c2e1574530499afc.tar.gz STC-modified-32a3454eb463f86d7ace7b29c2e1574530499afc.zip | |
Merge branch 'master' of github.com:tylov/STC into master
| -rw-r--r-- | README.md | 16 | ||||
| -rw-r--r-- | include/stc/crandom.h | 15 |
2 files changed, 27 insertions, 4 deletions
@@ -87,6 +87,22 @@ int main(void) { cvec_float_del(&vec);
}
```
+In order to include two **cvec**s with different element types, include cvec.h twice. For structs, specify a compare function (or none), as `<` and `==` operators does not work on them (this enables sorting and searching).
+```c
+#define i_val struct One
+#define i_tag one
+#define i_cmp c_no_compare
+#include <stc/cvec.h>
+
+#define i_val struct Two
+#define i_tag two
+#define i_cmp c_no_compare
+#include <stc/cvec.h>
+...
+cvec_one v1 = cvec_one_init();
+cvec_two v2 = cvec_two_init();
+```
+
With six different containers:
```c
#include <stdio.h>
diff --git a/include/stc/crandom.h b/include/stc/crandom.h index 38ab6b4b..eef4b6a4 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -118,13 +118,20 @@ STC_INLINE double stc64_uniformf(stc64_t* rng, stc64_uniformf_t* dist) { }
/* Unbiased bounded uniform distribution. */
-#ifdef c_umul128
STC_INLINE int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) {
+#ifdef c_umul128
uint64_t lo, hi;
do { c_umul128(stc64_rand(rng), d->range, &lo, &hi); } while (lo < d->threshold);
return d->lower + hi;
-}
+#else
+ uint64_t x, r;
+ do {
+ x = stc64_rand(rng);
+ r = x % d->range;
+ } while (x - r > -d->range);
+ return d->lower + r;
#endif
+}
STC_INLINE int32_t stc32_uniform(stc32_t* rng, stc32_uniform_t* d) {
uint64_t val;
@@ -165,14 +172,14 @@ STC_DEF stc32_t stc32_with_seq(uint32_t seed, uint32_t seq) { for (int i = 0; i < 6; ++i) stc32_rand(&rng);
return rng;
}
-#ifdef c_umul128
+
/* Init unbiased uniform uint RNG with bounds [low, high] */
STC_DEF stc64_uniform_t stc64_uniform_init(int64_t low, int64_t high) {
stc64_uniform_t dist = {low, (uint64_t) (high - low + 1)};
dist.threshold = (uint64_t)(-dist.range) % dist.range;
return dist;
}
-#endif
+
STC_DEF stc32_uniform_t stc32_uniform_init(int32_t low, int32_t high) {
stc32_uniform_t dist = {low, (uint32_t) (high - low + 1)};
dist.threshold = (uint32_t)(-dist.range) % dist.range;
|
