1 /* SparseSet implementation.
2 Copyright (C) 2007 Free Software Foundation, Inc.
3 Contributed by Peter Bergner <bergner@vnet.ibm.com>
5 This file is part of GCC.
7 GCC is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 3, or (at your option) any later
12 GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with GCC; see the file COPYING3. If not see
19 <http://www.gnu.org/licenses/>. */
21 #ifndef GCC_SPARSESET_H
22 #define GCC_SPARSESET_H
26 #define SPARSESET_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
27 #define SPARSESET_ELT_TYPE unsigned int
29 /* Data Structure used for the SparseSet representation. */
31 typedef struct sparseset_def
33 SPARSESET_ELT_TYPE
*dense
; /* Dense array. */
34 SPARSESET_ELT_TYPE
*sparse
; /* Sparse array. */
35 SPARSESET_ELT_TYPE members
; /* Number of elements. */
36 SPARSESET_ELT_TYPE size
; /* Maximum number of elements. */
37 SPARSESET_ELT_TYPE iter
; /* Iterator index. */
38 unsigned char iter_inc
; /* Iteration increment amount. */
40 SPARSESET_ELT_TYPE elms
[2]; /* Combined dense and sparse arrays. */
43 #define sparseset_free(MAP) free(MAP)
44 extern sparseset
sparseset_alloc (SPARSESET_ELT_TYPE n_elms
);
45 extern void sparseset_clear_bit (sparseset
, SPARSESET_ELT_TYPE
);
46 extern void sparseset_copy (sparseset
, sparseset
);
47 extern void sparseset_and (sparseset
, sparseset
, sparseset
);
48 extern void sparseset_and_compl (sparseset
, sparseset
, sparseset
);
49 extern void sparseset_ior (sparseset
, sparseset
, sparseset
);
50 extern bool sparseset_equal_p (sparseset
, sparseset
);
53 Clear the set of all elements. */
56 sparseset_clear (sparseset s
)
62 /* Return the number of elements currently in the set. */
64 static inline SPARSESET_ELT_TYPE
65 sparseset_cardinality (sparseset s
)
70 /* Return the maximum number of elements this set can hold. */
72 static inline SPARSESET_ELT_TYPE
73 sparseset_size (sparseset s
)
78 /* Return true if e is a member of the set S, otherwise return false. */
81 sparseset_bit_p (sparseset s
, SPARSESET_ELT_TYPE e
)
83 SPARSESET_ELT_TYPE idx
;
85 gcc_assert (e
< s
->size
);
89 return idx
< s
->members
&& s
->dense
[idx
] == e
;
92 /* Low level insertion routine not meant for use outside of sparseset.[ch].
93 Assumes E is valid and not already a member of the set S. */
96 sparseset_insert_bit (sparseset s
, SPARSESET_ELT_TYPE e
, SPARSESET_ELT_TYPE idx
)
102 /* Operation: S = S + {e}
103 Insert E into the set S, if it isn't already a member. */
106 sparseset_set_bit (sparseset s
, SPARSESET_ELT_TYPE e
)
108 if (!sparseset_bit_p (s
, e
))
109 sparseset_insert_bit (s
, e
, s
->members
++);
112 /* Return and remove an arbitrary element from the set S. */
114 static inline SPARSESET_ELT_TYPE
115 sparseset_pop (sparseset s
)
117 SPARSESET_ELT_TYPE mem
= s
->members
;
119 gcc_assert (mem
!= 0);
121 s
->members
= mem
- 1;
122 return s
->dense
[mem
];
126 sparseset_iter_init (sparseset s
)
134 sparseset_iter_p (sparseset s
)
136 if (s
->iterating
&& s
->iter
< s
->members
)
139 return s
->iterating
= false;
142 static inline SPARSESET_ELT_TYPE
143 sparseset_iter_elm (sparseset s
)
145 return s
->dense
[s
->iter
];
149 sparseset_iter_next (sparseset s
)
151 s
->iter
+= s
->iter_inc
;
155 #define EXECUTE_IF_SET_IN_SPARSESET(SPARSESET, ITER) \
156 for (sparseset_iter_init (SPARSESET); \
157 sparseset_iter_p (SPARSESET) \
158 && (((ITER) = sparseset_iter_elm (SPARSESET)) || 1); \
159 sparseset_iter_next (SPARSESET))
161 #endif /* GCC_SPARSESET_H */