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