XPoint 0.1.0
Hardware-agnostic crosspoint matrix routing library
Loading...
Searching...
No Matches
BitPool.h
Go to the documentation of this file.
1// SPDX-License-Identifier: AGPL-3.0-only
2// Copyright (c) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
3// https://github.com/dstroy0/XPoint
4
21#ifndef BITPOOL_H
22#define BITPOOL_H
23
24#include <stdint.h>
25#include <string.h>
26
27#if defined(__GNUC__) || defined(__clang__)
28#define XPOINT_PACKED __attribute__((packed))
29#else
30#define XPOINT_PACKED
31#endif
32
34{
35 public:
40 static uint16_t wordsFor(uint32_t nBits)
41 {
42 return (nBits == 0u) ? 0u : (uint16_t)((nBits + 31u) / 32u);
43 }
44
53 explicit BitPool(uint32_t nBits);
54
65 BitPool(uint32_t *words, uint32_t nBits);
66
67 ~BitPool();
68
69 BitPool(const BitPool &) = delete;
70 BitPool &operator=(const BitPool &) = delete;
71
73 bool valid() const
74 {
75 return _words != nullptr;
76 }
77
85 bool get(uint32_t n) const
86 {
87 return (_words[n >> 5] >> (n & 0x1Fu)) & 1u;
88 }
89
98 void set(uint32_t n, bool v)
99 {
100 if (v)
101 _words[n >> 5] |= (1u << (n & 0x1Fu));
102 else
103 _words[n >> 5] &= ~(1u << (n & 0x1Fu));
104 }
105
107 void clear();
108
109 private:
110 uint32_t *_words;
111 uint16_t _nWords;
112 bool _owns;
113};
114
115#endif // BITPOOL_H
#define XPOINT_PACKED
Definition BitPool.h:30
Definition BitPool.h:34
BitPool(const BitPool &)=delete
void set(uint32_t n, bool v)
Write bit n.
Definition BitPool.h:98
BitPool & operator=(const BitPool &)=delete
bool valid() const
True if the pool has a valid (non-null) word array.
Definition BitPool.h:73
bool get(uint32_t n) const
Read bit n.
Definition BitPool.h:85
static uint16_t wordsFor(uint32_t nBits)
Words required to hold nBits bits.
Definition BitPool.h:40