Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/libadt/vectset.hpp
32285 views
/*1* Copyright (c) 1997, 2011, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_VM_LIBADT_VECTSET_HPP25#define SHARE_VM_LIBADT_VECTSET_HPP2627#include "libadt/set.hpp"2829// Vector Sets - An Abstract Data Type30//INTERFACE3132// These sets can grow or shrink, based on the initial size and the largest33// element currently in them. Slow and bulky for sparse sets, these sets34// are super for dense sets. They are fast and compact when dense.3536// TIME:37// O(1) - Insert, Delete, Member, Sort38// O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,39// Equal, ChooseMember, Forall4041// SPACE: (max_element)/(8*sizeof(int))424344//------------------------------VectorSet--------------------------------------45class VectorSet : public Set {46friend class VectorSetI; // Friendly iterator class47protected:48uint size; // Size of data IN LONGWORDS (32bits)49uint32 *data; // The data, bit packed5051void slamin( const VectorSet& s ); // Initialize one set with another52int compare(const VectorSet &s) const; // Compare set contents53void grow(uint newsize); // Grow vector to required bitsize5455public:56VectorSet(Arena *arena); // Creates a new, empty set.57VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts58Set &operator =(const Set &s); // Set clone; deep-copy guts59VectorSet &operator =(const VectorSet &s) // Set clone; deep-copy guts60{ if( &s != this ) { slamin(s); } return *this; }61~VectorSet() {}62Set &clone(void) const { return *(new VectorSet(*this)); }6364Set &operator <<=(uint elem); // Add member to set65VectorSet operator << (uint elem) // Add member to new set66{ VectorSet foo(*this); foo <<= elem; return foo; }67Set &operator >>=(uint elem); // Delete member from set68VectorSet operator >> (uint elem) // Delete member from new set69{ VectorSet foo(*this); foo >>= elem; return foo; }7071VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set72Set &operator &=(const Set &s); // Intersect sets into first set73VectorSet operator & (const VectorSet &s) const74{ VectorSet foo(*this); foo &= s; return foo; }7576VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set77Set &operator |=(const Set &s); // Intersect sets into first set78VectorSet operator | (const VectorSet &s) const79{ VectorSet foo(*this); foo |= s; return foo; }8081VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set82Set &operator -=(const Set &s); // Intersect sets into first set83VectorSet operator - (const VectorSet &s) const84{ VectorSet foo(*this); foo -= s; return foo; }8586int operator ==(const VectorSet &s) const; // True if sets are equal87int operator ==(const Set &s) const; // True if sets are equal88int operator < (const VectorSet &s) const; // True if strict subset89int operator < (const Set &s) const; // True if strict subset90int operator <=(const VectorSet &s) const; // True if subset relation holds.91int operator <=(const Set &s) const; // True if subset relation holds.92int disjoint (const Set &s) const; // True if sets are disjoint9394int operator [](uint elem) const; // Test for membership95uint getelem(void) const; // Return a random element96void Clear(void); // Clear a set97uint Size(void) const; // Number of elements in the Set.98void Sort(void); // Sort before iterating99int hash() const; // Hash function100void Reset(void) { // Reset a set101memset( data, 0, size*sizeof(uint32) );102}103104/* Removed for MCC BUG105operator const VectorSet* (void) const { return this; } */106const VectorSet *asVectorSet() const { return this; }107108// Expose internals for speed-critical fast iterators109uint word_size() const { return size; }110uint32 *EXPOSE() const { return data; }111112// Fast inlined "test and set". Replaces the idiom:113// if( visited[idx] ) return;114// visited <<= idx;115// With:116// if( visited.test_set(idx) ) return;117//118int test_set( uint elem ) {119uint word = elem >> 5; // Get the longword offset120if( word >= size ) // Beyond the last?121return test_set_grow(elem); // Then grow; set; return 0;122uint32 mask = 1L << (elem & 31); // Get bit mask123uint32 datum = data[word] & mask;// Get bit124data[word] |= mask; // Set bit125return datum; // Return bit126}127int test_set_grow( uint elem ) { // Insert & return 0;128(*this) <<= elem; // Insert into set129return 0; // Return 0!130}131132// Fast inlined test133int test( uint elem ) const {134uint word = elem >> 5; // Get the longword offset135if( word >= size ) return 0; // Beyond the last?136uint32 mask = 1L << (elem & 31); // Get bit mask137return data[word] & mask; // Get bit138}139140// Fast inlined set141void set( uint elem ) {142uint word = elem >> 5; // Get the longword offset143if( word >= size ) { // Beyond the last?144test_set_grow(elem); // Then grow and set145} else {146uint32 mask = 1L << (elem & 31); // Get bit mask147data[word] |= mask; // Set bit148}149}150151152private:153SetI_ *iterate(uint&) const;154};155156//------------------------------Iteration--------------------------------------157// Loop thru all elements of the set, setting "elem" to the element numbers158// in random order. Inserted or deleted elements during this operation may159// or may not be iterated over; untouched elements will be affected once.160// Usage: for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }161162class VectorSetI : public StackObj {163friend class VectorSet;164const VectorSet *s;165uint i, j;166uint32 mask;167uint next(void);168169public:170uint elem; // The publically accessible element171172VectorSetI( const VectorSet *vset ) :173s(vset),174i((uint)-1L),175j((uint)-1L),176mask((unsigned)(1L<<31)) {177elem = next();178}179180void operator ++(void) { elem = next(); }181int test(void) { return i < s->size; }182};183184#endif // SHARE_VM_LIBADT_VECTSET_HPP185186187