0N/A/*
1879N/A * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
0N/A * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
0N/A *
0N/A * This code is free software; you can redistribute it and/or modify it
0N/A * under the terms of the GNU General Public License version 2 only, as
0N/A * published by the Free Software Foundation.
0N/A *
0N/A * This code is distributed in the hope that it will be useful, but WITHOUT
0N/A * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
0N/A * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
0N/A * version 2 for more details (a copy is included in the LICENSE file that
0N/A * accompanied this code).
0N/A *
0N/A * You should have received a copy of the GNU General Public License version
0N/A * 2 along with this work; if not, write to the Free Software Foundation,
0N/A * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
0N/A *
1472N/A * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
1472N/A * or visit www.oracle.com if you need additional information or have any
1472N/A * questions.
0N/A *
0N/A */
0N/A
1879N/A#ifndef SHARE_VM_LIBADT_SET_HPP
1879N/A#define SHARE_VM_LIBADT_SET_HPP
1879N/A
1879N/A#include "libadt/port.hpp"
1879N/A#include "memory/allocation.hpp"
1879N/A
0N/A// Sets - An Abstract Data Type
0N/A
0N/A//INTERFACE
0N/A
0N/Aclass SparseSet;
0N/Aclass VectorSet;
0N/Aclass ListSet;
0N/Aclass CoSet;
0N/A
0N/Aclass ostream;
0N/Aclass SetI_;
0N/A
0N/A// These sets can grow or shrink, based on the initial size and the largest
0N/A// element currently in them. Basically, they allow a bunch of bits to be
0N/A// grouped together, tested, set & cleared, intersected, etc. The basic
0N/A// Set class is an abstract class, and cannot be constructed. Instead,
0N/A// one of VectorSet, SparseSet, or ListSet is created. Each variation has
0N/A// different asymptotic running times for different operations, and different
0N/A// constants of proportionality as well.
0N/A// {n = number of elements, N = largest element}
0N/A
0N/A// VectorSet SparseSet ListSet
0N/A// Create O(N) O(1) O(1)
0N/A// Clear O(N) O(1) O(1)
0N/A// Insert O(1) O(1) O(log n)
0N/A// Delete O(1) O(1) O(log n)
0N/A// Member O(1) O(1) O(log n)
0N/A// Size O(N) O(1) O(1)
0N/A// Copy O(N) O(n) O(n)
0N/A// Union O(N) O(n) O(n log n)
0N/A// Intersect O(N) O(n) O(n log n)
0N/A// Difference O(N) O(n) O(n log n)
0N/A// Equal O(N) O(n) O(n log n)
0N/A// ChooseMember O(N) O(1) O(1)
0N/A// Sort O(1) O(n log n) O(1)
0N/A// Forall O(N) O(n) O(n)
0N/A// Complement O(1) O(1) O(1)
0N/A
0N/A// TIME: N/32 n 8*n Accesses
0N/A// SPACE: N/8 4*N+4*n 8*n Bytes
0N/A
0N/A// Create: Make an empty set
0N/A// Clear: Remove all the elements of a Set
0N/A// Insert: Insert an element into a Set; duplicates are ignored
0N/A// Delete: Removes an element from a Set
0N/A// Member: Tests for membership in a Set
0N/A// Size: Returns the number of members of a Set
0N/A// Copy: Copy or assign one Set to another
0N/A// Union: Union 2 sets together
0N/A// Intersect: Intersect 2 sets together
0N/A// Difference: Compute A & !B; remove from set A those elements in set B
0N/A// Equal: Test for equality between 2 sets
0N/A// ChooseMember Pick a random member
0N/A// Sort: If no other operation changes the set membership, a following
0N/A// Forall will iterate the members in ascending order.
0N/A// Forall: Iterate over the elements of a Set. Operations that modify
0N/A// the set membership during iteration work, but the iterator may
0N/A// skip any member or duplicate any member.
0N/A// Complement: Only supported in the Co-Set variations. It adds a small
0N/A// constant-time test to every Set operation.
0N/A//
0N/A// PERFORMANCE ISSUES:
0N/A// If you "cast away" the specific set variation you are using, and then do
0N/A// operations on the basic "Set" object you will pay a virtual function call
0N/A// to get back the specific set variation. On the other hand, using the
0N/A// generic Set means you can change underlying implementations by just
0N/A// changing the initial declaration. Examples:
0N/A// void foo(VectorSet vs1, VectorSet vs2) { vs1 |= vs2; }
0N/A// "foo" must be called with a VectorSet. The vector set union operation
0N/A// is called directly.
0N/A// void foo(Set vs1, Set vs2) { vs1 |= vs2; }
0N/A// "foo" may be called with *any* kind of sets; suppose it is called with
0N/A// VectorSets. Two virtual function calls are used to figure out the that vs1
0N/A// and vs2 are VectorSets. In addition, if vs2 is not a VectorSet then a
0N/A// temporary VectorSet copy of vs2 will be made before the union proceeds.
0N/A//
0N/A// VectorSets have a small constant. Time and space are proportional to the
0N/A// largest element. Fine for dense sets and largest element < 10,000.
0N/A// SparseSets have a medium constant. Time is proportional to the number of
0N/A// elements, space is proportional to the largest element.
0N/A// Fine (but big) with the largest element < 100,000.
0N/A// ListSets have a big constant. Time *and space* are proportional to the
0N/A// number of elements. They work well for a few elements of *any* size
0N/A// (i.e. sets of pointers)!
0N/A
0N/A//------------------------------Set--------------------------------------------
0N/Aclass Set : public ResourceObj {
0N/A public:
0N/A
0N/A // Creates a new, empty set.
0N/A // DO NOT CONSTRUCT A Set. THIS IS AN ABSTRACT CLASS, FOR INHERITENCE ONLY
0N/A Set(Arena *arena) : _set_arena(arena) {};
0N/A
0N/A // Creates a new set from an existing set
0N/A // DO NOT CONSTRUCT A Set. THIS IS AN ABSTRACT CLASS, FOR INHERITENCE ONLY
0N/A Set(const Set &) {};
0N/A
0N/A // Set assignment; deep-copy guts
0N/A virtual Set &operator =(const Set &s)=0;
0N/A virtual Set &clone(void) const=0;
0N/A
0N/A // Virtual destructor
0N/A virtual ~Set() {};
0N/A
0N/A // Add member to set
0N/A virtual Set &operator <<=(uint elem)=0;
0N/A // virtual Set operator << (uint elem);
0N/A
0N/A // Delete member from set
0N/A virtual Set &operator >>=(uint elem)=0;
0N/A // virtual Set operator >> (uint elem);
0N/A
0N/A // Membership test. Result is Zero (absent)/ Non-Zero (present)
0N/A virtual int operator [](uint elem) const=0;
0N/A
0N/A // Intersect sets
0N/A virtual Set &operator &=(const Set &s)=0;
0N/A // virtual Set operator & (const Set &s) const;
0N/A
0N/A // Union sets
0N/A virtual Set &operator |=(const Set &s)=0;
0N/A // virtual Set operator | (const Set &s) const;
0N/A
0N/A // Difference sets
0N/A virtual Set &operator -=(const Set &s)=0;
0N/A // virtual Set operator - (const Set &s) const;
0N/A
0N/A // Tests for equality. Result is Zero (false)/ Non-Zero (true)
0N/A virtual int operator ==(const Set &s) const=0;
0N/A int operator !=(const Set &s) const { return !(*this == s); }
0N/A virtual int disjoint(const Set &s) const=0;
0N/A
0N/A // Tests for strict subset. Result is Zero (false)/ Non-Zero (true)
0N/A virtual int operator < (const Set &s) const=0;
0N/A int operator > (const Set &s) const { return s < *this; }
0N/A
0N/A // Tests for subset. Result is Zero (false)/ Non-Zero (true)
0N/A virtual int operator <=(const Set &s) const=0;
0N/A int operator >=(const Set &s) const { return s <= *this; }
0N/A
0N/A // Return any member of the Set. Undefined if the Set is empty.
0N/A virtual uint getelem(void) const=0;
0N/A
0N/A // Clear all the elements in the Set
0N/A virtual void Clear(void)=0;
0N/A
0N/A // Return the number of members in the Set
0N/A virtual uint Size(void) const=0;
0N/A
0N/A // If an iterator follows a "Sort()" without any Set-modifying operations
0N/A // inbetween then the iterator will visit the elements in ascending order.
0N/A virtual void Sort(void)=0;
0N/A
0N/A // Convert a set to printable string in an allocated buffer.
0N/A // The caller must deallocate the string.
0N/A virtual char *setstr(void) const;
0N/A
0N/A // Print the Set on "stdout". Can be conveniently called in the debugger
0N/A void print() const;
0N/A
0N/A // Parse text from the string into the Set. Return length parsed.
0N/A virtual int parse(const char *s);
0N/A
0N/A // Convert a generic Set to a specific Set
0N/A /* Removed for MCC BUG
0N/A virtual operator const SparseSet* (void) const;
0N/A virtual operator const VectorSet* (void) const;
0N/A virtual operator const ListSet * (void) const;
0N/A virtual operator const CoSet * (void) const; */
0N/A virtual const SparseSet *asSparseSet(void) const;
0N/A virtual const VectorSet *asVectorSet(void) const;
0N/A virtual const ListSet *asListSet (void) const;
0N/A virtual const CoSet *asCoSet (void) const;
0N/A
0N/A // Hash the set. Sets of different types but identical elements will NOT
0N/A // hash the same. Same set type, same elements WILL hash the same.
0N/A virtual int hash() const = 0;
0N/A
0N/Aprotected:
0N/A friend class SetI;
0N/A friend class CoSet;
0N/A virtual class SetI_ *iterate(uint&) const=0;
0N/A
0N/A // Need storeage for the set
0N/A Arena *_set_arena;
0N/A};
0N/Atypedef Set&((*Set_Constructor)(Arena *arena));
0N/Aextern Set &ListSet_Construct(Arena *arena);
0N/Aextern Set &VectorSet_Construct(Arena *arena);
0N/Aextern Set &SparseSet_Construct(Arena *arena);
0N/A
0N/A//------------------------------Iteration--------------------------------------
0N/A// Loop thru all elements of the set, setting "elem" to the element numbers
0N/A// in random order. Inserted or deleted elements during this operation may
0N/A// or may not be iterated over; untouched elements will be affected once.
0N/A
0N/A// Usage: for( SetI i(s); i.test(); i++ ) { body = i.elem; } ...OR...
0N/A// for( i.reset(s); i.test(); i++ ) { body = i.elem; }
0N/A
0N/Aclass SetI_ : public ResourceObj {
0N/Aprotected:
0N/A friend class SetI;
0N/A virtual ~SetI_();
0N/A virtual uint next(void)=0;
0N/A virtual int test(void)=0;
0N/A};
0N/A
0N/Aclass SetI {
0N/Aprotected:
0N/A SetI_ *impl;
0N/Apublic:
0N/A uint elem; // The publically accessible element
0N/A
0N/A SetI( const Set *s ) { impl = s->iterate(elem); }
0N/A ~SetI() { delete impl; }
0N/A void reset( const Set *s ) { delete impl; impl = s->iterate(elem); }
0N/A void operator ++(void) { elem = impl->next(); }
0N/A int test(void) { return impl->test(); }
0N/A};
0N/A
1879N/A#endif // SHARE_VM_LIBADT_SET_HPP