hotspot/src/share/vm/libadt/vectset.cpp
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, 2005, 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
// Vector Sets - An Abstract Data Type
489c9b5090e2 Initial load
duke
parents:
diff changeset
    26
489c9b5090e2 Initial load
duke
parents:
diff changeset
    27
#include "incls/_precompiled.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
#include "incls/_vectset.cpp.incl"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    29
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
// %%%%% includes not needed with AVM framework - Ungar
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
// #include "port.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
//IMPLEMENTATION
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// #include "vectset.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// BitsInByte is a lookup table which tells the number of bits that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// are in the looked-up number.  It is very useful in VectorSet_Size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
uint8 bitsInByte[256] = {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
  0, 1, 1, 2, 1, 2, 2, 3, 1, 2, 2, 3, 2, 3, 3, 4,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
  1, 2, 2, 3, 2, 3, 3, 4, 2, 3, 3, 4, 3, 4, 4, 5,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  2, 3, 3, 4, 3, 4, 4, 5, 3, 4, 4, 5, 4, 5, 5, 6,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  3, 4, 4, 5, 4, 5, 5, 6, 4, 5, 5, 6, 5, 6, 6, 7,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  4, 5, 5, 6, 5, 6, 6, 7, 5, 6, 6, 7, 6, 7, 7, 8
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
//------------------------------VectorSet--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
// Create a new, empty Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
VectorSet::VectorSet(Arena *arena) : Set(arena) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  size = 2;                     // Small initial size
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  data = (uint32 *)_set_arena->Amalloc(size*sizeof(uint32));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  data[0] = 0;                  // No elements
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
  data[1] = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
//------------------------------Construct--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
Set &VectorSet_Construct(Arena *arena)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  return *(new VectorSet(arena));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
//------------------------------operator=--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
Set &VectorSet::operator = (const Set &set)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  if( &set == this ) return *this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  FREE_FAST(data);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  slamin(*(set.asVectorSet()));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  return *this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
//------------------------------slamin-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
// Initialize one set with another.  No regard is made to the existing Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
void VectorSet::slamin(const VectorSet& s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
  size = s.size;                // Use new size
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  data = (uint32*)s._set_arena->Amalloc(size*sizeof(uint32)); // Make array of required size
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  memcpy( data, s.data, size*sizeof(uint32) ); // Fill the array
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
//------------------------------grow-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
// Expand the existing set to a bigger size
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
void VectorSet::grow( uint newsize )
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
  newsize = (newsize+31) >> 5;  // Convert to longwords
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
  uint x = size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
  while( x < newsize ) x <<= 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
  data = (uint32 *)_set_arena->Arealloc(data, size*sizeof(uint32), x*sizeof(uint32));
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  memset((char *)(data + size), 0, (x - size)*sizeof(uint32));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  size = x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
//------------------------------operator<<=------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
// Insert a member into an existing Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
Set &VectorSet::operator <<= (uint elem)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
  register uint word = elem >> 5;            // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
  register uint32 mask = 1L << (elem & 31);  // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  if( word >= size )            // Need to grow set?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
    grow(elem+1);               // Then grow it
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  data[word] |= mask;           // Set new bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  return *this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
//------------------------------operator>>=------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
// Delete a member from an existing Set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
Set &VectorSet::operator >>= (uint elem)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
  register uint word = elem >> 5; // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  if( word >= size )              // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
    return *this;                 // Then it's clear & return clear
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  register uint32 mask = 1L << (elem & 31);     // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  data[word] &= ~mask;            // Clear bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  return *this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
//------------------------------operator&=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
// Intersect one set into another.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
VectorSet &VectorSet::operator &= (const VectorSet &s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
  // NOTE: The intersection is never any larger than the smallest set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
  if( s.size < size ) size = s.size; // Get smaller size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
  register uint32 *u1 = data;   // Pointer to the destination data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
  register uint32 *u2 = s.data; // Pointer to the source data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
  for( uint i=0; i<size; i++)   // For data in set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    *u1++ &= *u2++;             // Copy and AND longwords
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
  return *this;                 // Return set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
//------------------------------operator&=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
Set &VectorSet::operator &= (const Set &set)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
  return (*this) &= *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
//------------------------------operator|=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
// Union one set into another.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
VectorSet &VectorSet::operator |= (const VectorSet &s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  // This many words must be unioned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
  register uint cnt = ((size<s.size)?size:s.size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
  register uint32 *u1 = data;   // Pointer to the destination data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
  register uint32 *u2 = s.data; // Pointer to the source data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
  for( uint i=0; i<cnt; i++)    // Copy and OR the two sets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    *u1++ |= *u2++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
  if( size < s.size ) {         // Is set 2 larger than set 1?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
    // Extend result by larger set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
    grow(s.size*sizeof(uint32)*8);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
    memcpy(&data[cnt], u2, (s.size - cnt)*sizeof(uint32));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
  return *this;                 // Return result set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
//------------------------------operator|=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
Set &VectorSet::operator |= (const Set &set)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
  return (*this) |= *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
//------------------------------operator-=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
// Difference one set from another.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
VectorSet &VectorSet::operator -= (const VectorSet &s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  // This many words must be unioned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  register uint cnt = ((size<s.size)?size:s.size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  register uint32 *u1 = data;   // Pointer to the destination data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
  register uint32 *u2 = s.data; // Pointer to the source data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  for( uint i=0; i<cnt; i++ )   // For data in set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    *u1++ &= ~(*u2++);          // A <-- A & ~B  with longwords
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  return *this;                 // Return new set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
//------------------------------operator-=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
Set &VectorSet::operator -= (const Set &set)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
  return (*this) -= *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
//------------------------------compare----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
// Compute 2 booleans: bits in A not B, bits in B not A.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
// Return X0 --  A is not a subset of B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
//        X1 --  A is a subset of B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
//        0X --  B is not a subset of A
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
//        1X --  B is a subset of A
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
int VectorSet::compare (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  register uint32 *u1 = data;   // Pointer to the destination data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
  register uint32 *u2 = s.data; // Pointer to the source data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
  register uint32 AnotB = 0, BnotA = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
  // This many words must be unioned
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  register uint cnt = ((size<s.size)?size:s.size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
  // Get bits for both sets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
  uint i;                       // Exit value of loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
  for( i=0; i<cnt; i++ ) {      // For data in BOTH sets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
    register uint32 A = *u1++;  // Data from one guy
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
    register uint32 B = *u2++;  // Data from other guy
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
    AnotB |= (A & ~B);          // Compute bits in A not B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
    BnotA |= (B & ~A);          // Compute bits in B not A
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  // Get bits from bigger set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  if( size < s.size ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
    for( ; i<s.size; i++ )      // For data in larger set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
      BnotA |= *u2++;           // These bits are in B not A
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
    for( ; i<size; i++ )        // For data in larger set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
      AnotB |= *u1++;           // These bits are in A not B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
  // Set & return boolean flags
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
  return ((!BnotA)<<1) + (!AnotB);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
//------------------------------operator==-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
// Test for set equality
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
int VectorSet::operator == (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
  return compare(s) == 3;       // TRUE if A and B are mutual subsets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
//------------------------------operator==-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
int VectorSet::operator == (const Set &set) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
  return (*this) == *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
//------------------------------disjoint---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
// Check for sets being disjoint.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
int VectorSet::disjoint(const Set &set) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  const VectorSet &s = *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  // NOTE: The intersection is never any larger than the smallest set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  register uint small = ((size<s.size)?size:s.size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
  register uint32 *u1 = data;   // Pointer to the destination data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
  register uint32 *u2 = s.data; // Pointer to the source data
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  for( uint i=0; i<small; i++)  // For data in set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    if( *u1++ & *u2++ )         // If any elements in common
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
      return 0;                 // Then not disjoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  return 1;                     // Else disjoint
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
//------------------------------operator<--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
// Test for strict subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
int VectorSet::operator < (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
  return compare(s) == 1;       // A subset B, B not subset A
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
//------------------------------operator<--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
int VectorSet::operator < (const Set &set) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
  return (*this) < *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
//------------------------------operator<=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
// Test for subset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
int VectorSet::operator <= (const VectorSet &s) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  return compare(s) & 1;        // A subset B
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
//------------------------------operator<=-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
int VectorSet::operator <= (const Set &set) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  // The cast is a virtual function that checks that "set" is a VectorSet.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  return (*this) <= *(set.asVectorSet());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
//------------------------------operator[]-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
// Test for membership.  A Zero/Non-Zero value is returned!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
int VectorSet::operator[](uint elem) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  register uint word = elem >> 5; // Get the longword offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  if( word >= size )              // Beyond the last?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    return 0;                     // Then it's clear
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
  register uint32 mask = 1L << (elem & 31);  // Get bit mask
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  return ((data[word] & mask))!=0;           // Return the sense of the bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
//------------------------------getelem----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
// Get any element from the set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
uint VectorSet::getelem(void) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
  uint i;                       // Exit value of loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  for( i=0; i<size; i++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
    if( data[i] )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  uint32 word = data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  int j;                        // Exit value of loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  for( j= -1; word; j++, word>>=1 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
  return (i<<5)+j;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   312
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
//------------------------------Clear------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
// Clear a set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
void VectorSet::Clear(void)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  if( size > 100 ) {            // Reclaim storage only if huge
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
    FREE_RESOURCE_ARRAY(uint32,data,size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
    size = 2;                   // Small initial size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
    data = NEW_RESOURCE_ARRAY(uint32,size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  memset( data, 0, size*sizeof(uint32) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
//------------------------------Size-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
// Return number of elements in a Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
uint VectorSet::Size(void) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
  uint sum = 0;                 // Cumulative size so far.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
  uint8 *currByte = (uint8*)data;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
  for( uint32 i = 0; i < (size<<2); i++) // While have bytes to process
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
    sum += bitsInByte[*currByte++];      // Add bits in current byte to size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
  return sum;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
//------------------------------Sort-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
// Sort the elements for the next forall statement
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
void VectorSet::Sort(void)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
//------------------------------hash-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
int VectorSet::hash() const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
  uint32 _xor = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  uint lim = ((size<4)?size:4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  for( uint i = 0; i < lim; i++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
    _xor ^= data[i];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  return (int)_xor;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
//------------------------------iterate----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
SetI_ *VectorSet::iterate(uint &elem) const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   354
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   355
  VSetI_ *foo = (new(ResourceObj::C_HEAP) VSetI_(this));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   356
  elem = foo->next();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   357
  return foo;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   358
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   359
489c9b5090e2 Initial load
duke
parents:
diff changeset
   360
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
   361
//------------------------------VSetI_-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
// Initialize the innards of a VectorSet iterator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
VSetI_::VSetI_( const VectorSet *vset ) : s(vset)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  i = (uint)-1L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  j = (uint)-1L;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  mask = (unsigned)(1L<<31);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   368
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
//------------------------------next-------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
// Find and return the next element of a vector set, or return garbage and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
// make "VSetI_::test()" fail.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
uint VSetI_::next(void)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
  j++;                          // Next element in word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
  mask = (mask & max_jint) << 1;// Next bit in word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  do {                          // Do While still have words
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
    while( mask ) {             // While have bits in word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
      if( s->data[i] & mask ) { // If found a bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
        return (i<<5)+j;        // Return the bit address
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
      j++;                      // Skip to next bit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
      mask = (mask & max_jint) << 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
    j = 0;                      // No more bits in word; setup for next word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
    mask = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
    for( i++; (i<s->size) && (!s->data[i]); i++ ); // Skip to non-zero word
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  } while( i<s->size );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
  return max_juint;             // No element, iterated them all
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
}