From 959eab1e1f590ba4e5b521f106ae48ff2e493421 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 13 Oct 2021 23:22:19 +0200 Subject: Maintenance update. template.h now includes cstr.h when i_key_str or i_val_str is defined. Minor optimizations. --- include/stc/cdeq.h | 2 +- include/stc/cpque.h | 22 +++++++++------------- include/stc/cstack.h | 7 ++++--- include/stc/cvec.h | 6 +++--- include/stc/template.h | 4 ++++ 5 files changed, 21 insertions(+), 20 deletions(-) (limited to 'include') diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 8bfda771..0157f967 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -108,7 +108,7 @@ STC_INLINE cx_value_t* cx_memb(_emplace_front)(Self* self, i_valraw raw) { } STC_INLINE void cx_memb(_pop_back)(Self* self) { - size_t i = --cdeq_rep_(self)->size; i_valdel(&self->data[i]); + cx_value_t* p = &self->data[--cdeq_rep_(self)->size]; i_valdel(p); } STC_INLINE cx_value_t* cx_memb(_at)(const Self* self, size_t idx) { diff --git a/include/stc/cpque.h b/include/stc/cpque.h index b7a26360..0a06fc40 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -98,21 +98,17 @@ cx_memb(_push_back)(Self* self, cx_value_t value) { STC_INLINE void cx_memb(_pop_back)(Self* self) - { --self->size; i_valdel(&self->data[self->size]); } + { cx_value_t* p = &self->data[--self->size]; i_valdel(p); } /* -------------------------- IMPLEMENTATION ------------------------- */ #if !defined(STC_HEADER) || defined(STC_IMPLEMENTATION) || defined(i_imp) STC_DEF void -cx_memb(_sift_down_)(cx_value_t* arr, size_t i, size_t n) { - size_t r = i, c = i << 1; - while (c <= n) { +cx_memb(_sift_down_)(cx_value_t* arr, size_t idx, size_t n) { + for (size_t r = idx, c = idx << 1; c <= n; c <<= 1) { c += (c < n && i_cmp(&arr[c], &arr[c + 1]) < 0); - if (i_cmp(&arr[r], &arr[c]) < 0) { - cx_value_t tmp = arr[r]; arr[r] = arr[c]; arr[r = c] = tmp; - } else - return; - c <<= 1; + if (i_cmp(&arr[r], &arr[c]) >= 0) return; + cx_value_t t = arr[r]; arr[r] = arr[c]; arr[r = c] = t; } } @@ -143,10 +139,10 @@ cx_memb(_push)(Self* self, cx_value_t value) { if (self->size == self->capacity) cx_memb(_reserve)(self, self->size*3/2 + 4); cx_value_t *arr = self->data - 1; /* base 1 */ - size_t i = ++self->size; - for (; i > 1 && i_cmp(&arr[i >> 1], &value) < 0; i >>= 1) - arr[i] = arr[i >> 1]; - arr[i] = value; + size_t c = ++self->size; + for (; c > 1 && i_cmp(&arr[c >> 1], &value) < 0; c >>= 1) + arr[c] = arr[c >> 1]; + arr[c] = value; } #endif diff --git a/include/stc/cstack.h b/include/stc/cstack.h index 00d99fa1..3283eaf0 100644 --- a/include/stc/cstack.h +++ b/include/stc/cstack.h @@ -53,8 +53,9 @@ STC_INLINE Self cx_memb(_with_size)(size_t size, i_val fill) { } STC_INLINE void cx_memb(_clear)(Self* self) { - size_t i = self->size; self->size = 0; - while (i--) i_valdel(&self->data[i]); + cx_value_t *p = self->data + self->size; + while (p-- != self->data) i_valdel(p); + self->size = 0; } STC_INLINE void cx_memb(_del)(Self* self) @@ -73,7 +74,7 @@ STC_INLINE cx_value_t* cx_memb(_top)(const Self* self) { return &self->data[self->size - 1]; } STC_INLINE void cx_memb(_pop)(Self* self) - { --self->size; i_valdel(&self->data[self->size]); } + { cx_value_t* p = &self->data[--self->size]; i_valdel(p); } STC_INLINE void cx_memb(_reserve)(Self* self, size_t n) { if (n >= self->size) diff --git a/include/stc/cvec.h b/include/stc/cvec.h index b8d0ac33..ff5b4399 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -108,7 +108,7 @@ STC_INLINE cx_value_t* cx_memb(_back)(const Self* self) STC_INLINE cx_value_t* cx_memb(_emplace_back)(Self* self, i_valraw raw) { return cx_memb(_push_back)(self, i_valfrom(raw)); } STC_INLINE void cx_memb(_pop_back)(Self* self) - { size_t i = --cvec_rep_(self)->size; i_valdel(&self->data[i]); } + { cx_value_t* p = &self->data[--cvec_rep_(self)->size]; i_valdel(p); } STC_INLINE cx_iter_t cx_memb(_begin)(const Self* self) { return c_make(cx_iter_t){self->data}; } STC_INLINE cx_iter_t cx_memb(_end)(const Self* self) @@ -265,13 +265,13 @@ cx_memb(_reserve)(Self* self, size_t cap) { } STC_DEF void -cx_memb(_resize)(Self* self, size_t len, i_val null_val) { +cx_memb(_resize)(Self* self, size_t len, i_val fill) { if (len > cx_memb(_capacity)(*self)) cx_memb(_reserve)(self, len); struct cvec_rep* rep = cvec_rep_(self); size_t i, n = rep->size; for (i = len; i < n; ++i) i_valdel(&self->data[i]); - for (i = n; i < len; ++i) self->data[i] = null_val; + for (i = n; i < len; ++i) self->data[i] = fill; if (rep->cap) rep->size = len; } diff --git a/include/stc/template.h b/include/stc/template.h index 865e5b83..d72228aa 100644 --- a/include/stc/template.h +++ b/include/stc/template.h @@ -47,6 +47,10 @@ #error if i_keyraw or i_keyto defined, i_keyfrom a must be defined #endif +#if defined i_key_str || defined i_val_str + #include "cstr.h" +#endif + #ifdef i_cnt #define i_tag i_cnt #undef i_prefix -- cgit v1.2.3