Path: blob/aarch64-shenandoah-jdk8u272-b10/hotspot/src/share/vm/libadt/set.hpp
32285 views
/*1* Copyright (c) 1997, 2010, 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_SET_HPP25#define SHARE_VM_LIBADT_SET_HPP2627#include "libadt/port.hpp"28#include "memory/allocation.hpp"2930// Sets - An Abstract Data Type3132//INTERFACE3334class SparseSet;35class VectorSet;36class ListSet;37class CoSet;3839class ostream;40class SetI_;4142// These sets can grow or shrink, based on the initial size and the largest43// element currently in them. Basically, they allow a bunch of bits to be44// grouped together, tested, set & cleared, intersected, etc. The basic45// Set class is an abstract class, and cannot be constructed. Instead,46// one of VectorSet, SparseSet, or ListSet is created. Each variation has47// different asymptotic running times for different operations, and different48// constants of proportionality as well.49// {n = number of elements, N = largest element}5051// VectorSet SparseSet ListSet52// Create O(N) O(1) O(1)53// Clear O(N) O(1) O(1)54// Insert O(1) O(1) O(log n)55// Delete O(1) O(1) O(log n)56// Member O(1) O(1) O(log n)57// Size O(N) O(1) O(1)58// Copy O(N) O(n) O(n)59// Union O(N) O(n) O(n log n)60// Intersect O(N) O(n) O(n log n)61// Difference O(N) O(n) O(n log n)62// Equal O(N) O(n) O(n log n)63// ChooseMember O(N) O(1) O(1)64// Sort O(1) O(n log n) O(1)65// Forall O(N) O(n) O(n)66// Complement O(1) O(1) O(1)6768// TIME: N/32 n 8*n Accesses69// SPACE: N/8 4*N+4*n 8*n Bytes7071// Create: Make an empty set72// Clear: Remove all the elements of a Set73// Insert: Insert an element into a Set; duplicates are ignored74// Delete: Removes an element from a Set75// Member: Tests for membership in a Set76// Size: Returns the number of members of a Set77// Copy: Copy or assign one Set to another78// Union: Union 2 sets together79// Intersect: Intersect 2 sets together80// Difference: Compute A & !B; remove from set A those elements in set B81// Equal: Test for equality between 2 sets82// ChooseMember Pick a random member83// Sort: If no other operation changes the set membership, a following84// Forall will iterate the members in ascending order.85// Forall: Iterate over the elements of a Set. Operations that modify86// the set membership during iteration work, but the iterator may87// skip any member or duplicate any member.88// Complement: Only supported in the Co-Set variations. It adds a small89// constant-time test to every Set operation.90//91// PERFORMANCE ISSUES:92// If you "cast away" the specific set variation you are using, and then do93// operations on the basic "Set" object you will pay a virtual function call94// to get back the specific set variation. On the other hand, using the95// generic Set means you can change underlying implementations by just96// changing the initial declaration. Examples:97// void foo(VectorSet vs1, VectorSet vs2) { vs1 |= vs2; }98// "foo" must be called with a VectorSet. The vector set union operation99// is called directly.100// void foo(Set vs1, Set vs2) { vs1 |= vs2; }101// "foo" may be called with *any* kind of sets; suppose it is called with102// VectorSets. Two virtual function calls are used to figure out the that vs1103// and vs2 are VectorSets. In addition, if vs2 is not a VectorSet then a104// temporary VectorSet copy of vs2 will be made before the union proceeds.105//106// VectorSets have a small constant. Time and space are proportional to the107// largest element. Fine for dense sets and largest element < 10,000.108// SparseSets have a medium constant. Time is proportional to the number of109// elements, space is proportional to the largest element.110// Fine (but big) with the largest element < 100,000.111// ListSets have a big constant. Time *and space* are proportional to the112// number of elements. They work well for a few elements of *any* size113// (i.e. sets of pointers)!114115//------------------------------Set--------------------------------------------116class Set : public ResourceObj {117public:118119// Creates a new, empty set.120// DO NOT CONSTRUCT A Set. THIS IS AN ABSTRACT CLASS, FOR INHERITENCE ONLY121Set(Arena *arena) : _set_arena(arena) {};122123// Creates a new set from an existing set124// DO NOT CONSTRUCT A Set. THIS IS AN ABSTRACT CLASS, FOR INHERITENCE ONLY125Set(const Set &) {};126127// Set assignment; deep-copy guts128virtual Set &operator =(const Set &s)=0;129virtual Set &clone(void) const=0;130131// Virtual destructor132virtual ~Set() {};133134// Add member to set135virtual Set &operator <<=(uint elem)=0;136// virtual Set operator << (uint elem);137138// Delete member from set139virtual Set &operator >>=(uint elem)=0;140// virtual Set operator >> (uint elem);141142// Membership test. Result is Zero (absent)/ Non-Zero (present)143virtual int operator [](uint elem) const=0;144145// Intersect sets146virtual Set &operator &=(const Set &s)=0;147// virtual Set operator & (const Set &s) const;148149// Union sets150virtual Set &operator |=(const Set &s)=0;151// virtual Set operator | (const Set &s) const;152153// Difference sets154virtual Set &operator -=(const Set &s)=0;155// virtual Set operator - (const Set &s) const;156157// Tests for equality. Result is Zero (false)/ Non-Zero (true)158virtual int operator ==(const Set &s) const=0;159int operator !=(const Set &s) const { return !(*this == s); }160virtual int disjoint(const Set &s) const=0;161162// Tests for strict subset. Result is Zero (false)/ Non-Zero (true)163virtual int operator < (const Set &s) const=0;164int operator > (const Set &s) const { return s < *this; }165166// Tests for subset. Result is Zero (false)/ Non-Zero (true)167virtual int operator <=(const Set &s) const=0;168int operator >=(const Set &s) const { return s <= *this; }169170// Return any member of the Set. Undefined if the Set is empty.171virtual uint getelem(void) const=0;172173// Clear all the elements in the Set174virtual void Clear(void)=0;175176// Return the number of members in the Set177virtual uint Size(void) const=0;178179// If an iterator follows a "Sort()" without any Set-modifying operations180// inbetween then the iterator will visit the elements in ascending order.181virtual void Sort(void)=0;182183// Convert a set to printable string in an allocated buffer.184// The caller must deallocate the string.185virtual char *setstr(void) const;186187// Print the Set on "stdout". Can be conveniently called in the debugger188void print() const;189190// Parse text from the string into the Set. Return length parsed.191virtual int parse(const char *s);192193// Convert a generic Set to a specific Set194/* Removed for MCC BUG195virtual operator const SparseSet* (void) const;196virtual operator const VectorSet* (void) const;197virtual operator const ListSet * (void) const;198virtual operator const CoSet * (void) const; */199virtual const SparseSet *asSparseSet(void) const;200virtual const VectorSet *asVectorSet(void) const;201virtual const ListSet *asListSet (void) const;202virtual const CoSet *asCoSet (void) const;203204// Hash the set. Sets of different types but identical elements will NOT205// hash the same. Same set type, same elements WILL hash the same.206virtual int hash() const = 0;207208protected:209friend class SetI;210friend class CoSet;211virtual class SetI_ *iterate(uint&) const=0;212213// Need storeage for the set214Arena *_set_arena;215};216typedef Set&((*Set_Constructor)(Arena *arena));217extern Set &ListSet_Construct(Arena *arena);218extern Set &VectorSet_Construct(Arena *arena);219extern Set &SparseSet_Construct(Arena *arena);220221//------------------------------Iteration--------------------------------------222// Loop thru all elements of the set, setting "elem" to the element numbers223// in random order. Inserted or deleted elements during this operation may224// or may not be iterated over; untouched elements will be affected once.225226// Usage: for( SetI i(s); i.test(); i++ ) { body = i.elem; } ...OR...227// for( i.reset(s); i.test(); i++ ) { body = i.elem; }228229class SetI_ : public ResourceObj {230protected:231friend class SetI;232virtual ~SetI_();233virtual uint next(void)=0;234virtual int test(void)=0;235};236237class SetI {238protected:239SetI_ *impl;240public:241uint elem; // The publically accessible element242243SetI( const Set *s ) { impl = s->iterate(elem); }244~SetI() { delete impl; }245void reset( const Set *s ) { delete impl; impl = s->iterate(elem); }246void operator ++(void) { elem = impl->next(); }247int test(void) { return impl->test(); }248};249250#endif // SHARE_VM_LIBADT_SET_HPP251252253