vectset.hpp revision 1879:f95d63e2154a
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_VECTSET_HPP
26#define SHARE_VM_LIBADT_VECTSET_HPP
27
28#include "libadt/set.hpp"
29
30// Vector Sets - An Abstract Data Type
31//INTERFACE
32
33// These sets can grow or shrink, based on the initial size and the largest
34// element currently in them.  Slow and bulky for sparse sets, these sets
35// are super for dense sets.  They are fast and compact when dense.
36
37// TIME:
38// O(1) - Insert, Delete, Member, Sort
39// O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,
40//                  Equal, ChooseMember, Forall
41
42// SPACE: (max_element)/(8*sizeof(int))
43
44
45//------------------------------VectorSet--------------------------------------
46class VectorSet : public Set {
47friend class VectorSetI;        // Friendly iterator class
48protected:
49  uint size;                    // Size of data IN LONGWORDS (32bits)
50  uint32 *data;                 // The data, bit packed
51
52  void slamin( const VectorSet& s );     // Initialize one set with another
53  int compare(const VectorSet &s) const; // Compare set contents
54  void grow(uint newsize);               // Grow vector to required bitsize
55
56public:
57  VectorSet(Arena *arena);                      // Creates a new, empty set.
58  VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts
59  Set &operator =(const Set &s);                // Set clone; deep-copy guts
60  VectorSet &operator =(const VectorSet &s)     // Set clone; deep-copy guts
61  { if( &s != this ) { slamin(s); } return *this; }
62  ~VectorSet() {}
63  Set &clone(void) const { return *(new VectorSet(*this)); }
64
65  Set &operator <<=(uint elem);          // Add member to set
66  VectorSet operator << (uint elem)      // Add member to new set
67  { VectorSet foo(*this); foo <<= elem; return foo; }
68  Set &operator >>=(uint elem);          // Delete member from set
69  VectorSet operator >> (uint elem)      // Delete member from new set
70  { VectorSet foo(*this); foo >>= elem; return foo; }
71
72  VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set
73  Set       &operator &=(const Set       &s); // Intersect sets into first set
74  VectorSet operator & (const VectorSet &s) const
75  { VectorSet foo(*this); foo &= s; return foo; }
76
77  VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set
78  Set       &operator |=(const Set       &s); // Intersect sets into first set
79  VectorSet operator | (const VectorSet &s) const
80  { VectorSet foo(*this); foo |= s; return foo; }
81
82  VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set
83  Set       &operator -=(const Set       &s); // Intersect sets into first set
84  VectorSet operator - (const VectorSet &s) const
85  { VectorSet foo(*this); foo -= s; return foo; }
86
87  int operator ==(const VectorSet &s) const;  // True if sets are equal
88  int operator ==(const Set       &s) const;  // True if sets are equal
89  int operator < (const VectorSet &s) const;  // True if strict subset
90  int operator < (const Set       &s) const;  // True if strict subset
91  int operator <=(const VectorSet &s) const;  // True if subset relation holds.
92  int operator <=(const Set       &s) const;  // True if subset relation holds.
93  int disjoint   (const Set       &s) const;  // True if sets are disjoint
94
95  int operator [](uint elem) const; // Test for membership
96  uint getelem(void) const;         // Return a random element
97  void Clear(void);                 // Clear a set
98  uint Size(void) const;            // Number of elements in the Set.
99  void Sort(void);                  // Sort before iterating
100  int hash() const;                 // Hash function
101
102  /* Removed for MCC BUG
103     operator const VectorSet* (void) const { return this; } */
104  const VectorSet *asVectorSet() const { return this; }
105
106  // Expose internals for speed-critical fast iterators
107  uint word_size() const { return size; }
108  uint32 *EXPOSE() const { return data; }
109
110  // Fast inlined "test and set".  Replaces the idiom:
111  //     if( visited[idx] ) return;
112  //     visited <<= idx;
113  // With:
114  //     if( visited.test_set(idx) ) return;
115  //
116  int test_set( uint elem ) {
117    uint word = elem >> 5;           // Get the longword offset
118    if( word >= size )               // Beyond the last?
119      return test_set_grow(elem);    // Then grow; set; return 0;
120    uint32 mask = 1L << (elem & 31); // Get bit mask
121    uint32 datum = data[word] & mask;// Get bit
122    data[word] |= mask;              // Set bit
123    return datum;                    // Return bit
124  }
125  int test_set_grow( uint elem ) {    // Insert & return 0;
126    (*this) <<= elem;                 // Insert into set
127    return 0;                         // Return 0!
128  }
129
130  // Fast inlined test
131  int test( uint elem ) const {
132    uint word = elem >> 5;      // Get the longword offset
133    if( word >= size ) return 0; // Beyond the last?
134    uint32 mask = 1L << (elem & 31); // Get bit mask
135    return data[word] & mask;   // Get bit
136  }
137
138  // Fast inlined set
139  void set( uint elem ) {
140    uint word = elem >> 5;      // Get the longword offset
141    if( word >= size ) {        // Beyond the last?
142      test_set_grow(elem);      // Then grow and set
143    } else {
144      uint32 mask = 1L << (elem & 31); // Get bit mask
145      data[word] |= mask;       // Set bit
146    }
147  }
148
149
150private:
151  friend class VSetI_;
152  SetI_ *iterate(uint&) const;
153};
154
155//------------------------------Iteration--------------------------------------
156// Loop thru all elements of the set, setting "elem" to the element numbers
157// in random order.  Inserted or deleted elements during this operation may
158// or may not be iterated over; untouched elements will be affected once.
159// Usage:  for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }
160
161class VSetI_ : public SetI_ {
162  friend class VectorSet;
163  friend class VectorSetI;
164  const VectorSet *s;
165  uint i, j;
166  uint32 mask;
167  VSetI_(const VectorSet *vset);
168  uint next(void);
169  int test(void) { return i < s->size; }
170};
171
172class VectorSetI : public SetI {
173public:
174  VectorSetI( const VectorSet *s ) : SetI(s) { }
175  void operator ++(void) { elem = ((VSetI_*)impl)->next(); }
176  int test(void) { return ((VSetI_*)impl)->test(); }
177};
178
179#endif // SHARE_VM_LIBADT_VECTSET_HPP
180