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