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