annotate src/share/vm/libadt/vectset.hpp @ 528:37b3ca071522

6793825: Missing include dependancies for GCC without predefined headers Summary: With predefined headers off for gcc, some .inline.hpp files aren't included to make definition visible for inline functions Reviewed-by: jcoomes, xlu
author coleenp
date Wed, 14 Jan 2009 20:14:19 -0500
parents a61af66fc99e
children c18cbe5936b8
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
a61af66fc99e Initial load
duke
parents:
diff changeset
1 /*
a61af66fc99e Initial load
duke
parents:
diff changeset
2 * Copyright 1997 Sun Microsystems, Inc. All Rights Reserved.
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 *
a61af66fc99e Initial load
duke
parents:
diff changeset
19 * Please contact Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
a61af66fc99e Initial load
duke
parents:
diff changeset
20 * CA 95054 USA or visit www.sun.com if you need additional information or
a61af66fc99e Initial load
duke
parents:
diff changeset
21 * have any questions.
a61af66fc99e Initial load
duke
parents:
diff changeset
22 *
a61af66fc99e Initial load
duke
parents:
diff changeset
23 */
a61af66fc99e Initial load
duke
parents:
diff changeset
24
a61af66fc99e Initial load
duke
parents:
diff changeset
25 #ifndef _VECTOR_SET_
a61af66fc99e Initial load
duke
parents:
diff changeset
26 #define _VECTOR_SET_
a61af66fc99e Initial load
duke
parents:
diff changeset
27 // Vector Sets - An Abstract Data Type
a61af66fc99e Initial load
duke
parents:
diff changeset
28 //INTERFACE
a61af66fc99e Initial load
duke
parents:
diff changeset
29
a61af66fc99e Initial load
duke
parents:
diff changeset
30 // These sets can grow or shrink, based on the initial size and the largest
a61af66fc99e Initial load
duke
parents:
diff changeset
31 // element currently in them. Slow and bulky for sparse sets, these sets
a61af66fc99e Initial load
duke
parents:
diff changeset
32 // are super for dense sets. They are fast and compact when dense.
a61af66fc99e Initial load
duke
parents:
diff changeset
33
a61af66fc99e Initial load
duke
parents:
diff changeset
34 // TIME:
a61af66fc99e Initial load
duke
parents:
diff changeset
35 // O(1) - Insert, Delete, Member, Sort
a61af66fc99e Initial load
duke
parents:
diff changeset
36 // O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,
a61af66fc99e Initial load
duke
parents:
diff changeset
37 // Equal, ChooseMember, Forall
a61af66fc99e Initial load
duke
parents:
diff changeset
38
a61af66fc99e Initial load
duke
parents:
diff changeset
39 // SPACE: (max_element)/(8*sizeof(int))
a61af66fc99e Initial load
duke
parents:
diff changeset
40
a61af66fc99e Initial load
duke
parents:
diff changeset
41
a61af66fc99e Initial load
duke
parents:
diff changeset
42 //------------------------------VectorSet--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
43 class VectorSet : public Set {
a61af66fc99e Initial load
duke
parents:
diff changeset
44 friend class VectorSetI; // Friendly iterator class
a61af66fc99e Initial load
duke
parents:
diff changeset
45 protected:
a61af66fc99e Initial load
duke
parents:
diff changeset
46 uint size; // Size of data IN LONGWORDS (32bits)
a61af66fc99e Initial load
duke
parents:
diff changeset
47 uint32 *data; // The data, bit packed
a61af66fc99e Initial load
duke
parents:
diff changeset
48
a61af66fc99e Initial load
duke
parents:
diff changeset
49 void slamin( const VectorSet& s ); // Initialize one set with another
a61af66fc99e Initial load
duke
parents:
diff changeset
50 int compare(const VectorSet &s) const; // Compare set contents
a61af66fc99e Initial load
duke
parents:
diff changeset
51 void grow(uint newsize); // Grow vector to required bitsize
a61af66fc99e Initial load
duke
parents:
diff changeset
52
a61af66fc99e Initial load
duke
parents:
diff changeset
53 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
54 VectorSet(Arena *arena); // Creates a new, empty set.
a61af66fc99e Initial load
duke
parents:
diff changeset
55 VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts
a61af66fc99e Initial load
duke
parents:
diff changeset
56 Set &operator =(const Set &s); // Set clone; deep-copy guts
a61af66fc99e Initial load
duke
parents:
diff changeset
57 VectorSet &operator =(const VectorSet &s) // Set clone; deep-copy guts
a61af66fc99e Initial load
duke
parents:
diff changeset
58 { if( &s != this ) { slamin(s); } return *this; }
a61af66fc99e Initial load
duke
parents:
diff changeset
59 ~VectorSet() {}
a61af66fc99e Initial load
duke
parents:
diff changeset
60 Set &clone(void) const { return *(new VectorSet(*this)); }
a61af66fc99e Initial load
duke
parents:
diff changeset
61
a61af66fc99e Initial load
duke
parents:
diff changeset
62 Set &operator <<=(uint elem); // Add member to set
a61af66fc99e Initial load
duke
parents:
diff changeset
63 VectorSet operator << (uint elem) // Add member to new set
a61af66fc99e Initial load
duke
parents:
diff changeset
64 { VectorSet foo(*this); foo <<= elem; return foo; }
a61af66fc99e Initial load
duke
parents:
diff changeset
65 Set &operator >>=(uint elem); // Delete member from set
a61af66fc99e Initial load
duke
parents:
diff changeset
66 VectorSet operator >> (uint elem) // Delete member from new set
a61af66fc99e Initial load
duke
parents:
diff changeset
67 { VectorSet foo(*this); foo >>= elem; return foo; }
a61af66fc99e Initial load
duke
parents:
diff changeset
68
a61af66fc99e Initial load
duke
parents:
diff changeset
69 VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set
a61af66fc99e Initial load
duke
parents:
diff changeset
70 Set &operator &=(const Set &s); // Intersect sets into first set
a61af66fc99e Initial load
duke
parents:
diff changeset
71 VectorSet operator & (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
72 { VectorSet foo(*this); foo &= s; return foo; }
a61af66fc99e Initial load
duke
parents:
diff changeset
73
a61af66fc99e Initial load
duke
parents:
diff changeset
74 VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set
a61af66fc99e Initial load
duke
parents:
diff changeset
75 Set &operator |=(const Set &s); // Intersect sets into first set
a61af66fc99e Initial load
duke
parents:
diff changeset
76 VectorSet operator | (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
77 { VectorSet foo(*this); foo |= s; return foo; }
a61af66fc99e Initial load
duke
parents:
diff changeset
78
a61af66fc99e Initial load
duke
parents:
diff changeset
79 VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set
a61af66fc99e Initial load
duke
parents:
diff changeset
80 Set &operator -=(const Set &s); // Intersect sets into first set
a61af66fc99e Initial load
duke
parents:
diff changeset
81 VectorSet operator - (const VectorSet &s) const
a61af66fc99e Initial load
duke
parents:
diff changeset
82 { VectorSet foo(*this); foo -= s; return foo; }
a61af66fc99e Initial load
duke
parents:
diff changeset
83
a61af66fc99e Initial load
duke
parents:
diff changeset
84 int operator ==(const VectorSet &s) const; // True if sets are equal
a61af66fc99e Initial load
duke
parents:
diff changeset
85 int operator ==(const Set &s) const; // True if sets are equal
a61af66fc99e Initial load
duke
parents:
diff changeset
86 int operator < (const VectorSet &s) const; // True if strict subset
a61af66fc99e Initial load
duke
parents:
diff changeset
87 int operator < (const Set &s) const; // True if strict subset
a61af66fc99e Initial load
duke
parents:
diff changeset
88 int operator <=(const VectorSet &s) const; // True if subset relation holds.
a61af66fc99e Initial load
duke
parents:
diff changeset
89 int operator <=(const Set &s) const; // True if subset relation holds.
a61af66fc99e Initial load
duke
parents:
diff changeset
90 int disjoint (const Set &s) const; // True if sets are disjoint
a61af66fc99e Initial load
duke
parents:
diff changeset
91
a61af66fc99e Initial load
duke
parents:
diff changeset
92 int operator [](uint elem) const; // Test for membership
a61af66fc99e Initial load
duke
parents:
diff changeset
93 uint getelem(void) const; // Return a random element
a61af66fc99e Initial load
duke
parents:
diff changeset
94 void Clear(void); // Clear a set
a61af66fc99e Initial load
duke
parents:
diff changeset
95 uint Size(void) const; // Number of elements in the Set.
a61af66fc99e Initial load
duke
parents:
diff changeset
96 void Sort(void); // Sort before iterating
a61af66fc99e Initial load
duke
parents:
diff changeset
97 int hash() const; // Hash function
a61af66fc99e Initial load
duke
parents:
diff changeset
98
a61af66fc99e Initial load
duke
parents:
diff changeset
99 /* Removed for MCC BUG
a61af66fc99e Initial load
duke
parents:
diff changeset
100 operator const VectorSet* (void) const { return this; } */
a61af66fc99e Initial load
duke
parents:
diff changeset
101 const VectorSet *asVectorSet() const { return this; }
a61af66fc99e Initial load
duke
parents:
diff changeset
102
a61af66fc99e Initial load
duke
parents:
diff changeset
103 // Expose internals for speed-critical fast iterators
a61af66fc99e Initial load
duke
parents:
diff changeset
104 uint word_size() const { return size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
105 uint32 *EXPOSE() const { return data; }
a61af66fc99e Initial load
duke
parents:
diff changeset
106
a61af66fc99e Initial load
duke
parents:
diff changeset
107 // Fast inlined "test and set". Replaces the idiom:
a61af66fc99e Initial load
duke
parents:
diff changeset
108 // if( visited[idx] ) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
109 // visited <<= idx;
a61af66fc99e Initial load
duke
parents:
diff changeset
110 // With:
a61af66fc99e Initial load
duke
parents:
diff changeset
111 // if( visited.test_set(idx) ) return;
a61af66fc99e Initial load
duke
parents:
diff changeset
112 //
a61af66fc99e Initial load
duke
parents:
diff changeset
113 int test_set( uint elem ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
114 uint word = elem >> 5; // Get the longword offset
a61af66fc99e Initial load
duke
parents:
diff changeset
115 if( word >= size ) // Beyond the last?
a61af66fc99e Initial load
duke
parents:
diff changeset
116 return test_set_grow(elem); // Then grow; set; return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
117 uint32 mask = 1L << (elem & 31); // Get bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
118 uint32 datum = data[word] & mask;// Get bit
a61af66fc99e Initial load
duke
parents:
diff changeset
119 data[word] |= mask; // Set bit
a61af66fc99e Initial load
duke
parents:
diff changeset
120 return datum; // Return bit
a61af66fc99e Initial load
duke
parents:
diff changeset
121 }
a61af66fc99e Initial load
duke
parents:
diff changeset
122 int test_set_grow( uint elem ) { // Insert & return 0;
a61af66fc99e Initial load
duke
parents:
diff changeset
123 (*this) <<= elem; // Insert into set
a61af66fc99e Initial load
duke
parents:
diff changeset
124 return 0; // Return 0!
a61af66fc99e Initial load
duke
parents:
diff changeset
125 }
a61af66fc99e Initial load
duke
parents:
diff changeset
126
a61af66fc99e Initial load
duke
parents:
diff changeset
127 // Fast inlined test
a61af66fc99e Initial load
duke
parents:
diff changeset
128 int test( uint elem ) const {
a61af66fc99e Initial load
duke
parents:
diff changeset
129 uint word = elem >> 5; // Get the longword offset
a61af66fc99e Initial load
duke
parents:
diff changeset
130 if( word >= size ) return 0; // Beyond the last?
a61af66fc99e Initial load
duke
parents:
diff changeset
131 uint32 mask = 1L << (elem & 31); // Get bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
132 return data[word] & mask; // Get bit
a61af66fc99e Initial load
duke
parents:
diff changeset
133 }
a61af66fc99e Initial load
duke
parents:
diff changeset
134
a61af66fc99e Initial load
duke
parents:
diff changeset
135 // Fast inlined set
a61af66fc99e Initial load
duke
parents:
diff changeset
136 void set( uint elem ) {
a61af66fc99e Initial load
duke
parents:
diff changeset
137 uint word = elem >> 5; // Get the longword offset
a61af66fc99e Initial load
duke
parents:
diff changeset
138 if( word >= size ) { // Beyond the last?
a61af66fc99e Initial load
duke
parents:
diff changeset
139 test_set_grow(elem); // Then grow and set
a61af66fc99e Initial load
duke
parents:
diff changeset
140 } else {
a61af66fc99e Initial load
duke
parents:
diff changeset
141 uint32 mask = 1L << (elem & 31); // Get bit mask
a61af66fc99e Initial load
duke
parents:
diff changeset
142 data[word] |= mask; // Set bit
a61af66fc99e Initial load
duke
parents:
diff changeset
143 }
a61af66fc99e Initial load
duke
parents:
diff changeset
144 }
a61af66fc99e Initial load
duke
parents:
diff changeset
145
a61af66fc99e Initial load
duke
parents:
diff changeset
146
a61af66fc99e Initial load
duke
parents:
diff changeset
147 private:
a61af66fc99e Initial load
duke
parents:
diff changeset
148 friend class VSetI_;
a61af66fc99e Initial load
duke
parents:
diff changeset
149 SetI_ *iterate(uint&) const;
a61af66fc99e Initial load
duke
parents:
diff changeset
150 };
a61af66fc99e Initial load
duke
parents:
diff changeset
151
a61af66fc99e Initial load
duke
parents:
diff changeset
152 //------------------------------Iteration--------------------------------------
a61af66fc99e Initial load
duke
parents:
diff changeset
153 // Loop thru all elements of the set, setting "elem" to the element numbers
a61af66fc99e Initial load
duke
parents:
diff changeset
154 // in random order. Inserted or deleted elements during this operation may
a61af66fc99e Initial load
duke
parents:
diff changeset
155 // or may not be iterated over; untouched elements will be affected once.
a61af66fc99e Initial load
duke
parents:
diff changeset
156 // Usage: for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }
a61af66fc99e Initial load
duke
parents:
diff changeset
157
a61af66fc99e Initial load
duke
parents:
diff changeset
158 class VSetI_ : public SetI_ {
a61af66fc99e Initial load
duke
parents:
diff changeset
159 friend class VectorSet;
a61af66fc99e Initial load
duke
parents:
diff changeset
160 friend class VectorSetI;
a61af66fc99e Initial load
duke
parents:
diff changeset
161 const VectorSet *s;
a61af66fc99e Initial load
duke
parents:
diff changeset
162 uint i, j;
a61af66fc99e Initial load
duke
parents:
diff changeset
163 uint32 mask;
a61af66fc99e Initial load
duke
parents:
diff changeset
164 VSetI_(const VectorSet *vset);
a61af66fc99e Initial load
duke
parents:
diff changeset
165 uint next(void);
a61af66fc99e Initial load
duke
parents:
diff changeset
166 int test(void) { return i < s->size; }
a61af66fc99e Initial load
duke
parents:
diff changeset
167 };
a61af66fc99e Initial load
duke
parents:
diff changeset
168
a61af66fc99e Initial load
duke
parents:
diff changeset
169 class VectorSetI : public SetI {
a61af66fc99e Initial load
duke
parents:
diff changeset
170 public:
a61af66fc99e Initial load
duke
parents:
diff changeset
171 VectorSetI( const VectorSet *s ) : SetI(s) { }
a61af66fc99e Initial load
duke
parents:
diff changeset
172 void operator ++(void) { elem = ((VSetI_*)impl)->next(); }
a61af66fc99e Initial load
duke
parents:
diff changeset
173 int test(void) { return ((VSetI_*)impl)->test(); }
a61af66fc99e Initial load
duke
parents:
diff changeset
174 };
a61af66fc99e Initial load
duke
parents:
diff changeset
175
a61af66fc99e Initial load
duke
parents:
diff changeset
176 #endif // _VECTOR_SET_