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