Previously, there was an error when operator-> was returning an invalid type. See error C2839.
362 lines
10 KiB
C++
362 lines
10 KiB
C++
/*
|
|
* This program is free software; you can redistribute it and/or
|
|
* modify it under the terms of the GNU General Public License
|
|
* as published by the Free Software Foundation; either version 2
|
|
* of the License, or (at your option) any later version.
|
|
*
|
|
* This program is distributed in the hope that it will be useful,
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
* GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License
|
|
* along with this program; if not, write to the Free Software Foundation,
|
|
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
|
|
*/
|
|
|
|
#ifndef __BLI_MAP_SLOTS_HH__
|
|
#define __BLI_MAP_SLOTS_HH__
|
|
|
|
/** \file
|
|
* \ingroup bli
|
|
*
|
|
* This file contains slot types that are supposed to be used with blender::Map.
|
|
*
|
|
* Every slot type has to be able to hold a value of type Key, a value of type Value and state
|
|
* information. A map slot has three possible states: empty, occupied and removed.
|
|
*
|
|
* When a slot is occupied, it stores instances of type Key and Value.
|
|
*
|
|
* A map slot type has to implement a couple of methods that are explained in SimpleMapSlot.
|
|
* 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.
|
|
*
|
|
* Possible Improvements:
|
|
* - Implement slot type that stores the hash.
|
|
*/
|
|
|
|
#include "BLI_memory_utils.hh"
|
|
|
|
namespace blender {
|
|
|
|
/**
|
|
* The simplest possible map slot. It stores the slot state and the optional key and value
|
|
* instances in separate variables. Depending on the alignment requirement of the key and value,
|
|
* many bytes might be wasted.
|
|
*/
|
|
template<typename Key, typename Value> class SimpleMapSlot {
|
|
private:
|
|
enum State : uint8_t {
|
|
Empty = 0,
|
|
Occupied = 1,
|
|
Removed = 2,
|
|
};
|
|
|
|
State state_;
|
|
TypedBuffer<Key> key_buffer_;
|
|
TypedBuffer<Value> value_buffer_;
|
|
|
|
public:
|
|
/**
|
|
* After the default constructor has run, the slot has to be in the empty state.
|
|
*/
|
|
SimpleMapSlot()
|
|
{
|
|
state_ = Empty;
|
|
}
|
|
|
|
/**
|
|
* The destructor also has to destruct the key and value, if the slot is currently occupied.
|
|
*/
|
|
~SimpleMapSlot()
|
|
{
|
|
if (state_ == Occupied) {
|
|
key_buffer_.ref().~Key();
|
|
value_buffer_.ref().~Value();
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The copy constructor has to copy the state. If the other slot was occupied, a copy of the key
|
|
* and value have to be made as well.
|
|
*/
|
|
SimpleMapSlot(const SimpleMapSlot &other)
|
|
{
|
|
state_ = other.state_;
|
|
if (other.state_ == Occupied) {
|
|
new (&key_buffer_) Key(*other.key_buffer_);
|
|
new (&value_buffer_) Value(*other.value_buffer_);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* The move constructor has to copy the state. If the other slot was occupied, the key and value
|
|
* from the other have to moved as well. The other slot stays in the state it was in before. Its
|
|
* optionally stored key and value remain in a moved-from state.
|
|
*/
|
|
SimpleMapSlot(SimpleMapSlot &&other) noexcept
|
|
{
|
|
state_ = other.state_;
|
|
if (other.state_ == Occupied) {
|
|
new (&key_buffer_) Key(std::move(*other.key_buffer_));
|
|
new (&value_buffer_) Value(std::move(*other.value_buffer_));
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Returns a non-const pointer to the position where the key is stored.
|
|
*/
|
|
Key *key()
|
|
{
|
|
return key_buffer_;
|
|
}
|
|
|
|
/**
|
|
* Returns a const pointer to the position where the key is stored.
|
|
*/
|
|
const Key *key() const
|
|
{
|
|
return key_buffer_;
|
|
}
|
|
|
|
/**
|
|
* Returns a non-const pointer to the position where the value is stored.
|
|
*/
|
|
Value *value()
|
|
{
|
|
return value_buffer_;
|
|
}
|
|
|
|
/**
|
|
* Returns a const pointer to the position where the value is stored.
|
|
*/
|
|
const Value *value() const
|
|
{
|
|
return value_buffer_;
|
|
}
|
|
|
|
/**
|
|
* Returns true if the slot currently contains a key and a value.
|
|
*/
|
|
bool is_occupied() const
|
|
{
|
|
return state_ == Occupied;
|
|
}
|
|
|
|
/**
|
|
* Returns true if the slot is empty, i.e. it does not contain a key and is not in removed state.
|
|
*/
|
|
bool is_empty() const
|
|
{
|
|
return state_ == Empty;
|
|
}
|
|
|
|
/**
|
|
* Returns the hash of the currently stored key. In this simple map slot implementation, we just
|
|
* computed the hash here. Other implementations might store the hash in the slot instead.
|
|
*/
|
|
template<typename Hash> uint32_t get_hash(const Hash &hash)
|
|
{
|
|
BLI_assert(this->is_occupied());
|
|
return hash(*key_buffer_);
|
|
}
|
|
|
|
/**
|
|
* Move the other slot into this slot and destruct it. We do destruction here, because this way
|
|
* we can avoid a comparison with the state, since we know the slot is occupied.
|
|
*/
|
|
void relocate_occupied_here(SimpleMapSlot &other, uint32_t UNUSED(hash))
|
|
{
|
|
BLI_assert(!this->is_occupied());
|
|
BLI_assert(other.is_occupied());
|
|
state_ = Occupied;
|
|
new (&key_buffer_) Key(std::move(*other.key_buffer_));
|
|
new (&value_buffer_) Value(std::move(*other.value_buffer_));
|
|
other.key_buffer_.ref().~Key();
|
|
other.value_buffer_.ref().~Value();
|
|
}
|
|
|
|
/**
|
|
* Returns true, when this slot is occupied and contains a key that compares equal to the given
|
|
* key. The hash can be used by other slot implementations to determine inequality faster.
|
|
*/
|
|
template<typename ForwardKey, typename IsEqual>
|
|
bool contains(const ForwardKey &key, const IsEqual &is_equal, uint32_t UNUSED(hash)) const
|
|
{
|
|
if (state_ == Occupied) {
|
|
return is_equal(key, *key_buffer_);
|
|
}
|
|
return false;
|
|
}
|
|
|
|
/**
|
|
* Change the state of this slot from empty/removed to occupied. The key/value has to be
|
|
* constructed by calling the constructor with the given key/value as parameter.
|
|
*/
|
|
template<typename ForwardKey, typename ForwardValue>
|
|
void occupy(ForwardKey &&key, ForwardValue &&value, uint32_t hash)
|
|
{
|
|
BLI_assert(!this->is_occupied());
|
|
this->occupy_without_value(std::forward<ForwardKey>(key), hash);
|
|
new (&value_buffer_) Value(std::forward<ForwardValue>(value));
|
|
}
|
|
|
|
/**
|
|
* Change the state of this slot from empty/removed to occupied, but leave the value
|
|
* uninitialized. The caller is responsible to construct the value afterwards.
|
|
*/
|
|
template<typename ForwardKey> void occupy_without_value(ForwardKey &&key, uint32_t UNUSED(hash))
|
|
{
|
|
BLI_assert(!this->is_occupied());
|
|
state_ = Occupied;
|
|
new (&key_buffer_) Key(std::forward<ForwardKey>(key));
|
|
}
|
|
|
|
/**
|
|
* Change the state of this slot from occupied to removed. The key and value have to be
|
|
* destructed as well.
|
|
*/
|
|
void remove()
|
|
{
|
|
BLI_assert(this->is_occupied());
|
|
state_ = Removed;
|
|
key_buffer_.ref().~Key();
|
|
value_buffer_.ref().~Value();
|
|
}
|
|
};
|
|
|
|
/**
|
|
* An IntrusiveMapSlot 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 Value, typename KeyInfo> class IntrusiveMapSlot {
|
|
private:
|
|
Key key_ = KeyInfo::get_empty();
|
|
TypedBuffer<Value> value_buffer_;
|
|
|
|
public:
|
|
IntrusiveMapSlot() = default;
|
|
|
|
~IntrusiveMapSlot()
|
|
{
|
|
if (KeyInfo::is_not_empty_or_removed(key_)) {
|
|
value_buffer_.ref().~Value();
|
|
}
|
|
}
|
|
|
|
IntrusiveMapSlot(const IntrusiveMapSlot &other) : key_(other.key_)
|
|
{
|
|
if (KeyInfo::is_not_empty_or_removed(key_)) {
|
|
new (&value_buffer_) Value(*other.value_buffer_);
|
|
}
|
|
}
|
|
|
|
IntrusiveMapSlot(IntrusiveMapSlot &&other) noexcept : key_(other.key_)
|
|
{
|
|
if (KeyInfo::is_not_empty_or_removed(key_)) {
|
|
new (&value_buffer_) Value(std::move(*other.value_buffer_));
|
|
}
|
|
}
|
|
|
|
Key *key()
|
|
{
|
|
return &key_;
|
|
}
|
|
|
|
const Key *key() const
|
|
{
|
|
return &key_;
|
|
}
|
|
|
|
Value *value()
|
|
{
|
|
return value_buffer_;
|
|
}
|
|
|
|
const Value *value() const
|
|
{
|
|
return value_buffer_;
|
|
}
|
|
|
|
bool is_occupied() const
|
|
{
|
|
return KeyInfo::is_not_empty_or_removed(key_);
|
|
}
|
|
|
|
bool is_empty() const
|
|
{
|
|
return KeyInfo::is_empty(key_);
|
|
}
|
|
|
|
template<typename Hash> uint32_t get_hash(const Hash &hash)
|
|
{
|
|
BLI_assert(this->is_occupied());
|
|
return hash(key_);
|
|
}
|
|
|
|
void relocate_occupied_here(IntrusiveMapSlot &other, uint32_t UNUSED(hash))
|
|
{
|
|
BLI_assert(!this->is_occupied());
|
|
BLI_assert(other.is_occupied());
|
|
key_ = std::move(other.key_);
|
|
new (&value_buffer_) Value(std::move(*other.value_buffer_));
|
|
other.key_.~Key();
|
|
other.value_buffer_.ref().~Value();
|
|
}
|
|
|
|
template<typename ForwardKey, typename IsEqual>
|
|
bool contains(const ForwardKey &key, const IsEqual &is_equal, uint32_t UNUSED(hash)) const
|
|
{
|
|
BLI_assert(KeyInfo::is_not_empty_or_removed(key));
|
|
return is_equal(key, key_);
|
|
}
|
|
|
|
template<typename ForwardKey, typename ForwardValue>
|
|
void occupy(ForwardKey &&key, ForwardValue &&value, uint32_t hash)
|
|
{
|
|
BLI_assert(!this->is_occupied());
|
|
BLI_assert(KeyInfo::is_not_empty_or_removed(key));
|
|
this->occupy_without_value(std::forward<ForwardKey>(key), hash);
|
|
new (&value_buffer_) Value(std::forward<ForwardValue>(value));
|
|
}
|
|
|
|
template<typename ForwardKey> void occupy_without_value(ForwardKey &&key, uint32_t UNUSED(hash))
|
|
{
|
|
BLI_assert(!this->is_occupied());
|
|
BLI_assert(KeyInfo::is_not_empty_or_removed(key));
|
|
key_ = std::forward<ForwardKey>(key);
|
|
}
|
|
|
|
void remove()
|
|
{
|
|
BLI_assert(this->is_occupied());
|
|
KeyInfo::remove(key_);
|
|
value_buffer_.ref().~Value();
|
|
}
|
|
};
|
|
|
|
template<typename Key, typename Value> struct DefaultMapSlot;
|
|
|
|
/**
|
|
* Use SimpleMapSlot by default, because it is the smallest slot type, that works for all keys.
|
|
*/
|
|
template<typename Key, typename Value> struct DefaultMapSlot {
|
|
using type = SimpleMapSlot<Key, Value>;
|
|
};
|
|
|
|
/**
|
|
* 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, typename Value> struct DefaultMapSlot<Key *, Value> {
|
|
using type = IntrusiveMapSlot<Key *, Value, PointerKeyInfo<Key *>>;
|
|
};
|
|
|
|
} // namespace blender
|
|
|
|
#endif /* __BLI_MAP_SLOTS_HH__ */
|