src/hotspot/share/libadt/dict.hpp
author stefank
Thu, 08 Mar 2018 09:56:29 +0100
changeset 49360 886acec3b4c6
parent 47216 71c04702a3d5
child 53244 9807daeb47c4
permissions -rw-r--r--
8199275: Fix inclusions of allocation.inline.hpp Reviewed-by: coleenp, kbarrett, dholmes
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
49360
886acec3b4c6 8199275: Fix inclusions of allocation.inline.hpp
stefank
parents: 47216
diff changeset
     2
 * Copyright (c) 1997, 2018, 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: 2131
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 2131
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: 2131
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    25
#ifndef SHARE_VM_LIBADT_DICT_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    26
#define SHARE_VM_LIBADT_DICT_HPP
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    27
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    28
// Dictionaries - An Abstract Data Type
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
    29
49360
886acec3b4c6 8199275: Fix inclusions of allocation.inline.hpp
stefank
parents: 47216
diff changeset
    30
#include "memory/allocation.hpp"
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    31
#include "memory/resourceArea.hpp"
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    32
#include "runtime/thread.hpp"
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    33
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    34
class Dict;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    35
489c9b5090e2 Initial load
duke
parents:
diff changeset
    36
// These dictionaries define a key-value mapping.  They can be inserted to,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    37
// searched or deleted from.  They grow and shrink as needed.  The key is a
489c9b5090e2 Initial load
duke
parents:
diff changeset
    38
// pointer to something (or anything which can be stored in a pointer).  A
489c9b5090e2 Initial load
duke
parents:
diff changeset
    39
// key comparison routine determines if two keys are equal or not.  A hash
489c9b5090e2 Initial load
duke
parents:
diff changeset
    40
// function can be provided; if it's not provided the key itself is used
489c9b5090e2 Initial load
duke
parents:
diff changeset
    41
// instead.  A nice string hash function is included.
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    42
typedef int32_t (*CmpKey)(const void *key1, const void *key2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
typedef int  (*Hash)(const void *key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
typedef void (*FuncDict)(const void *key, const void *val, Dict *d);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
class Dict : public ResourceObj { // Dictionary structure
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  class Arena *_arena;          // Where to draw storage from
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  class bucket *_bin;           // Hash table is array of buckets
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  uint _size;                   // Size (# of slots) in hash table
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    51
  uint32_t _cnt;                // Number of key-value pairs in hash table
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  const Hash _hash;             // Hashing function
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
  const CmpKey _cmp;            // Key comparison function
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  void doubhash( void );        // Double hash table size
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
  friend class DictI;            // Friendly iterator function
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
  // cmp is a key comparision routine.  hash is a routine to hash a key.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
  Dict( CmpKey cmp, Hash hash );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
  Dict( CmpKey cmp, Hash hash, Arena *arena, int size=16 );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
  ~Dict();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
  Dict( const Dict & );         // Deep-copy guts
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
  Dict &operator =( const Dict & );
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
  // Zap to empty; ready for re-use
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
  void Clear();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
489c9b5090e2 Initial load
duke
parents:
diff changeset
    70
  // Return # of key-value pairs in dict
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    71
  uint32_t Size(void) const { return _cnt; }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    72
489c9b5090e2 Initial load
duke
parents:
diff changeset
    73
  // Insert inserts the given key-value pair into the dictionary.  The prior
489c9b5090e2 Initial load
duke
parents:
diff changeset
    74
  // value of the key is returned; NULL if the key was not previously defined.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    75
  void *Insert(void *key, void *val, bool replace = true); // A new key-value
489c9b5090e2 Initial load
duke
parents:
diff changeset
    76
  void *Delete(void *key);        // Delete & return old
489c9b5090e2 Initial load
duke
parents:
diff changeset
    77
489c9b5090e2 Initial load
duke
parents:
diff changeset
    78
  // Find finds the value of a given key; or NULL if not found.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    79
  // The dictionary is NOT changed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    80
  void *operator [](const void *key) const;  // Do a lookup
489c9b5090e2 Initial load
duke
parents:
diff changeset
    81
489c9b5090e2 Initial load
duke
parents:
diff changeset
    82
  // == compares two dictionaries; they must have the same keys (their keys
489c9b5090e2 Initial load
duke
parents:
diff changeset
    83
  // must match using CmpKey) and they must have the same values (pointer
489c9b5090e2 Initial load
duke
parents:
diff changeset
    84
  // comparison).  If so 1 is returned, if not 0 is returned.
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
    85
  int32_t operator ==(const Dict &d) const;   // Compare dictionaries for equal
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    86
489c9b5090e2 Initial load
duke
parents:
diff changeset
    87
  // Print out the dictionary contents as key-value pairs
489c9b5090e2 Initial load
duke
parents:
diff changeset
    88
  void print();
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
489c9b5090e2 Initial load
duke
parents:
diff changeset
    91
// Hashing functions
489c9b5090e2 Initial load
duke
parents:
diff changeset
    92
int hashstr(const void *s);        // Nice string hash
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1
diff changeset
    93
// Slimey cheap hash function; no guaranteed performance.  Better than the
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    94
// default for pointers, especially on MS-DOS machines.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    95
int hashptr(const void *key);
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1
diff changeset
    96
// Slimey cheap hash function; no guaranteed performance.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    97
int hashkey(const void *key);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    98
489c9b5090e2 Initial load
duke
parents:
diff changeset
    99
// Key comparators
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
   100
int32_t cmpstr(const void *k1, const void *k2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   101
// Slimey cheap key comparator.
24425
53764d2358f9 8041415: remove port.{cpp,hpp} files
zgu
parents: 7397
diff changeset
   102
int32_t cmpkey(const void *key1, const void *key2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   103
489c9b5090e2 Initial load
duke
parents:
diff changeset
   104
//------------------------------Iteration--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   105
// The class of dictionary iterators.  Fails in the presences of modifications
489c9b5090e2 Initial load
duke
parents:
diff changeset
   106
// to the dictionary during iteration (including searches).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   107
// Usage:  for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   108
class DictI {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   109
 private:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   110
  const Dict *_d;               // Dictionary being iterated over
489c9b5090e2 Initial load
duke
parents:
diff changeset
   111
  uint _i;                      // Counter over the bins
489c9b5090e2 Initial load
duke
parents:
diff changeset
   112
  uint _j;                      // Counter inside each bin
489c9b5090e2 Initial load
duke
parents:
diff changeset
   113
 public:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   114
  const void *_key, *_value;    // Easy access to the key-value pair
489c9b5090e2 Initial load
duke
parents:
diff changeset
   115
  DictI( const Dict *d ) {reset(d);}; // Create a new iterator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   116
  void reset( const Dict *dict );     // Reset existing iterator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   117
  void operator ++(void);             // Increment iterator
489c9b5090e2 Initial load
duke
parents:
diff changeset
   118
  int test(void) { return _i<_d->_size;} // Test for end of iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   119
};
489c9b5090e2 Initial load
duke
parents:
diff changeset
   120
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 5547
diff changeset
   121
#endif // SHARE_VM_LIBADT_DICT_HPP