PR testsuite/32076
[official-gcc.git] / gcc / sparseset.h
blob96ee19acdd787fb2f2e06700a8420a91a0fa8c13
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
10 version.
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
15 for more details.
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
24 #include "system.h"
25 #include <assert.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. */
40 bool iterating;
41 SPARSESET_ELT_TYPE elms[2]; /* Combined dense and sparse arrays. */
42 } *sparseset;
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);
53 /* Operation: S = {}
54 Clear the set of all elements. */
56 static inline void
57 sparseset_clear (sparseset s)
59 s->members = 0;
60 s->iterating = false;
63 /* Return the number of elements currently in the set. */
65 static inline SPARSESET_ELT_TYPE
66 sparseset_cardinality (sparseset s)
68 return s->members;
71 /* Return the maximum number of elements this set can hold. */
73 static inline SPARSESET_ELT_TYPE
74 sparseset_size (sparseset s)
76 return s->size;
79 /* Return true if e is a member of the set S, otherwise return false. */
81 static inline bool
82 sparseset_bit_p (sparseset s, SPARSESET_ELT_TYPE e)
84 SPARSESET_ELT_TYPE idx;
86 gcc_assert (e < s->size);
88 idx = s->sparse[e];
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. */
96 static inline void
97 sparseset_insert_bit (sparseset s, SPARSESET_ELT_TYPE e, SPARSESET_ELT_TYPE idx)
99 s->sparse[e] = idx;
100 s->dense[idx] = e;
103 /* Operation: S = S + {e}
104 Insert E into the set S, if it isn't already a member. */
106 static inline void
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];
126 static inline void
127 sparseset_iter_init (sparseset s)
129 s->iter = 0;
130 s->iter_inc = 1;
131 s->iterating = true;
134 static inline bool
135 sparseset_iter_p (sparseset s)
137 if (s->iterating && s->iter < s->members)
138 return true;
139 else
140 return s->iterating = false;
143 static inline SPARSESET_ELT_TYPE
144 sparseset_iter_elm (sparseset s)
146 return s->dense[s->iter];
149 static inline void
150 sparseset_iter_next (sparseset s)
152 s->iter += s->iter_inc;
153 s->iter_inc = 1;
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 */