src/hotspot/share/libadt/set.cpp
author mikael
Thu, 12 Jul 2018 17:29:48 -0700
changeset 51050 96ea37459ca7
parent 47216 71c04702a3d5
child 53546 63eb7e38ce84
permissions -rw-r--r--
8207011: Remove uses of the register storage class specifier Reviewed-by: kbarrett, kvn
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 13963
diff changeset
     2
 * Copyright (c) 1997, 2014, 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
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#include "libadt/set.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "memory/allocation.inline.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    28
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
// Sets - An Abstract Data Type
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
489c9b5090e2 Initial load
duke
parents:
diff changeset
    31
#include <stdio.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
#include <assert.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
#include <string.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
#include <stdlib.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//-------------------------Virtual Functions-----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// These functions MUST be implemented by the inheriting class.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
class SparseSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
/* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
   Set::operator const SparseSet*() const { assert(0); return NULL; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
const SparseSet *Set::asSparseSet() const { assert(0); return NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
class VectorSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
/* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
   Set::operator const VectorSet*() const { assert(0); return NULL; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
const VectorSet *Set::asVectorSet() const { assert(0); return NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
class ListSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
/* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
   Set::operator const ListSet*() const { assert(0); return NULL; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
const ListSet *Set::asListSet() const { assert(0); return NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
class CoSet;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
/* Removed for MCC BUG
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
   Set::operator const CoSet*() const { assert(0); return NULL; } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
const CoSet *Set::asCoSet() const { assert(0); return NULL; }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
//------------------------------setstr-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
// Create a string with a printable representation of a set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
// The caller must deallocate the string.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
char *Set::setstr() const
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
{
30239
dc83236ebb28 8077364: "if( !this )" construct prevents build on Xcode 6.3
sla
parents: 24429
diff changeset
    60
  if( this == NULL ) return os::strdup("{no set}");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  Set &set = clone();           // Virtually copy the basic set.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  set.Sort();                   // Sort elements for in-order retrieval
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  uint len = 128;               // Total string space
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    65
  char *buf = NEW_C_HEAP_ARRAY(char,len, mtCompiler);// Some initial string space
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
51050
96ea37459ca7 8207011: Remove uses of the register storage class specifier
mikael
parents: 47216
diff changeset
    67
  char *s = buf;                // Current working string pointer
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  *s++ = '{';
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  *s = '\0';
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  // For all elements of the Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
  uint hi = (uint)-2, lo = (uint)-2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  for( SetI i(&set); i.test(); ++i ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
    if( hi+1 == i.elem ) {        // Moving sequentially thru range?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
      hi = i.elem;                // Yes, just update hi end of range
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    } else {                      // Else range ended
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      if( buf+len-s < 25 ) {      // Generous trailing space for upcoming numbers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
        int offset = (int)(s-buf);// Not enuf space; compute offset into buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
        len <<= 1;                // Double string size
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    80
        buf = REALLOC_C_HEAP_ARRAY(char,buf,len, mtCompiler); // Reallocate doubled size
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
        s = buf+offset;         // Get working pointer into new bigger buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
      if( lo != (uint)-2 ) {    // Startup?  No!  Then print previous range.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
        if( lo != hi ) sprintf(s,"%d-%d,",lo,hi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
        else sprintf(s,"%d,",lo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
        s += strlen(s);         // Advance working string
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
      hi = lo = i.elem;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
  if( lo != (uint)-2 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
    if( buf+len-s < 25 ) {      // Generous trailing space for upcoming numbers
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
      int offset = (int)(s-buf);// Not enuf space; compute offset into buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
      len <<= 1;                // Double string size
13195
be27e1b6a4b9 6995781: Native Memory Tracking (Phase 1)
zgu
parents: 7397
diff changeset
    95
      buf = (char*)ReallocateHeap(buf,len, mtCompiler); // Reallocate doubled size
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      s = buf+offset;           // Get working pointer into new bigger buffer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    if( lo != hi ) sprintf(s,"%d-%d}",lo,hi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
    else sprintf(s,"%d}",lo);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
  } else strcat(s,"}");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  // Don't delete the clone 'set' since it is allocated on Arena.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  return buf;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
//------------------------------print------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
// Handier print routine
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
void Set::print() const
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
  char *printable_set = setstr();
24424
2658d7834c6e 8037816: Fix for 8036122 breaks build with Xcode5/clang
drchase
parents: 13963
diff changeset
   110
  tty->print_cr("%s", printable_set);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  FreeHeap(printable_set);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
//------------------------------parse------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
// Convert a textual representation of a Set, to a Set and union into "this"
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
// Set.  Return the amount of text parsed in "len", or zero in "len".
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
int Set::parse(const char *s)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
{
51050
96ea37459ca7 8207011: Remove uses of the register storage class specifier
mikael
parents: 47216
diff changeset
   119
  char c;                       // Parse character
96ea37459ca7 8207011: Remove uses of the register storage class specifier
mikael
parents: 47216
diff changeset
   120
  const char *t = s;            // Save the starting position of s.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
  do c = *s++;                  // Skip characters
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
  while( c && (c <= ' ') );     // Till no more whitespace or EOS
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
  if( c != '{' ) return 0;      // Oops, not a Set openner
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
  if( *s == '}' ) return 2;     // The empty Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  // Sets are filled with values of the form "xx," or "xx-yy," with the comma
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
  // a "}" at the very end.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  while( 1 ) {                  // While have elements in the Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
    char *u;                    // Pointer to character ending parse
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
    uint hi, i;                 // Needed for range handling below
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
    uint elem = (uint)strtoul(s,&u,10);// Get element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
    if( u == s ) return 0;      // Bogus crude
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
    s = u;                      // Skip over the number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
    c = *s++;                   // Get the number seperator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
    switch ( c ) {              // Different seperators
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
    case '}':                   // Last simple element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
    case ',':                   // Simple element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
      (*this) <<= elem;         // Insert the simple element into the Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
      break;                    // Go get next element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
    case '-':                   // Range
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
      hi = (uint)strtoul(s,&u,10); // Get element
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
      if( u == s ) return 0;    // Bogus crude
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
      for( i=elem; i<=hi; i++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
        (*this) <<= i;          // Insert the entire range into the Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
      s = u;                    // Skip over the number
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
      c = *s++;                 // Get the number seperator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    if( c == '}' ) break;       // End of the Set
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    if( c != ',' ) return 0;    // Bogus garbage
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
  return (int)(s-t);            // Return length parsed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
//------------------------------Iterator---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
SetI_::~SetI_()
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
{
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
}