src/share/vm/libadt/vectset.cpp

Wed, 01 Dec 2010 15:04:06 +0100

author
stefank
date
Wed, 01 Dec 2010 15:04:06 +0100
changeset 2325
c760f78e0a53
parent 2314
f95d63e2154a
child 2556
3763ca6579b7
permissions
-rw-r--r--

7003125: precompiled.hpp is included when precompiled headers are not used
Summary: Added an ifndef DONT_USE_PRECOMPILED_HEADER to precompiled.hpp. Set up DONT_USE_PRECOMPILED_HEADER when compiling with Sun Studio or when the user specifies USE_PRECOMPILED_HEADER=0. Fixed broken include dependencies.
Reviewed-by: coleenp, kvn

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

mercurial