Wed, 27 Apr 2016 01:25:04 +0800
Initial load
http://hg.openjdk.java.net/jdk8u/jdk8u/hotspot/
changeset: 6782:28b50d07f6f8
tag: jdk8u25-b17
aoqi@0 | 1 | /* |
aoqi@0 | 2 | * Copyright (c) 1998, 2010, Oracle and/or its affiliates. All rights reserved. |
aoqi@0 | 3 | * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER. |
aoqi@0 | 4 | * |
aoqi@0 | 5 | * This code is free software; you can redistribute it and/or modify it |
aoqi@0 | 6 | * under the terms of the GNU General Public License version 2 only, as |
aoqi@0 | 7 | * published by the Free Software Foundation. |
aoqi@0 | 8 | * |
aoqi@0 | 9 | * This code is distributed in the hope that it will be useful, but WITHOUT |
aoqi@0 | 10 | * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
aoqi@0 | 11 | * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
aoqi@0 | 12 | * version 2 for more details (a copy is included in the LICENSE file that |
aoqi@0 | 13 | * accompanied this code). |
aoqi@0 | 14 | * |
aoqi@0 | 15 | * You should have received a copy of the GNU General Public License version |
aoqi@0 | 16 | * 2 along with this work; if not, write to the Free Software Foundation, |
aoqi@0 | 17 | * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA. |
aoqi@0 | 18 | * |
aoqi@0 | 19 | * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA |
aoqi@0 | 20 | * or visit www.oracle.com if you need additional information or have any |
aoqi@0 | 21 | * questions. |
aoqi@0 | 22 | * |
aoqi@0 | 23 | */ |
aoqi@0 | 24 | |
aoqi@0 | 25 | #ifndef SHARE_VM_ADLC_DICT2_HPP |
aoqi@0 | 26 | #define SHARE_VM_ADLC_DICT2_HPP |
aoqi@0 | 27 | |
aoqi@0 | 28 | // Dictionaries - An Abstract Data Type |
aoqi@0 | 29 | |
aoqi@0 | 30 | |
aoqi@0 | 31 | class Dict; |
aoqi@0 | 32 | |
aoqi@0 | 33 | // These dictionaries define a key-value mapping. They can be inserted to, |
aoqi@0 | 34 | // searched or deleted from. They grow and shrink as needed. The key is a |
aoqi@0 | 35 | // pointer to something (or anything which can be stored in a pointer). A |
aoqi@0 | 36 | // key comparison routine determines if two keys are equal or not. A hash |
aoqi@0 | 37 | // function can be provided; if it's not provided the key itself is used |
aoqi@0 | 38 | // instead. A nice string hash function is included. |
aoqi@0 | 39 | typedef int (*CmpKey)(const void *key1, const void *key2); |
aoqi@0 | 40 | typedef int (*Hash)(const void *key); |
aoqi@0 | 41 | typedef void (*PrintKeyOrValue)(const void *key_or_value); |
aoqi@0 | 42 | typedef void (*FuncDict)(const void *key, const void *val, Dict *d); |
aoqi@0 | 43 | |
aoqi@0 | 44 | class Dict { // Dictionary structure |
aoqi@0 | 45 | private: |
aoqi@0 | 46 | class Arena *_arena; // Where to draw storage from |
aoqi@0 | 47 | class bucket *_bin; // Hash table is array of buckets |
aoqi@0 | 48 | int _size; // Size (# of slots) in hash table |
aoqi@0 | 49 | int _cnt; // Number of key-value pairs in hash table |
aoqi@0 | 50 | const Hash _hash; // Hashing function |
aoqi@0 | 51 | const CmpKey _cmp; // Key comparison function |
aoqi@0 | 52 | void doubhash( void ); // Double hash table size |
aoqi@0 | 53 | |
aoqi@0 | 54 | public: |
aoqi@0 | 55 | friend class DictI; // Friendly iterator function |
aoqi@0 | 56 | |
aoqi@0 | 57 | // cmp is a key comparision routine. hash is a routine to hash a key. |
aoqi@0 | 58 | Dict( CmpKey cmp, Hash hash ); |
aoqi@0 | 59 | Dict( CmpKey cmp, Hash hash, Arena *arena ); |
aoqi@0 | 60 | void init(); |
aoqi@0 | 61 | ~Dict(); |
aoqi@0 | 62 | |
aoqi@0 | 63 | Dict( const Dict & ); // Deep-copy guts |
aoqi@0 | 64 | Dict &operator =( const Dict & ); |
aoqi@0 | 65 | |
aoqi@0 | 66 | // Zap to empty; ready for re-use |
aoqi@0 | 67 | void Clear(); |
aoqi@0 | 68 | |
aoqi@0 | 69 | // Return # of key-value pairs in dict |
aoqi@0 | 70 | int Size(void) const { return _cnt; } |
aoqi@0 | 71 | |
aoqi@0 | 72 | // Insert inserts the given key-value pair into the dictionary. The prior |
aoqi@0 | 73 | // value of the key is returned; NULL if the key was not previously defined. |
aoqi@0 | 74 | const void *Insert(const void *key, const void *val); // A new key-value |
aoqi@0 | 75 | const void *Delete(void *key); // Delete & return old |
aoqi@0 | 76 | |
aoqi@0 | 77 | // Find finds the value of a given key; or NULL if not found. |
aoqi@0 | 78 | // The dictionary is NOT changed. |
aoqi@0 | 79 | const void *operator [](const void *key) const; // Do a lookup |
aoqi@0 | 80 | |
aoqi@0 | 81 | // == compares two dictionaries; they must have the same keys (their keys |
aoqi@0 | 82 | // must match using CmpKey) and they must have the same values (pointer |
aoqi@0 | 83 | // comparison). If so 1 is returned, if not 0 is returned. |
aoqi@0 | 84 | int operator ==(const Dict &d) const; // Compare dictionaries for equal |
aoqi@0 | 85 | |
aoqi@0 | 86 | // Print out the dictionary contents as key-value pairs |
aoqi@0 | 87 | void print(); |
aoqi@0 | 88 | void print(PrintKeyOrValue print_key, PrintKeyOrValue print_value); |
aoqi@0 | 89 | }; |
aoqi@0 | 90 | |
aoqi@0 | 91 | // Hashing functions |
aoqi@0 | 92 | int hashstr(const void *s); // Nice string hash |
aoqi@0 | 93 | // Slimey cheap hash function; no guaranteed performance. Better than the |
aoqi@0 | 94 | // default for pointers, especially on MS-DOS machines. |
aoqi@0 | 95 | int hashptr(const void *key); |
aoqi@0 | 96 | // Slimey cheap hash function; no guaranteed performance. |
aoqi@0 | 97 | int hashkey(const void *key); |
aoqi@0 | 98 | |
aoqi@0 | 99 | // Key comparators |
aoqi@0 | 100 | int cmpstr(const void *k1, const void *k2); |
aoqi@0 | 101 | // Slimey cheap key comparator. |
aoqi@0 | 102 | int cmpkey(const void *key1, const void *key2); |
aoqi@0 | 103 | |
aoqi@0 | 104 | //------------------------------Iteration-------------------------------------- |
aoqi@0 | 105 | // The class of dictionary iterators. Fails in the presences of modifications |
aoqi@0 | 106 | // to the dictionary during iteration (including searches). |
aoqi@0 | 107 | // Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;} |
aoqi@0 | 108 | class DictI { |
aoqi@0 | 109 | private: |
aoqi@0 | 110 | const Dict *_d; // Dictionary being iterated over |
aoqi@0 | 111 | int _i; // Counter over the bins |
aoqi@0 | 112 | int _j; // Counter inside each bin |
aoqi@0 | 113 | public: |
aoqi@0 | 114 | const void *_key, *_value; // Easy access to the key-value pair |
aoqi@0 | 115 | DictI( const Dict *d ) {reset(d);}; // Create a new iterator |
aoqi@0 | 116 | void reset( const Dict *dict ); // Reset existing iterator |
aoqi@0 | 117 | void operator ++(void); // Increment iterator |
aoqi@0 | 118 | int test(void) { return _i<_d->_size;} // Test for end of iteration |
aoqi@0 | 119 | }; |
aoqi@0 | 120 | |
aoqi@0 | 121 | #endif // SHARE_VM_ADLC_DICT2_HPP |