1 /* 2 * Copyright (c) 1997, 2024, 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 AnyObj { 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 // Allocated words 46 uint _data_size; 47 uint32_t* _data; 48 Arena* _set_arena; 49 ReallocMark _nesting; // Safety checks for arena reallocation 50 51 void init(Arena* arena); 52 53 // Grow vector to required word capacity 54 void maybe_grow(uint new_word_capacity) { 55 if (new_word_capacity >= _size) { 56 grow(new_word_capacity); 57 } 58 } 59 void grow(uint new_word_capacity); 60 61 public: 62 VectorSet(); 63 VectorSet(Arena* arena); 64 ~VectorSet() {} 65 66 NONCOPYABLE(VectorSet); 67 VectorSet& operator=(VectorSet&&) = delete; 68 // Allow move constructor for && (eg. capture return of function) 69 VectorSet(VectorSet&&) = default; 70 71 void insert(uint elem); 72 bool is_empty() const; 73 void reset() { 74 _size = 0; 75 } 76 void clear() { 77 reset(); 78 } 79 80 // Fast inlined "test and set". Replaces the idiom: 81 // if (visited.test(idx)) return; 82 // visited.set(idx); 83 // With: 84 // if (visited.test_set(idx)) return; 85 // 86 bool test_set(uint elem) { 87 uint32_t word = elem >> word_bits; 88 maybe_grow(word); 89 uint32_t mask = 1U << (elem & bit_mask); 90 uint32_t data = _data[word]; 91 _data[word] = data | mask; 92 return (data & mask) != 0; 93 } 94 95 // Fast inlined test 96 bool test(uint elem) const { 97 uint32_t word = elem >> word_bits; 98 if (word >= _size) { 99 return false; 100 } 101 uint32_t mask = 1U << (elem & bit_mask); 102 return (_data[word] & mask) != 0; 103 } 104 105 void remove(uint elem) { 106 uint32_t word = elem >> word_bits; 107 if (word >= _size) { 108 return; 109 } 110 uint32_t mask = 1U << (elem & bit_mask); 111 _data[word] &= ~mask; // Clear bit 112 } 113 114 // Fast inlined set 115 void set(uint elem) { 116 uint32_t word = elem >> word_bits; 117 maybe_grow(word); 118 uint32_t mask = 1U << (elem & bit_mask); 119 _data[word] |= mask; 120 } 121 }; 122 123 #endif // SHARE_LIBADT_VECTSET_HPP