Skip to content

Commit 8c33522

Browse files
authored
ba_check_and_set_bit (#54)
* add bit array check and set * update changelog
1 parent 270a3cb commit 8c33522

File tree

4 files changed

+58
-5
lines changed

4 files changed

+58
-5
lines changed

CHANGELOG.md

+20-4
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,22 @@
11
# C-Utils
22

3+
## Version 0.2.5
4+
5+
***Updates:***
6+
7+
*c-utils*
8+
* Add check and set function to return value of bit prior to setting
9+
10+
*fileutils*
11+
* Add `fs_is_symlink()` function to check if a file or directory is actually a symlink
12+
13+
*strlib*
14+
* Better memory check for `s_append_alt()`
15+
16+
*graph*
17+
* Cleaned-up a re-used variable name to ensure no confusion for the user
18+
19+
320
## Version 0.2.4
421
***New Libraries:***
522
* permutations
@@ -23,7 +40,6 @@
2340
* Fix output for `between` checks on failed assertion
2441

2542

26-
2743
## Version 0.2.3
2844
***Updates:***
2945

@@ -54,14 +70,14 @@
5470
## Version 0.2.1
5571
***Updates:***
5672

57-
*Graph:*
73+
*graph*
5874
* Traversal of the graph from a particular vertex
5975
* Breadth First
6076
* Depth First
6177
* OpenMP support
6278

63-
*StringLib:*
64-
* Micro optimizations for s_trim and s_standardize_whitespace
79+
*stringlib:*
80+
* Micro optimizations for `s_trim` and `s_standardize_whitespace`
6581

6682

6783
## Version 0.2.0

src/bitarray.c

+7
Original file line numberDiff line numberDiff line change
@@ -85,6 +85,13 @@ int ba_check_bit(bitarray_t ba, size_t bit) {
8585
return (CHECK_BIT(ba->arr, bit) != 0) ? BIT_SET : BIT_NOT_SET;
8686
}
8787

88+
int ba_check_and_set_bit(bitarray_t ba, size_t bit) {
89+
int is_set = ba_check_bit(ba, bit);
90+
91+
if (is_set == BIT_NOT_SET)
92+
ba_set_bit(ba, bit);
93+
return is_set;
94+
}
8895

8996
int ba_toggle_bit(bitarray_t ba, size_t bit) {
9097
if (bit >= ba->num_bits)

src/bitarray.h

+5-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
*** Author: Tyler Barrus
77
88
***
9-
*** Version: 0.1.1
9+
*** Version: 0.1.2
1010
*** Purpose: Simple bit array library
1111
***
1212
*** License: MIT 2019
@@ -56,6 +56,10 @@ int ba_set_bit(bitarray_t ba, size_t bit);
5656
BIT_NOT_SET if false */
5757
int ba_check_bit(bitarray_t ba, size_t bit);
5858

59+
/* Check if bit `bit` was previously set; return BIT_SET if true and
60+
BIT_NOT_SET if false and set the bit to 1 */
61+
int ba_check_and_set_bit(bitarray_t ba, size_t bit);
62+
5963
/* Toggle the bit by setting it to BIT_NOT_SET if currently is BIT_SET or
6064
vice versa; returns the updated value of the bit */
6165
int ba_toggle_bit(bitarray_t ba, size_t bit);

tests/bitarray_test.c

+26
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,31 @@ MU_TEST(test_check_bit) {
6565
ba_free(ba);
6666
}
6767

68+
/*******************************************************************************
69+
* Test check and set bit
70+
*******************************************************************************/
71+
MU_TEST(test_check_and_set_bit) {
72+
bitarray_t ba = ba_init(125); /* bit array of length 125 */
73+
int res;
74+
res = ba_set_bit(ba, 0);
75+
res = ba_set_bit(ba, 50);
76+
res = ba_set_bit(ba, 75);
77+
78+
/* check out of bounds still caught */
79+
res = ba_check_and_set_bit(ba, 125);
80+
mu_assert_int_eq(BITARRAY_INDEX_ERROR, res);
81+
82+
res = ba_check_and_set_bit(ba, 0);
83+
mu_assert_int_eq(BIT_SET, res);
84+
85+
res = ba_check_and_set_bit(ba, 55);
86+
mu_assert_int_eq(BIT_NOT_SET, res);
87+
88+
res = ba_check_and_set_bit(ba, 55);
89+
mu_assert_int_eq(BIT_SET, res);
90+
91+
ba_free(ba);
92+
}
6893

6994
/*******************************************************************************
7095
* Test clear bit
@@ -189,6 +214,7 @@ MU_TEST_SUITE(test_suite) {
189214
MU_RUN_TEST(test_default_setup);
190215
MU_RUN_TEST(test_set_bit);
191216
MU_RUN_TEST(test_check_bit);
217+
MU_RUN_TEST(test_check_and_set_bit);
192218
MU_RUN_TEST(test_clear_bit);
193219
MU_RUN_TEST(test_reset_bitarray);
194220
MU_RUN_TEST(test_print_array);

0 commit comments

Comments
 (0)