hotspot/src/share/vm/libadt/dict.cpp
author jlaskey
Tue, 23 Jul 2013 12:00:29 -0300
changeset 19089 51cfdcf21d35
parent 7397 5b173b4ca846
child 24425 53764d2358f9
child 24424 2658d7834c6e
permissions -rw-r--r--
8021130: Comments need to be tokens Reviewed-by: lagergren, attila Contributed-by: james.laskey@oracle.com
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
     2
 * Copyright (c) 1997, 2010, 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: 2154
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2154
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: 2154
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/dict.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    28
#include "memory/resourceArea.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
#include "runtime/thread.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    30
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    31
// Dictionaries - An Abstract Data Type
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    32
489c9b5090e2 Initial load
duke
parents:
diff changeset
    33
// %%%%% includes not needed with AVM framework - Ungar
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
// #include "port.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
//IMPLEMENTATION
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// #include "dict.hpp"
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
#include <assert.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// The iostream is not needed and it gets confused for gcc by the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    42
// define of bool.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
// #include <iostream.h>
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
//------------------------------data-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
// String hash tables
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
#define MAXID 20
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
static byte initflag = 0;       // True after 1st initialization
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
static const char shft[MAXID] = {1,2,3,4,5,6,7,1,2,3,4,5,6,7,1,2,3,4,5,6};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
static short xsum[MAXID];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
//------------------------------bucket---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
class bucket : public ResourceObj {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  uint _cnt, _max;              // Size of bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  void **_keyvals;              // Array of keys and values
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//------------------------------Dict-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
// The dictionary is kept has a hash table.  The hash table is a even power
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
// of two, for nice modulo operations.  Each bucket in the hash table points
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
// to a linear list of key-value pairs; each key & value is just a (void *).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// The list starts with a count.  A hash lookup finds the list head, then a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
// simple linear scan finds the key.  If the table gets too full, it's
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
// doubled in size; the total amount of EXTRA times all hash functions are
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
// computed for the doubling is no more than the current size - thus the
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
// doubling in size costs no more than a constant factor in speed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
Dict::Dict(CmpKey initcmp, Hash inithash) : _hash(inithash), _cmp(initcmp),
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  _arena(Thread::current()->resource_area()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    71
  int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Precompute table of null character hashes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  if( !initflag ) {             // Not initializated yet?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
    xsum[0] = (1<<shft[0])+1;   // Initialize
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
    for(i=1; i<MAXID; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
      xsum[i] = (1<<shft[i])+1+xsum[i-1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
    initflag = 1;               // Never again
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  _size = 16;                   // Size is a power of 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  _cnt = 0;                     // Dictionary is empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    85
  memset(_bin,0,sizeof(bucket)*_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
Dict::Dict(CmpKey initcmp, Hash inithash, Arena *arena, int size)
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
: _hash(inithash), _cmp(initcmp), _arena(arena) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
  int i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
  // Precompute table of null character hashes
489c9b5090e2 Initial load
duke
parents:
diff changeset
    93
  if( !initflag ) {             // Not initializated yet?
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
    xsum[0] = (1<<shft[0])+1;   // Initialize
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
    for(i=1; i<MAXID; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    96
      xsum[i] = (1<<shft[i])+1+xsum[i-1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
    initflag = 1;               // Never again
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   100
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
  i=16;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   102
  while( i < size ) i <<= 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
  _size = i;                    // Size is a power of 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
  _cnt = 0;                     // Dictionary is empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
  _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
  memset(_bin,0,sizeof(bucket)*_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
//------------------------------~Dict------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
// Delete an existing dictionary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
Dict::~Dict() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  /*
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
  tty->print("~Dict %d/%d: ",_cnt,_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  for( uint i=0; i < _size; i++) // For complete new table do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
    tty->print("%d ",_bin[i]._cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  tty->print("\n");*/
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  /*for( uint i=0; i<_size; i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
    FREE_FAST( _bin[i]._keyvals );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
    } */
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   121
489c9b5090e2 Initial load
duke
parents:
diff changeset
   122
//------------------------------Clear----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   123
// Zap to empty; ready for re-use
489c9b5090e2 Initial load
duke
parents:
diff changeset
   124
void Dict::Clear() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   125
  _cnt = 0;                     // Empty contents
489c9b5090e2 Initial load
duke
parents:
diff changeset
   126
  for( uint i=0; i<_size; i++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   127
    _bin[i]._cnt = 0;           // Empty buckets, but leave allocated
489c9b5090e2 Initial load
duke
parents:
diff changeset
   128
  // Leave _size & _bin alone, under the assumption that dictionary will
489c9b5090e2 Initial load
duke
parents:
diff changeset
   129
  // grow to this size again.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   130
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   131
489c9b5090e2 Initial load
duke
parents:
diff changeset
   132
//------------------------------doubhash---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   133
// Double hash table size.  If can't do so, just suffer.  If can, then run
489c9b5090e2 Initial load
duke
parents:
diff changeset
   134
// thru old hash table, moving things to new table.  Note that since hash
489c9b5090e2 Initial load
duke
parents:
diff changeset
   135
// table doubled, exactly 1 new bit is exposed in the mask - so everything
489c9b5090e2 Initial load
duke
parents:
diff changeset
   136
// in the old table ends up on 1 of two lists in the new table; a hi and a
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
// lo list depending on the value of the bit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
void Dict::doubhash(void) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
  uint oldsize = _size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   140
  _size <<= 1;                  // Double in size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   141
  _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*oldsize, sizeof(bucket)*_size );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   142
  memset( &_bin[oldsize], 0, oldsize*sizeof(bucket) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   143
  // Rehash things to spread into new table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   144
  for( uint i=0; i < oldsize; i++) { // For complete OLD table do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   145
    bucket *b = &_bin[i];       // Handy shortcut for _bin[i]
489c9b5090e2 Initial load
duke
parents:
diff changeset
   146
    if( !b->_keyvals ) continue;        // Skip empties fast
489c9b5090e2 Initial load
duke
parents:
diff changeset
   147
489c9b5090e2 Initial load
duke
parents:
diff changeset
   148
    bucket *nb = &_bin[i+oldsize];  // New bucket shortcut
489c9b5090e2 Initial load
duke
parents:
diff changeset
   149
    uint j = b->_max;               // Trim new bucket to nearest power of 2
489c9b5090e2 Initial load
duke
parents:
diff changeset
   150
    while( j > b->_cnt ) j >>= 1;   // above old bucket _cnt
489c9b5090e2 Initial load
duke
parents:
diff changeset
   151
    if( !j ) j = 1;             // Handle zero-sized buckets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   152
    nb->_max = j<<1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   153
    // Allocate worst case space for key-value pairs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   154
    nb->_keyvals = (void**)_arena->Amalloc_4( sizeof(void *)*nb->_max*2 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   155
    uint nbcnt = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   156
489c9b5090e2 Initial load
duke
parents:
diff changeset
   157
    for( j=0; j<b->_cnt; j++ ) {  // Rehash all keys in this bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   158
      void *key = b->_keyvals[j+j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   159
      if( (_hash( key ) & (_size-1)) != i ) { // Moving to hi bucket?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   160
        nb->_keyvals[nbcnt+nbcnt] = key;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   161
        nb->_keyvals[nbcnt+nbcnt+1] = b->_keyvals[j+j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   162
        nb->_cnt = nbcnt = nbcnt+1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   163
        b->_cnt--;              // Remove key/value from lo bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   164
        b->_keyvals[j+j  ] = b->_keyvals[b->_cnt+b->_cnt  ];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   165
        b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   166
        j--;                    // Hash compacted element also
489c9b5090e2 Initial load
duke
parents:
diff changeset
   167
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   168
    } // End of for all key-value pairs in bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   169
  } // End of for all buckets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   170
489c9b5090e2 Initial load
duke
parents:
diff changeset
   171
489c9b5090e2 Initial load
duke
parents:
diff changeset
   172
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   173
489c9b5090e2 Initial load
duke
parents:
diff changeset
   174
//------------------------------Dict-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   175
// Deep copy a dictionary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
Dict::Dict( const Dict &d ) : _size(d._size), _cnt(d._cnt), _hash(d._hash),_cmp(d._cmp), _arena(d._arena) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  _bin = (bucket*)_arena->Amalloc_4(sizeof(bucket)*_size);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   178
  memcpy( _bin, d._bin, sizeof(bucket)*_size );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  for( uint i=0; i<_size; i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    if( !_bin[i]._keyvals ) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
    _bin[i]._keyvals=(void**)_arena->Amalloc_4( sizeof(void *)*_bin[i]._max*2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
    memcpy( _bin[i]._keyvals, d._bin[i]._keyvals,_bin[i]._cnt*2*sizeof(void*));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
//------------------------------Dict-----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
// Deep copy a dictionary.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
Dict &Dict::operator =( const Dict &d ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
  if( _size < d._size ) {       // If must have more buckets
489c9b5090e2 Initial load
duke
parents:
diff changeset
   190
    _arena = d._arena;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   191
    _bin = (bucket*)_arena->Arealloc( _bin, sizeof(bucket)*_size, sizeof(bucket)*d._size );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
    memset( &_bin[_size], 0, (d._size-_size)*sizeof(bucket) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    _size = d._size;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  uint i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  for( i=0; i<_size; i++ ) // All buckets are empty
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    _bin[i]._cnt = 0;           // But leave bucket allocations alone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
  _cnt = d._cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
  *(Hash*)(&_hash) = d._hash;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   200
  *(CmpKey*)(&_cmp) = d._cmp;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
  for( i=0; i<_size; i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    bucket *b = &d._bin[i];     // Shortcut to source bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    for( uint j=0; j<b->_cnt; j++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   204
      Insert( b->_keyvals[j+j], b->_keyvals[j+j+1] );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   205
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   206
  return *this;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   207
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   208
489c9b5090e2 Initial load
duke
parents:
diff changeset
   209
//------------------------------Insert----------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   210
// Insert or replace a key/value pair in the given dictionary.  If the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   211
// dictionary is too full, it's size is doubled.  The prior value being
489c9b5090e2 Initial load
duke
parents:
diff changeset
   212
// replaced is returned (NULL if this is a 1st insertion of that key).  If
489c9b5090e2 Initial load
duke
parents:
diff changeset
   213
// an old value is found, it's swapped with the prior key-value pair on the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   214
// list.  This moves a commonly searched-for value towards the list head.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   215
void *Dict::Insert(void *key, void *val, bool replace) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   216
  uint hash = _hash( key );     // Get hash key
489c9b5090e2 Initial load
duke
parents:
diff changeset
   217
  uint i = hash & (_size-1);    // Get hash key, corrected for size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   218
  bucket *b = &_bin[i];         // Handy shortcut
489c9b5090e2 Initial load
duke
parents:
diff changeset
   219
  for( uint j=0; j<b->_cnt; j++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
    if( !_cmp(key,b->_keyvals[j+j]) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   221
      if (!replace) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   222
        return b->_keyvals[j+j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   223
      } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
        void *prior = b->_keyvals[j+j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
        b->_keyvals[j+j  ] = key;       // Insert current key-value
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
        b->_keyvals[j+j+1] = val;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   227
        return prior;           // Return prior
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
  if( ++_cnt > _size ) {        // Hash table is full
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
    doubhash();                 // Grow whole table if too full
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
    i = hash & (_size-1);       // Rehash
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    b = &_bin[i];               // Handy shortcut
489c9b5090e2 Initial load
duke
parents:
diff changeset
   235
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   236
  if( b->_cnt == b->_max ) {    // Must grow bucket?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
    if( !b->_keyvals ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
      b->_max = 2;              // Initial bucket size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
      b->_keyvals = (void**)_arena->Amalloc_4(sizeof(void*) * b->_max * 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
      b->_keyvals = (void**)_arena->Arealloc(b->_keyvals, sizeof(void*) * b->_max * 2, sizeof(void*) * b->_max * 4);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
      b->_max <<= 1;            // Double bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
  b->_keyvals[b->_cnt+b->_cnt  ] = key;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
  b->_keyvals[b->_cnt+b->_cnt+1] = val;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
  b->_cnt++;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   248
  return NULL;                  // Nothing found prior
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
//------------------------------Delete---------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
// Find & remove a value from dictionary. Return old value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
void *Dict::Delete(void *key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
  uint i = _hash( key ) & (_size-1);    // Get hash key, corrected for size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
  bucket *b = &_bin[i];         // Handy shortcut
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  for( uint j=0; j<b->_cnt; j++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
    if( !_cmp(key,b->_keyvals[j+j]) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
      void *prior = b->_keyvals[j+j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
      b->_cnt--;                // Remove key/value from lo bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
      b->_keyvals[j+j  ] = b->_keyvals[b->_cnt+b->_cnt  ];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
      b->_keyvals[j+j+1] = b->_keyvals[b->_cnt+b->_cnt+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
      _cnt--;                   // One less thing in table
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
      return prior;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
//------------------------------FindDict-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
// Find a key-value pair in the given dictionary.  If not found, return NULL.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
// If found, move key-value pair towards head of list.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
void *Dict::operator [](const void *key) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
  uint i = _hash( key ) & (_size-1);    // Get hash key, corrected for size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
  bucket *b = &_bin[i];         // Handy shortcut
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
  for( uint j=0; j<b->_cnt; j++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
    if( !_cmp(key,b->_keyvals[j+j]) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
      return b->_keyvals[j+j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   277
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
//------------------------------CmpDict--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
// CmpDict compares two dictionaries; they must have the same keys (their
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
// keys must match using CmpKey) and they must have the same values (pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
// comparison).  If so 1 is returned, if not 0 is returned.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
int32 Dict::operator ==(const Dict &d2) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  if( _cnt != d2._cnt ) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  if( _hash != d2._hash ) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  if( _cmp != d2._cmp ) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  for( uint i=0; i < _size; i++) {      // For complete hash table do
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
    bucket *b = &_bin[i];       // Handy shortcut
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
    if( b->_cnt != d2._bin[i]._cnt ) return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
    if( memcmp(b->_keyvals, d2._bin[i]._keyvals, b->_cnt*2*sizeof(void*) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
      return 0;                 // Key-value pairs must match
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
  return 1;                     // All match, is OK
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
//------------------------------print------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
// Handier print routine
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
void Dict::print() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  DictI i(this); // Moved definition in iterator here because of g++.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
  tty->print("Dict@0x%lx[%d] = {", this, _cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
  for( ; i.test(); ++i ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   303
    tty->print("(0x%lx,0x%lx),", i._key, i._value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  tty->print_cr("}");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
//------------------------------Hashing Functions----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
// Convert string to hash key.  This algorithm implements a universal hash
489c9b5090e2 Initial load
duke
parents:
diff changeset
   310
// function with the multipliers frozen (ok, so it's not universal).  The
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
// multipliers (and allowable characters) are all odd, so the resultant sum
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2026
diff changeset
   312
// is odd - guaranteed not divisible by any power of two, so the hash tables
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
// can be any power of two with good results.  Also, I choose multipliers
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
// that have only 2 bits set (the low is always set to be odd) so
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
// multiplication requires only shifts and adds.  Characters are required to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
// be in the range 0-127 (I double & add 1 to force oddness).  Keys are
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
// limited to MAXID characters in length.  Experimental evidence on 150K of
489c9b5090e2 Initial load
duke
parents:
diff changeset
   318
// C text shows excellent spreading of values for any size hash table.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
int hashstr(const void *t) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   320
  register char c, k = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  register int32 sum = 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  register const char *s = (const char *)t;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   323
489c9b5090e2 Initial load
duke
parents:
diff changeset
   324
  while( ((c = *s++) != '\0') && (k < MAXID-1) ) { // Get characters till null or MAXID-1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   325
    c = (c<<1)+1;               // Characters are always odd!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
    sum += c + (c<<shft[k++]);  // Universal hash function
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
  return (int)((sum+xsum[k]) >> 1); // Hash key, un-modulo'd table size
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
//------------------------------hashptr--------------------------------------
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2026
diff changeset
   332
// Slimey cheap hash function; no guaranteed performance.  Better than the
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
// default for pointers, especially on MS-DOS machines.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
int hashptr(const void *key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
#ifdef __TURBOC__
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
    return ((intptr_t)key >> 16);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
#else  // __TURBOC__
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    return ((intptr_t)key >> 2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 2026
diff changeset
   342
// Slimey cheap hash function; no guaranteed performance.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
int hashkey(const void *key) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
  return (intptr_t)key;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
//------------------------------Key Comparator Functions---------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
int32 cmpstr(const void *k1, const void *k2) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  return strcmp((const char *)k1,(const char *)k2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   350
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
2026
d15da7324f58 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 1
diff changeset
   352
// Cheap key comparator.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   353
int32 cmpkey(const void *key1, const void *key2) {
2026
d15da7324f58 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 1
diff changeset
   354
  if (key1 == key2) return 0;
d15da7324f58 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 1
diff changeset
   355
  intptr_t delta = (intptr_t)key1 - (intptr_t)key2;
d15da7324f58 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 1
diff changeset
   356
  if (delta > 0) return 1;
d15da7324f58 6798785: Crash in OopFlow::build_oop_map: incorrect comparison of 64bit pointers
never
parents: 1
diff changeset
   357
  return -1;
1
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
//------------------------------reset------------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
// Create an iterator and initialize the first variables.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
void DictI::reset( const Dict *dict ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
  _d = dict;                    // The dictionary
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
  _i = (uint)-1;                // Before the first bin
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
  _j = 0;                       // Nothing left in the current bin
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
  ++(*this);                    // Step to first real value
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 the next key-value pair in the dictionary, or return a NULL key and
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
// value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
void DictI::operator ++(void) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
  if( _j-- ) {                  // Still working in current bin?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    _key   = _d->_bin[_i]._keyvals[_j+_j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    _value = _d->_bin[_i]._keyvals[_j+_j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
  while( ++_i < _d->_size ) {   // Else scan for non-zero bucket
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
    _j = _d->_bin[_i]._cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
    if( !_j ) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
    _j--;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
    _key   = _d->_bin[_i]._keyvals[_j+_j];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
    _value = _d->_bin[_i]._keyvals[_j+_j+1];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
  _key = _value = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
}