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