Add clang format and used it for bitset (#240)

* Add clang format file .clang-format

* Use clang format on bitset


---------

Co-authored-by: MeirGavish <meir.gavish@gmail.com>
This commit is contained in:
Rickey
2025-11-18 08:56:32 +02:00
committed by GitHub
co-authored by MeirGavish
parent f3f375cef1
commit a4caa44580
4 changed files with 108 additions and 47 deletions
+26 -23
View File
@@ -1,7 +1,8 @@
#include "bitset.h"
#include "util.h"
void bitset_set_idx(Bitset *bitset, int idx, bool on)
void bitset_set_idx(Bitset* bitset, int idx, bool on)
{
uint32_t i = idx / BITSET_BITS_PER_WORD;
uint32_t b = idx % BITSET_BITS_PER_WORD;
@@ -10,10 +11,10 @@ void bitset_set_idx(Bitset *bitset, int idx, bool on)
// These are more efficient, but removed for readability
// See: https://github.com/cellos51/balatro-gba/pull/132#discussion_r2365966071
// Divide by 32 to get the word index
//uint32_t i = idx >> 5;
// uint32_t i = idx >> 5;
// Get last 5-bits, same as a modulo (% 32) operation on positive numbers
//uint32_t b = idx & 0x1F;
if(on)
// uint32_t b = idx & 0x1F;
if (on)
{
bitset->w[i] |= (uint32_t)1 << b;
}
@@ -23,7 +24,7 @@ void bitset_set_idx(Bitset *bitset, int idx, bool on)
}
}
int bitset_allocate_idx(Bitset *bitset)
int bitset_allocate_idx(Bitset* bitset)
{
for (uint32_t i = 0; i < bitset->nwords; i++)
{
@@ -49,24 +50,25 @@ int bitset_allocate_idx(Bitset *bitset)
return UNDEFINED;
}
void bitset_clear(Bitset *bitset)
void bitset_clear(Bitset* bitset)
{
for(int i = 0; i < bitset->nwords; i++)
for (int i = 0; i < bitset->nwords; i++)
{
bitset->w[i] = 0;
}
}
bool bitset_is_empty(Bitset *bitset)
bool bitset_is_empty(Bitset* bitset)
{
for(int i = 0; i < bitset->nwords; i++)
for (int i = 0; i < bitset->nwords; i++)
{
if(bitset->w[i]) return false;
if (bitset->w[i])
return false;
}
return true;
}
bool bitset_get_idx(Bitset *bitset, int idx)
bool bitset_get_idx(Bitset* bitset, int idx)
{
uint32_t i = idx / BITSET_BITS_PER_WORD;
uint32_t b = idx % BITSET_BITS_PER_WORD;
@@ -74,11 +76,11 @@ bool bitset_get_idx(Bitset *bitset, int idx)
return bitset->w[i] & (uint32_t)1 << b;
}
int bitset_num_set_bits(Bitset *bitset)
int bitset_num_set_bits(Bitset* bitset)
{
int sum = 0;
for(int i = 0; i < bitset->nwords; i++)
for (int i = 0; i < bitset->nwords; i++)
{
sum += __builtin_popcount(bitset->w[i]);
}
@@ -86,23 +88,25 @@ int bitset_num_set_bits(Bitset *bitset)
return sum;
}
int bitset_find_idx_of_nth_set(const Bitset *bitset, int n)
int bitset_find_idx_of_nth_set(const Bitset* bitset, int n)
{
int tracker = 0;
int prev_tracker = 0;
for(int i = 0; i < bitset->nwords; i++)
for (int i = 0; i < bitset->nwords; i++)
{
tracker += __builtin_popcount(bitset->w[i]);
if(tracker > n)
if (tracker > n)
{
// The index is here somewhere
int base = prev_tracker - 1; // this one is to count the 1's not the offset, underflow to -1 is good for finding the 0 index
int offset = bitset->nbits * i; // this one is for the actual offset we want to map the id to
// this one is to count the 1's not the offset, underflow to -1 is good for finding the 0 index
int base = prev_tracker - 1;
// this one is for the actual offset we want to map the id to
int offset = bitset->nbits * i;
for (int j = 0; j < bitset->nbits; j++)
{
if(base == n)
if (base == n)
{
return offset - 1;
}
@@ -121,8 +125,7 @@ int bitset_find_idx_of_nth_set(const Bitset *bitset, int n)
BitsetItr bitset_itr_create(const Bitset* bitset)
{
BitsetItr itr =
{
BitsetItr itr = {
.bitset = bitset,
.word = 0,
.bit = 0,
@@ -142,13 +145,13 @@ int bitset_itr_next(BitsetItr* itr)
// So one last thing you could do is something like `bitset_allocate_idx` does with the
// __builtin_ctz function as well.
//
// The point being, this can be very slow, but it's simple and can be much faster.
// The point being, this can be very slow, but it's simple and can be much faster.
for (; itr->word < itr->bitset->nwords; itr->word++)
{
for (; itr->bit < itr->bitset->nbits; itr->bit++)
{
itr->itr++;
if(itr->bitset->w[itr->word] & (1 << itr->bit))
if (itr->bitset->w[itr->word] & (1 << itr->bit))
{
// if itr->bit == nbits on the next run, the for loop will handle it
itr->bit++;