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