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