1 /* 2 * Copyright (c) 1997, 2012, Oracle and/or its affiliates. All rights reserved. 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. 4 * 5 * This code is free software; you can redistribute it and/or modify it 6 * under the terms of the GNU General Public License version 2 only, as 7 * published by the Free Software Foundation. 8 * 9 * This code is distributed in the hope that it will be useful, but WITHOUT 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License 12 * version 2 for more details (a copy is included in the LICENSE file that 13 * accompanied this code). 14 * 15 * You should have received a copy of the GNU General Public License version 16 * 2 along with this work; if not, write to the Free Software Foundation, 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. 18 * 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA 20 * or visit www.oracle.com if you need additional information or have any 21 * questions. 22 * 23 */ 24 25 #include "precompiled.hpp" 26 #include "libadt/set.hpp" 27 #include "memory/allocation.inline.hpp" 28 29 // Sets - An Abstract Data Type 30 31 // %%%%% includes not needed with AVM framework - Ungar 32 // #include "port.hpp" 33 //IMPLEMENTATION 34 // #include "set.hpp" 35 36 #include <stdio.h> 37 #include <assert.h> 38 #include <string.h> 39 #include <stdlib.h> 40 41 // Not needed and it causes terouble for gcc. 42 // 43 // #include <iostream.h> 44 45 //-------------------------Virtual Functions----------------------------------- 46 // These functions MUST be implemented by the inheriting class. 47 class SparseSet; 48 /* Removed for MCC BUG 49 Set::operator const SparseSet*() const { assert(0); return NULL; } */ 50 const SparseSet *Set::asSparseSet() const { assert(0); return NULL; } 51 class VectorSet; 52 /* Removed for MCC BUG 53 Set::operator const VectorSet*() const { assert(0); return NULL; } */ 54 const VectorSet *Set::asVectorSet() const { assert(0); return NULL; } 55 class ListSet; 56 /* Removed for MCC BUG 57 Set::operator const ListSet*() const { assert(0); return NULL; } */ 58 const ListSet *Set::asListSet() const { assert(0); return NULL; } 59 class CoSet; 60 /* Removed for MCC BUG 61 Set::operator const CoSet*() const { assert(0); return NULL; } */ 62 const CoSet *Set::asCoSet() const { assert(0); return NULL; } 63 64 //------------------------------setstr----------------------------------------- 65 // Create a string with a printable representation of a set. 66 // The caller must deallocate the string. 67 char *Set::setstr() const 68 { 69 if( !this ) return os::strdup("{no set}"); 70 Set &set = clone(); // Virtually copy the basic set. 71 set.Sort(); // Sort elements for in-order retrieval 72 73 uint len = 128; // Total string space 74 char *buf = NEW_C_HEAP_ARRAY(char,len, mtCompiler);// Some initial string space 75 76 register char *s = buf; // Current working string pointer 77 *s++ = '{'; 78 *s = '\0'; 79 80 // For all elements of the Set 81 uint hi = (uint)-2, lo = (uint)-2; 82 for( SetI i(&set); i.test(); ++i ) { 83 if( hi+1 == i.elem ) { // Moving sequentially thru range? 84 hi = i.elem; // Yes, just update hi end of range 85 } else { // Else range ended 86 if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers 87 int offset = (int)(s-buf);// Not enuf space; compute offset into buffer 88 len <<= 1; // Double string size 89 buf = REALLOC_C_HEAP_ARRAY(char,buf,len, mtCompiler); // Reallocate doubled size 90 s = buf+offset; // Get working pointer into new bigger buffer 91 } 92 if( lo != (uint)-2 ) { // Startup? No! Then print previous range. 93 if( lo != hi ) sprintf(s,"%d-%d,",lo,hi); 94 else sprintf(s,"%d,",lo); 95 s += strlen(s); // Advance working string 96 } 97 hi = lo = i.elem; 98 } 99 } 100 if( lo != (uint)-2 ) { 101 if( buf+len-s < 25 ) { // Generous trailing space for upcoming numbers 102 int offset = (int)(s-buf);// Not enuf space; compute offset into buffer 103 len <<= 1; // Double string size 104 buf = (char*)ReallocateHeap(buf,len, mtCompiler); // Reallocate doubled size 105 s = buf+offset; // Get working pointer into new bigger buffer 106 } 107 if( lo != hi ) sprintf(s,"%d-%d}",lo,hi); 108 else sprintf(s,"%d}",lo); 109 } else strcat(s,"}"); 110 // Don't delete the clone 'set' since it is allocated on Arena. 111 return buf; 112 } 113 114 //------------------------------print------------------------------------------ 115 // Handier print routine 116 void Set::print() const 117 { 118 char *printable_set = setstr(); 119 tty->print_cr(printable_set); 120 FreeHeap(printable_set); 121 } 122 123 //------------------------------parse------------------------------------------ 124 // Convert a textual representation of a Set, to a Set and union into "this" 125 // Set. Return the amount of text parsed in "len", or zero in "len". 126 int Set::parse(const char *s) 127 { 128 register char c; // Parse character 129 register const char *t = s; // Save the starting position of s. 130 do c = *s++; // Skip characters 131 while( c && (c <= ' ') ); // Till no more whitespace or EOS 132 if( c != '{' ) return 0; // Oops, not a Set openner 133 if( *s == '}' ) return 2; // The empty Set 134 135 // Sets are filled with values of the form "xx," or "xx-yy," with the comma 136 // a "}" at the very end. 137 while( 1 ) { // While have elements in the Set 138 char *u; // Pointer to character ending parse 139 uint hi, i; // Needed for range handling below 140 uint elem = (uint)strtoul(s,&u,10);// Get element 141 if( u == s ) return 0; // Bogus crude 142 s = u; // Skip over the number 143 c = *s++; // Get the number seperator 144 switch ( c ) { // Different seperators 145 case '}': // Last simple element 146 case ',': // Simple element 147 (*this) <<= elem; // Insert the simple element into the Set 148 break; // Go get next element 149 case '-': // Range 150 hi = (uint)strtoul(s,&u,10); // Get element 151 if( u == s ) return 0; // Bogus crude 152 for( i=elem; i<=hi; i++ ) 153 (*this) <<= i; // Insert the entire range into the Set 154 s = u; // Skip over the number 155 c = *s++; // Get the number seperator 156 break; 157 } 158 if( c == '}' ) break; // End of the Set 159 if( c != ',' ) return 0; // Bogus garbage 160 } 161 return (int)(s-t); // Return length parsed 162 } 163 164 //------------------------------Iterator--------------------------------------- 165 SetI_::~SetI_() 166 { 167 }