Skip to content

Commit

Permalink
Optimized reserve() in both hmap and queue + some cleanup.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tyge Lovset committed Jan 12, 2025
1 parent 918d764 commit 48d1922
Show file tree
Hide file tree
Showing 6 changed files with 15 additions and 22 deletions.
6 changes: 4 additions & 2 deletions docs/algorithm_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -469,7 +469,9 @@ Find linearily in containers using a predicate. `value` is a pointer to each ele
- `c_find_reverse_if(CntType, startiter, enditer, outiter_ptr, pred)`

### c_append, c_append_if
Append linearily in containers using a predicate. `value` is a pointer to each element in predicate.
Clones any container onto an arbitrary container type, optionally using a predicate to filter out elements.
Requires only that the element types are equal for the two containers.
`value` is the pointer to each element in predicate. See example below for usage.
- `c_append(CntType, outcnt_ptr, cnt)`
- `c_append(OutCnt, outcnt_ptr, CntType, cnt)`
- `c_append_if(CntType, outcnt_ptr, cnt, pred)`
Expand Down Expand Up @@ -502,10 +504,10 @@ Erase linearily in containers using a predicate. `value` is a pointer to each el

int main(void)
{
// Clone all *value > 10 to outvec. Note: `value` is a pointer to current element
Vec vec = c_make(Vec, {2, 30, 21, 5, 9, 11});
Vec outvec = {0};

// Clone all *value > 10 to outvec. Note: `value` is a pointer to current element
c_append_if(Vec, &outvec, vec, *value > 10);
c_foreach (i, Vec, outvec) printf(" %d", *i.ref);
puts("");
Expand Down
4 changes: 2 additions & 2 deletions include/stc/hmap.h
Original file line number Diff line number Diff line change
Expand Up @@ -452,11 +452,11 @@ _c_MEMB(_bucket_insert_)(const Self* self, const _m_keyraw* rkeyptr) {
STC_DEF bool
_c_MEMB(_reserve)(Self* self, const isize _newcap) {
const isize _oldbucks = self->bucket_count;
if (_newcap != self->size && _newcap <= _oldbucks)
return true;
isize _newbucks = (isize)((float)_newcap / (i_max_load_factor)) + 4;
_newbucks = c_next_pow2(_newbucks);

if (_newcap < self->size || _newbucks == _oldbucks)
return true;
Self map = {
_i_malloc(_m_value, _newbucks),
_i_calloc(struct hmap_meta, _newbucks + 1),
Expand Down
2 changes: 1 addition & 1 deletion include/stc/pqueue.h
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
#define _i_is_pqueue
#include "priv/template.h"
#ifndef i_declared
_c_DEFTYPES(_c_pqueue_types, Self, i_key);
_c_DEFTYPES(_c_vec_types, Self, i_key);
#endif
typedef i_keyraw _m_raw;

Expand Down
14 changes: 7 additions & 7 deletions include/stc/priv/queue_prv.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@ _c_DEFTYPES(_c_deque_types, Self, i_key);
#endif
typedef i_keyraw _m_raw;

STC_API Self _c_MEMB(_with_capacity)(const isize n);
STC_API bool _c_MEMB(_reserve)(Self* self, const isize n);
STC_API Self _c_MEMB(_with_capacity)(const isize cap);
STC_API bool _c_MEMB(_reserve)(Self* self, const isize cap);
STC_API void _c_MEMB(_clear)(Self* self);
STC_API void _c_MEMB(_drop)(const Self* cself);
STC_API _m_value* _c_MEMB(_push)(Self* self, _m_value value); // push_back
Expand Down Expand Up @@ -176,17 +176,17 @@ _c_MEMB(_drop)(const Self* cself) {
}

STC_DEF Self
_c_MEMB(_with_capacity)(const isize n) {
_c_MEMB(_with_capacity)(const isize cap) {
Self cx = {0};
_c_MEMB(_reserve)(&cx, n);
_c_MEMB(_reserve)(&cx, cap);
return cx;
}

STC_DEF bool
_c_MEMB(_reserve)(Self* self, const isize n) {
if (n <= self->capmask)
_c_MEMB(_reserve)(Self* self, const isize cap) {
isize oldpow2 = self->capmask + 1, newpow2 = c_next_pow2(cap + 1);
if (newpow2 <= oldpow2)
return true;
isize oldpow2 = self->capmask + 1, newpow2 = c_next_pow2(n + 1);
_m_value* d = (_m_value *)i_realloc(self->cbuf, oldpow2*c_sizeof *d, newpow2*c_sizeof *d);
if (d == NULL)
return false;
Expand Down
2 changes: 1 addition & 1 deletion include/stc/stack.h
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
#define i_no_clone
_c_DEFTYPES(_c_stack_fixed, Self, i_key, i_capacity);
#else
_c_DEFTYPES(_c_stack_types, Self, i_key);
_c_DEFTYPES(_c_vec_types, Self, i_key);
#endif
#endif
typedef i_keyraw _m_raw;
Expand Down
9 changes: 0 additions & 9 deletions include/stc/types.h
Original file line number Diff line number Diff line change
Expand Up @@ -199,18 +199,9 @@ typedef union {
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
typedef struct SELF { SELF##_value data[CAP]; ptrdiff_t size; } SELF

#define _c_stack_types(SELF, VAL) \
typedef VAL SELF##_value; \
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
typedef struct SELF { SELF##_value* data; ptrdiff_t size, capacity; _i_aux_struct } SELF

#define _c_vec_types(SELF, VAL) \
typedef VAL SELF##_value; \
typedef struct { SELF##_value *ref, *end; } SELF##_iter; \
typedef struct SELF { SELF##_value *data; ptrdiff_t size, capacity; _i_aux_struct } SELF

#define _c_pqueue_types(SELF, VAL) \
typedef VAL SELF##_value; \
typedef struct SELF { SELF##_value* data; ptrdiff_t size, capacity; _i_aux_struct } SELF

#endif // STC_TYPES_H_INCLUDED

0 comments on commit 48d1922

Please sign in to comment.