annotate src/share/vm/libadt/set.cpp @ 1972:f95d63e2154a

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