annotate src/share/vm/libadt/vectset.cpp @ 1716:be3f9c242c9d

6948538: CMS: BOT walkers can fall into object allocation and initialization cracks Summary: GC workers now recognize an intermediate transient state of blocks which are allocated but have not yet completed initialization. blk_start() calls do not attempt to determine the size of a block in the transient state, rather waiting for the block to become initialized so that it is safe to query its size. Audited and ensured the order of initialization of object fields (klass, free bit and size) to respect block state transition protocol. Also included some new assertion checking code enabled in debug mode. Reviewed-by: chrisphi, johnc, poonam
author ysr
date Mon, 16 Aug 2010 15:58:42 -0700
parents c18cbe5936b8
children f95d63e2154a
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
2 * Copyright (c) 1997, 2005, Oracle and/or its affiliates. All rights reserved.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
a61af66fc99e Initial load
duke
parents:
diff changeset
4 *
a61af66fc99e Initial load
duke
parents:
diff changeset
5 * This code is free software; you can redistribute it and/or modify it
a61af66fc99e Initial load
duke
parents:
diff changeset
6 * under the terms of the GNU General Public License version 2 only, as
a61af66fc99e Initial load
duke
parents:
diff changeset
7 * published by the Free Software Foundation.
a61af66fc99e Initial load
duke
parents:
diff changeset
8 *
a61af66fc99e Initial load
duke
parents:
diff changeset
9 * This code is distributed in the hope that it will be useful, but WITHOUT
a61af66fc99e Initial load
duke
parents:
diff changeset
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
a61af66fc99e Initial load
duke
parents:
diff changeset
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
a61af66fc99e Initial load
duke
parents:
diff changeset
12 * version 2 for more details (a copy is included in the LICENSE file that
a61af66fc99e Initial load
duke
parents:
diff changeset
13 * accompanied this code).
a61af66fc99e Initial load
duke
parents:
diff changeset
14 *
a61af66fc99e Initial load
duke
parents:
diff changeset
15 * You should have received a copy of the GNU General Public License version
a61af66fc99e Initial load
duke
parents:
diff changeset
16 * 2 along with this work; if not, write to the Free Software Foundation,
a61af66fc99e Initial load
duke
parents:
diff changeset
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
a61af66fc99e Initial load
duke
parents:
diff changeset
18 *
1552
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
20 * or visit www.oracle.com if you need additional information or have any
c18cbe5936b8 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 0
diff changeset
21 * questions.
0
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 // Vector Sets - An Abstract Data Type
a61af66fc99e Initial load
duke
parents:
diff changeset
26
a61af66fc99e Initial load
duke
parents:
diff changeset
27 #include "incls/_precompiled.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
28 #include "incls/_vectset.cpp.incl"
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 // %%%%% includes not needed with AVM framework - Ungar
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // #include "port.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
32 //IMPLEMENTATION
a61af66fc99e Initial load
duke
parents:
diff changeset
33 // #include "vectset.hpp"
a61af66fc99e Initial load
duke
parents:
diff changeset
34
a61af66fc99e Initial load
duke
parents:
diff changeset
35 // BitsInByte is a lookup table which tells the number of bits that
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // are in the looked-up number. It is very useful in VectorSet_Size.
a61af66fc99e Initial load
duke
parents:
diff changeset
37
a61af66fc99e Initial load
duke
parents:
diff changeset
38 uint8 bitsInByte[256] = {
a61af66fc99e Initial load
duke
parents:
diff changeset
39 0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
a61af66fc99e Initial load
duke
parents:
diff changeset
40 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
a61af66fc99e Initial load
duke
parents:
diff changeset
41 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
a61af66fc99e Initial load
duke
parents:
diff changeset
42 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
a61af66fc99e Initial load
duke
parents:
diff changeset
43 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
a61af66fc99e Initial load
duke
parents:
diff changeset
44 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
a61af66fc99e Initial load
duke
parents:
diff changeset
45 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
a61af66fc99e Initial load
duke
parents:
diff changeset
46 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
a61af66fc99e Initial load
duke
parents:
diff changeset
47 1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
a61af66fc99e Initial load
duke
parents:
diff changeset
48 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
a61af66fc99e Initial load
duke
parents:
diff changeset
49 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
a61af66fc99e Initial load
duke
parents:
diff changeset
50 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
a61af66fc99e Initial load
duke
parents:
diff changeset
51 2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
a61af66fc99e Initial load
duke
parents:
diff changeset
52 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
a61af66fc99e Initial load
duke
parents:
diff changeset
53 3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
a61af66fc99e Initial load
duke
parents:
diff changeset
54 4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
a61af66fc99e Initial load
duke
parents:
diff changeset
55 };
a61af66fc99e Initial load
duke
parents:
diff changeset
56
a61af66fc99e Initial load
duke
parents:
diff changeset
57 //------------------------------VectorSet--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
58 // Create a new, empty Set.
a61af66fc99e Initial load
duke
parents:
diff changeset
59 VectorSet::VectorSet(Arena *arena) : Set(arena) {
a61af66fc99e Initial load
duke
parents:
diff changeset
60 size = 2; // Small initial size
a61af66fc99e Initial load
duke
parents:
diff changeset
61 data = (uint32 *)_set_arena->Amalloc(size*sizeof(uint32));
a61af66fc99e Initial load
duke
parents:
diff changeset
62 data[0] = 0; // No elements
a61af66fc99e Initial load
duke
parents:
diff changeset
63 data[1] = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
64 }
a61af66fc99e Initial load
duke
parents:
diff changeset
65
a61af66fc99e Initial load
duke
parents:
diff changeset
66 //------------------------------Construct--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
67 Set &VectorSet_Construct(Arena *arena)
a61af66fc99e Initial load
duke
parents:
diff changeset
68 {
a61af66fc99e Initial load
duke
parents:
diff changeset
69 return *(new VectorSet(arena));
a61af66fc99e Initial load
duke
parents:
diff changeset
70 }
a61af66fc99e Initial load
duke
parents:
diff changeset
71
a61af66fc99e Initial load
duke
parents:
diff changeset
72 //------------------------------operator=--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
73 Set &VectorSet::operator = (const Set &set)
a61af66fc99e Initial load
duke
parents:
diff changeset
74 {
a61af66fc99e Initial load
duke
parents:
diff changeset
75 if( &set == this ) return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
76 FREE_FAST(data);
a61af66fc99e Initial load
duke
parents:
diff changeset
77 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
78 slamin(*(set.asVectorSet()));
a61af66fc99e Initial load
duke
parents:
diff changeset
79 return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
80 }
a61af66fc99e Initial load
duke
parents:
diff changeset
81
a61af66fc99e Initial load
duke
parents:
diff changeset
82 //------------------------------slamin-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
83 // Initialize one set with another. No regard is made to the existing Set.
a61af66fc99e Initial load
duke
parents:
diff changeset
84 void VectorSet::slamin(const VectorSet& s)
a61af66fc99e Initial load
duke
parents:
diff changeset
85 {
a61af66fc99e Initial load
duke
parents:
diff changeset
86 size = s.size; // Use new size
a61af66fc99e Initial load
duke
parents:
diff changeset
87 data = (uint32*)s._set_arena->Amalloc(size*sizeof(uint32)); // Make array of required size
a61af66fc99e Initial load
duke
parents:
diff changeset
88 memcpy( data, s.data, size*sizeof(uint32) ); // Fill the array
a61af66fc99e Initial load
duke
parents:
diff changeset
89 }
a61af66fc99e Initial load
duke
parents:
diff changeset
90
a61af66fc99e Initial load
duke
parents:
diff changeset
91 //------------------------------grow-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
92 // Expand the existing set to a bigger size
a61af66fc99e Initial load
duke
parents:
diff changeset
93 void VectorSet::grow( uint newsize )
a61af66fc99e Initial load
duke
parents:
diff changeset
94 {
a61af66fc99e Initial load
duke
parents:
diff changeset
95 newsize = (newsize+31) >> 5; // Convert to longwords
a61af66fc99e Initial load
duke
parents:
diff changeset
96 uint x = size;
a61af66fc99e Initial load
duke
parents:
diff changeset
97 while( x < newsize ) x <<= 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
98 data = (uint32 *)_set_arena->Arealloc(data, size*sizeof(uint32), x*sizeof(uint32));
a61af66fc99e Initial load
duke
parents:
diff changeset
99 memset((char *)(data + size), 0, (x - size)*sizeof(uint32));
a61af66fc99e Initial load
duke
parents:
diff changeset
100 size = x;
a61af66fc99e Initial load
duke
parents:
diff changeset
101 }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 //------------------------------operator<<=------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
104 // Insert a member into an existing Set.
a61af66fc99e Initial load
duke
parents:
diff changeset
105 Set &VectorSet::operator <<= (uint elem)
a61af66fc99e Initial load
duke
parents:
diff changeset
106 {
a61af66fc99e Initial load
duke
parents:
diff changeset
107 register uint word = elem >> 5; // Get the longword offset
a61af66fc99e Initial load
duke
parents:
diff changeset
108 register uint32 mask = 1L << (elem & 31); // Get bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
109
a61af66fc99e Initial load
duke
parents:
diff changeset
110 if( word >= size ) // Need to grow set?
a61af66fc99e Initial load
duke
parents:
diff changeset
111 grow(elem+1); // Then grow it
a61af66fc99e Initial load
duke
parents:
diff changeset
112 data[word] |= mask; // Set new bit
a61af66fc99e Initial load
duke
parents:
diff changeset
113 return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
114 }
a61af66fc99e Initial load
duke
parents:
diff changeset
115
a61af66fc99e Initial load
duke
parents:
diff changeset
116 //------------------------------operator>>=------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
117 // Delete a member from an existing Set.
a61af66fc99e Initial load
duke
parents:
diff changeset
118 Set &VectorSet::operator >>= (uint elem)
a61af66fc99e Initial load
duke
parents:
diff changeset
119 {
a61af66fc99e Initial load
duke
parents:
diff changeset
120 register uint word = elem >> 5; // Get the longword offset
a61af66fc99e Initial load
duke
parents:
diff changeset
121 if( word >= size ) // Beyond the last?
a61af66fc99e Initial load
duke
parents:
diff changeset
122 return *this; // Then it's clear & return clear
a61af66fc99e Initial load
duke
parents:
diff changeset
123 register uint32 mask = 1L << (elem & 31); // Get bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
124 data[word] &= ~mask; // Clear bit
a61af66fc99e Initial load
duke
parents:
diff changeset
125 return *this;
a61af66fc99e Initial load
duke
parents:
diff changeset
126 }
a61af66fc99e Initial load
duke
parents:
diff changeset
127
a61af66fc99e Initial load
duke
parents:
diff changeset
128 //------------------------------operator&=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
129 // Intersect one set into another.
a61af66fc99e Initial load
duke
parents:
diff changeset
130 VectorSet &VectorSet::operator &= (const VectorSet &s)
a61af66fc99e Initial load
duke
parents:
diff changeset
131 {
a61af66fc99e Initial load
duke
parents:
diff changeset
132 // NOTE: The intersection is never any larger than the smallest set.
a61af66fc99e Initial load
duke
parents:
diff changeset
133 if( s.size < size ) size = s.size; // Get smaller size
a61af66fc99e Initial load
duke
parents:
diff changeset
134 register uint32 *u1 = data; // Pointer to the destination data
a61af66fc99e Initial load
duke
parents:
diff changeset
135 register uint32 *u2 = s.data; // Pointer to the source data
a61af66fc99e Initial load
duke
parents:
diff changeset
136 for( uint i=0; i<size; i++) // For data in set
a61af66fc99e Initial load
duke
parents:
diff changeset
137 *u1++ &= *u2++; // Copy and AND longwords
a61af66fc99e Initial load
duke
parents:
diff changeset
138 return *this; // Return set
a61af66fc99e Initial load
duke
parents:
diff changeset
139 }
a61af66fc99e Initial load
duke
parents:
diff changeset
140
a61af66fc99e Initial load
duke
parents:
diff changeset
141 //------------------------------operator&=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
142 Set &VectorSet::operator &= (const Set &set)
a61af66fc99e Initial load
duke
parents:
diff changeset
143 {
a61af66fc99e Initial load
duke
parents:
diff changeset
144 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
145 return (*this) &= *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
146 }
a61af66fc99e Initial load
duke
parents:
diff changeset
147
a61af66fc99e Initial load
duke
parents:
diff changeset
148 //------------------------------operator|=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
149 // Union one set into another.
a61af66fc99e Initial load
duke
parents:
diff changeset
150 VectorSet &VectorSet::operator |= (const VectorSet &s)
a61af66fc99e Initial load
duke
parents:
diff changeset
151 {
a61af66fc99e Initial load
duke
parents:
diff changeset
152 // This many words must be unioned
a61af66fc99e Initial load
duke
parents:
diff changeset
153 register uint cnt = ((size<s.size)?size:s.size);
a61af66fc99e Initial load
duke
parents:
diff changeset
154 register uint32 *u1 = data; // Pointer to the destination data
a61af66fc99e Initial load
duke
parents:
diff changeset
155 register uint32 *u2 = s.data; // Pointer to the source data
a61af66fc99e Initial load
duke
parents:
diff changeset
156 for( uint i=0; i<cnt; i++) // Copy and OR the two sets
a61af66fc99e Initial load
duke
parents:
diff changeset
157 *u1++ |= *u2++;
a61af66fc99e Initial load
duke
parents:
diff changeset
158 if( size < s.size ) { // Is set 2 larger than set 1?
a61af66fc99e Initial load
duke
parents:
diff changeset
159 // Extend result by larger set
a61af66fc99e Initial load
duke
parents:
diff changeset
160 grow(s.size*sizeof(uint32)*8);
a61af66fc99e Initial load
duke
parents:
diff changeset
161 memcpy(&data[cnt], u2, (s.size - cnt)*sizeof(uint32));
a61af66fc99e Initial load
duke
parents:
diff changeset
162 }
a61af66fc99e Initial load
duke
parents:
diff changeset
163 return *this; // Return result set
a61af66fc99e Initial load
duke
parents:
diff changeset
164 }
a61af66fc99e Initial load
duke
parents:
diff changeset
165
a61af66fc99e Initial load
duke
parents:
diff changeset
166 //------------------------------operator|=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
167 Set &VectorSet::operator |= (const Set &set)
a61af66fc99e Initial load
duke
parents:
diff changeset
168 {
a61af66fc99e Initial load
duke
parents:
diff changeset
169 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
170 return (*this) |= *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
171 }
a61af66fc99e Initial load
duke
parents:
diff changeset
172
a61af66fc99e Initial load
duke
parents:
diff changeset
173 //------------------------------operator-=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
174 // Difference one set from another.
a61af66fc99e Initial load
duke
parents:
diff changeset
175 VectorSet &VectorSet::operator -= (const VectorSet &s)
a61af66fc99e Initial load
duke
parents:
diff changeset
176 {
a61af66fc99e Initial load
duke
parents:
diff changeset
177 // This many words must be unioned
a61af66fc99e Initial load
duke
parents:
diff changeset
178 register uint cnt = ((size<s.size)?size:s.size);
a61af66fc99e Initial load
duke
parents:
diff changeset
179 register uint32 *u1 = data; // Pointer to the destination data
a61af66fc99e Initial load
duke
parents:
diff changeset
180 register uint32 *u2 = s.data; // Pointer to the source data
a61af66fc99e Initial load
duke
parents:
diff changeset
181 for( uint i=0; i<cnt; i++ ) // For data in set
a61af66fc99e Initial load
duke
parents:
diff changeset
182 *u1++ &= ~(*u2++); // A <-- A & ~B with longwords
a61af66fc99e Initial load
duke
parents:
diff changeset
183 return *this; // Return new set
a61af66fc99e Initial load
duke
parents:
diff changeset
184 }
a61af66fc99e Initial load
duke
parents:
diff changeset
185
a61af66fc99e Initial load
duke
parents:
diff changeset
186 //------------------------------operator-=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
187 Set &VectorSet::operator -= (const Set &set)
a61af66fc99e Initial load
duke
parents:
diff changeset
188 {
a61af66fc99e Initial load
duke
parents:
diff changeset
189 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
190 return (*this) -= *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
191 }
a61af66fc99e Initial load
duke
parents:
diff changeset
192
a61af66fc99e Initial load
duke
parents:
diff changeset
193 //------------------------------compare----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
194 // Compute 2 booleans: bits in A not B, bits in B not A.
a61af66fc99e Initial load
duke
parents:
diff changeset
195 // Return X0 -- A is not a subset of B
a61af66fc99e Initial load
duke
parents:
diff changeset
196 // X1 -- A is a subset of B
a61af66fc99e Initial load
duke
parents:
diff changeset
197 // 0X -- B is not a subset of A
a61af66fc99e Initial load
duke
parents:
diff changeset
198 // 1X -- B is a subset of A
a61af66fc99e Initial load
duke
parents:
diff changeset
199 int VectorSet::compare (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
200 {
a61af66fc99e Initial load
duke
parents:
diff changeset
201 register uint32 *u1 = data; // Pointer to the destination data
a61af66fc99e Initial load
duke
parents:
diff changeset
202 register uint32 *u2 = s.data; // Pointer to the source data
a61af66fc99e Initial load
duke
parents:
diff changeset
203 register uint32 AnotB = 0, BnotA = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
204 // This many words must be unioned
a61af66fc99e Initial load
duke
parents:
diff changeset
205 register uint cnt = ((size<s.size)?size:s.size);
a61af66fc99e Initial load
duke
parents:
diff changeset
206
a61af66fc99e Initial load
duke
parents:
diff changeset
207 // Get bits for both sets
a61af66fc99e Initial load
duke
parents:
diff changeset
208 uint i; // Exit value of loop
a61af66fc99e Initial load
duke
parents:
diff changeset
209 for( i=0; i<cnt; i++ ) { // For data in BOTH sets
a61af66fc99e Initial load
duke
parents:
diff changeset
210 register uint32 A = *u1++; // Data from one guy
a61af66fc99e Initial load
duke
parents:
diff changeset
211 register uint32 B = *u2++; // Data from other guy
a61af66fc99e Initial load
duke
parents:
diff changeset
212 AnotB |= (A & ~B); // Compute bits in A not B
a61af66fc99e Initial load
duke
parents:
diff changeset
213 BnotA |= (B & ~A); // Compute bits in B not A
a61af66fc99e Initial load
duke
parents:
diff changeset
214 }
a61af66fc99e Initial load
duke
parents:
diff changeset
215
a61af66fc99e Initial load
duke
parents:
diff changeset
216 // Get bits from bigger set
a61af66fc99e Initial load
duke
parents:
diff changeset
217 if( size < s.size ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
218 for( ; i<s.size; i++ ) // For data in larger set
a61af66fc99e Initial load
duke
parents:
diff changeset
219 BnotA |= *u2++; // These bits are in B not A
a61af66fc99e Initial load
duke
parents:
diff changeset
220 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
221 for( ; i<size; i++ ) // For data in larger set
a61af66fc99e Initial load
duke
parents:
diff changeset
222 AnotB |= *u1++; // These bits are in A not B
a61af66fc99e Initial load
duke
parents:
diff changeset
223 }
a61af66fc99e Initial load
duke
parents:
diff changeset
224
a61af66fc99e Initial load
duke
parents:
diff changeset
225 // Set & return boolean flags
a61af66fc99e Initial load
duke
parents:
diff changeset
226 return ((!BnotA)<<1) + (!AnotB);
a61af66fc99e Initial load
duke
parents:
diff changeset
227 }
a61af66fc99e Initial load
duke
parents:
diff changeset
228
a61af66fc99e Initial load
duke
parents:
diff changeset
229 //------------------------------operator==-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
230 // Test for set equality
a61af66fc99e Initial load
duke
parents:
diff changeset
231 int VectorSet::operator == (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
232 {
a61af66fc99e Initial load
duke
parents:
diff changeset
233 return compare(s) == 3; // TRUE if A and B are mutual subsets
a61af66fc99e Initial load
duke
parents:
diff changeset
234 }
a61af66fc99e Initial load
duke
parents:
diff changeset
235
a61af66fc99e Initial load
duke
parents:
diff changeset
236 //------------------------------operator==-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
237 int VectorSet::operator == (const Set &set) const
a61af66fc99e Initial load
duke
parents:
diff changeset
238 {
a61af66fc99e Initial load
duke
parents:
diff changeset
239 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
240 return (*this) == *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
241 }
a61af66fc99e Initial load
duke
parents:
diff changeset
242
a61af66fc99e Initial load
duke
parents:
diff changeset
243 //------------------------------disjoint---------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
244 // Check for sets being disjoint.
a61af66fc99e Initial load
duke
parents:
diff changeset
245 int VectorSet::disjoint(const Set &set) const
a61af66fc99e Initial load
duke
parents:
diff changeset
246 {
a61af66fc99e Initial load
duke
parents:
diff changeset
247 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
248 const VectorSet &s = *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
249
a61af66fc99e Initial load
duke
parents:
diff changeset
250 // NOTE: The intersection is never any larger than the smallest set.
a61af66fc99e Initial load
duke
parents:
diff changeset
251 register uint small = ((size<s.size)?size:s.size);
a61af66fc99e Initial load
duke
parents:
diff changeset
252 register uint32 *u1 = data; // Pointer to the destination data
a61af66fc99e Initial load
duke
parents:
diff changeset
253 register uint32 *u2 = s.data; // Pointer to the source data
a61af66fc99e Initial load
duke
parents:
diff changeset
254 for( uint i=0; i<small; i++) // For data in set
a61af66fc99e Initial load
duke
parents:
diff changeset
255 if( *u1++ & *u2++ ) // If any elements in common
a61af66fc99e Initial load
duke
parents:
diff changeset
256 return 0; // Then not disjoint
a61af66fc99e Initial load
duke
parents:
diff changeset
257 return 1; // Else disjoint
a61af66fc99e Initial load
duke
parents:
diff changeset
258 }
a61af66fc99e Initial load
duke
parents:
diff changeset
259
a61af66fc99e Initial load
duke
parents:
diff changeset
260 //------------------------------operator<--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
261 // Test for strict subset
a61af66fc99e Initial load
duke
parents:
diff changeset
262 int VectorSet::operator < (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
263 {
a61af66fc99e Initial load
duke
parents:
diff changeset
264 return compare(s) == 1; // A subset B, B not subset A
a61af66fc99e Initial load
duke
parents:
diff changeset
265 }
a61af66fc99e Initial load
duke
parents:
diff changeset
266
a61af66fc99e Initial load
duke
parents:
diff changeset
267 //------------------------------operator<--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
268 int VectorSet::operator < (const Set &set) const
a61af66fc99e Initial load
duke
parents:
diff changeset
269 {
a61af66fc99e Initial load
duke
parents:
diff changeset
270 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
271 return (*this) < *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
272 }
a61af66fc99e Initial load
duke
parents:
diff changeset
273
a61af66fc99e Initial load
duke
parents:
diff changeset
274 //------------------------------operator<=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
275 // Test for subset
a61af66fc99e Initial load
duke
parents:
diff changeset
276 int VectorSet::operator <= (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
277 {
a61af66fc99e Initial load
duke
parents:
diff changeset
278 return compare(s) & 1; // A subset B
a61af66fc99e Initial load
duke
parents:
diff changeset
279 }
a61af66fc99e Initial load
duke
parents:
diff changeset
280
a61af66fc99e Initial load
duke
parents:
diff changeset
281 //------------------------------operator<=-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
282 int VectorSet::operator <= (const Set &set) const
a61af66fc99e Initial load
duke
parents:
diff changeset
283 {
a61af66fc99e Initial load
duke
parents:
diff changeset
284 // The cast is a virtual function that checks that "set" is a VectorSet.
a61af66fc99e Initial load
duke
parents:
diff changeset
285 return (*this) <= *(set.asVectorSet());
a61af66fc99e Initial load
duke
parents:
diff changeset
286 }
a61af66fc99e Initial load
duke
parents:
diff changeset
287
a61af66fc99e Initial load
duke
parents:
diff changeset
288 //------------------------------operator[]-------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
289 // Test for membership. A Zero/Non-Zero value is returned!
a61af66fc99e Initial load
duke
parents:
diff changeset
290 int VectorSet::operator[](uint elem) const
a61af66fc99e Initial load
duke
parents:
diff changeset
291 {
a61af66fc99e Initial load
duke
parents:
diff changeset
292 register uint word = elem >> 5; // Get the longword offset
a61af66fc99e Initial load
duke
parents:
diff changeset
293 if( word >= size ) // Beyond the last?
a61af66fc99e Initial load
duke
parents:
diff changeset
294 return 0; // Then it's clear
a61af66fc99e Initial load
duke
parents:
diff changeset
295 register uint32 mask = 1L << (elem & 31); // Get bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
296 return ((data[word] & mask))!=0; // Return the sense of the bit
a61af66fc99e Initial load
duke
parents:
diff changeset
297 }
a61af66fc99e Initial load
duke
parents:
diff changeset
298
a61af66fc99e Initial load
duke
parents:
diff changeset
299 //------------------------------getelem----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
300 // Get any element from the set.
a61af66fc99e Initial load
duke
parents:
diff changeset
301 uint VectorSet::getelem(void) const
a61af66fc99e Initial load
duke
parents:
diff changeset
302 {
a61af66fc99e Initial load
duke
parents:
diff changeset
303 uint i; // Exit value of loop
a61af66fc99e Initial load
duke
parents:
diff changeset
304 for( i=0; i<size; i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
305 if( data[i] )
a61af66fc99e Initial load
duke
parents:
diff changeset
306 break;
a61af66fc99e Initial load
duke
parents:
diff changeset
307 uint32 word = data[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
308 int j; // Exit value of loop
a61af66fc99e Initial load
duke
parents:
diff changeset
309 for( j= -1; word; j++, word>>=1 );
a61af66fc99e Initial load
duke
parents:
diff changeset
310 return (i<<5)+j;
a61af66fc99e Initial load
duke
parents:
diff changeset
311 }
a61af66fc99e Initial load
duke
parents:
diff changeset
312
a61af66fc99e Initial load
duke
parents:
diff changeset
313 //------------------------------Clear------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
314 // Clear a set
a61af66fc99e Initial load
duke
parents:
diff changeset
315 void VectorSet::Clear(void)
a61af66fc99e Initial load
duke
parents:
diff changeset
316 {
a61af66fc99e Initial load
duke
parents:
diff changeset
317 if( size > 100 ) { // Reclaim storage only if huge
a61af66fc99e Initial load
duke
parents:
diff changeset
318 FREE_RESOURCE_ARRAY(uint32,data,size);
a61af66fc99e Initial load
duke
parents:
diff changeset
319 size = 2; // Small initial size
a61af66fc99e Initial load
duke
parents:
diff changeset
320 data = NEW_RESOURCE_ARRAY(uint32,size);
a61af66fc99e Initial load
duke
parents:
diff changeset
321 }
a61af66fc99e Initial load
duke
parents:
diff changeset
322 memset( data, 0, size*sizeof(uint32) );
a61af66fc99e Initial load
duke
parents:
diff changeset
323 }
a61af66fc99e Initial load
duke
parents:
diff changeset
324
a61af66fc99e Initial load
duke
parents:
diff changeset
325 //------------------------------Size-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
326 // Return number of elements in a Set
a61af66fc99e Initial load
duke
parents:
diff changeset
327 uint VectorSet::Size(void) const
a61af66fc99e Initial load
duke
parents:
diff changeset
328 {
a61af66fc99e Initial load
duke
parents:
diff changeset
329 uint sum = 0; // Cumulative size so far.
a61af66fc99e Initial load
duke
parents:
diff changeset
330 uint8 *currByte = (uint8*)data;
a61af66fc99e Initial load
duke
parents:
diff changeset
331 for( uint32 i = 0; i < (size<<2); i++) // While have bytes to process
a61af66fc99e Initial load
duke
parents:
diff changeset
332 sum += bitsInByte[*currByte++]; // Add bits in current byte to size.
a61af66fc99e Initial load
duke
parents:
diff changeset
333 return sum;
a61af66fc99e Initial load
duke
parents:
diff changeset
334 }
a61af66fc99e Initial load
duke
parents:
diff changeset
335
a61af66fc99e Initial load
duke
parents:
diff changeset
336 //------------------------------Sort-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
337 // Sort the elements for the next forall statement
a61af66fc99e Initial load
duke
parents:
diff changeset
338 void VectorSet::Sort(void)
a61af66fc99e Initial load
duke
parents:
diff changeset
339 {
a61af66fc99e Initial load
duke
parents:
diff changeset
340 }
a61af66fc99e Initial load
duke
parents:
diff changeset
341
a61af66fc99e Initial load
duke
parents:
diff changeset
342 //------------------------------hash-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
343 int VectorSet::hash() const
a61af66fc99e Initial load
duke
parents:
diff changeset
344 {
a61af66fc99e Initial load
duke
parents:
diff changeset
345 uint32 _xor = 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
346 uint lim = ((size<4)?size:4);
a61af66fc99e Initial load
duke
parents:
diff changeset
347 for( uint i = 0; i < lim; i++ )
a61af66fc99e Initial load
duke
parents:
diff changeset
348 _xor ^= data[i];
a61af66fc99e Initial load
duke
parents:
diff changeset
349 return (int)_xor;
a61af66fc99e Initial load
duke
parents:
diff changeset
350 }
a61af66fc99e Initial load
duke
parents:
diff changeset
351
a61af66fc99e Initial load
duke
parents:
diff changeset
352 //------------------------------iterate----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
353 SetI_ *VectorSet::iterate(uint &elem) const
a61af66fc99e Initial load
duke
parents:
diff changeset
354 {
a61af66fc99e Initial load
duke
parents:
diff changeset
355 VSetI_ *foo = (new(ResourceObj::C_HEAP) VSetI_(this));
a61af66fc99e Initial load
duke
parents:
diff changeset
356 elem = foo->next();
a61af66fc99e Initial load
duke
parents:
diff changeset
357 return foo;
a61af66fc99e Initial load
duke
parents:
diff changeset
358 }
a61af66fc99e Initial load
duke
parents:
diff changeset
359
a61af66fc99e Initial load
duke
parents:
diff changeset
360 //=============================================================================
a61af66fc99e Initial load
duke
parents:
diff changeset
361 //------------------------------VSetI_-----------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
362 // Initialize the innards of a VectorSet iterator
a61af66fc99e Initial load
duke
parents:
diff changeset
363 VSetI_::VSetI_( const VectorSet *vset ) : s(vset)
a61af66fc99e Initial load
duke
parents:
diff changeset
364 {
a61af66fc99e Initial load
duke
parents:
diff changeset
365 i = (uint)-1L;
a61af66fc99e Initial load
duke
parents:
diff changeset
366 j = (uint)-1L;
a61af66fc99e Initial load
duke
parents:
diff changeset
367 mask = (unsigned)(1L<<31);
a61af66fc99e Initial load
duke
parents:
diff changeset
368 }
a61af66fc99e Initial load
duke
parents:
diff changeset
369
a61af66fc99e Initial load
duke
parents:
diff changeset
370 //------------------------------next-------------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
371 // Find and return the next element of a vector set, or return garbage and
a61af66fc99e Initial load
duke
parents:
diff changeset
372 // make "VSetI_::test()" fail.
a61af66fc99e Initial load
duke
parents:
diff changeset
373 uint VSetI_::next(void)
a61af66fc99e Initial load
duke
parents:
diff changeset
374 {
a61af66fc99e Initial load
duke
parents:
diff changeset
375 j++; // Next element in word
a61af66fc99e Initial load
duke
parents:
diff changeset
376 mask = (mask & max_jint) << 1;// Next bit in word
a61af66fc99e Initial load
duke
parents:
diff changeset
377 do { // Do While still have words
a61af66fc99e Initial load
duke
parents:
diff changeset
378 while( mask ) { // While have bits in word
a61af66fc99e Initial load
duke
parents:
diff changeset
379 if( s->data[i] & mask ) { // If found a bit
a61af66fc99e Initial load
duke
parents:
diff changeset
380 return (i<<5)+j; // Return the bit address
a61af66fc99e Initial load
duke
parents:
diff changeset
381 }
a61af66fc99e Initial load
duke
parents:
diff changeset
382 j++; // Skip to next bit
a61af66fc99e Initial load
duke
parents:
diff changeset
383 mask = (mask & max_jint) << 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
384 }
a61af66fc99e Initial load
duke
parents:
diff changeset
385 j = 0; // No more bits in word; setup for next word
a61af66fc99e Initial load
duke
parents:
diff changeset
386 mask = 1;
a61af66fc99e Initial load
duke
parents:
diff changeset
387 for( i++; (i<s->size) && (!s->data[i]); i++ ); // Skip to non-zero word
a61af66fc99e Initial load
duke
parents:
diff changeset
388 } while( i<s->size );
a61af66fc99e Initial load
duke
parents:
diff changeset
389 return max_juint; // No element, iterated them all
a61af66fc99e Initial load
duke
parents:
diff changeset
390 }