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 #include "libiberty.h"
22 #include "sparseset.h"
25 /* Allocate and clear a n_elms SparseSet. */
28 sparseset_alloc (SPARSESET_ELT_TYPE n_elms
)
30 unsigned int n_bytes
= sizeof (struct sparseset_def
)
31 + ((n_elms
- 1) * 2 * sizeof (SPARSESET_ELT_TYPE
));
33 sparseset set
= (sparseset
) xmalloc (n_bytes
);
34 set
->dense
= &(set
->elms
[0]);
35 set
->sparse
= &(set
->elms
[n_elms
]);
37 sparseset_clear (set
);
41 /* Low level routine not meant for use outside of sparseset.[ch].
42 Assumes idx1 < s->members and idx2 < s->members. */
45 sparseset_swap (sparseset s
, SPARSESET_ELT_TYPE idx1
, SPARSESET_ELT_TYPE idx2
)
47 SPARSESET_ELT_TYPE tmp
= s
->dense
[idx2
];
48 sparseset_insert_bit (s
, s
->dense
[idx1
], idx2
);
49 sparseset_insert_bit (s
, tmp
, idx1
);
52 /* Operation: S = S - {e}
53 Delete e from the set S if it is a member of S. */
56 sparseset_clear_bit (sparseset s
, SPARSESET_ELT_TYPE e
)
58 if (sparseset_bit_p (s
, e
))
60 SPARSESET_ELT_TYPE idx
= s
->sparse
[e
];
61 SPARSESET_ELT_TYPE iter
= s
->iter
;
62 SPARSESET_ELT_TYPE mem
= s
->members
- 1;
64 /* If we are iterating over this set and we want to delete a
65 member we've already visited, then we swap the element we
66 want to delete with the element at the current iteration
67 index so that it plays well together with the code below
68 that actually removes the element. */
69 if (s
->iterating
&& idx
<= iter
)
73 sparseset_swap (s
, idx
, iter
);
79 /* Replace the element we want to delete with the last element
80 in the dense array and then decrement s->members, effectively
81 removing the element we want to delete. */
82 sparseset_insert_bit (s
, s
->dense
[mem
], idx
);
88 Restrictions: none. */
91 sparseset_copy (sparseset d
, sparseset s
)
99 for (i
= 0; i
< s
->members
; i
++)
100 sparseset_insert_bit (d
, s
->dense
[i
], i
);
101 d
->members
= s
->members
;
104 /* Operation: D = A & B.
105 Restrictions: none. */
108 sparseset_and (sparseset d
, sparseset a
, sparseset b
)
110 SPARSESET_ELT_TYPE e
;
115 sparseset_copy (d
, a
);
119 if (d
== a
|| d
== b
)
121 sparseset s
= (d
== a
) ? b
: a
;
123 EXECUTE_IF_SET_IN_SPARSESET (d
, e
)
124 if (!sparseset_bit_p (s
, e
))
125 sparseset_clear_bit (d
, e
);
131 if (sparseset_cardinality (a
) < sparseset_cardinality (b
))
143 EXECUTE_IF_SET_IN_SPARSESET (sml
, e
)
144 if (sparseset_bit_p (lrg
, e
))
145 sparseset_set_bit (d
, e
);
149 /* Operation: D = A & ~B.
150 Restrictions: D != B, unless D == A == B. */
153 sparseset_and_compl (sparseset d
, sparseset a
, sparseset b
)
155 SPARSESET_ELT_TYPE e
;
167 if (sparseset_cardinality (d
) < sparseset_cardinality (b
))
169 EXECUTE_IF_SET_IN_SPARSESET (d
, e
)
170 if (sparseset_bit_p (b
, e
))
171 sparseset_clear_bit (d
, e
);
175 EXECUTE_IF_SET_IN_SPARSESET (b
, e
)
176 sparseset_clear_bit (d
, e
);
182 EXECUTE_IF_SET_IN_SPARSESET (a
, e
)
183 if (!sparseset_bit_p (b
, e
))
184 sparseset_set_bit (d
, e
);
188 /* Operation: D = A | B.
189 Restrictions: none. */
192 sparseset_ior (sparseset d
, sparseset a
, sparseset b
)
194 SPARSESET_ELT_TYPE e
;
197 sparseset_copy (d
, a
);
200 EXECUTE_IF_SET_IN_SPARSESET (a
, e
)
201 sparseset_set_bit (d
, e
);
206 sparseset_copy (d
, a
);
207 EXECUTE_IF_SET_IN_SPARSESET (b
, e
)
208 sparseset_set_bit (d
, e
);
213 Restrictions: none. */
216 sparseset_equal_p (sparseset a
, sparseset b
)
218 SPARSESET_ELT_TYPE e
;
223 if (sparseset_cardinality (a
) != sparseset_cardinality (b
))
226 EXECUTE_IF_SET_IN_SPARSESET (a
, e
)
227 if (!sparseset_bit_p (b
, e
))