Path: blob/master/src/hotspot/share/libadt/vectset.hpp
40951 views
/*1* Copyright (c) 1997, 2019, Oracle and/or its affiliates. All rights reserved.2* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.3*4* This code is free software; you can redistribute it and/or modify it5* under the terms of the GNU General Public License version 2 only, as6* published by the Free Software Foundation.7*8* This code is distributed in the hope that it will be useful, but WITHOUT9* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or10* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License11* version 2 for more details (a copy is included in the LICENSE file that12* accompanied this code).13*14* You should have received a copy of the GNU General Public License version15* 2 along with this work; if not, write to the Free Software Foundation,16* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.17*18* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA19* or visit www.oracle.com if you need additional information or have any20* questions.21*22*/2324#ifndef SHARE_LIBADT_VECTSET_HPP25#define SHARE_LIBADT_VECTSET_HPP2627#include "memory/allocation.hpp"28#include "utilities/copy.hpp"2930// Vector Sets3132// These sets can grow or shrink, based on the initial size and the largest33// element currently in them.3435//------------------------------VectorSet--------------------------------------36class VectorSet : public ResourceObj {37private:3839static const uint word_bits = 5;40static const uint bit_mask = 31;4142// Used 32-bit words43uint _size;44uint32_t* _data;45// Allocated words46uint _data_size;47Arena* _set_arena;4849void init(Arena* arena);50// Grow vector to required word capacity51void grow(uint new_word_capacity);52public:53VectorSet();54VectorSet(Arena* arena);55~VectorSet() {}5657void insert(uint elem);58bool is_empty() const;59void reset() {60_size = 0;61}62void clear() {63reset();64}6566// Fast inlined "test and set". Replaces the idiom:67// if (visited.test(idx)) return;68// visited.set(idx);69// With:70// if (visited.test_set(idx)) return;71//72bool test_set(uint elem) {73uint32_t word = elem >> word_bits;74if (word >= _size) {75// Then grow76grow(word);77}78uint32_t mask = 1U << (elem & bit_mask);79uint32_t data = _data[word];80_data[word] = data | mask;81return (data & mask) != 0;82}8384// Fast inlined test85bool test(uint elem) const {86uint32_t word = elem >> word_bits;87if (word >= _size) {88return false;89}90uint32_t mask = 1U << (elem & bit_mask);91return (_data[word] & mask) != 0;92}9394void remove(uint elem) {95uint32_t word = elem >> word_bits;96if (word >= _size) {97return;98}99uint32_t mask = 1U << (elem & bit_mask);100_data[word] &= ~mask; // Clear bit101}102103// Fast inlined set104void set(uint elem) {105uint32_t word = elem >> word_bits;106if (word >= _size) {107grow(word);108}109uint32_t mask = 1U << (elem & bit_mask);110_data[word] |= mask;111}112};113114#endif // SHARE_LIBADT_VECTSET_HPP115116117