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