hotspot/src/share/vm/libadt/vectset.hpp
author trims
Thu, 27 May 2010 19:08:38 -0700
changeset 5547 f4b087cbb361
parent 1 489c9b5090e2
child 7397 5b173b4ca846
permissions -rw-r--r--
6941466: Oracle rebranding changes for Hotspot repositories Summary: Change all the Sun copyrights to Oracle copyright Reviewed-by: ohair
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
     2
 * Copyright (c) 1997, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 1
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
489c9b5090e2 Initial load
duke
parents:
diff changeset
    25
#ifndef _VECTOR_SET_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
#define _VECTOR_SET_
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
// Vector Sets - An Abstract Data Type
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
//INTERFACE
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// These sets can grow or shrink, based on the initial size and the largest
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// element currently in them.  Slow and bulky for sparse sets, these sets
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// are super for dense sets.  They are fast and compact when dense.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
// TIME:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// O(1) - Insert, Delete, Member, Sort
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
//                  Equal, ChooseMember, Forall
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// SPACE: (max_element)/(8*sizeof(int))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//------------------------------VectorSet--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
class VectorSet : public Set {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
friend class VectorSetI;        // Friendly iterator class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  uint size;                    // Size of data IN LONGWORDS (32bits)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  uint32 *data;                 // The data, bit packed
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  void slamin( const VectorSet& s );     // Initialize one set with another
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  int compare(const VectorSet &s) const; // Compare set contents
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  void grow(uint newsize);               // Grow vector to required bitsize
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  VectorSet(Arena *arena);                      // Creates a new, empty set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  Set &operator =(const Set &s);                // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  VectorSet &operator =(const VectorSet &s)     // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
  { if( &s != this ) { slamin(s); } return *this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  ~VectorSet() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  Set &clone(void) const { return *(new VectorSet(*this)); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  Set &operator <<=(uint elem);          // Add member to set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  VectorSet operator << (uint elem)      // Add member to new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  { VectorSet foo(*this); foo <<= elem; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  Set &operator >>=(uint elem);          // Delete member from set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  VectorSet operator >> (uint elem)      // Delete member from new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  { VectorSet foo(*this); foo >>= elem; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  VectorSet &operator &=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  Set       &operator &=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  VectorSet operator & (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  { VectorSet foo(*this); foo &= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  VectorSet &operator |=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  Set       &operator |=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  VectorSet operator | (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  { VectorSet foo(*this); foo |= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  Set       &operator -=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
  VectorSet operator - (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  { VectorSet foo(*this); foo -= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  int operator ==(const VectorSet &s) const;  // True if sets are equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  int operator ==(const Set       &s) const;  // True if sets are equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  int operator < (const VectorSet &s) const;  // True if strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  int operator < (const Set       &s) const;  // True if strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  int operator <=(const VectorSet &s) const;  // True if subset relation holds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  int operator <=(const Set       &s) const;  // True if subset relation holds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  int disjoint   (const Set       &s) const;  // True if sets are disjoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  int operator [](uint elem) const; // Test for membership
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  uint getelem(void) const;         // Return a random element
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  void Clear(void);                 // Clear a set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  uint Size(void) const;            // Number of elements in the Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  void Sort(void);                  // Sort before iterating
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  int hash() const;                 // Hash function
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  /* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
     operator const VectorSet* (void) const { return this; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  const VectorSet *asVectorSet() const { return this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  // Expose internals for speed-critical fast iterators
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  uint word_size() const { return size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  uint32 *EXPOSE() const { return data; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  // Fast inlined "test and set".  Replaces the idiom:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  //     if( visited[idx] ) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  //     visited <<= idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  // With:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  //     if( visited.test_set(idx) ) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  int test_set( uint elem ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
    uint word = elem >> 5;           // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    if( word >= size )               // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
      return test_set_grow(elem);    // Then grow; set; return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
    uint32 mask = 1L << (elem & 31); // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    uint32 datum = data[word] & mask;// Get bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    data[word] |= mask;              // Set bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
    return datum;                    // Return bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  int test_set_grow( uint elem ) {    // Insert & return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    (*this) <<= elem;                 // Insert into set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
    return 0;                         // Return 0!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  // Fast inlined test
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  int test( uint elem ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    uint word = elem >> 5;      // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    if( word >= size ) return 0; // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    uint32 mask = 1L << (elem & 31); // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    return data[word] & mask;   // Get bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  // Fast inlined set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  void set( uint elem ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    uint word = elem >> 5;      // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
    if( word >= size ) {        // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      test_set_grow(elem);      // Then grow and set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      uint32 mask = 1L << (elem & 31); // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      data[word] |= mask;       // Set bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
  friend class VSetI_;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
  SetI_ *iterate(uint&) const;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
//------------------------------Iteration--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
// Loop thru all elements of the set, setting "elem" to the element numbers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
// in random order.  Inserted or deleted elements during this operation may
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
// or may not be iterated over; untouched elements will be affected once.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
// Usage:  for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
class VSetI_ : public SetI_ {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
  friend class VectorSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
  friend class VectorSetI;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
  const VectorSet *s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  uint i, j;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  uint32 mask;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
  VSetI_(const VectorSet *vset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
  uint next(void);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  int test(void) { return i < s->size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
class VectorSetI : public SetI {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
  VectorSetI( const VectorSet *s ) : SetI(s) { }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
  void operator ++(void) { elem = ((VSetI_*)impl)->next(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
  int test(void) { return ((VSetI_*)impl)->test(); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
#endif // _VECTOR_SET_