src/share/vm/libadt/set.cpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 435
a61af66fc99e
child 2314
f95d63e2154a
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

duke@435 1 /*
trims@1907 2 * Copyright (c) 1997, 2006, 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
duke@435 25 // Sets - An Abstract Data Type
duke@435 26
duke@435 27 #include "incls/_precompiled.incl"
duke@435 28 #include "incls/_set.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 "set.hpp"
duke@435 34
duke@435 35 #include <stdio.h>
duke@435 36 #include <assert.h>
duke@435 37 #include <string.h>
duke@435 38 #include <stdlib.h>
duke@435 39
duke@435 40 // Not needed and it causes terouble for gcc.
duke@435 41 //
duke@435 42 // #include <iostream.h>
duke@435 43
duke@435 44 //-------------------------Virtual Functions-----------------------------------
duke@435 45 // These functions MUST be implemented by the inheriting class.
duke@435 46 class SparseSet;
duke@435 47 /* Removed for MCC BUG
duke@435 48 Set::operator const SparseSet*() const { assert(0); return NULL; } */
duke@435 49 const SparseSet *Set::asSparseSet() const { assert(0); return NULL; }
duke@435 50 class VectorSet;
duke@435 51 /* Removed for MCC BUG
duke@435 52 Set::operator const VectorSet*() const { assert(0); return NULL; } */
duke@435 53 const VectorSet *Set::asVectorSet() const { assert(0); return NULL; }
duke@435 54 class ListSet;
duke@435 55 /* Removed for MCC BUG
duke@435 56 Set::operator const ListSet*() const { assert(0); return NULL; } */
duke@435 57 const ListSet *Set::asListSet() const { assert(0); return NULL; }
duke@435 58 class CoSet;
duke@435 59 /* Removed for MCC BUG
duke@435 60 Set::operator const CoSet*() const { assert(0); return NULL; } */
duke@435 61 const CoSet *Set::asCoSet() const { assert(0); return NULL; }
duke@435 62
duke@435 63 //------------------------------setstr-----------------------------------------
duke@435 64 // Create a string with a printable representation of a set.
duke@435 65 // The caller must deallocate the string.
duke@435 66 char *Set::setstr() const
duke@435 67 {
duke@435 68 if( !this ) return os::strdup("{no set}");
duke@435 69 Set &set = clone(); // Virtually copy the basic set.
duke@435 70 set.Sort(); // Sort elements for in-order retrieval
duke@435 71
duke@435 72 uint len = 128; // Total string space
duke@435 73 char *buf = NEW_C_HEAP_ARRAY(char,len);// Some initial string space
duke@435 74
duke@435 75 register char *s = buf; // Current working string pointer
duke@435 76 *s++ = '{';
duke@435 77 *s = '\0';
duke@435 78
duke@435 79 // For all elements of the Set
duke@435 80 uint hi = (uint)-2, lo = (uint)-2;
duke@435 81 for( SetI i(&set); i.test(); ++i ) {
duke@435 82 if( hi+1 == i.elem ) { // Moving sequentially thru range?
duke@435 83 hi = i.elem; // Yes, just update hi end of range
duke@435 84 } else { // Else range ended
duke@435 85 if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers
duke@435 86 int offset = (int)(s-buf);// Not enuf space; compute offset into buffer
duke@435 87 len <<= 1; // Double string size
duke@435 88 buf = REALLOC_C_HEAP_ARRAY(char,buf,len); // Reallocate doubled size
duke@435 89 s = buf+offset; // Get working pointer into new bigger buffer
duke@435 90 }
duke@435 91 if( lo != (uint)-2 ) { // Startup? No! Then print previous range.
duke@435 92 if( lo != hi ) sprintf(s,"%d-%d,",lo,hi);
duke@435 93 else sprintf(s,"%d,",lo);
duke@435 94 s += strlen(s); // Advance working string
duke@435 95 }
duke@435 96 hi = lo = i.elem;
duke@435 97 }
duke@435 98 }
duke@435 99 if( lo != (uint)-2 ) {
duke@435 100 if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers
duke@435 101 int offset = (int)(s-buf);// Not enuf space; compute offset into buffer
duke@435 102 len <<= 1; // Double string size
duke@435 103 buf = (char*)ReallocateHeap(buf,len); // Reallocate doubled size
duke@435 104 s = buf+offset; // Get working pointer into new bigger buffer
duke@435 105 }
duke@435 106 if( lo != hi ) sprintf(s,"%d-%d}",lo,hi);
duke@435 107 else sprintf(s,"%d}",lo);
duke@435 108 } else strcat(s,"}");
duke@435 109 // Don't delete the clone 'set' since it is allocated on Arena.
duke@435 110 return buf;
duke@435 111 }
duke@435 112
duke@435 113 //------------------------------print------------------------------------------
duke@435 114 // Handier print routine
duke@435 115 void Set::print() const
duke@435 116 {
duke@435 117 char *printable_set = setstr();
duke@435 118 tty->print_cr(printable_set);
duke@435 119 FreeHeap(printable_set);
duke@435 120 }
duke@435 121
duke@435 122 //------------------------------parse------------------------------------------
duke@435 123 // Convert a textual representation of a Set, to a Set and union into "this"
duke@435 124 // Set. Return the amount of text parsed in "len", or zero in "len".
duke@435 125 int Set::parse(const char *s)
duke@435 126 {
duke@435 127 register char c; // Parse character
duke@435 128 register const char *t = s; // Save the starting position of s.
duke@435 129 do c = *s++; // Skip characters
duke@435 130 while( c && (c <= ' ') ); // Till no more whitespace or EOS
duke@435 131 if( c != '{' ) return 0; // Oops, not a Set openner
duke@435 132 if( *s == '}' ) return 2; // The empty Set
duke@435 133
duke@435 134 // Sets are filled with values of the form "xx," or "xx-yy," with the comma
duke@435 135 // a "}" at the very end.
duke@435 136 while( 1 ) { // While have elements in the Set
duke@435 137 char *u; // Pointer to character ending parse
duke@435 138 uint hi, i; // Needed for range handling below
duke@435 139 uint elem = (uint)strtoul(s,&u,10);// Get element
duke@435 140 if( u == s ) return 0; // Bogus crude
duke@435 141 s = u; // Skip over the number
duke@435 142 c = *s++; // Get the number seperator
duke@435 143 switch ( c ) { // Different seperators
duke@435 144 case '}': // Last simple element
duke@435 145 case ',': // Simple element
duke@435 146 (*this) <<= elem; // Insert the simple element into the Set
duke@435 147 break; // Go get next element
duke@435 148 case '-': // Range
duke@435 149 hi = (uint)strtoul(s,&u,10); // Get element
duke@435 150 if( u == s ) return 0; // Bogus crude
duke@435 151 for( i=elem; i<=hi; i++ )
duke@435 152 (*this) <<= i; // Insert the entire range into the Set
duke@435 153 s = u; // Skip over the number
duke@435 154 c = *s++; // Get the number seperator
duke@435 155 break;
duke@435 156 }
duke@435 157 if( c == '}' ) break; // End of the Set
duke@435 158 if( c != ',' ) return 0; // Bogus garbage
duke@435 159 }
duke@435 160 return (int)(s-t); // Return length parsed
duke@435 161 }
duke@435 162
duke@435 163 //------------------------------Iterator---------------------------------------
duke@435 164 SetI_::~SetI_()
duke@435 165 {
duke@435 166 }

mercurial