From 9af33149569e14ffcac0150ce694bcf0af7baac5 Mon Sep 17 00:00:00 2001 From: Tyge Løvset Date: Wed, 20 Jan 2021 11:47:53 +0100 Subject: Added cbits_at(). Fix in astar.c --- docs/cbits_api.md | 4 +++- examples/stc_astar.c | 8 ++++---- stc/cbits.h | 26 ++++++++++++++++++-------- 3 files changed, 25 insertions(+), 13 deletions(-) diff --git a/docs/cbits_api.md b/docs/cbits_api.md index f2e135fd..9da2ad04 100644 --- a/docs/cbits_api.md +++ b/docs/cbits_api.md @@ -47,11 +47,13 @@ bool cbits_is_subset(cbits_t set, cbits_t other); bool cbits_is_superset(cbits_t set, cbits_t other); char* cbits_to_str(cbits_t set, char* str, size_t start, intptr_t stop); +bool cbits_test(cbits_t set, size_t i); +bool cbits_at(cbits_t set, size_t i); // same as cbits_test() + void cbits_set(cbits_t *self, size_t i); void cbits_reset(cbits_t *self, size_t i); void cbits_set_value(cbits_t *self, size_t i, bool value); void cbits_flip(cbits_t *self, size_t i); -bool cbits_test(cbits_t set, size_t i); void cbits_set_all(cbits_t *self, bool value); void cbits_set_all64(cbits_t *self, uint64_t pattern); void cbits_flip_all(cbits_t *self); diff --git a/examples/stc_astar.c b/examples/stc_astar.c index 6e970a3c..6421693a 100644 --- a/examples/stc_astar.c +++ b/examples/stc_astar.c @@ -25,7 +25,7 @@ mpoint_init(int x, int y, int width) int mpoint_compare_priority(const MazePoint* a, const MazePoint* b) { - // NB! returning 0 gives 14 steps shorter path!? hmm.. + //return 0; // NB! gives 14 steps shorter path!? hmm.. return (a->priorty > b->priorty) - (a->priorty < b->priorty); } @@ -96,7 +96,7 @@ astar(cstr maze, int width) { -1, -1, 0, width }, { 0, -1, 0, width }, { 1, -1, 0, width }, }; - for (size_t i = 0; i < c_arraylen(deltas); i++) + c_forrange (i, c_arraylen(deltas)) { MazePoint delta = deltas[i]; MazePoint next = mpoint_init(current.x + delta.x, current.y + delta.y, width); @@ -106,10 +106,10 @@ astar(cstr maze, int width) csmap_mc_value_t* cost = csmap_mc_find(&cost_so_far, next); if (!cost || new_cost < cost->second) { - csmap_mc_emplace(&cost_so_far, next, new_cost); + csmap_mc_put(&cost_so_far, next, new_cost); // update (put) next.priorty = new_cost + abs(goal.x - next.x) + abs(goal.y - next.y); cpque_mp_push(&frontier, next); - csmap_ms_emplace(&came_from, next, current); + csmap_ms_put(&came_from, next, current); } } } diff --git a/stc/cbits.h b/stc/cbits.h index 258f9d09..0ac0f4ad 100644 --- a/stc/cbits.h +++ b/stc/cbits.h @@ -33,14 +33,20 @@ int main() { cbits_t bset = cbits_with_size(23, true); cbits_reset(&bset, 9); cbits_resize(&bset, 43, false); - printf("%4zu: ", bset.size); c_forrange (i, bset.size) printf("%d", cbits_test(&bset, i)); + + printf("%4zu: ", bset.size); + c_forrange (i, bset.size) + printf("%d", cbits_at(&bset, i)); puts(""); cbits_set(&bset, 28); cbits_resize(&bset, 77, true); cbits_resize(&bset, 93, false); cbits_resize(&bset, 102, true); cbits_set_value(&bset, 99, false); - printf("%4zu: ", bset.size); c_forrange (i, bset.size) printf("%d", cbits_test(&bset, i)); + + printf("%4zu: ", bset.size); + c_forrange (i, bset.size) + printf("%d", cbits_at(&bset, i)); puts(""); cbits_del(&bset); } @@ -51,10 +57,10 @@ int main() { typedef struct cbits { uint64_t* _arr; size_t size; } cbits_t, cbits; -STC_API cbits_t cbits_with_size(size_t size, bool value); -STC_API cbits_t cbits_from_str(const char* str); +STC_API cbits_t cbits_with_size(size_t size, bool value); +STC_API cbits_t cbits_from_str(const char* str); STC_API char* cbits_to_str(cbits_t set, char* str, size_t start, intptr_t stop); -STC_API cbits_t cbits_clone(cbits_t other); +STC_API cbits_t cbits_clone(cbits_t other); STC_API void cbits_resize(cbits_t* self, size_t size, bool value); STC_API size_t cbits_count(cbits_t set); STC_API bool cbits_is_disjoint(cbits_t set, cbits_t other); @@ -84,6 +90,13 @@ STC_INLINE cbits_t cbits_move(cbits_t* self) { return tmp; } +STC_INLINE bool cbits_test(cbits_t set, size_t i) { + return (set._arr[i >> 6] & (1ull << (i & 63))) != 0; +} +STC_INLINE bool cbits_at(cbits_t set, size_t i) { + return (set._arr[i >> 6] & (1ull << (i & 63))) != 0; +} + STC_INLINE void cbits_set(cbits_t *self, size_t i) { self->_arr[i >> 6] |= 1ull << (i & 63); } @@ -96,9 +109,6 @@ STC_INLINE void cbits_set_value(cbits_t *self, size_t i, bool value) { STC_INLINE void cbits_flip(cbits_t *self, size_t i) { self->_arr[i >> 6] ^= 1ull << (i & 63); } -STC_INLINE bool cbits_test(cbits_t set, size_t i) { - return (set._arr[i >> 6] & (1ull << (i & 63))) != 0; -} STC_INLINE void cbits_set_all(cbits_t *self, bool value) { memset(self->_arr, value ? 0xff : 0x0, ((self->size + 63) >> 6) * 8); } -- cgit v1.2.3