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