2007-10-09 Rodrigo Kumpera <rkumpera@novell.com>
[mono.git] / mono / utils / monobitset.h
1 #ifndef __MONO_BITSET_H__
2 #define __MONO_BITSET_H__
3
4 #include <glib.h>
5
6 #ifndef MONO_ZERO_LEN_ARRAY
7 #ifdef __GNUC__
8 #define MONO_ZERO_LEN_ARRAY 0
9 #else
10 #define MONO_ZERO_LEN_ARRAY 1
11 #endif
12 #endif
13
14 #define MONO_BITSET_BITS_PER_CHUNK (8 * sizeof (gsize))
15
16 typedef struct {
17         gsize size;
18         gsize flags;
19         gsize data [MONO_ZERO_LEN_ARRAY];
20 } MonoBitSet;
21
22 typedef void (*MonoBitSetFunc) (guint idx, gpointer data);
23
24 enum {
25         MONO_BITSET_DONT_FREE = 1
26 };
27
28 /* Fast access to bits which depends on the implementation of the bitset */
29 #define mono_bitset_test_fast(set,n) ((set)->data [(n)/MONO_BITSET_BITS_PER_CHUNK] & ((gsize)1 << ((n) % MONO_BITSET_BITS_PER_CHUNK)))
30 #define mono_bitset_test_fast(set,n) ((set)->data [(n)/MONO_BITSET_BITS_PER_CHUNK] & ((gsize)1 << ((n) % MONO_BITSET_BITS_PER_CHUNK)))
31 #define mono_bitset_set_fast(set,n) do { (set)->data [(n)/MONO_BITSET_BITS_PER_CHUNK] |= ((gsize)1 << ((n) % MONO_BITSET_BITS_PER_CHUNK)); } while (0)
32 #define mono_bitset_clear_fast(set,n) do { (set)->data [(n)/MONO_BITSET_BITS_PER_CHUNK] &= ~((gsize)1 << ((n) % MONO_BITSET_BITS_PER_CHUNK)); } while (0)
33 #define mono_bitset_get_fast(set,n) ((set)->data[(n)])
34
35 #define mono_bitset_copyto_fast(src,dest) do { memcpy (&(dest)->data, &(src)->data, (dest)->size / 8); } while (0)
36
37 #define mono_bitset_union_fast(dest,src) do { \
38     MonoBitSet *tmp_src = (src); \
39     MonoBitSet *tmp_dest = (dest); \
40     int i, size; \
41         size = tmp_dest->size / MONO_BITSET_BITS_PER_CHUNK; \
42         for (i = 0; i < size; ++i) \
43                 tmp_dest->data [i] |= tmp_src->data [i]; \
44 } while (0)
45
46 #define mono_bitset_sub_fast(dest,src) do { \
47     MonoBitSet *tmp_src = (src); \
48     MonoBitSet *tmp_dest = (dest); \
49     int i, size; \
50         size = tmp_dest->size / MONO_BITSET_BITS_PER_CHUNK; \
51         for (i = 0; i < size; ++i) \
52                 tmp_dest->data [i] &= ~tmp_src->data [i]; \
53 } while (0)
54
55 /*
56  * Interface documentation can be found in the c-file.
57  * Interface documentation by Dennis Haney.
58  */
59
60 guint32     mono_bitset_alloc_size   (guint32 max_size, guint32 flags);
61
62 MonoBitSet* mono_bitset_new          (guint32 max_size, guint32 flags);
63
64 MonoBitSet* mono_bitset_mem_new      (gpointer mem, guint32 max_size, guint32 flags);
65
66 void        mono_bitset_free         (MonoBitSet *set); 
67
68 void        mono_bitset_set          (MonoBitSet *set, guint32 pos);
69
70 void        mono_bitset_set_all      (MonoBitSet *set);
71
72 int         mono_bitset_test         (const MonoBitSet *set, guint32 pos);
73
74 gsize       mono_bitset_test_bulk    (const MonoBitSet *set, guint32 pos);
75
76 void        mono_bitset_clear        (MonoBitSet *set, guint32 pos);
77
78 void        mono_bitset_clear_all    (MonoBitSet *set);
79
80 void        mono_bitset_invert       (MonoBitSet *set);
81
82 guint32     mono_bitset_size         (const MonoBitSet *set);
83
84 guint32     mono_bitset_count        (const MonoBitSet *set);
85
86 void        mono_bitset_low_high     (const MonoBitSet *set, guint32 *low, guint32 *high);
87
88 int         mono_bitset_find_start   (const MonoBitSet *set);
89
90 int         mono_bitset_find_first   (const MonoBitSet *set, gint pos);
91
92 int         mono_bitset_find_last    (const MonoBitSet *set, gint pos);
93
94 int         mono_bitset_find_first_unset (const MonoBitSet *set, gint pos);
95
96 MonoBitSet* mono_bitset_clone        (const MonoBitSet *set, guint32 new_size);
97
98 void        mono_bitset_copyto       (const MonoBitSet *src, MonoBitSet *dest);
99
100 void        mono_bitset_union        (MonoBitSet *dest, const MonoBitSet *src);
101
102 void        mono_bitset_intersection (MonoBitSet *dest, const MonoBitSet *src);
103
104 void        mono_bitset_sub          (MonoBitSet *dest, const MonoBitSet *src);
105
106 gboolean    mono_bitset_equal        (const MonoBitSet *src, const MonoBitSet *src1);
107
108 void        mono_bitset_foreach      (MonoBitSet *set, MonoBitSetFunc func, gpointer data);
109
110 void        mono_bitset_intersection_2 (MonoBitSet *dest, const MonoBitSet *src1, const MonoBitSet *src2);
111
112 #endif /* __MONO_BITSET_H__ */