duke@435: /* trims@1907: * Copyright (c) 1997, 2006, Oracle and/or its affiliates. All rights reserved. duke@435: * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. duke@435: * duke@435: * This code is free software; you can redistribute it and/or modify it duke@435: * under the terms of the GNU General Public License version 2 only, as duke@435: * published by the Free Software Foundation. duke@435: * duke@435: * This code is distributed in the hope that it will be useful, but WITHOUT duke@435: * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or duke@435: * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License duke@435: * version 2 for more details (a copy is included in the LICENSE file that duke@435: * accompanied this code). duke@435: * duke@435: * You should have received a copy of the GNU General Public License version duke@435: * 2 along with this work; if not, write to the Free Software Foundation, duke@435: * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. duke@435: * trims@1907: * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA trims@1907: * or visit www.oracle.com if you need additional information or have any trims@1907: * questions. duke@435: * duke@435: */ duke@435: duke@435: // Sets - An Abstract Data Type duke@435: duke@435: #include "incls/_precompiled.incl" duke@435: #include "incls/_set.cpp.incl" duke@435: duke@435: // %%%%% includes not needed with AVM framework - Ungar duke@435: // #include "port.hpp" duke@435: //IMPLEMENTATION duke@435: // #include "set.hpp" duke@435: duke@435: #include duke@435: #include duke@435: #include duke@435: #include duke@435: duke@435: // Not needed and it causes terouble for gcc. duke@435: // duke@435: // #include duke@435: duke@435: //-------------------------Virtual Functions----------------------------------- duke@435: // These functions MUST be implemented by the inheriting class. duke@435: class SparseSet; duke@435: /* Removed for MCC BUG duke@435: Set::operator const SparseSet*() const { assert(0); return NULL; } */ duke@435: const SparseSet *Set::asSparseSet() const { assert(0); return NULL; } duke@435: class VectorSet; duke@435: /* Removed for MCC BUG duke@435: Set::operator const VectorSet*() const { assert(0); return NULL; } */ duke@435: const VectorSet *Set::asVectorSet() const { assert(0); return NULL; } duke@435: class ListSet; duke@435: /* Removed for MCC BUG duke@435: Set::operator const ListSet*() const { assert(0); return NULL; } */ duke@435: const ListSet *Set::asListSet() const { assert(0); return NULL; } duke@435: class CoSet; duke@435: /* Removed for MCC BUG duke@435: Set::operator const CoSet*() const { assert(0); return NULL; } */ duke@435: const CoSet *Set::asCoSet() const { assert(0); return NULL; } duke@435: duke@435: //------------------------------setstr----------------------------------------- duke@435: // Create a string with a printable representation of a set. duke@435: // The caller must deallocate the string. duke@435: char *Set::setstr() const duke@435: { duke@435: if( !this ) return os::strdup("{no set}"); duke@435: Set &set = clone(); // Virtually copy the basic set. duke@435: set.Sort(); // Sort elements for in-order retrieval duke@435: duke@435: uint len = 128; // Total string space duke@435: char *buf = NEW_C_HEAP_ARRAY(char,len);// Some initial string space duke@435: duke@435: register char *s = buf; // Current working string pointer duke@435: *s++ = '{'; duke@435: *s = '\0'; duke@435: duke@435: // For all elements of the Set duke@435: uint hi = (uint)-2, lo = (uint)-2; duke@435: for( SetI i(&set); i.test(); ++i ) { duke@435: if( hi+1 == i.elem ) { // Moving sequentially thru range? duke@435: hi = i.elem; // Yes, just update hi end of range duke@435: } else { // Else range ended duke@435: if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers duke@435: int offset = (int)(s-buf);// Not enuf space; compute offset into buffer duke@435: len <<= 1; // Double string size duke@435: buf = REALLOC_C_HEAP_ARRAY(char,buf,len); // Reallocate doubled size duke@435: s = buf+offset; // Get working pointer into new bigger buffer duke@435: } duke@435: if( lo != (uint)-2 ) { // Startup? No! Then print previous range. duke@435: if( lo != hi ) sprintf(s,"%d-%d,",lo,hi); duke@435: else sprintf(s,"%d,",lo); duke@435: s += strlen(s); // Advance working string duke@435: } duke@435: hi = lo = i.elem; duke@435: } duke@435: } duke@435: if( lo != (uint)-2 ) { duke@435: if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers duke@435: int offset = (int)(s-buf);// Not enuf space; compute offset into buffer duke@435: len <<= 1; // Double string size duke@435: buf = (char*)ReallocateHeap(buf,len); // Reallocate doubled size duke@435: s = buf+offset; // Get working pointer into new bigger buffer duke@435: } duke@435: if( lo != hi ) sprintf(s,"%d-%d}",lo,hi); duke@435: else sprintf(s,"%d}",lo); duke@435: } else strcat(s,"}"); duke@435: // Don't delete the clone 'set' since it is allocated on Arena. duke@435: return buf; duke@435: } duke@435: duke@435: //------------------------------print------------------------------------------ duke@435: // Handier print routine duke@435: void Set::print() const duke@435: { duke@435: char *printable_set = setstr(); duke@435: tty->print_cr(printable_set); duke@435: FreeHeap(printable_set); duke@435: } duke@435: duke@435: //------------------------------parse------------------------------------------ duke@435: // Convert a textual representation of a Set, to a Set and union into "this" duke@435: // Set. Return the amount of text parsed in "len", or zero in "len". duke@435: int Set::parse(const char *s) duke@435: { duke@435: register char c; // Parse character duke@435: register const char *t = s; // Save the starting position of s. duke@435: do c = *s++; // Skip characters duke@435: while( c && (c <= ' ') ); // Till no more whitespace or EOS duke@435: if( c != '{' ) return 0; // Oops, not a Set openner duke@435: if( *s == '}' ) return 2; // The empty Set duke@435: duke@435: // Sets are filled with values of the form "xx," or "xx-yy," with the comma duke@435: // a "}" at the very end. duke@435: while( 1 ) { // While have elements in the Set duke@435: char *u; // Pointer to character ending parse duke@435: uint hi, i; // Needed for range handling below duke@435: uint elem = (uint)strtoul(s,&u,10);// Get element duke@435: if( u == s ) return 0; // Bogus crude duke@435: s = u; // Skip over the number duke@435: c = *s++; // Get the number seperator duke@435: switch ( c ) { // Different seperators duke@435: case '}': // Last simple element duke@435: case ',': // Simple element duke@435: (*this) <<= elem; // Insert the simple element into the Set duke@435: break; // Go get next element duke@435: case '-': // Range duke@435: hi = (uint)strtoul(s,&u,10); // Get element duke@435: if( u == s ) return 0; // Bogus crude duke@435: for( i=elem; i<=hi; i++ ) duke@435: (*this) <<= i; // Insert the entire range into the Set duke@435: s = u; // Skip over the number duke@435: c = *s++; // Get the number seperator duke@435: break; duke@435: } duke@435: if( c == '}' ) break; // End of the Set duke@435: if( c != ',' ) return 0; // Bogus garbage duke@435: } duke@435: return (int)(s-t); // Return length parsed duke@435: } duke@435: duke@435: //------------------------------Iterator--------------------------------------- duke@435: SetI_::~SetI_() duke@435: { duke@435: }