diff options
| author | Tyge Løvset <[email protected]> | 2021-10-06 20:19:10 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-10-06 20:19:10 +0200 |
| commit | d8eaf9acb03d97ce66935d14ed4ca768637c9466 (patch) | |
| tree | d54e46a03183335b7cec172acaddca20defa562f /include | |
| parent | 435fd25e5c74aea72eb20f4007977183cdbe0919 (diff) | |
| download | STC-modified-d8eaf9acb03d97ce66935d14ed4ca768637c9466.tar.gz STC-modified-d8eaf9acb03d97ce66935d14ed4ca768637c9466.zip | |
shrink_to_fit() improvements.
Diffstat (limited to 'include')
| -rw-r--r-- | include/stc/cdeq.h | 6 | ||||
| -rw-r--r-- | include/stc/cvec.h | 14 |
2 files changed, 11 insertions, 9 deletions
diff --git a/include/stc/cdeq.h b/include/stc/cdeq.h index 01590f47..8bfda771 100644 --- a/include/stc/cdeq.h +++ b/include/stc/cdeq.h @@ -95,8 +95,10 @@ cx_memb(_with_capacity)(size_t n) { STC_INLINE void
cx_memb(_shrink_to_fit)(Self *self) {
- Self cx = cx_memb(_clone)(*self);
- cx_memb(_del)(self); *self = cx;
+ if (cx_memb(_size)(*self) != cx_memb(_capacity)(*self)) {
+ Self cx = cx_memb(_clone)(*self);
+ cx_memb(_del)(self); *self = cx;
+ }
}
#ifndef i_queue
diff --git a/include/stc/cvec.h b/include/stc/cvec.h index c6dcb147..b8d0ac33 100644 --- a/include/stc/cvec.h +++ b/include/stc/cvec.h @@ -134,8 +134,7 @@ cx_memb(_with_capacity)(size_t size) { STC_INLINE void
cx_memb(_shrink_to_fit)(Self *self) {
- Self cx = cx_memb(_clone)(*self);
- cx_memb(_del)(self); *self = cx;
+ cx_memb(_reserve)(self, cx_memb(_size)(*self));
}
STC_INLINE void
@@ -255,10 +254,10 @@ cx_memb(_del)(Self* self) { STC_DEF void
cx_memb(_reserve)(Self* self, size_t cap) {
struct cvec_rep* rep = cvec_rep_(self);
- size_t len = rep->size, oldcap = rep->cap;
- if (cap > oldcap) {
- rep = (struct cvec_rep*) c_realloc(oldcap ? rep : NULL,
- offsetof(struct cvec_rep, data) + cap*sizeof(i_val));
+ size_t len = rep->size;
+ if (cap >= len && cap != rep->cap) {
+ rep = (struct cvec_rep*) c_realloc(rep->cap ? rep : NULL,
+ offsetof(struct cvec_rep, data) + cap*sizeof(i_val));
self->data = (cx_value_t*) rep->data;
rep->size = len;
rep->cap = cap;
@@ -267,7 +266,8 @@ 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(_reserve)(self, len);
+ 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]);
|
