aboutsummaryrefslogtreecommitdiff
path: root/src/bitmask.h
blob: 846180e6412b92760cd1b8c1406f4aa384055bc8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
/* This file is part of Mailfromd.
   Copyright (C) 2009, 2010 Sergey Poznyakoff

   This program is free software; you can redistribute it and/or modify
   it under the terms of the GNU General Public License as published by
   the Free Software Foundation; either version 3, or (at your option)
   any later version.

   This program is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
   GNU General Public License for more details.

   You should have received a copy of the GNU General Public License
   along with this program.  If not, see <http://www.gnu.org/licenses/>. */

#include <stdlib.h>

/* FIXME: Ensure that sizeof(bitmask_bits_t)<=sizeof(void*) */
typedef unsigned bitmask_bits_t;

struct bitmask
{
	size_t bm_nset;              /* Number of bits set */
	size_t bm_size;              /* Number of elements in bm_bits */
	bitmask_bits_t *bm_bits;     
};

#define NBMBITS (sizeof(bitmask_bits_t) * 8)

#define BIT_WORD(n) ((n) / NBMBITS)
#define BIT_MASK(n) ((bitmask_bits_t)1 << ((n) % NBMBITS))

#define bitmask_zero(bm)						\
	do {								\
		memset((bm)->bm_bits, 0,				\
		       (bm)->bm_size * sizeof(bitmask_bits_t));		\
		(bm)->bm_nset = 0;					\
	} while (0)

#define bitmask_init(bm) memset(bm, 0, sizeof((bm)[0])) 
#define bitmask_nset(bm) ((bm)->bm_nset)
#define bitmask_max(bm) ((bm)->bm_nset * NBMBITS)

#define __bitmask_expand(bm,n)					 	  \
	do {								  \
		size_t __n = n;						  \
		(bm)->bm_bits = xrealloc((bm)->bm_bits,			  \
					 (__n) * sizeof(bitmask_bits_t)); \
		memset((bm)->bm_bits + (bm)->bm_size, 0,		  \
		       ((__n) - (bm)->bm_size) * sizeof(bitmask_bits_t)); \
		(bm)->bm_size = __n;					  \
	} while (0)

#define bitmask_set(bm,n)						\
	do {								\
		unsigned num = BIT_WORD(n);				\
		if (num >= (bm)->bm_size)				\
			__bitmask_expand(bm, num+1);			\
		(bm)->bm_bits[num] |= BIT_MASK(n);			\
		(bm)->bm_nset++;					\
	} while (0)

#define bitmask_isset(bm,n)						\
	((BIT_WORD(n) < (bm)->bm_size)					\
	 && (bm)->bm_bits[BIT_WORD(n)] & BIT_MASK(n))

#define bitmask_clr(bm,n)						\
	do {								\
		unsigned num = BIT_WORD(n);				\
		if (num < (bm)->bm_size) {				\
			(bm)->bm_bits[num] &= ~BIT_MASK(n);		\
			(bm)->bm_nset--;				\
		}							\
	} while (0)

#define bitmask_merge(d,s)					\
	do {							\
		int __i;					\
		if ((s)->bm_size > (d)->bm_size)		\
			__bitmask_expand(d, (s)->bm_size);	\
		for (__i = 0; __i < (s)->bm_size; __i++)	\
			(d)->bm_bits[__i] |= (s)->bm_bits[__i];	\
	} while (0)

Return to:

Send suggestions and report system problems to the System administrator.