diff options
| author | Tyge Løvset <[email protected]> | 2022-05-30 14:22:44 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2022-05-30 14:22:44 +0200 |
| commit | 58bb58e7980d1eae175ed66dbe873893a05ab81e (patch) | |
| tree | 2756b2d1b62264c34a6d40265dc85f07eb2f1112 /include/stc | |
| parent | b28d3fa7c3b9233ca485014744bf84e6c4f5a1d3 (diff) | |
| download | STC-modified-58bb58e7980d1eae175ed66dbe873893a05ab81e.tar.gz STC-modified-58bb58e7980d1eae175ed66dbe873893a05ab81e.zip | |
Done refactoring:
- Non-templated types (cstr, csview, cbits, crandom) have no longer default static linking. Now i_header is defined, i.e. files are as headers only.
==> Define `i_implement` before file inclusion. Still possible to do static linkage by defining `i_static` before inclusion or global STC_STATIC.
- Templated containers still have static linkage by default.
- Renamed csview_substr(), csview_slice() to csview_substr_ex(), csview_slice_ex(). Added simpler inlined csview_substr(), csview_slice().
Diffstat (limited to 'include/stc')
| -rw-r--r-- | include/stc/cbits.h | 1 | ||||
| -rw-r--r-- | include/stc/ccommon.h | 4 | ||||
| -rw-r--r-- | include/stc/crandom.h | 35 | ||||
| -rw-r--r-- | include/stc/cstr.h | 2 | ||||
| -rw-r--r-- | include/stc/csview.h | 38 |
5 files changed, 53 insertions, 27 deletions
diff --git a/include/stc/cbits.h b/include/stc/cbits.h index e35e4225..9c7081f7 100644 --- a/include/stc/cbits.h +++ b/include/stc/cbits.h @@ -51,6 +51,7 @@ int main() { */
#ifndef CBITS_H_INCLUDED
+#define i_header
#include "ccommon.h"
#include <stdlib.h>
#include <string.h>
diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index f27190af..9c8ba414 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -252,8 +252,8 @@ STC_INLINE char* c_strnstrn(const char *s, const char *needle, #undef STC_API
#undef STC_DEF
-#if !defined i_static && (defined(i_header) || defined(i_implement) || \
- defined(STC_HEADER) || defined(STC_IMPLEMENT))
+#if !defined(i_static) && !defined(STC_STATIC) && (defined(i_header) || defined(STC_HEADER) || \
+ defined(i_implement) || defined(STC_IMPLEMENT))
# define STC_API extern
# define STC_DEF
#else
diff --git a/include/stc/crandom.h b/include/stc/crandom.h index a9877b50..a3f68279 100644 --- a/include/stc/crandom.h +++ b/include/stc/crandom.h @@ -20,6 +20,7 @@ * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
+#define i_header
#include "ccommon.h"
#ifndef CRANDOM_H_INCLUDED
@@ -73,6 +74,10 @@ STC_INLINE stc64_t stc64_new(uint64_t seed) STC_API stc64_uniform_t stc64_uniform_new(int64_t low, int64_t high);
STC_API int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* dist);
+/* Normal distribution PRNG */
+STC_API double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist);
+
+
/* Main stc64 prng */
STC_INLINE uint64_t stc64_rand(stc64_t* rng) {
uint64_t *s = rng->state; enum {LR=24, RS=11, LS=3};
@@ -104,21 +109,6 @@ STC_INLINE stc64_normalf_t stc64_normalf_new(double mean, double stddev) { return c_make(stc64_normalf_t){mean, stddev, 0.0, 0};
}
-/* Normal distribution PRNG */
-STC_INLINE double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
- double u1, u2, s, m;
- if (dist->has_next++ & 1)
- return dist->next * dist->stddev + dist->mean;
- do {
- u1 = 2.0 * stc64_randf(rng) - 1.0;
- u2 = 2.0 * stc64_randf(rng) - 1.0;
- s = u1*u1 + u2*u2;
- } while (s >= 1.0 || s == 0.0);
- m = sqrt(-2.0 * log(s) / s);
- dist->next = u2 * m;
- return (u1 * m) * dist->stddev + dist->mean;
-}
-
/* Following functions are deprecated (will be removed in the future): */
STC_INLINE void stc64_srandom(uint64_t seed) { csrandom(seed); }
STC_INLINE uint64_t stc64_random() { return crandom(); }
@@ -181,6 +171,21 @@ STC_DEF int64_t stc64_uniform(stc64_t* rng, stc64_uniform_t* d) { #endif
}
+/* Normal distribution PRNG */
+STC_DEF double stc64_normalf(stc64_t* rng, stc64_normalf_t* dist) {
+ double u1, u2, s, m;
+ if (dist->has_next++ & 1)
+ return dist->next * dist->stddev + dist->mean;
+ do {
+ u1 = 2.0 * stc64_randf(rng) - 1.0;
+ u2 = 2.0 * stc64_randf(rng) - 1.0;
+ s = u1*u1 + u2*u2;
+ } while (s >= 1.0 || s == 0.0);
+ m = sqrt(-2.0 * log(s) / s);
+ dist->next = u2 * m;
+ return (u1 * m) * dist->stddev + dist->mean;
+}
+
#endif
#endif
#undef i_opt
diff --git a/include/stc/cstr.h b/include/stc/cstr.h index 561bb1ca..47dff3cd 100644 --- a/include/stc/cstr.h +++ b/include/stc/cstr.h @@ -30,6 +30,7 @@ #ifndef CSTR_H_INCLUDED
#define CSTR_H_INCLUDED
+#define i_header
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -537,5 +538,4 @@ STC_DEF int cstr_printf(cstr* self, const char* fmt, ...) { #undef i_header
#undef i_static
#undef i_implement
-//#undef i_implement
#endif // !STC_CSTR_V1
diff --git a/include/stc/csview.h b/include/stc/csview.h index ba0a7568..eb79c482 100644 --- a/include/stc/csview.h +++ b/include/stc/csview.h @@ -23,6 +23,7 @@ #ifndef CSVIEW_H_INCLUDED
#define CSVIEW_H_INCLUDED
+#define i_header
#include "ccommon.h"
#include "forward.h"
#include "utf8.h"
@@ -31,8 +32,8 @@ #define csview_new(literal) c_sv(literal)
#define csview_npos (SIZE_MAX >> 1)
-STC_API csview csview_substr(csview sv, intptr_t pos, size_t n);
-STC_API csview csview_slice(csview sv, intptr_t p1, intptr_t p2);
+STC_API csview csview_substr_ex(csview sv, intptr_t pos, size_t n);
+STC_API csview csview_slice_ex(csview sv, intptr_t p1, intptr_t p2);
STC_API csview csview_token(csview sv, csview sep, size_t* start);
STC_INLINE csview csview_init() { return csview_null; }
@@ -69,6 +70,18 @@ STC_INLINE bool csview_ends_with(csview sv, csview sub) { return !memcmp(sv.str + sv.size - sub.size, sub.str, sub.size);
}
+STC_INLINE csview csview_substr(csview sv, size_t pos, size_t n) {
+ if (pos + n > sv.size) n = sv.size - pos;
+ sv.str += pos, sv.size = n;
+ return sv;
+}
+
+STC_INLINE csview csview_slice(csview sv, size_t p1, size_t p2) {
+ if (p2 > sv.size) p2 = sv.size;
+ sv.str += p1, sv.size = p2 > p1 ? p2 - p1 : 0;
+ return sv;
+}
+
/* iterator */
STC_INLINE csview_iter csview_begin(const csview* self)
{ return c_make(csview_iter){.chr = {self->str, utf8_codep_size(self->str)}}; }
@@ -80,9 +93,6 @@ STC_INLINE void csview_next(csview_iter* it) { it->ref += it->chr.size; it->chr.size = utf8_codep_size(it->ref); }
/* utf8 */
-STC_INLINE bool csview_valid_u8(csview sv) // depends on src/utf8utils.c
- { return utf8_valid_n(sv.str, sv.size); }
-
STC_INLINE size_t csview_size_u8(csview sv)
{ return utf8_size_n(sv.str, sv.size); }
@@ -92,6 +102,10 @@ STC_INLINE csview csview_substr_u8(csview sv, size_t u8pos, size_t u8len) { return sv;
}
+STC_INLINE bool csview_valid_u8(csview sv) // depends on src/utf8utils.c
+ { return utf8_valid_n(sv.str, sv.size); }
+
+
/* csview interaction with cstr: */
#ifdef CSTR_H_INCLUDED
@@ -101,12 +115,18 @@ STC_INLINE csview csview_from_s(const cstr* self) STC_INLINE cstr cstr_from_sv(csview sv)
{ return cstr_from_n(sv.str, sv.size); }
-STC_INLINE csview cstr_substr(const cstr* self, intptr_t pos, size_t n)
+STC_INLINE csview cstr_substr(const cstr* self, size_t pos, size_t n)
{ return csview_substr(csview_from_s(self), pos, n); }
-STC_INLINE csview cstr_slice(const cstr* self, intptr_t p1, intptr_t p2)
+STC_INLINE csview cstr_slice(const cstr* self, size_t p1, size_t p2)
{ return csview_slice(csview_from_s(self), p1, p2); }
+STC_INLINE csview cstr_substr_ex(const cstr* self, intptr_t pos, size_t n)
+ { return csview_substr_ex(csview_from_s(self), pos, n); }
+
+STC_INLINE csview cstr_slice_ex(const cstr* self, intptr_t p1, intptr_t p2)
+ { return csview_slice_ex(csview_from_s(self), p1, p2); }
+
STC_INLINE csview cstr_assign_sv(cstr* self, csview sv)
{ return c_make(csview){cstr_assign_n(self, sv.str, sv.size), sv.size}; }
@@ -155,7 +175,7 @@ STC_INLINE uint64_t csview_hash(const csview *self) #if defined(i_implement)
STC_DEF csview
-csview_substr(csview sv, intptr_t pos, size_t n) {
+csview_substr_ex(csview sv, intptr_t pos, size_t n) {
if (pos < 0) {
pos += sv.size;
if (pos < 0) pos = 0;
@@ -167,7 +187,7 @@ csview_substr(csview sv, intptr_t pos, size_t n) { }
STC_DEF csview
-csview_slice(csview sv, intptr_t p1, intptr_t p2) {
+csview_slice_ex(csview sv, intptr_t p1, intptr_t p2) {
if (p1 < 0) {
p1 += sv.size;
if (p1 < 0) p1 = 0;
|
