1*e4b17023SJohn Marino /* SparseSet implementation.
2*e4b17023SJohn Marino Copyright (C) 2007, 2010 Free Software Foundation, Inc.
3*e4b17023SJohn Marino Contributed by Peter Bergner <bergner@vnet.ibm.com>
4*e4b17023SJohn Marino
5*e4b17023SJohn Marino This file is part of GCC.
6*e4b17023SJohn Marino
7*e4b17023SJohn Marino GCC is free software; you can redistribute it and/or modify it under
8*e4b17023SJohn Marino the terms of the GNU General Public License as published by the Free
9*e4b17023SJohn Marino Software Foundation; either version 3, or (at your option) any later
10*e4b17023SJohn Marino version.
11*e4b17023SJohn Marino
12*e4b17023SJohn Marino GCC is distributed in the hope that it will be useful, but WITHOUT ANY
13*e4b17023SJohn Marino WARRANTY; without even the implied warranty of MERCHANTABILITY or
14*e4b17023SJohn Marino FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
15*e4b17023SJohn Marino for more details.
16*e4b17023SJohn Marino
17*e4b17023SJohn Marino You should have received a copy of the GNU General Public License
18*e4b17023SJohn Marino along with GCC; see the file COPYING3. If not see
19*e4b17023SJohn Marino <http://www.gnu.org/licenses/>. */
20*e4b17023SJohn Marino
21*e4b17023SJohn Marino #ifndef GCC_SPARSESET_H
22*e4b17023SJohn Marino #define GCC_SPARSESET_H
23*e4b17023SJohn Marino
24*e4b17023SJohn Marino #define SPARSESET_ELT_BITS ((unsigned) HOST_BITS_PER_WIDEST_FAST_INT)
25*e4b17023SJohn Marino #define SPARSESET_ELT_TYPE unsigned int
26*e4b17023SJohn Marino
27*e4b17023SJohn Marino /* Data Structure used for the SparseSet representation. */
28*e4b17023SJohn Marino
29*e4b17023SJohn Marino typedef struct sparseset_def
30*e4b17023SJohn Marino {
31*e4b17023SJohn Marino SPARSESET_ELT_TYPE *dense; /* Dense array. */
32*e4b17023SJohn Marino SPARSESET_ELT_TYPE *sparse; /* Sparse array. */
33*e4b17023SJohn Marino SPARSESET_ELT_TYPE members; /* Number of elements. */
34*e4b17023SJohn Marino SPARSESET_ELT_TYPE size; /* Maximum number of elements. */
35*e4b17023SJohn Marino SPARSESET_ELT_TYPE iter; /* Iterator index. */
36*e4b17023SJohn Marino unsigned char iter_inc; /* Iteration increment amount. */
37*e4b17023SJohn Marino bool iterating;
38*e4b17023SJohn Marino SPARSESET_ELT_TYPE elms[2]; /* Combined dense and sparse arrays. */
39*e4b17023SJohn Marino } *sparseset;
40*e4b17023SJohn Marino
41*e4b17023SJohn Marino #define sparseset_free(MAP) free(MAP)
42*e4b17023SJohn Marino extern sparseset sparseset_alloc (SPARSESET_ELT_TYPE n_elms);
43*e4b17023SJohn Marino extern void sparseset_clear_bit (sparseset, SPARSESET_ELT_TYPE);
44*e4b17023SJohn Marino extern void sparseset_copy (sparseset, sparseset);
45*e4b17023SJohn Marino extern void sparseset_and (sparseset, sparseset, sparseset);
46*e4b17023SJohn Marino extern void sparseset_and_compl (sparseset, sparseset, sparseset);
47*e4b17023SJohn Marino extern void sparseset_ior (sparseset, sparseset, sparseset);
48*e4b17023SJohn Marino extern bool sparseset_equal_p (sparseset, sparseset);
49*e4b17023SJohn Marino
50*e4b17023SJohn Marino /* Operation: S = {}
51*e4b17023SJohn Marino Clear the set of all elements. */
52*e4b17023SJohn Marino
53*e4b17023SJohn Marino static inline void
sparseset_clear(sparseset s)54*e4b17023SJohn Marino sparseset_clear (sparseset s)
55*e4b17023SJohn Marino {
56*e4b17023SJohn Marino s->members = 0;
57*e4b17023SJohn Marino s->iterating = false;
58*e4b17023SJohn Marino }
59*e4b17023SJohn Marino
60*e4b17023SJohn Marino /* Return the number of elements currently in the set. */
61*e4b17023SJohn Marino
62*e4b17023SJohn Marino static inline SPARSESET_ELT_TYPE
sparseset_cardinality(sparseset s)63*e4b17023SJohn Marino sparseset_cardinality (sparseset s)
64*e4b17023SJohn Marino {
65*e4b17023SJohn Marino return s->members;
66*e4b17023SJohn Marino }
67*e4b17023SJohn Marino
68*e4b17023SJohn Marino /* Return the maximum number of elements this set can hold. */
69*e4b17023SJohn Marino
70*e4b17023SJohn Marino static inline SPARSESET_ELT_TYPE
sparseset_size(sparseset s)71*e4b17023SJohn Marino sparseset_size (sparseset s)
72*e4b17023SJohn Marino {
73*e4b17023SJohn Marino return s->size;
74*e4b17023SJohn Marino }
75*e4b17023SJohn Marino
76*e4b17023SJohn Marino /* Return true if e is a member of the set S, otherwise return false. */
77*e4b17023SJohn Marino
78*e4b17023SJohn Marino static inline bool
sparseset_bit_p(sparseset s,SPARSESET_ELT_TYPE e)79*e4b17023SJohn Marino sparseset_bit_p (sparseset s, SPARSESET_ELT_TYPE e)
80*e4b17023SJohn Marino {
81*e4b17023SJohn Marino SPARSESET_ELT_TYPE idx;
82*e4b17023SJohn Marino
83*e4b17023SJohn Marino gcc_assert (e < s->size);
84*e4b17023SJohn Marino
85*e4b17023SJohn Marino idx = s->sparse[e];
86*e4b17023SJohn Marino
87*e4b17023SJohn Marino return idx < s->members && s->dense[idx] == e;
88*e4b17023SJohn Marino }
89*e4b17023SJohn Marino
90*e4b17023SJohn Marino /* Low level insertion routine not meant for use outside of sparseset.[ch].
91*e4b17023SJohn Marino Assumes E is valid and not already a member of the set S. */
92*e4b17023SJohn Marino
93*e4b17023SJohn Marino static inline void
sparseset_insert_bit(sparseset s,SPARSESET_ELT_TYPE e,SPARSESET_ELT_TYPE idx)94*e4b17023SJohn Marino sparseset_insert_bit (sparseset s, SPARSESET_ELT_TYPE e, SPARSESET_ELT_TYPE idx)
95*e4b17023SJohn Marino {
96*e4b17023SJohn Marino s->sparse[e] = idx;
97*e4b17023SJohn Marino s->dense[idx] = e;
98*e4b17023SJohn Marino }
99*e4b17023SJohn Marino
100*e4b17023SJohn Marino /* Operation: S = S + {e}
101*e4b17023SJohn Marino Insert E into the set S, if it isn't already a member. */
102*e4b17023SJohn Marino
103*e4b17023SJohn Marino static inline void
sparseset_set_bit(sparseset s,SPARSESET_ELT_TYPE e)104*e4b17023SJohn Marino sparseset_set_bit (sparseset s, SPARSESET_ELT_TYPE e)
105*e4b17023SJohn Marino {
106*e4b17023SJohn Marino if (!sparseset_bit_p (s, e))
107*e4b17023SJohn Marino sparseset_insert_bit (s, e, s->members++);
108*e4b17023SJohn Marino }
109*e4b17023SJohn Marino
110*e4b17023SJohn Marino /* Return and remove an arbitrary element from the set S. */
111*e4b17023SJohn Marino
112*e4b17023SJohn Marino static inline SPARSESET_ELT_TYPE
sparseset_pop(sparseset s)113*e4b17023SJohn Marino sparseset_pop (sparseset s)
114*e4b17023SJohn Marino {
115*e4b17023SJohn Marino SPARSESET_ELT_TYPE mem = s->members;
116*e4b17023SJohn Marino
117*e4b17023SJohn Marino gcc_assert (mem != 0);
118*e4b17023SJohn Marino
119*e4b17023SJohn Marino s->members = mem - 1;
120*e4b17023SJohn Marino return s->dense[mem];
121*e4b17023SJohn Marino }
122*e4b17023SJohn Marino
123*e4b17023SJohn Marino static inline void
sparseset_iter_init(sparseset s)124*e4b17023SJohn Marino sparseset_iter_init (sparseset s)
125*e4b17023SJohn Marino {
126*e4b17023SJohn Marino s->iter = 0;
127*e4b17023SJohn Marino s->iter_inc = 1;
128*e4b17023SJohn Marino s->iterating = true;
129*e4b17023SJohn Marino }
130*e4b17023SJohn Marino
131*e4b17023SJohn Marino static inline bool
sparseset_iter_p(sparseset s)132*e4b17023SJohn Marino sparseset_iter_p (sparseset s)
133*e4b17023SJohn Marino {
134*e4b17023SJohn Marino if (s->iterating && s->iter < s->members)
135*e4b17023SJohn Marino return true;
136*e4b17023SJohn Marino else
137*e4b17023SJohn Marino return s->iterating = false;
138*e4b17023SJohn Marino }
139*e4b17023SJohn Marino
140*e4b17023SJohn Marino static inline SPARSESET_ELT_TYPE
sparseset_iter_elm(sparseset s)141*e4b17023SJohn Marino sparseset_iter_elm (sparseset s)
142*e4b17023SJohn Marino {
143*e4b17023SJohn Marino return s->dense[s->iter];
144*e4b17023SJohn Marino }
145*e4b17023SJohn Marino
146*e4b17023SJohn Marino static inline void
sparseset_iter_next(sparseset s)147*e4b17023SJohn Marino sparseset_iter_next (sparseset s)
148*e4b17023SJohn Marino {
149*e4b17023SJohn Marino s->iter += s->iter_inc;
150*e4b17023SJohn Marino s->iter_inc = 1;
151*e4b17023SJohn Marino }
152*e4b17023SJohn Marino
153*e4b17023SJohn Marino #define EXECUTE_IF_SET_IN_SPARSESET(SPARSESET, ITER) \
154*e4b17023SJohn Marino for (sparseset_iter_init (SPARSESET); \
155*e4b17023SJohn Marino sparseset_iter_p (SPARSESET) \
156*e4b17023SJohn Marino && (((ITER) = sparseset_iter_elm (SPARSESET)) || 1); \
157*e4b17023SJohn Marino sparseset_iter_next (SPARSESET))
158*e4b17023SJohn Marino
159*e4b17023SJohn Marino #endif /* GCC_SPARSESET_H */
160