src/share/vm/libadt/vectset.hpp

Thu, 27 May 2010 19:08:38 -0700

author
trims
date
Thu, 27 May 2010 19:08:38 -0700
changeset 1907
c18cbe5936b8
parent 435
a61af66fc99e
child 2314
f95d63e2154a
permissions
-rw-r--r--

6941466: Oracle rebranding changes for Hotspot repositories
Summary: Change all the Sun copyrights to Oracle copyright
Reviewed-by: ohair

duke@435 1 /*
trims@1907 2 * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
duke@435 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
duke@435 4 *
duke@435 5 * This code is free software; you can redistribute it and/or modify it
duke@435 6 * under the terms of the GNU General Public License version 2 only, as
duke@435 7 * published by the Free Software Foundation.
duke@435 8 *
duke@435 9 * This code is distributed in the hope that it will be useful, but WITHOUT
duke@435 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
duke@435 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
duke@435 12 * version 2 for more details (a copy is included in the LICENSE file that
duke@435 13 * accompanied this code).
duke@435 14 *
duke@435 15 * You should have received a copy of the GNU General Public License version
duke@435 16 * 2 along with this work; if not, write to the Free Software Foundation,
duke@435 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
duke@435 18 *
trims@1907 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
trims@1907 20 * or visit www.oracle.com if you need additional information or have any
trims@1907 21 * questions.
duke@435 22 *
duke@435 23 */
duke@435 24
duke@435 25 #ifndef _VECTOR_SET_
duke@435 26 #define _VECTOR_SET_
duke@435 27 // Vector Sets - An Abstract Data Type
duke@435 28 //INTERFACE
duke@435 29
duke@435 30 // These sets can grow or shrink, based on the initial size and the largest
duke@435 31 // element currently in them. Slow and bulky for sparse sets, these sets
duke@435 32 // are super for dense sets. They are fast and compact when dense.
duke@435 33
duke@435 34 // TIME:
duke@435 35 // O(1) - Insert, Delete, Member, Sort
duke@435 36 // O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,
duke@435 37 // Equal, ChooseMember, Forall
duke@435 38
duke@435 39 // SPACE: (max_element)/(8*sizeof(int))
duke@435 40
duke@435 41
duke@435 42 //------------------------------VectorSet--------------------------------------
duke@435 43 class VectorSet : public Set {
duke@435 44 friend class VectorSetI; // Friendly iterator class
duke@435 45 protected:
duke@435 46 uint size; // Size of data IN LONGWORDS (32bits)
duke@435 47 uint32 *data; // The data, bit packed
duke@435 48
duke@435 49 void slamin( const VectorSet& s ); // Initialize one set with another
duke@435 50 int compare(const VectorSet &s) const; // Compare set contents
duke@435 51 void grow(uint newsize); // Grow vector to required bitsize
duke@435 52
duke@435 53 public:
duke@435 54 VectorSet(Arena *arena); // Creates a new, empty set.
duke@435 55 VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts
duke@435 56 Set &operator =(const Set &s); // Set clone; deep-copy guts
duke@435 57 VectorSet &operator =(const VectorSet &s) // Set clone; deep-copy guts
duke@435 58 { if( &s != this ) { slamin(s); } return *this; }
duke@435 59 ~VectorSet() {}
duke@435 60 Set &clone(void) const { return *(new VectorSet(*this)); }
duke@435 61
duke@435 62 Set &operator <<=(uint elem); // Add member to set
duke@435 63 VectorSet operator << (uint elem) // Add member to new set
duke@435 64 { VectorSet foo(*this); foo <<= elem; return foo; }
duke@435 65 Set &operator >>=(uint elem); // Delete member from set
duke@435 66 VectorSet operator >> (uint elem) // Delete member from new set
duke@435 67 { VectorSet foo(*this); foo >>= elem; return foo; }
duke@435 68
duke@435 69 VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set
duke@435 70 Set &operator &=(const Set &s); // Intersect sets into first set
duke@435 71 VectorSet operator & (const VectorSet &s) const
duke@435 72 { VectorSet foo(*this); foo &= s; return foo; }
duke@435 73
duke@435 74 VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set
duke@435 75 Set &operator |=(const Set &s); // Intersect sets into first set
duke@435 76 VectorSet operator | (const VectorSet &s) const
duke@435 77 { VectorSet foo(*this); foo |= s; return foo; }
duke@435 78
duke@435 79 VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set
duke@435 80 Set &operator -=(const Set &s); // Intersect sets into first set
duke@435 81 VectorSet operator - (const VectorSet &s) const
duke@435 82 { VectorSet foo(*this); foo -= s; return foo; }
duke@435 83
duke@435 84 int operator ==(const VectorSet &s) const; // True if sets are equal
duke@435 85 int operator ==(const Set &s) const; // True if sets are equal
duke@435 86 int operator < (const VectorSet &s) const; // True if strict subset
duke@435 87 int operator < (const Set &s) const; // True if strict subset
duke@435 88 int operator <=(const VectorSet &s) const; // True if subset relation holds.
duke@435 89 int operator <=(const Set &s) const; // True if subset relation holds.
duke@435 90 int disjoint (const Set &s) const; // True if sets are disjoint
duke@435 91
duke@435 92 int operator [](uint elem) const; // Test for membership
duke@435 93 uint getelem(void) const; // Return a random element
duke@435 94 void Clear(void); // Clear a set
duke@435 95 uint Size(void) const; // Number of elements in the Set.
duke@435 96 void Sort(void); // Sort before iterating
duke@435 97 int hash() const; // Hash function
duke@435 98
duke@435 99 /* Removed for MCC BUG
duke@435 100 operator const VectorSet* (void) const { return this; } */
duke@435 101 const VectorSet *asVectorSet() const { return this; }
duke@435 102
duke@435 103 // Expose internals for speed-critical fast iterators
duke@435 104 uint word_size() const { return size; }
duke@435 105 uint32 *EXPOSE() const { return data; }
duke@435 106
duke@435 107 // Fast inlined "test and set". Replaces the idiom:
duke@435 108 // if( visited[idx] ) return;
duke@435 109 // visited <<= idx;
duke@435 110 // With:
duke@435 111 // if( visited.test_set(idx) ) return;
duke@435 112 //
duke@435 113 int test_set( uint elem ) {
duke@435 114 uint word = elem >> 5; // Get the longword offset
duke@435 115 if( word >= size ) // Beyond the last?
duke@435 116 return test_set_grow(elem); // Then grow; set; return 0;
duke@435 117 uint32 mask = 1L << (elem & 31); // Get bit mask
duke@435 118 uint32 datum = data[word] & mask;// Get bit
duke@435 119 data[word] |= mask; // Set bit
duke@435 120 return datum; // Return bit
duke@435 121 }
duke@435 122 int test_set_grow( uint elem ) { // Insert & return 0;
duke@435 123 (*this) <<= elem; // Insert into set
duke@435 124 return 0; // Return 0!
duke@435 125 }
duke@435 126
duke@435 127 // Fast inlined test
duke@435 128 int test( uint elem ) const {
duke@435 129 uint word = elem >> 5; // Get the longword offset
duke@435 130 if( word >= size ) return 0; // Beyond the last?
duke@435 131 uint32 mask = 1L << (elem & 31); // Get bit mask
duke@435 132 return data[word] & mask; // Get bit
duke@435 133 }
duke@435 134
duke@435 135 // Fast inlined set
duke@435 136 void set( uint elem ) {
duke@435 137 uint word = elem >> 5; // Get the longword offset
duke@435 138 if( word >= size ) { // Beyond the last?
duke@435 139 test_set_grow(elem); // Then grow and set
duke@435 140 } else {
duke@435 141 uint32 mask = 1L << (elem & 31); // Get bit mask
duke@435 142 data[word] |= mask; // Set bit
duke@435 143 }
duke@435 144 }
duke@435 145
duke@435 146
duke@435 147 private:
duke@435 148 friend class VSetI_;
duke@435 149 SetI_ *iterate(uint&) const;
duke@435 150 };
duke@435 151
duke@435 152 //------------------------------Iteration--------------------------------------
duke@435 153 // Loop thru all elements of the set, setting "elem" to the element numbers
duke@435 154 // in random order. Inserted or deleted elements during this operation may
duke@435 155 // or may not be iterated over; untouched elements will be affected once.
duke@435 156 // Usage: for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }
duke@435 157
duke@435 158 class VSetI_ : public SetI_ {
duke@435 159 friend class VectorSet;
duke@435 160 friend class VectorSetI;
duke@435 161 const VectorSet *s;
duke@435 162 uint i, j;
duke@435 163 uint32 mask;
duke@435 164 VSetI_(const VectorSet *vset);
duke@435 165 uint next(void);
duke@435 166 int test(void) { return i < s->size; }
duke@435 167 };
duke@435 168
duke@435 169 class VectorSetI : public SetI {
duke@435 170 public:
duke@435 171 VectorSetI( const VectorSet *s ) : SetI(s) { }
duke@435 172 void operator ++(void) { elem = ((VSetI_*)impl)->next(); }
duke@435 173 int test(void) { return ((VSetI_*)impl)->test(); }
duke@435 174 };
duke@435 175
duke@435 176 #endif // _VECTOR_SET_

mercurial