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
27 #define SPARSESET_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
28 #define SPARSESET_ELT_TYPE unsigned int
30 /* Data Structure used for the SparseSet representation. */
32 typedef struct sparseset_def
34 SPARSESET_ELT_TYPE
*dense
; /* Dense array. */
35 SPARSESET_ELT_TYPE
*sparse
; /* Sparse array. */
36 SPARSESET_ELT_TYPE members
; /* Number of elements. */
37 SPARSESET_ELT_TYPE size
; /* Maximum number of elements. */
38 SPARSESET_ELT_TYPE iter
; /* Iterator index. */
39 unsigned char iter_inc
; /* Iteration increment amount. */
41 SPARSESET_ELT_TYPE elms
[2]; /* Combined dense and sparse arrays. */
44 #define sparseset_free(MAP) free(MAP)
45 extern sparseset
sparseset_alloc (SPARSESET_ELT_TYPE n_elms
);
46 extern void sparseset_clear_bit (sparseset
, SPARSESET_ELT_TYPE
);
47 extern void sparseset_copy (sparseset
, sparseset
);
48 extern void sparseset_and (sparseset
, sparseset
, sparseset
);
49 extern void sparseset_and_compl (sparseset
, sparseset
, sparseset
);
50 extern void sparseset_ior (sparseset
, sparseset
, sparseset
);
51 extern bool sparseset_equal_p (sparseset
, sparseset
);
54 Clear the set of all elements. */
57 sparseset_clear (sparseset s
)
63 /* Return the number of elements currently in the set. */
65 static inline SPARSESET_ELT_TYPE
66 sparseset_cardinality (sparseset s
)
71 /* Return the maximum number of elements this set can hold. */
73 static inline SPARSESET_ELT_TYPE
74 sparseset_size (sparseset s
)
79 /* Return true if e is a member of the set S, otherwise return false. */
82 sparseset_bit_p (sparseset s
, SPARSESET_ELT_TYPE e
)
84 SPARSESET_ELT_TYPE idx
;
86 gcc_assert (e
< s
->size
);
90 return idx
< s
->members
&& s
->dense
[idx
] == e
;
93 /* Low level insertion routine not meant for use outside of sparseset.[ch].
94 Assumes E is valid and not already a member of the set S. */
97 sparseset_insert_bit (sparseset s
, SPARSESET_ELT_TYPE e
, SPARSESET_ELT_TYPE idx
)
103 /* Operation: S = S + {e}
104 Insert E into the set S, if it isn't already a member. */
107 sparseset_set_bit (sparseset s
, SPARSESET_ELT_TYPE e
)
109 if (!sparseset_bit_p (s
, e
))
110 sparseset_insert_bit (s
, e
, s
->members
++);
113 /* Return and remove an arbitrary element from the set S. */
115 static inline SPARSESET_ELT_TYPE
116 sparseset_pop (sparseset s
)
118 SPARSESET_ELT_TYPE mem
= s
->members
;
120 gcc_assert (mem
!= 0);
122 s
->members
= mem
- 1;
123 return s
->dense
[mem
];
127 sparseset_iter_init (sparseset s
)
135 sparseset_iter_p (sparseset s
)
137 if (s
->iterating
&& s
->iter
< s
->members
)
140 return s
->iterating
= false;
143 static inline SPARSESET_ELT_TYPE
144 sparseset_iter_elm (sparseset s
)
146 return s
->dense
[s
->iter
];
150 sparseset_iter_next (sparseset s
)
152 s
->iter
+= s
->iter_inc
;
156 #define EXECUTE_IF_SET_IN_SPARSESET(SPARSESET, ITER) \
157 for (sparseset_iter_init (SPARSESET); \
158 sparseset_iter_p (SPARSESET) \
159 && (((ITER) = sparseset_iter_elm (SPARSESET)) || 1); \
160 sparseset_iter_next (SPARSESET))
162 #endif /* GCC_SPARSESET_H */