2023-08-15 16:20:26 +02:00
|
|
|
/* SPDX-FileCopyrightText: 2023 Blender Authors
|
2023-05-31 16:19:06 +02:00
|
|
|
*
|
|
|
|
* SPDX-License-Identifier: GPL-2.0-or-later */
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
/** \file
|
|
|
|
* \ingroup bli
|
|
|
|
*
|
2020-06-09 10:27:24 +02:00
|
|
|
* This file contains different slot types that are supposed to be used with blender::Set.
|
2020-06-09 10:10:56 +02:00
|
|
|
*
|
|
|
|
* Every slot type has to be able to hold a value of the Key type and state information.
|
|
|
|
* A set slot has three possible states: empty, occupied and removed.
|
|
|
|
*
|
|
|
|
* Only when a slot is occupied, it stores an instance of type Key.
|
|
|
|
*
|
|
|
|
* A set slot type has to implement a couple of methods that are explained in SimpleSetSlot.
|
|
|
|
* A slot type is assumed to be trivially destructible, when it is not in occupied state. So the
|
|
|
|
* destructor might not be called in that case.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "BLI_memory_utils.hh"
|
|
|
|
#include "BLI_string_ref.hh"
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
namespace blender {
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* The simplest possible set slot. It stores the slot state and the optional key instance in
|
|
|
|
* separate variables. Depending on the alignment requirement of the key, many bytes might be
|
|
|
|
* wasted.
|
|
|
|
*/
|
|
|
|
template<typename Key> class SimpleSetSlot {
|
|
|
|
private:
|
|
|
|
enum State : uint8_t {
|
|
|
|
Empty = 0,
|
|
|
|
Occupied = 1,
|
|
|
|
Removed = 2,
|
|
|
|
};
|
|
|
|
|
2020-07-03 14:15:05 +02:00
|
|
|
State state_;
|
2020-07-06 10:56:26 +02:00
|
|
|
TypedBuffer<Key> key_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
/**
|
|
|
|
* After the default constructor has run, the slot has to be in the empty state.
|
|
|
|
*/
|
|
|
|
SimpleSetSlot()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = Empty;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The destructor also has to destruct the key, if the slot is currently occupied.
|
|
|
|
*/
|
|
|
|
~SimpleSetSlot()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (state_ == Occupied) {
|
2020-07-06 10:56:26 +02:00
|
|
|
key_buffer_.ref().~Key();
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The copy constructor has to copy the state. If the other slot was occupied, a copy of the key
|
|
|
|
* has to be made as well.
|
|
|
|
*/
|
|
|
|
SimpleSetSlot(const SimpleSetSlot &other)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = other.state_;
|
|
|
|
if (other.state_ == Occupied) {
|
2020-07-06 10:56:26 +02:00
|
|
|
new (&key_buffer_) Key(*other.key_buffer_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* The move constructor has to copy the state. If the other slot was occupied, the key from the
|
|
|
|
* other slot has to be moved as well. The other slot stays in the state it was in before. Its
|
|
|
|
* optionally stored key remains in a moved-from state.
|
|
|
|
*/
|
2020-08-24 17:24:13 +02:00
|
|
|
SimpleSetSlot(SimpleSetSlot &&other) noexcept(std::is_nothrow_move_constructible_v<Key>)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = other.state_;
|
|
|
|
if (other.state_ == Occupied) {
|
2020-07-06 10:56:26 +02:00
|
|
|
new (&key_buffer_) Key(std::move(*other.key_buffer_));
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a non-const pointer to the position where the key is stored.
|
|
|
|
*/
|
|
|
|
Key *key()
|
|
|
|
{
|
2020-07-06 10:56:26 +02:00
|
|
|
return key_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get a const pointer to the position where the key is stored.
|
|
|
|
*/
|
|
|
|
const Key *key() const
|
|
|
|
{
|
2020-07-06 10:56:26 +02:00
|
|
|
return key_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the slot currently contains a key.
|
|
|
|
*/
|
|
|
|
bool is_occupied() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return state_ == Occupied;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true if the slot is empty, i.e. it does not contain a key and is not in removed state.
|
|
|
|
*/
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return state_ == Empty;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return the hash of the currently stored key. In this simple set slot implementation, we just
|
|
|
|
* compute the hash here. Other implementations might store the hash in the slot instead.
|
|
|
|
*/
|
2020-07-20 12:16:20 +02:00
|
|
|
template<typename Hash> uint64_t get_hash(const Hash &hash) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(this->is_occupied());
|
2020-07-06 10:56:26 +02:00
|
|
|
return hash(*key_buffer_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return true, when this slot is occupied and contains a key that compares equal to the given
|
|
|
|
* key. The hash is used by other slot implementations to determine inequality faster.
|
|
|
|
*/
|
|
|
|
template<typename ForwardKey, typename IsEqual>
|
2022-10-04 00:37:25 +02:00
|
|
|
bool contains(const ForwardKey &key, const IsEqual &is_equal, uint64_t /*hash*/) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (state_ == Occupied) {
|
2020-07-06 10:56:26 +02:00
|
|
|
return is_equal(key, *key_buffer_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the state of this slot from empty/removed to occupied. The key has to be constructed
|
|
|
|
* by calling the constructor with the given key as parameter.
|
|
|
|
*/
|
2022-10-04 00:37:25 +02:00
|
|
|
template<typename ForwardKey> void occupy(ForwardKey &&key, uint64_t /*hash*/)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(!this->is_occupied());
|
2020-07-06 10:56:26 +02:00
|
|
|
new (&key_buffer_) Key(std::forward<ForwardKey>(key));
|
2020-08-24 17:24:13 +02:00
|
|
|
state_ = Occupied;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Change the state of this slot from occupied to removed. The key has to be destructed as well.
|
|
|
|
*/
|
|
|
|
void remove()
|
|
|
|
{
|
|
|
|
BLI_assert(this->is_occupied());
|
2020-07-06 10:56:26 +02:00
|
|
|
key_buffer_.ref().~Key();
|
2020-08-24 17:24:13 +02:00
|
|
|
state_ = Removed;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This set slot implementation stores the hash of the key within the slot. This helps when
|
|
|
|
* computing the hash or an equality check is expensive.
|
|
|
|
*/
|
|
|
|
template<typename Key> class HashedSetSlot {
|
|
|
|
private:
|
|
|
|
enum State : uint8_t {
|
|
|
|
Empty = 0,
|
|
|
|
Occupied = 1,
|
|
|
|
Removed = 2,
|
|
|
|
};
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
uint64_t hash_;
|
2020-07-03 14:15:05 +02:00
|
|
|
State state_;
|
2020-07-06 10:56:26 +02:00
|
|
|
TypedBuffer<Key> key_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
HashedSetSlot()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = Empty;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
~HashedSetSlot()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
if (state_ == Occupied) {
|
2020-07-06 10:56:26 +02:00
|
|
|
key_buffer_.ref().~Key();
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
HashedSetSlot(const HashedSetSlot &other)
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = other.state_;
|
|
|
|
if (other.state_ == Occupied) {
|
|
|
|
hash_ = other.hash_;
|
2020-07-06 10:56:26 +02:00
|
|
|
new (&key_buffer_) Key(*other.key_buffer_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-08-24 17:24:13 +02:00
|
|
|
HashedSetSlot(HashedSetSlot &&other) noexcept(std::is_nothrow_move_constructible_v<Key>)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = other.state_;
|
|
|
|
if (other.state_ == Occupied) {
|
|
|
|
hash_ = other.hash_;
|
2020-07-06 10:56:26 +02:00
|
|
|
new (&key_buffer_) Key(std::move(*other.key_buffer_));
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Key *key()
|
|
|
|
{
|
2020-07-06 10:56:26 +02:00
|
|
|
return key_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Key *key() const
|
|
|
|
{
|
2020-07-06 10:56:26 +02:00
|
|
|
return key_buffer_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_occupied() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return state_ == Occupied;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return state_ == Empty;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 00:37:25 +02:00
|
|
|
template<typename Hash> uint64_t get_hash(const Hash & /*hash*/) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(this->is_occupied());
|
2020-07-03 14:15:05 +02:00
|
|
|
return hash_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardKey, typename IsEqual>
|
2020-07-20 12:16:20 +02:00
|
|
|
bool contains(const ForwardKey &key, const IsEqual &is_equal, const uint64_t hash) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
2021-07-07 04:55:19 +02:00
|
|
|
/* `hash_` might be uninitialized here, but that is ok. */
|
2020-07-03 14:15:05 +02:00
|
|
|
if (hash_ == hash) {
|
|
|
|
if (state_ == Occupied) {
|
2020-07-06 10:56:26 +02:00
|
|
|
return is_equal(key, *key_buffer_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
template<typename ForwardKey> void occupy(ForwardKey &&key, const uint64_t hash)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(!this->is_occupied());
|
2020-08-24 17:24:13 +02:00
|
|
|
new (&key_buffer_) Key(std::forward<ForwardKey>(key));
|
2020-07-03 14:15:05 +02:00
|
|
|
state_ = Occupied;
|
|
|
|
hash_ = hash;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove()
|
|
|
|
{
|
|
|
|
BLI_assert(this->is_occupied());
|
2020-07-06 10:56:26 +02:00
|
|
|
key_buffer_.ref().~Key();
|
2020-08-24 17:24:13 +02:00
|
|
|
state_ = Removed;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* An IntrusiveSetSlot uses two special values of the key to indicate whether the slot is empty or
|
|
|
|
* removed. This saves some memory in all cases and is more efficient in many cases. The KeyInfo
|
|
|
|
* type indicates which specific values are used. An example for a KeyInfo implementation is
|
|
|
|
* PointerKeyInfo.
|
|
|
|
*
|
|
|
|
* The special key values are expected to be trivially destructible.
|
|
|
|
*/
|
|
|
|
template<typename Key, typename KeyInfo> class IntrusiveSetSlot {
|
|
|
|
private:
|
2020-07-03 14:15:05 +02:00
|
|
|
Key key_ = KeyInfo::get_empty();
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
public:
|
|
|
|
IntrusiveSetSlot() = default;
|
|
|
|
~IntrusiveSetSlot() = default;
|
|
|
|
IntrusiveSetSlot(const IntrusiveSetSlot &other) = default;
|
2020-08-24 17:24:13 +02:00
|
|
|
IntrusiveSetSlot(IntrusiveSetSlot &&other) noexcept(std::is_nothrow_move_constructible_v<Key>) =
|
|
|
|
default;
|
2020-06-09 10:10:56 +02:00
|
|
|
|
|
|
|
Key *key()
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return &key_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
const Key *key() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return &key_;
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_occupied() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return KeyInfo::is_not_empty_or_removed(key_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty() const
|
|
|
|
{
|
2020-07-03 14:15:05 +02:00
|
|
|
return KeyInfo::is_empty(key_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
2020-07-20 12:16:20 +02:00
|
|
|
template<typename Hash> uint64_t get_hash(const Hash &hash) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(this->is_occupied());
|
2020-07-03 14:15:05 +02:00
|
|
|
return hash(key_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
template<typename ForwardKey, typename IsEqual>
|
2022-10-04 00:37:25 +02:00
|
|
|
bool contains(const ForwardKey &key, const IsEqual &is_equal, const uint64_t /*hash*/) const
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(KeyInfo::is_not_empty_or_removed(key));
|
2020-07-03 14:15:05 +02:00
|
|
|
return is_equal(key_, key);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
2022-10-04 00:37:25 +02:00
|
|
|
template<typename ForwardKey> void occupy(ForwardKey &&key, const uint64_t /*hash*/)
|
2020-06-09 10:10:56 +02:00
|
|
|
{
|
|
|
|
BLI_assert(!this->is_occupied());
|
|
|
|
BLI_assert(KeyInfo::is_not_empty_or_removed(key));
|
2020-07-03 14:15:05 +02:00
|
|
|
key_ = std::forward<ForwardKey>(key);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
void remove()
|
|
|
|
{
|
|
|
|
BLI_assert(this->is_occupied());
|
2020-07-03 14:15:05 +02:00
|
|
|
KeyInfo::remove(key_);
|
2020-06-09 10:10:56 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* This exists just to make it more convenient to define which special integer values can be used
|
|
|
|
* to indicate an empty and removed value.
|
|
|
|
*/
|
|
|
|
template<typename Int, Int EmptyValue, Int RemovedValue>
|
|
|
|
using IntegerSetSlot = IntrusiveSetSlot<Int, TemplatedKeyInfo<Int, EmptyValue, RemovedValue>>;
|
|
|
|
|
|
|
|
template<typename Key> struct DefaultSetSlot;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use SimpleSetSlot by default, because it is the smallest slot type that works for all key types.
|
|
|
|
*/
|
|
|
|
template<typename Key> struct DefaultSetSlot {
|
|
|
|
using type = SimpleSetSlot<Key>;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Store the hash of a string in the slot by default. Recomputing the hash or doing string
|
|
|
|
* comparisons can be relatively costly.
|
|
|
|
*/
|
|
|
|
template<> struct DefaultSetSlot<std::string> {
|
|
|
|
using type = HashedSetSlot<std::string>;
|
|
|
|
};
|
|
|
|
template<> struct DefaultSetSlot<StringRef> {
|
|
|
|
using type = HashedSetSlot<StringRef>;
|
|
|
|
};
|
|
|
|
template<> struct DefaultSetSlot<StringRefNull> {
|
|
|
|
using type = HashedSetSlot<StringRefNull>;
|
|
|
|
};
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Use a special slot type for pointer keys, because we can store whether a slot is empty or
|
|
|
|
* removed with special pointer values.
|
|
|
|
*/
|
|
|
|
template<typename Key> struct DefaultSetSlot<Key *> {
|
|
|
|
using type = IntrusiveSetSlot<Key *, PointerKeyInfo<Key *>>;
|
|
|
|
};
|
|
|
|
|
2020-06-09 10:27:24 +02:00
|
|
|
} // namespace blender
|