rlib
Convenience library for useful things
Loading...
Searching...
No Matches
rbitset.h
Go to the documentation of this file.
1/* RLIB - Convenience library for useful things
2 * Copyright (C) 2016 Haakon Sporsheim <haakon.sporsheim@gmail.com>
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 3.0 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13 *
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library.
16 * See the COPYING file at the root of the source repository.
17 */
18#ifndef __R_BITSET_H__
19#define __R_BITSET_H__
20
21#if !defined(__RLIB_H_INCLUDE_GUARD__) && !defined(RLIB_COMPILATION)
22#error "#include <rlib.h> only please."
23#endif
24
46#include <rlib/rtypes.h>
47#include <rlib/rmem.h>
48
50
59typedef void (*RBitsetFunc) (rsize bit, rpointer user);
60
68typedef struct {
71 rbsword data[0];
72} RBitset;
73
80#define r_bitset_init_stack(BS, BITS) \
81 (((BS) = r_alloca0 (sizeof (RBitset) + sizeof (rbsword) * (1 + (BITS - 1) / (sizeof (rbsword) * 8)))) != NULL && \
82 ((BS)->bits = (BITS)) > 0 && \
83 ((BS)->words = (1 + (BITS - 1) / (sizeof (rbsword) * 8))) > 0)
88#define r_bitset_init_heap(BS, BITS) \
89 (((BS) = r_malloc0 (sizeof (RBitset) + sizeof (rbsword) * (1 + (BITS - 1) / (sizeof (rbsword) * 8)))) != NULL && \
90 ((BS)->bits = (BITS)) > 0 && \
91 ((BS)->words = (1 + (BITS - 1) / (sizeof (rbsword) * 8))) > 0)
92
105 const rsize * bits, rsize count, rboolean set);
107#define r_bitset_clear(bs) r_bitset_set_all (bs, FALSE)
132#define r_bitset_inv(bs) r_bitset_not (bs, bs)
137
153
162
167R_API void r_bitset_foreach (const RBitset * bitset, rboolean set,
168 RBitsetFunc func, rpointer user);
169
173R_API rboolean r_bitset_or (RBitset * dest, const RBitset * a, const RBitset * b);
175R_API rboolean r_bitset_xor (RBitset * dest, const RBitset * a, const RBitset * b);
177R_API rboolean r_bitset_and (RBitset * dest, const RBitset * a, const RBitset * b);
178
179R_END_DECLS
180
183#endif /* __R_BITSET_H__ */
184
rboolean r_bitset_set_u8_at(RBitset *bitset, ruint8 u8, rsize bit)
Write an 8-bit value starting at bit position bit.
void r_bitset_foreach(const RBitset *bitset, rboolean set, RBitsetFunc func, rpointer user)
Invoke func once for every bit equal to set (TRUE = visit set bits, FALSE = visit clear bits).
ruint16 r_bitset_get_u16_at(const RBitset *bitset, rsize bit)
Read a 16-bit value starting at bit position bit.
ruint64 rbsword
Machine word backing RBitset storage (64 bits).
Definition rbitset.h:52
rboolean r_bitset_copy(RBitset *dest, const RBitset *src)
Copy src into dest (both must have the same bit count).
RBitset * r_bitset_new_from_binary(rconstpointer data, rsize size)
Allocate a bitset from a packed binary blob.
rboolean r_bitset_set_from_human_readable(RBitset *bitset, const rchar *str, rsize *bits)
Parse a human-readable bitset spec (e.g. "0,3-5,17") into bitset.
rboolean r_bitset_is_bit_set(const RBitset *bitset, rsize bit)
TRUE iff bit is set.
ruint64 r_bitset_get_u64_at(const RBitset *bitset, rsize bit)
Read a 64-bit value starting at bit position bit.
rboolean r_bitset_set_bit(RBitset *bitset, rsize bit, rboolean set)
Set or clear a single bit.
rboolean r_bitset_not(RBitset *dest, const RBitset *src)
dest = ~src.
rboolean r_bitset_set_bits(RBitset *bitset, const rsize *bits, rsize count, rboolean set)
Set or clear each bit listed in bits.
rboolean r_bitset_set_u64_at(RBitset *bitset, ruint64 u64, rsize bit)
Write a 64-bit value starting at bit position bit.
rboolean r_bitset_and(RBitset *dest, const RBitset *a, const RBitset *b)
dest = a & b.
rboolean r_bitset_set_u16_at(RBitset *bitset, ruint16 u16, rsize bit)
Write a 16-bit value starting at bit position bit.
rboolean r_bitset_shl(RBitset *a, ruint count)
In-place left shift by count bits.
rchar * r_bitset_to_human_readable(const RBitset *bitset)
Render the set bits as a human-readable string (e.g. "0,3-5,17").
ruint8 r_bitset_get_u8_at(const RBitset *bitset, rsize bit)
Read an 8-bit value starting at bit position bit.
rboolean r_bitset_xor(RBitset *dest, const RBitset *a, const RBitset *b)
dest = a ^ b.
rboolean r_bitset_set_from_human_readable_file(RBitset *bitset, const rchar *file, rsize *bits)
Same as r_bitset_set_from_human_readable but reads the spec from file.
rboolean r_bitset_set_n_bits_at(RBitset *bitset, rsize n, rsize bit, rboolean set)
Set or clear n consecutive bits starting at bit.
rboolean r_bitset_or(RBitset *dest, const RBitset *a, const RBitset *b)
dest = a | b.
void(* RBitsetFunc)(rsize bit, rpointer user)
Per-bit visitor signature for r_bitset_foreach.
Definition rbitset.h:59
rsize r_bitset_popcount(const RBitset *bitset)
Number of set bits.
ruint32 r_bitset_get_u32_at(const RBitset *bitset, rsize bit)
Read a 32-bit value starting at bit position bit.
rsize r_bitset_ctz(const RBitset *bitset)
Count trailing zeros (from bit 0).
rboolean r_bitset_set_all(RBitset *bitset, rboolean set)
Set every bit to set.
rsize r_bitset_clz(const RBitset *bitset)
Count leading zeros (from the high bit).
rboolean r_bitset_set_u32_at(RBitset *bitset, ruint32 u32, rsize bit)
Write a 32-bit value starting at bit position bit.
rboolean r_bitset_shr(RBitset *a, ruint count)
In-place right shift by count bits.
#define R_API
Public-API decoration: resolves to R_API_EXPORT while building rlib and R_API_IMPORT for consumers.
Definition rmacros.h:115
#define R_BEGIN_DECLS
Open an extern "C" block under C++ (no-op in C).
Definition rmacros.h:196
char rchar
Default character type (char).
Definition rtypes.h:137
int rboolean
Boolean type (typedef'd to int).
Definition rtypes.h:59
unsigned int ruint
Unsigned int.
Definition rtypes.h:157
unsigned int ruint32
Unsigned 32-bit integer.
Definition rtypes.h:192
void * rpointer
Generic pointer (alias for void *).
Definition rtypes.h:327
unsigned char ruint8
Unsigned 8-bit integer.
Definition rtypes.h:190
const void * rconstpointer
Generic const pointer (alias for const void *).
Definition rtypes.h:329
unsigned long rsize
Unsigned pointer-sized size type (like size_t).
Definition rtypes.h:290
unsigned long ruint64
Unsigned 64-bit integer.
Definition rtypes.h:193
unsigned short ruint16
Unsigned 16-bit integer.
Definition rtypes.h:191
Memory allocation, byte-buffer operations, and pattern scanning.
Foundational type aliases used by every rlib header.
Fixed-width bitset.
Definition rbitset.h:68
rsize words
Definition rbitset.h:70
rsize bits
Definition rbitset.h:69