From 4a421181052483cef097746fcaaa9027aaf99a7d Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Thu, 13 Jan 2022 14:49:13 +0100 Subject: Fixed missing i_eq default setting in template.h, carc and cbox. cstr_substr_utf8 added. --- benchmarks/plotbench/cpque_benchmark.cpp | 63 ++++++++++++++++++++++---------- include/stc/carc.h | 7 +++- include/stc/cbox.h | 7 +++- include/stc/ccommon.h | 4 +- include/stc/template.h | 10 ++--- include/stc/utf8.h | 25 ++++++++----- 6 files changed, 78 insertions(+), 38 deletions(-) diff --git a/benchmarks/plotbench/cpque_benchmark.cpp b/benchmarks/plotbench/cpque_benchmark.cpp index 2b6d3748..84bbe468 100644 --- a/benchmarks/plotbench/cpque_benchmark.cpp +++ b/benchmarks/plotbench/cpque_benchmark.cpp @@ -7,41 +7,64 @@ #define i_tag f #include -int main() +#include + +static const uint32_t seed = 1234; + +void std_test() { - uint32_t seed = time(NULL); stc64_t rng; int N = 10000000, M = 10; - cpque_f pq = cpque_f_with_size(N, 0.0f); + std::priority_queue, std::greater> pq; rng = stc64_init(seed); clock_t start = clock(); c_forrange (i, N) - pq.data[i] = (float) stc64_randf(&rng)*100000; + pq.push((float) stc64_randf(&rng)*100000); - cpque_f_make_heap(&pq); printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); + printf("%g ", pq.top()); - c_forrange (i, int, M) { - printf("%g ", *cpque_f_top(&pq)); - cpque_f_pop(&pq); + start = clock(); + c_forrange (i, N) { + pq.pop(); } - start = clock(); - c_forrange (i, int, M, N) - cpque_f_pop(&pq); - printf("\n\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); + printf("\npopped PQ: %f secs\n\n", (clock() - start) / (float) CLOCKS_PER_SEC); +} - start = clock(); - c_forrange (i, int, N) - cpque_f_push(&pq, (float) stc64_randf(&rng)*100000); - printf("pushed PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); - c_forrange (i, int, M) { +void stc_test() +{ + stc64_t rng; + int N = 10000000, M = 10; + + c_auto (cpque_f, pq) + { + rng = stc64_init(seed); + clock_t start = clock(); + c_forrange (i, N) + cpque_f_push(&pq, (float) stc64_randf(&rng)*100000); + + printf("Built priority queue: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); printf("%g ", *cpque_f_top(&pq)); - cpque_f_pop(&pq); + + c_forrange (i, int, M) { + cpque_f_pop(&pq); + } + + start = clock(); + c_forrange (i, int, M, N) + cpque_f_pop(&pq); + printf("\npopped PQ: %f secs\n", (clock() - start) / (float) CLOCKS_PER_SEC); } - puts(""); +} + - cpque_f_drop(&pq); +int main() +{ + puts("STD P.QUEUE:"); + std_test(); + puts("\nSTC P.QUEUE:"); + stc_test(); } diff --git a/include/stc/carc.h b/include/stc/carc.h index b33fef2f..ccb23a73 100644 --- a/include/stc/carc.h +++ b/include/stc/carc.h @@ -194,7 +194,12 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { - return !_cx_memb(_value_cmp)(x, y); + #if c_option(c_no_cmp) + return x == y; + #else + i_valraw rx = i_valto(x), ry = i_valto(x); + return i_eq(&rx, &ry); + #endif } #undef _i_atomic_inc #undef _i_atomic_dec_and_test diff --git a/include/stc/cbox.h b/include/stc/cbox.h index 42f5ede8..5eb1f1e3 100644 --- a/include/stc/cbox.h +++ b/include/stc/cbox.h @@ -170,6 +170,11 @@ _cx_memb(_value_cmp)(const _cx_value* x, const _cx_value* y) { STC_INLINE bool _cx_memb(_value_eq)(const _cx_value* x, const _cx_value* y) { - return !_cx_memb(_value_cmp)(x, y); + #if c_option(c_no_cmp) + return x == y; + #else + i_valraw rx = i_valto(x), ry = i_valto(x); + return i_eq(&rx, &ry); + #endif } #include "template.h" diff --git a/include/stc/ccommon.h b/include/stc/ccommon.h index d58545e2..292ac9cc 100644 --- a/include/stc/ccommon.h +++ b/include/stc/ccommon.h @@ -50,8 +50,8 @@ #define _c_APPLY_ARG_N(args) c_EXPAND(_c_ARG_N args) #define _c_RSEQ_N 16, 15, 14, 13, 12, 11, 10, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 -#define _c_ARG_N(_0, _1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, \ - _13, _14, _15, N, ...) N +#define _c_ARG_N(_1, _2, _3, _4, _5, _6, _7, _8, _9, _10, _11, _12, _13, \ + _14, _15, _16, N, ...) N #define c_static_assert(cond) \ typedef char c_PASTE(_static_assert_line_, __LINE__)[(cond) ? 1 : -1] diff --git a/include/stc/template.h b/include/stc/template.h index 7670ec92..d5291eaa 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -169,11 +169,6 @@ #define i_keyraw i_key #define i_keyto c_default_toraw #endif - #if !defined i_eq && defined i_cmp - #define i_eq !i_cmp - #elif !defined i_eq - #define i_eq c_default_eq - #endif #ifndef i_keydrop #define i_keydrop c_default_drop #endif @@ -200,6 +195,11 @@ #ifndef i_valdrop #define i_valdrop c_default_drop #endif +#if !defined i_eq && defined i_cmp + #define i_eq !i_cmp +#elif !defined i_eq + #define i_eq c_default_eq +#endif #ifndef i_cmp #define i_cmp c_default_cmp #endif diff --git a/include/stc/utf8.h b/include/stc/utf8.h index a9383ef6..0455a0c5 100644 --- a/include/stc/utf8.h +++ b/include/stc/utf8.h @@ -27,19 +27,20 @@ STC_INLINE uint32_t utf8_peek(const char *s) return codepoint; } -STC_INLINE int utf8_codepoint_width(uint8_t c) +STC_INLINE int utf8_codepoint_width(char c) { - int ret = (c & 0xF0) == 0xE0; + uint8_t u = (uint8_t)c; + int ret = (u & 0xF0) == 0xE0; ret += ret << 1; // 3 - ret |= c < 0x80; // 1 - ret |= ((0xC1 < c) & (c < 0xE0)) << 1; // 2 - ret |= ((0xEF < c) & (c < 0xF5)) << 2; // 4 + ret |= u < 0x80; // 1 + ret |= ((0xC1 < u) & (u < 0xE0)) << 1; // 2 + ret |= ((0xEF < u) & (u < 0xF5)) << 2; // 4 return ret; } STC_INLINE const char *utf8_next(const char *s) { - const char* t = s + utf8_codepoint_width((uint8_t)s[0]); + const char* t = s + utf8_codepoint_width(s[0]); uintptr_t p = (uintptr_t)t; p &= (uintptr_t) -(*s != 0); @@ -52,6 +53,12 @@ STC_INLINE bool csview_valid_utf8(csview sv) STC_INLINE size_t csview_size_utf8(csview sv) { return utf8_size(sv.str); } + +STC_INLINE csview csview_substr_utf8(csview sv, size_t pos, size_t n) { + sv.str = utf8_at(sv.str, pos); + sv.size = utf8_at(sv.str, n) - sv.str; + return sv; +} #endif #ifdef CSTR_H_INCLUDED @@ -61,9 +68,9 @@ STC_INLINE bool cstr_valid_utf8(cstr s) STC_INLINE size_t cstr_size_utf8(cstr s) { return utf8_size(cstr_str(&s)); } -STC_INLINE csview cstr_at_utf8(cstr s, size_t idx) { - const char* str = utf8_at(cstr_str(&s), idx); - csview sv = {str, utf8_codepoint_width((uint8_t)str[0])}; +STC_INLINE csview cstr_substr_utf8(cstr s, size_t pos, size_t n) { + csview sv = {utf8_at(cstr_str(&s), pos)}; + sv.size = utf8_at(sv.str, n) - sv.str; return sv; } #endif -- cgit v1.2.3