Skip to content

Commit

Permalink
ba_check_and_set_bit (#54)
Browse files Browse the repository at this point in the history
* add bit array check and set
* update changelog
  • Loading branch information
barrust committed Sep 9, 2021
1 parent 270a3cb commit 8c33522
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 5 deletions.
24 changes: 20 additions & 4 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,22 @@
# C-Utils

## Version 0.2.5

***Updates:***

*c-utils*
* Add check and set function to return value of bit prior to setting

*fileutils*
* Add `fs_is_symlink()` function to check if a file or directory is actually a symlink

*strlib*
* Better memory check for `s_append_alt()`

*graph*
* Cleaned-up a re-used variable name to ensure no confusion for the user


## Version 0.2.4
***New Libraries:***
* permutations
Expand All @@ -23,7 +40,6 @@
* Fix output for `between` checks on failed assertion



## Version 0.2.3
***Updates:***

Expand Down Expand Up @@ -54,14 +70,14 @@
## Version 0.2.1
***Updates:***

*Graph:*
*graph*
* Traversal of the graph from a particular vertex
* Breadth First
* Depth First
* OpenMP support

*StringLib:*
* Micro optimizations for s_trim and s_standardize_whitespace
*stringlib:*
* Micro optimizations for `s_trim` and `s_standardize_whitespace`


## Version 0.2.0
Expand Down
7 changes: 7 additions & 0 deletions src/bitarray.c
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,13 @@ int ba_check_bit(bitarray_t ba, size_t bit) {
return (CHECK_BIT(ba->arr, bit) != 0) ? BIT_SET : BIT_NOT_SET;
}

int ba_check_and_set_bit(bitarray_t ba, size_t bit) {
int is_set = ba_check_bit(ba, bit);

if (is_set == BIT_NOT_SET)
ba_set_bit(ba, bit);
return is_set;
}

int ba_toggle_bit(bitarray_t ba, size_t bit) {
if (bit >= ba->num_bits)
Expand Down
6 changes: 5 additions & 1 deletion src/bitarray.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
*** Author: Tyler Barrus
*** email: barrust@gmail.com
***
*** Version: 0.1.1
*** Version: 0.1.2
*** Purpose: Simple bit array library
***
*** License: MIT 2019
Expand Down Expand Up @@ -56,6 +56,10 @@ int ba_set_bit(bitarray_t ba, size_t bit);
BIT_NOT_SET if false */
int ba_check_bit(bitarray_t ba, size_t bit);

/* Check if bit `bit` was previously set; return BIT_SET if true and
BIT_NOT_SET if false and set the bit to 1 */
int ba_check_and_set_bit(bitarray_t ba, size_t bit);

/* Toggle the bit by setting it to BIT_NOT_SET if currently is BIT_SET or
vice versa; returns the updated value of the bit */
int ba_toggle_bit(bitarray_t ba, size_t bit);
Expand Down
26 changes: 26 additions & 0 deletions tests/bitarray_test.c
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,31 @@ MU_TEST(test_check_bit) {
ba_free(ba);
}

/*******************************************************************************
* Test check and set bit
*******************************************************************************/
MU_TEST(test_check_and_set_bit) {
bitarray_t ba = ba_init(125); /* bit array of length 125 */
int res;
res = ba_set_bit(ba, 0);
res = ba_set_bit(ba, 50);
res = ba_set_bit(ba, 75);

/* check out of bounds still caught */
res = ba_check_and_set_bit(ba, 125);
mu_assert_int_eq(BITARRAY_INDEX_ERROR, res);

res = ba_check_and_set_bit(ba, 0);
mu_assert_int_eq(BIT_SET, res);

res = ba_check_and_set_bit(ba, 55);
mu_assert_int_eq(BIT_NOT_SET, res);

res = ba_check_and_set_bit(ba, 55);
mu_assert_int_eq(BIT_SET, res);

ba_free(ba);
}

/*******************************************************************************
* Test clear bit
Expand Down Expand Up @@ -189,6 +214,7 @@ MU_TEST_SUITE(test_suite) {
MU_RUN_TEST(test_default_setup);
MU_RUN_TEST(test_set_bit);
MU_RUN_TEST(test_check_bit);
MU_RUN_TEST(test_check_and_set_bit);
MU_RUN_TEST(test_clear_bit);
MU_RUN_TEST(test_reset_bitarray);
MU_RUN_TEST(test_print_array);
Expand Down

0 comments on commit 8c33522

Please sign in to comment.