Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/libadt/set.cpp
32285 views
/*1* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#include "precompiled.hpp"25#include "libadt/set.hpp"26#include "memory/allocation.inline.hpp"2728// Sets - An Abstract Data Type2930// %%%%% includes not needed with AVM framework - Ungar31// #include "port.hpp"32//IMPLEMENTATION33// #include "set.hpp"3435#include <stdio.h>36#include <assert.h>37#include <string.h>38#include <stdlib.h>3940// Not needed and it causes terouble for gcc.41//42// #include <iostream.h>4344//-------------------------Virtual Functions-----------------------------------45// These functions MUST be implemented by the inheriting class.46class SparseSet;47/* Removed for MCC BUG48Set::operator const SparseSet*() const { assert(0); return NULL; } */49const SparseSet *Set::asSparseSet() const { assert(0); return NULL; }50class VectorSet;51/* Removed for MCC BUG52Set::operator const VectorSet*() const { assert(0); return NULL; } */53const VectorSet *Set::asVectorSet() const { assert(0); return NULL; }54class ListSet;55/* Removed for MCC BUG56Set::operator const ListSet*() const { assert(0); return NULL; } */57const ListSet *Set::asListSet() const { assert(0); return NULL; }58class CoSet;59/* Removed for MCC BUG60Set::operator const CoSet*() const { assert(0); return NULL; } */61const CoSet *Set::asCoSet() const { assert(0); return NULL; }6263//------------------------------setstr-----------------------------------------64// Create a string with a printable representation of a set.65// The caller must deallocate the string.66char *Set::setstr() const67{68if( this == NULL ) return os::strdup("{no set}");69Set &set = clone(); // Virtually copy the basic set.70set.Sort(); // Sort elements for in-order retrieval7172uint len = 128; // Total string space73char *buf = NEW_C_HEAP_ARRAY(char,len, mtCompiler);// Some initial string space7475register char *s = buf; // Current working string pointer76*s++ = '{';77*s = '\0';7879// For all elements of the Set80uint hi = (uint)-2, lo = (uint)-2;81for( SetI i(&set); i.test(); ++i ) {82if( hi+1 == i.elem ) { // Moving sequentially thru range?83hi = i.elem; // Yes, just update hi end of range84} else { // Else range ended85if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers86int offset = (int)(s-buf);// Not enuf space; compute offset into buffer87len <<= 1; // Double string size88buf = REALLOC_C_HEAP_ARRAY(char,buf,len, mtCompiler); // Reallocate doubled size89s = buf+offset; // Get working pointer into new bigger buffer90}91if( lo != (uint)-2 ) { // Startup? No! Then print previous range.92if( lo != hi ) sprintf(s,"%d-%d,",lo,hi);93else sprintf(s,"%d,",lo);94s += strlen(s); // Advance working string95}96hi = lo = i.elem;97}98}99if( lo != (uint)-2 ) {100if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers101int offset = (int)(s-buf);// Not enuf space; compute offset into buffer102len <<= 1; // Double string size103buf = (char*)ReallocateHeap(buf,len, mtCompiler); // Reallocate doubled size104s = buf+offset; // Get working pointer into new bigger buffer105}106if( lo != hi ) sprintf(s,"%d-%d}",lo,hi);107else sprintf(s,"%d}",lo);108} else strcat(s,"}");109// Don't delete the clone 'set' since it is allocated on Arena.110return buf;111}112113//------------------------------print------------------------------------------114// Handier print routine115void Set::print() const116{117char *printable_set = setstr();118tty->print_cr("%s", printable_set);119FreeHeap(printable_set);120}121122//------------------------------parse------------------------------------------123// Convert a textual representation of a Set, to a Set and union into "this"124// Set. Return the amount of text parsed in "len", or zero in "len".125int Set::parse(const char *s)126{127register char c; // Parse character128register const char *t = s; // Save the starting position of s.129do c = *s++; // Skip characters130while( c && (c <= ' ') ); // Till no more whitespace or EOS131if( c != '{' ) return 0; // Oops, not a Set openner132if( *s == '}' ) return 2; // The empty Set133134// Sets are filled with values of the form "xx," or "xx-yy," with the comma135// a "}" at the very end.136while( 1 ) { // While have elements in the Set137char *u; // Pointer to character ending parse138uint hi, i; // Needed for range handling below139uint elem = (uint)strtoul(s,&u,10);// Get element140if( u == s ) return 0; // Bogus crude141s = u; // Skip over the number142c = *s++; // Get the number seperator143switch ( c ) { // Different seperators144case '}': // Last simple element145case ',': // Simple element146(*this) <<= elem; // Insert the simple element into the Set147break; // Go get next element148case '-': // Range149hi = (uint)strtoul(s,&u,10); // Get element150if( u == s ) return 0; // Bogus crude151for( i=elem; i<=hi; i++ )152(*this) <<= i; // Insert the entire range into the Set153s = u; // Skip over the number154c = *s++; // Get the number seperator155break;156}157if( c == '}' ) break; // End of the Set158if( c != ',' ) return 0; // Bogus garbage159}160return (int)(s-t); // Return length parsed161}162163//------------------------------Iterator---------------------------------------164SetI_::~SetI_()165{166}167168169