src/share/vm/adlc/dict2.hpp

changeset 0
f90c822e73f8
child 6876
710a3c8b516e
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/src/share/vm/adlc/dict2.hpp	Wed Apr 27 01:25:04 2016 +0800
     1.3 @@ -0,0 +1,121 @@
     1.4 +/*
     1.5 + * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved.
     1.6 + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     1.7 + *
     1.8 + * This code is free software; you can redistribute it and/or modify it
     1.9 + * under the terms of the GNU General Public License version 2 only, as
    1.10 + * published by the Free Software Foundation.
    1.11 + *
    1.12 + * This code is distributed in the hope that it will be useful, but WITHOUT
    1.13 + * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    1.14 + * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    1.15 + * version 2 for more details (a copy is included in the LICENSE file that
    1.16 + * accompanied this code).
    1.17 + *
    1.18 + * You should have received a copy of the GNU General Public License version
    1.19 + * 2 along with this work; if not, write to the Free Software Foundation,
    1.20 + * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    1.21 + *
    1.22 + * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    1.23 + * or visit www.oracle.com if you need additional information or have any
    1.24 + * questions.
    1.25 + *
    1.26 + */
    1.27 +
    1.28 +#ifndef SHARE_VM_ADLC_DICT2_HPP
    1.29 +#define SHARE_VM_ADLC_DICT2_HPP
    1.30 +
    1.31 +// Dictionaries - An Abstract Data Type
    1.32 +
    1.33 +
    1.34 +class Dict;
    1.35 +
    1.36 +// These dictionaries define a key-value mapping.  They can be inserted to,
    1.37 +// searched or deleted from.  They grow and shrink as needed.  The key is a
    1.38 +// pointer to something (or anything which can be stored in a pointer).  A
    1.39 +// key comparison routine determines if two keys are equal or not.  A hash
    1.40 +// function can be provided; if it's not provided the key itself is used
    1.41 +// instead.  A nice string hash function is included.
    1.42 +typedef int  (*CmpKey)(const void *key1, const void *key2);
    1.43 +typedef int  (*Hash)(const void *key);
    1.44 +typedef void (*PrintKeyOrValue)(const void *key_or_value);
    1.45 +typedef void (*FuncDict)(const void *key, const void *val, Dict *d);
    1.46 +
    1.47 +class Dict { // Dictionary structure
    1.48 + private:
    1.49 +  class Arena *_arena;          // Where to draw storage from
    1.50 +  class bucket *_bin;           // Hash table is array of buckets
    1.51 +  int _size;                    // Size (# of slots) in hash table
    1.52 +  int _cnt;                     // Number of key-value pairs in hash table
    1.53 +  const Hash _hash;             // Hashing function
    1.54 +  const CmpKey _cmp;            // Key comparison function
    1.55 +  void doubhash( void );        // Double hash table size
    1.56 +
    1.57 + public:
    1.58 +  friend class DictI;            // Friendly iterator function
    1.59 +
    1.60 +  // cmp is a key comparision routine.  hash is a routine to hash a key.
    1.61 +  Dict( CmpKey cmp, Hash hash );
    1.62 +  Dict( CmpKey cmp, Hash hash, Arena *arena );
    1.63 +  void init();
    1.64 +  ~Dict();
    1.65 +
    1.66 +  Dict( const Dict & );         // Deep-copy guts
    1.67 +  Dict &operator =( const Dict & );
    1.68 +
    1.69 +  // Zap to empty; ready for re-use
    1.70 +  void Clear();
    1.71 +
    1.72 +  // Return # of key-value pairs in dict
    1.73 +  int Size(void) const { return _cnt; }
    1.74 +
    1.75 +  // Insert inserts the given key-value pair into the dictionary.  The prior
    1.76 +  // value of the key is returned; NULL if the key was not previously defined.
    1.77 +  const void *Insert(const void *key, const void *val); // A new key-value
    1.78 +  const void *Delete(void *key);                        // Delete & return old
    1.79 +
    1.80 +  // Find finds the value of a given key; or NULL if not found.
    1.81 +  // The dictionary is NOT changed.
    1.82 +  const void *operator [](const void *key) const;  // Do a lookup
    1.83 +
    1.84 +  // == compares two dictionaries; they must have the same keys (their keys
    1.85 +  // must match using CmpKey) and they must have the same values (pointer
    1.86 +  // comparison).  If so 1 is returned, if not 0 is returned.
    1.87 +  int operator ==(const Dict &d) const;   // Compare dictionaries for equal
    1.88 +
    1.89 +  // Print out the dictionary contents as key-value pairs
    1.90 +  void print();
    1.91 +  void print(PrintKeyOrValue print_key, PrintKeyOrValue print_value);
    1.92 +};
    1.93 +
    1.94 +// Hashing functions
    1.95 +int hashstr(const void *s);        // Nice string hash
    1.96 +// Slimey cheap hash function; no guaranteed performance.  Better than the
    1.97 +// default for pointers, especially on MS-DOS machines.
    1.98 +int hashptr(const void *key);
    1.99 +// Slimey cheap hash function; no guaranteed performance.
   1.100 +int hashkey(const void *key);
   1.101 +
   1.102 +// Key comparators
   1.103 +int cmpstr(const void *k1, const void *k2);
   1.104 +// Slimey cheap key comparator.
   1.105 +int cmpkey(const void *key1, const void *key2);
   1.106 +
   1.107 +//------------------------------Iteration--------------------------------------
   1.108 +// The class of dictionary iterators.  Fails in the presences of modifications
   1.109 +// to the dictionary during iteration (including searches).
   1.110 +// Usage:  for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}
   1.111 +class DictI {
   1.112 + private:
   1.113 +  const Dict *_d;               // Dictionary being iterated over
   1.114 +  int _i;                      // Counter over the bins
   1.115 +  int _j;                      // Counter inside each bin
   1.116 + public:
   1.117 +  const void *_key, *_value;          // Easy access to the key-value pair
   1.118 +  DictI( const Dict *d ) {reset(d);}; // Create a new iterator
   1.119 +  void reset( const Dict *dict );     // Reset existing iterator
   1.120 +  void operator ++(void);             // Increment iterator
   1.121 +  int test(void) { return _i<_d->_size;} // Test for end of iteration
   1.122 +};
   1.123 +
   1.124 +#endif // SHARE_VM_ADLC_DICT2_HPP

mercurial