2 * This program is free software; you can redistribute it and/or modify
3 * it under the terms of the GNU General Public License as published by
4 * the Free Software Foundation; either version 2 of the License, or (at
5 * your option) any later version.
7 * This program is distributed in the hope that it will be useful, but
8 * WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
10 * General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program; if not, write to the Free Software
14 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
18 * Copyright © 2007 Pierre Habouzit
21 #ifndef MUTT_LIB_LIB_BITS_H
22 #define MUTT_LIB_LIB_BITS_H
24 typedef struct bits_t {
30 void bits_extend(bits_t *bits, int pos);
32 DO_INIT(bits_t, bits);
33 static inline void bits_wipe(bits_t *bits) {
34 p_delete(&bits->bits);
37 DO_DELETE(bits_t, bits);
39 static inline void bits_clear(bits_t *bits)
45 static inline bool bit_isset(const bits_t *bits, int pos)
47 int offs = (pos >> 3) - bits->start;
48 if (offs < 0 || offs > bits->size)
51 return (bits->bits[offs] & (1 << (pos & 7))) != 0;
54 static inline void bit_set(bits_t *bits, int pos)
56 int offs = (pos >> 3) - bits->start;
57 if (offs < 0 || offs > bits->size) {
58 bits_extend(bits, pos);
59 offs = (pos >> 3) - bits->start;
61 bits->bits[offs] |= 1 << (pos & 7);
64 static inline void bit_clear(bits_t *bits, int pos)
66 int offs = (pos >> 3) - bits->start;
67 if (offs < 0 || offs > bits->size)
69 bits->bits[offs] &= ~(1 << (pos & 7));
72 static inline void bit_toggle(bits_t *bits, int pos)
74 int offs = (pos >> 3) - bits->start;
75 if (offs < 0 || offs > bits->size) {
76 bits_extend(bits, pos);
77 offs = (pos >> 3) - bits->start;
79 bits->bits[offs] ^= 1 << (pos & 7);
83 #endif /* MUTT_LIB_LIB_BITS_H */