hotspot/src/share/vm/libadt/vectset.hpp
author ascarpino
Wed, 19 Oct 2016 10:49:52 -0700
changeset 41589 e638cce63968
parent 38022 342a29d198d8
permissions -rw-r--r--
8167591: Add MD5 to signed JAR restrictions Reviewed-by: mullan, jnimeh
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
     2
 * Copyright (c) 1997, 2011, 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
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_LIBADT_VECTSET_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_LIBADT_VECTSET_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "libadt/set.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
38022
342a29d198d8 8149488: Incorrect declaration of bitsInByte in regmask.cpp.
rraghavan
parents: 24425
diff changeset
    30
#define BITS_IN_BYTE_ARRAY_SIZE 256
342a29d198d8 8149488: Incorrect declaration of bitsInByte in regmask.cpp.
rraghavan
parents: 24425
diff changeset
    31
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
// Vector Sets - An Abstract Data Type
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
//INTERFACE
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// These sets can grow or shrink, based on the initial size and the largest
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// element currently in them.  Slow and bulky for sparse sets, these sets
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// are super for dense sets.  They are fast and compact when dense.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// TIME:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// O(1) - Insert, Delete, Member, Sort
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// O(max_element) - Create, Clear, Size, Copy, Union, Intersect, Difference,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
//                  Equal, ChooseMember, Forall
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// SPACE: (max_element)/(8*sizeof(int))
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
//------------------------------VectorSet--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
class VectorSet : public Set {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
friend class VectorSetI;        // Friendly iterator class
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
protected:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  uint size;                    // Size of data IN LONGWORDS (32bits)
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
    52
  uint32_t* data;               // The data, bit packed
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  void slamin( const VectorSet& s );     // Initialize one set with another
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
  int compare(const VectorSet &s) const; // Compare set contents
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  void grow(uint newsize);               // Grow vector to required bitsize
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  VectorSet(Arena *arena);                      // Creates a new, empty set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  VectorSet(const VectorSet &s) : Set(s._set_arena) {slamin(s);} // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  Set &operator =(const Set &s);                // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  VectorSet &operator =(const VectorSet &s)     // Set clone; deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  { if( &s != this ) { slamin(s); } return *this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  ~VectorSet() {}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  Set &clone(void) const { return *(new VectorSet(*this)); }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  Set &operator <<=(uint elem);          // Add member to set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  VectorSet operator << (uint elem)      // Add member to new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  { VectorSet foo(*this); foo <<= elem; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  Set &operator >>=(uint elem);          // Delete member from set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  VectorSet operator >> (uint elem)      // Delete member from new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  { VectorSet foo(*this); foo >>= elem; 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
  VectorSet &operator -=(const VectorSet &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  Set       &operator -=(const Set       &s); // Intersect sets into first set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  VectorSet operator - (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  { VectorSet foo(*this); foo -= s; return foo; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
  int operator ==(const VectorSet &s) const;  // True if sets are equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  int operator ==(const Set       &s) const;  // True if sets are equal
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  int operator < (const VectorSet &s) const;  // True if strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  int operator < (const Set       &s) const;  // True if strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  int operator <=(const VectorSet &s) const;  // True if subset relation holds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
  int operator <=(const Set       &s) const;  // True if subset relation holds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  int disjoint   (const Set       &s) const;  // True if sets are disjoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  int operator [](uint elem) const; // Test for membership
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  uint getelem(void) const;         // Return a random element
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  void Clear(void);                 // Clear a set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  uint Size(void) const;            // Number of elements in the Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  void Sort(void);                  // Sort before iterating
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  int hash() const;                 // Hash function
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   103
  void Reset(void) {                // Reset a set
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   104
    memset( data, 0, size*sizeof(uint32_t) );
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   105
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  /* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
     operator const VectorSet* (void) const { return this; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  const VectorSet *asVectorSet() const { return this; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  // Expose internals for speed-critical fast iterators
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  uint word_size() const { return size; }
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   113
  uint32_t* EXPOSE() const { return data; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  // Fast inlined "test and set".  Replaces the idiom:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  //     if( visited[idx] ) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  //     visited <<= idx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  // With:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
  //     if( visited.test_set(idx) ) return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  int test_set( uint elem ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    uint word = elem >> 5;           // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
    if( word >= size )               // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
      return test_set_grow(elem);    // Then grow; set; return 0;
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   125
    uint32_t mask = 1L << (elem & 31); // Get bit mask
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   126
    uint32_t datum = data[word] & mask;// Get bit
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    data[word] |= mask;              // Set bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
    return datum;                    // Return bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
  int test_set_grow( uint elem ) {    // Insert & return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    (*this) <<= elem;                 // Insert into set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    return 0;                         // Return 0!
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 test
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  int test( uint elem ) const {
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 ) return 0; // Beyond the last?
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   139
    uint32_t mask = 1L << (elem & 31); // Get bit mask
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    return data[word] & mask;   // Get bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  // Fast inlined set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  void set( uint elem ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    uint word = elem >> 5;      // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    if( word >= size ) {        // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      test_set_grow(elem);      // Then grow and set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    } else {
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   149
      uint32_t mask = 1L << (elem & 31); // Get bit mask
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
      data[word] |= mask;       // Set bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
private:
10975
446510be531a 7105611: Set::print() is broken
kvn
parents: 8319
diff changeset
   156
  SetI_ *iterate(uint&) const;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
//------------------------------Iteration--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
// Loop thru all elements of the set, setting "elem" to the element numbers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
// in random order.  Inserted or deleted elements during this operation may
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
// or may not be iterated over; untouched elements will be affected once.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
// Usage:  for( VectorSetI i(s); i.test(); i++ ) { body = i.elem; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   165
class VectorSetI : public StackObj {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
  friend class VectorSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
  const VectorSet *s;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
  uint i, j;
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 10975
diff changeset
   169
  uint32_t mask;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  uint next(void);
8319
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   171
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   172
public:
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   173
  uint elem;                    // The publically accessible element
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   174
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   175
  VectorSetI( const VectorSet *vset ) :
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   176
    s(vset),
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   177
    i((uint)-1L),
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   178
    j((uint)-1L),
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   179
    mask((unsigned)(1L<<31)) {
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   180
    elem = next();
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   181
  }
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   182
aedb3bd871bc 7013538: Java memory leak with escape analysis
kvn
parents: 7397
diff changeset
   183
  void operator ++(void) { elem = next(); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  int test(void) { return i < s->size; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   187
#endif // SHARE_VM_LIBADT_VECTSET_HPP