Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/libadt/dict.hpp
40951 views
1
/*
2
* Copyright (c) 1997, 2020, Oracle and/or its affiliates. All rights reserved.
3
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4
*
5
* This code is free software; you can redistribute it and/or modify it
6
* under the terms of the GNU General Public License version 2 only, as
7
* published by the Free Software Foundation.
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_LIBADT_DICT_HPP
26
#define SHARE_LIBADT_DICT_HPP
27
28
// Dictionaries - An Abstract Data Type
29
30
#include "memory/allocation.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
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_t _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(); // 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(const Dict &base, Arena* arena); // Deep-copy
62
~Dict();
63
64
// Return # of key-value pairs in dict
65
uint32_t Size(void) const { return _cnt; }
66
67
// Insert inserts the given key-value pair into the dictionary. The prior
68
// value of the key is returned; NULL if the key was not previously defined.
69
void* Insert(void* key, void* val, bool replace = true); // A new key-value
70
void* Delete(void* key); // Delete & return old
71
72
// Find finds the value of a given key; or NULL if not found.
73
// The dictionary is NOT changed.
74
void* operator [](const void* key) const; // Do a lookup
75
76
// Print out the dictionary contents as key-value pairs
77
void print();
78
};
79
80
// Hashing functions
81
int hashstr(const void* s); // Nice string hash
82
// Slimey cheap hash function; no guaranteed performance. Better than the
83
// default for pointers, especially on MS-DOS machines.
84
int hashptr(const void* key);
85
// Slimey cheap hash function; no guaranteed performance.
86
int hashkey(const void* key);
87
88
// Key comparators
89
int32_t cmpstr(const void* k1, const void* k2);
90
// Slimey cheap key comparator.
91
int32_t cmpkey(const void* key1, const void* key2);
92
93
//------------------------------Iteration--------------------------------------
94
// The class of dictionary iterators. Fails in the presences of modifications
95
// to the dictionary during iteration (including searches).
96
// Usage: for( DictI i(dict); i.test(); ++i ) { body = i.key; body = i.value;}
97
class DictI {
98
private:
99
const Dict* _d; // Dictionary being iterated over
100
uint _i; // Counter over the bins
101
uint _j; // Counter inside each bin
102
public:
103
const void* _key;
104
const void* _value;
105
DictI(const Dict* d) { reset(d); }; // Create a new iterator
106
void reset(const Dict* dict); // Reset existing iterator
107
void operator ++(void); // Increment iterator
108
int test(void) { return _i < _d->_size; } // Test for end of iteration
109
};
110
111
#endif // SHARE_LIBADT_DICT_HPP
112
113