diff options
| author | Tyge Løvset <[email protected]> | 2021-10-07 09:06:50 +0200 |
|---|---|---|
| committer | Tyge Løvset <[email protected]> | 2021-10-07 09:06:50 +0200 |
| commit | e1465027f5b3eb96a2a186dc35fb93d73ff9548d (patch) | |
| tree | 3577af371107eee4b66811a3c53e789cd37bad87 | |
| parent | d8eaf9acb03d97ce66935d14ed4ca768637c9466 (diff) | |
| download | STC-modified-e1465027f5b3eb96a2a186dc35fb93d73ff9548d.tar.gz STC-modified-e1465027f5b3eb96a2a186dc35fb93d73ff9548d.zip | |
Improved cpqueu_push() impl.
| -rw-r--r-- | include/stc/cpque.h | 13 |
1 files changed, 7 insertions, 6 deletions
diff --git a/include/stc/cpque.h b/include/stc/cpque.h index 642f04cb..b7a26360 100644 --- a/include/stc/cpque.h +++ b/include/stc/cpque.h @@ -140,12 +140,13 @@ cx_memb(_erase_at)(Self* self, size_t idx) { STC_DEF void
cx_memb(_push)(Self* self, cx_value_t value) {
- cx_memb(_push_back)(self, value); /* sift-up the value */
- size_t n = cx_memb(_size)(*self), c = n;
- cx_value_t *arr = self->data - 1;
- for (; c > 1 && i_cmp(&arr[c >> 1], &value) < 0; c >>= 1)
- arr[c] = arr[c >> 1];
- if (c != n) arr[c] = 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;
}
#endif
|
