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