1 /*
   2  * Copyright (c) 1997, 2010, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  *
  23  */
  24 
  25 #ifndef SHARE_VM_LIBADT_SET_HPP
  26 #define SHARE_VM_LIBADT_SET_HPP
  27 
  28 #include "libadt/port.hpp"
  29 #include "memory/allocation.hpp"
  30 
  31 #ifndef _SET_
  32 #define _SET_
  33 // Sets - An Abstract Data Type
  34 
  35 // Should not manually include these in AVM framework. %%%%% - Ungar
  36 // #ifndef _PORT_
  37 // #include "port.hpp"
  38 // #endif // _PORT_
  39 //INTERFACE
  40 
  41 class SparseSet;
  42 class VectorSet;
  43 class ListSet;
  44 class CoSet;
  45 
  46 class ostream;
  47 class SetI_;
  48 
  49 // These sets can grow or shrink, based on the initial size and the largest
  50 // element currently in them.  Basically, they allow a bunch of bits to be
  51 // grouped together, tested, set & cleared, intersected, etc.  The basic
  52 // Set class is an abstract class, and cannot be constructed.  Instead,
  53 // one of VectorSet, SparseSet, or ListSet is created.  Each variation has
  54 // different asymptotic running times for different operations, and different
  55 // constants of proportionality as well.
  56 // {n = number of elements, N = largest element}
  57 
  58 //              VectorSet       SparseSet       ListSet
  59 // Create       O(N)            O(1)            O(1)
  60 // Clear        O(N)            O(1)            O(1)
  61 // Insert       O(1)            O(1)            O(log n)
  62 // Delete       O(1)            O(1)            O(log n)
  63 // Member       O(1)            O(1)            O(log n)
  64 // Size         O(N)            O(1)            O(1)
  65 // Copy         O(N)            O(n)            O(n)
  66 // Union        O(N)            O(n)            O(n log n)
  67 // Intersect    O(N)            O(n)            O(n log n)
  68 // Difference   O(N)            O(n)            O(n log n)
  69 // Equal        O(N)            O(n)            O(n log n)
  70 // ChooseMember O(N)            O(1)            O(1)
  71 // Sort         O(1)            O(n log n)      O(1)
  72 // Forall       O(N)            O(n)            O(n)
  73 // Complement   O(1)            O(1)            O(1)
  74 
  75 // TIME:        N/32            n               8*n     Accesses
  76 // SPACE:       N/8             4*N+4*n         8*n     Bytes
  77 
  78 // Create:      Make an empty set
  79 // Clear:       Remove all the elements of a Set
  80 // Insert:      Insert an element into a Set; duplicates are ignored
  81 // Delete:      Removes an element from a Set
  82 // Member:      Tests for membership in a Set
  83 // Size:        Returns the number of members of a Set
  84 // Copy:        Copy or assign one Set to another
  85 // Union:       Union 2 sets together
  86 // Intersect:   Intersect 2 sets together
  87 // Difference:  Compute A & !B; remove from set A those elements in set B
  88 // Equal:       Test for equality between 2 sets
  89 // ChooseMember Pick a random member
  90 // Sort:        If no other operation changes the set membership, a following
  91 //              Forall will iterate the members in ascending order.
  92 // Forall:      Iterate over the elements of a Set.  Operations that modify
  93 //              the set membership during iteration work, but the iterator may
  94 //              skip any member or duplicate any member.
  95 // Complement:  Only supported in the Co-Set variations.  It adds a small
  96 //              constant-time test to every Set operation.
  97 //
  98 // PERFORMANCE ISSUES:
  99 // If you "cast away" the specific set variation you are using, and then do
 100 // operations on the basic "Set" object you will pay a virtual function call
 101 // to get back the specific set variation.  On the other hand, using the
 102 // generic Set means you can change underlying implementations by just
 103 // changing the initial declaration.  Examples:
 104 //      void foo(VectorSet vs1, VectorSet vs2) { vs1 |= vs2; }
 105 // "foo" must be called with a VectorSet.  The vector set union operation
 106 // is called directly.
 107 //      void foo(Set vs1, Set vs2) { vs1 |= vs2; }
 108 // "foo" may be called with *any* kind of sets; suppose it is called with
 109 // VectorSets.  Two virtual function calls are used to figure out the that vs1
 110 // and vs2 are VectorSets.  In addition, if vs2 is not a VectorSet then a
 111 // temporary VectorSet copy of vs2 will be made before the union proceeds.
 112 //
 113 // VectorSets have a small constant.  Time and space are proportional to the
 114 //   largest element.  Fine for dense sets and largest element < 10,000.
 115 // SparseSets have a medium constant.  Time is proportional to the number of
 116 //   elements, space is proportional to the largest element.
 117 //   Fine (but big) with the largest element < 100,000.
 118 // ListSets have a big constant.  Time *and space* are proportional to the
 119 //   number of elements.  They work well for a few elements of *any* size
 120 //   (i.e. sets of pointers)!
 121 
 122 //------------------------------Set--------------------------------------------
 123 class Set : public ResourceObj {
 124  public:
 125 
 126   // Creates a new, empty set.
 127   // DO NOT CONSTRUCT A Set.  THIS IS AN ABSTRACT CLASS, FOR INHERITENCE ONLY
 128   Set(Arena *arena) : _set_arena(arena) {};
 129 
 130   // Creates a new set from an existing set
 131   // DO NOT CONSTRUCT A Set.  THIS IS AN ABSTRACT CLASS, FOR INHERITENCE ONLY
 132   Set(const Set &) {};
 133 
 134   // Set assignment; deep-copy guts
 135   virtual Set &operator =(const Set &s)=0;
 136   virtual Set &clone(void) const=0;
 137 
 138   // Virtual destructor
 139   virtual ~Set() {};
 140 
 141   // Add member to set
 142   virtual Set &operator <<=(uint elem)=0;
 143   // virtual Set  operator << (uint elem);
 144 
 145   // Delete member from set
 146   virtual Set &operator >>=(uint elem)=0;
 147   // virtual Set  operator >> (uint elem);
 148 
 149   // Membership test.  Result is Zero (absent)/ Non-Zero (present)
 150   virtual int operator [](uint elem) const=0;
 151 
 152   // Intersect sets
 153   virtual Set &operator &=(const Set &s)=0;
 154   // virtual Set  operator & (const Set &s) const;
 155 
 156   // Union sets
 157   virtual Set &operator |=(const Set &s)=0;
 158   // virtual Set  operator | (const Set &s) const;
 159 
 160   // Difference sets
 161   virtual Set &operator -=(const Set &s)=0;
 162   // virtual Set  operator - (const Set &s) const;
 163 
 164   // Tests for equality.  Result is Zero (false)/ Non-Zero (true)
 165   virtual int operator ==(const Set &s) const=0;
 166   int operator !=(const Set &s) const { return !(*this == s); }
 167   virtual int disjoint(const Set &s) const=0;
 168 
 169   // Tests for strict subset.  Result is Zero (false)/ Non-Zero (true)
 170   virtual int operator < (const Set &s) const=0;
 171   int operator > (const Set &s) const { return s < *this; }
 172 
 173   // Tests for subset.  Result is Zero (false)/ Non-Zero (true)
 174   virtual int operator <=(const Set &s) const=0;
 175   int operator >=(const Set &s) const { return s <= *this; }
 176 
 177   // Return any member of the Set.  Undefined if the Set is empty.
 178   virtual uint getelem(void) const=0;
 179 
 180   // Clear all the elements in the Set
 181   virtual void Clear(void)=0;
 182 
 183   // Return the number of members in the Set
 184   virtual uint Size(void) const=0;
 185 
 186   // If an iterator follows a "Sort()" without any Set-modifying operations
 187   // inbetween then the iterator will visit the elements in ascending order.
 188   virtual void Sort(void)=0;
 189 
 190   // Convert a set to printable string in an allocated buffer.
 191   // The caller must deallocate the string.
 192   virtual char *setstr(void) const;
 193 
 194   // Print the Set on "stdout".  Can be conveniently called in the debugger
 195   void print() const;
 196 
 197   // Parse text from the string into the Set.  Return length parsed.
 198   virtual int parse(const char *s);
 199 
 200   // Convert a generic Set to a specific Set
 201   /* Removed for MCC BUG
 202      virtual operator const SparseSet* (void) const;
 203      virtual operator const VectorSet* (void) const;
 204      virtual operator const ListSet  * (void) const;
 205      virtual operator const CoSet    * (void) const; */
 206   virtual const SparseSet *asSparseSet(void) const;
 207   virtual const VectorSet *asVectorSet(void) const;
 208   virtual const ListSet   *asListSet  (void) const;
 209   virtual const CoSet     *asCoSet    (void) const;
 210 
 211   // Hash the set.  Sets of different types but identical elements will NOT
 212   // hash the same.  Same set type, same elements WILL hash the same.
 213   virtual int hash() const = 0;
 214 
 215 protected:
 216   friend class SetI;
 217   friend class CoSet;
 218   virtual class SetI_ *iterate(uint&) const=0;
 219 
 220   // Need storeage for the set
 221   Arena *_set_arena;
 222 };
 223 typedef Set&((*Set_Constructor)(Arena *arena));
 224 extern Set &ListSet_Construct(Arena *arena);
 225 extern Set &VectorSet_Construct(Arena *arena);
 226 extern Set &SparseSet_Construct(Arena *arena);
 227 
 228 //------------------------------Iteration--------------------------------------
 229 // Loop thru all elements of the set, setting "elem" to the element numbers
 230 // in random order.  Inserted or deleted elements during this operation may
 231 // or may not be iterated over; untouched elements will be affected once.
 232 
 233 // Usage:  for( SetI  i(s); i.test(); i++ ) { body = i.elem; }   ...OR...
 234 //         for( i.reset(s); i.test(); i++ ) { body = i.elem; }
 235 
 236 class SetI_ : public ResourceObj {
 237 protected:
 238   friend class SetI;
 239   virtual ~SetI_();
 240   virtual uint next(void)=0;
 241   virtual int test(void)=0;
 242 };
 243 
 244 class SetI {
 245 protected:
 246   SetI_ *impl;
 247 public:
 248   uint elem;                    // The publically accessible element
 249 
 250   SetI( const Set *s ) { impl = s->iterate(elem); }
 251   ~SetI() { delete impl; }
 252   void reset( const Set *s ) { delete impl; impl = s->iterate(elem); }
 253   void operator ++(void) { elem = impl->next(); }
 254   int test(void) { return impl->test(); }
 255 };
 256 
 257 #endif // _SET_
 258 
 259 #endif // SHARE_VM_LIBADT_SET_HPP