GBAlatro
A Demake of Balatro for the GBA
Loading...
Searching...
No Matches
bitset.h
Go to the documentation of this file.
1
6#ifndef BITSET_H
7#define BITSET_H
8
9#include <stdbool.h>
10#include <stdint.h>
11
18#define BITSET_BITS_PER_WORD 32
19
28#define BITSET_ARRAY_SIZE 8
29
34#define BITSET_MAX_BITS (BITSET_BITS_PER_WORD * BITSET_ARRAY_SIZE)
35
39typedef struct Bitset
40{
44 uint32_t* w;
45
49 uint32_t nbits;
50
54 uint32_t nwords;
55
59 uint32_t cap;
61
72typedef struct
73{
77 const Bitset* bitset;
78
82 int word;
83
87 int bit;
88
92 int itr;
93} BitsetItr;
94
102void bitset_set_idx(Bitset* bitset, int idx, bool on);
103
112bool bitset_get_idx(Bitset* bitset, int idx);
113
122
128void bitset_clear(Bitset* bitset);
129
137bool bitset_is_empty(Bitset* bitset);
138
146int bitset_num_set_bits(Bitset* bitset);
147
158int bitset_find_idx_of_nth_set(const Bitset* bitset, int n);
159
167BitsetItr bitset_itr_create(const Bitset* bitset);
168
176int bitset_itr_next(BitsetItr* itr);
177
198#define BITSET_DEFINE(name, capacity) \
199 static uint32_t name##_w[BITSET_ARRAY_SIZE] = {0}; \
200 static Bitset name = { \
201 .w = name##_w, \
202 .nbits = BITSET_BITS_PER_WORD, \
203 .nwords = BITSET_ARRAY_SIZE, \
204 .cap = capacity, \
205 };
206
207#endif // BITSET_H
bool bitset_get_idx(Bitset *bitset, int idx)
Get the value of a flag.
Definition bitset.c:72
bool bitset_is_empty(Bitset *bitset)
Check if a bitset is empty (all 0's)
Definition bitset.c:62
void bitset_clear(Bitset *bitset)
Clear the bitset, all to 0.
Definition bitset.c:54
int bitset_num_set_bits(Bitset *bitset)
Count how many bits are set to 1 in a bitset.
Definition bitset.c:80
int bitset_find_idx_of_nth_set(const Bitset *bitset, int n)
Find the index of the nth set bit.
Definition bitset.c:92
int bitset_set_next_free_idx(Bitset *bitset)
Set the next free index in the bitset and return the index value.
Definition bitset.c:27
BitsetItr bitset_itr_create(const Bitset *bitset)
Declare a BitsetItr.
Definition bitset.c:128
int bitset_itr_next(BitsetItr *itr)
Get the index of the next set bit in the bitset from a BitsetItr.
Definition bitset.c:140
void bitset_set_idx(Bitset *bitset, int idx, bool on)
Set a flag in a bitset to a value.
Definition bitset.c:5
An iterator into a Bitset.
Definition bitset.h:73
int bit
Current bit the iterator is on.
Definition bitset.h:87
int word
Current word the iterator is on.
Definition bitset.h:82
int itr
Number of bits that have been iterated through in total.
Definition bitset.h:92
const Bitset * bitset
Bitset this is iterating through
Definition bitset.h:77
A bitset spread across multiple uint32_t words.
Definition bitset.h:40
uint32_t nbits
Number of bits in a word, will be 32.
Definition bitset.h:49
uint32_t nwords
Number of words int the w array.
Definition bitset.h:54
uint32_t cap
Number of actual flags (nbits * nwords)
Definition bitset.h:59
uint32_t * w
Word array of uint32_t to hold the bitset data.
Definition bitset.h:44