Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
PojavLauncherTeam
GitHub Repository: PojavLauncherTeam/mobile
Path: blob/master/src/hotspot/share/libadt/vectset.hpp
40951 views
1
/*
2
* Copyright (c) 1997, 2019, 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_VECTSET_HPP
26
#define SHARE_LIBADT_VECTSET_HPP
27
28
#include "memory/allocation.hpp"
29
#include "utilities/copy.hpp"
30
31
// Vector Sets
32
33
// These sets can grow or shrink, based on the initial size and the largest
34
// element currently in them.
35
36
//------------------------------VectorSet--------------------------------------
37
class VectorSet : public ResourceObj {
38
private:
39
40
static const uint word_bits = 5;
41
static const uint bit_mask = 31;
42
43
// Used 32-bit words
44
uint _size;
45
uint32_t* _data;
46
// Allocated words
47
uint _data_size;
48
Arena* _set_arena;
49
50
void init(Arena* arena);
51
// Grow vector to required word capacity
52
void grow(uint new_word_capacity);
53
public:
54
VectorSet();
55
VectorSet(Arena* arena);
56
~VectorSet() {}
57
58
void insert(uint elem);
59
bool is_empty() const;
60
void reset() {
61
_size = 0;
62
}
63
void clear() {
64
reset();
65
}
66
67
// Fast inlined "test and set". Replaces the idiom:
68
// if (visited.test(idx)) return;
69
// visited.set(idx);
70
// With:
71
// if (visited.test_set(idx)) return;
72
//
73
bool test_set(uint elem) {
74
uint32_t word = elem >> word_bits;
75
if (word >= _size) {
76
// Then grow
77
grow(word);
78
}
79
uint32_t mask = 1U << (elem & bit_mask);
80
uint32_t data = _data[word];
81
_data[word] = data | mask;
82
return (data & mask) != 0;
83
}
84
85
// Fast inlined test
86
bool test(uint elem) const {
87
uint32_t word = elem >> word_bits;
88
if (word >= _size) {
89
return false;
90
}
91
uint32_t mask = 1U << (elem & bit_mask);
92
return (_data[word] & mask) != 0;
93
}
94
95
void remove(uint elem) {
96
uint32_t word = elem >> word_bits;
97
if (word >= _size) {
98
return;
99
}
100
uint32_t mask = 1U << (elem & bit_mask);
101
_data[word] &= ~mask; // Clear bit
102
}
103
104
// Fast inlined set
105
void set(uint elem) {
106
uint32_t word = elem >> word_bits;
107
if (word >= _size) {
108
grow(word);
109
}
110
uint32_t mask = 1U << (elem & bit_mask);
111
_data[word] |= mask;
112
}
113
};
114
115
#endif // SHARE_LIBADT_VECTSET_HPP
116
117