author | mikael |
Thu, 12 Jul 2018 17:29:48 -0700 | |
changeset 51050 | 96ea37459ca7 |
parent 47216 | 71c04702a3d5 |
child 53546 | 63eb7e38ce84 |
permissions | -rw-r--r-- |
1 | 1 |
/* |
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
13963
diff
changeset
|
2 |
* Copyright (c) 1997, 2014, Oracle and/or its affiliates. All rights reserved. |
1 | 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 |
* |
|
5547
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
19 |
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
20 |
* or visit www.oracle.com if you need additional information or have any |
f4b087cbb361
6941466: Oracle rebranding changes for Hotspot repositories
trims
parents:
1
diff
changeset
|
21 |
* questions. |
1 | 22 |
* |
23 |
*/ |
|
24 |
||
7397 | 25 |
#include "precompiled.hpp" |
26 |
#include "libadt/set.hpp" |
|
27 |
#include "memory/allocation.inline.hpp" |
|
1 | 28 |
|
7397 | 29 |
// Sets - An Abstract Data Type |
1 | 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; } */ |
|
41 |
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; } */ |
|
45 |
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; } */ |
|
49 |
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; } */ |
|
53 |
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. |
|
58 |
char *Set::setstr() const |
|
59 |
{ |
|
30239
dc83236ebb28
8077364: "if( !this )" construct prevents build on Xcode 6.3
sla
parents:
24429
diff
changeset
|
60 |
if( this == NULL ) return os::strdup("{no set}"); |
1 | 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 |
|
13195 | 65 |
char *buf = NEW_C_HEAP_ARRAY(char,len, mtCompiler);// Some initial string space |
1 | 66 |
|
51050
96ea37459ca7
8207011: Remove uses of the register storage class specifier
mikael
parents:
47216
diff
changeset
|
67 |
char *s = buf; // Current working string pointer |
1 | 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 |
|
13195 | 80 |
buf = REALLOC_C_HEAP_ARRAY(char,buf,len, mtCompiler); // Reallocate doubled size |
1 | 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 |
|
13195 | 95 |
buf = (char*)ReallocateHeap(buf,len, mtCompiler); // Reallocate doubled size |
1 | 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 |
|
107 |
void Set::print() const |
|
108 |
{ |
|
109 |
char *printable_set = setstr(); |
|
24424
2658d7834c6e
8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents:
13963
diff
changeset
|
110 |
tty->print_cr("%s", printable_set); |
1 | 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". |
|
117 |
int Set::parse(const char *s) |
|
118 |
{ |
|
51050
96ea37459ca7
8207011: Remove uses of the register storage class specifier
mikael
parents:
47216
diff
changeset
|
119 |
char c; // Parse character |
96ea37459ca7
8207011: Remove uses of the register storage class specifier
mikael
parents:
47216
diff
changeset
|
120 |
const char *t = s; // Save the starting position of s. |
1 | 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--------------------------------------- |
|
156 |
SetI_::~SetI_() |
|
157 |
{ |
|
158 |
} |