diff options
Diffstat (limited to 'source/blender/blenlib/BLI_map_slots.hh')
-rw-r--r-- | source/blender/blenlib/BLI_map_slots.hh | 144 |
1 files changed, 72 insertions, 72 deletions
diff --git a/source/blender/blenlib/BLI_map_slots.hh b/source/blender/blenlib/BLI_map_slots.hh index 9ea2c4cad89..b5360795a13 100644 --- a/source/blender/blenlib/BLI_map_slots.hh +++ b/source/blender/blenlib/BLI_map_slots.hh @@ -52,9 +52,9 @@ template<typename Key, typename Value> class SimpleMapSlot { Removed = 2, }; - State m_state; - AlignedBuffer<sizeof(Key), alignof(Key)> m_key_buffer; - AlignedBuffer<sizeof(Value), alignof(Value)> m_value_buffer; + State state_; + TypedBuffer<Key> key_buffer_; + TypedBuffer<Value> value_buffer_; public: /** @@ -62,7 +62,7 @@ template<typename Key, typename Value> class SimpleMapSlot { */ SimpleMapSlot() { - m_state = Empty; + state_ = Empty; } /** @@ -70,9 +70,9 @@ template<typename Key, typename Value> class SimpleMapSlot { */ ~SimpleMapSlot() { - if (m_state == Occupied) { - this->key()->~Key(); - this->value()->~Value(); + if (state_ == Occupied) { + key_buffer_.ref().~Key(); + value_buffer_.ref().~Value(); } } @@ -82,24 +82,24 @@ template<typename Key, typename Value> class SimpleMapSlot { */ SimpleMapSlot(const SimpleMapSlot &other) { - m_state = other.m_state; - if (other.m_state == Occupied) { - new (this->key()) Key(*other.key()); - new (this->value()) Value(*other.value()); + state_ = other.state_; + if (other.state_ == Occupied) { + new (&key_buffer_) Key(*other.key_buffer_); + new (&value_buffer_) Value(*other.value_buffer_); } } /** - * The move construtor has to copy the state. If the other slot was occupied, the key and value + * 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 { - m_state = other.m_state; - if (other.m_state == Occupied) { - new (this->key()) Key(std::move(*other.key())); - new (this->value()) Value(std::move(*other.value())); + 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_)); } } @@ -108,7 +108,7 @@ template<typename Key, typename Value> class SimpleMapSlot { */ Key *key() { - return (Key *)m_key_buffer.ptr(); + return key_buffer_; } /** @@ -116,7 +116,7 @@ template<typename Key, typename Value> class SimpleMapSlot { */ const Key *key() const { - return (const Key *)m_key_buffer.ptr(); + return key_buffer_; } /** @@ -124,7 +124,7 @@ template<typename Key, typename Value> class SimpleMapSlot { */ Value *value() { - return (Value *)m_value_buffer.ptr(); + return value_buffer_; } /** @@ -132,7 +132,7 @@ template<typename Key, typename Value> class SimpleMapSlot { */ const Value *value() const { - return (const Value *)m_value_buffer.ptr(); + return value_buffer_; } /** @@ -140,7 +140,7 @@ template<typename Key, typename Value> class SimpleMapSlot { */ bool is_occupied() const { - return m_state == Occupied; + return state_ == Occupied; } /** @@ -148,32 +148,32 @@ template<typename Key, typename Value> class SimpleMapSlot { */ bool is_empty() const { - return m_state == Empty; + 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) + template<typename Hash> uint64_t get_hash(const Hash &hash) { BLI_assert(this->is_occupied()); - return hash(*this->key()); + 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)) + void relocate_occupied_here(SimpleMapSlot &other, uint64_t UNUSED(hash)) { BLI_assert(!this->is_occupied()); BLI_assert(other.is_occupied()); - m_state = Occupied; - new (this->key()) Key(std::move(*other.key())); - new (this->value()) Value(std::move(*other.value())); - other.key()->~Key(); - other.value()->~Value(); + 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(); } /** @@ -181,10 +181,10 @@ template<typename Key, typename Value> class SimpleMapSlot { * 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 + bool contains(const ForwardKey &key, const IsEqual &is_equal, uint64_t UNUSED(hash)) const { - if (m_state == Occupied) { - return is_equal(key, *this->key()); + if (state_ == Occupied) { + return is_equal(key, *key_buffer_); } return false; } @@ -194,22 +194,22 @@ template<typename Key, typename Value> class SimpleMapSlot { * 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) + void occupy(ForwardKey &&key, ForwardValue &&value, uint64_t hash) { BLI_assert(!this->is_occupied()); this->occupy_without_value(std::forward<ForwardKey>(key), hash); - new (this->value()) Value(std::forward<ForwardValue>(value)); + 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)) + template<typename ForwardKey> void occupy_without_value(ForwardKey &&key, uint64_t UNUSED(hash)) { BLI_assert(!this->is_occupied()); - m_state = Occupied; - new (this->key()) Key(std::forward<ForwardKey>(key)); + state_ = Occupied; + new (&key_buffer_) Key(std::forward<ForwardKey>(key)); } /** @@ -219,9 +219,9 @@ template<typename Key, typename Value> class SimpleMapSlot { void remove() { BLI_assert(this->is_occupied()); - m_state = Removed; - this->key()->~Key(); - this->value()->~Value(); + state_ = Removed; + key_buffer_.ref().~Key(); + value_buffer_.ref().~Value(); } }; @@ -235,107 +235,107 @@ template<typename Key, typename Value> class SimpleMapSlot { */ template<typename Key, typename Value, typename KeyInfo> class IntrusiveMapSlot { private: - Key m_key = KeyInfo::get_empty(); - AlignedBuffer<sizeof(Value), alignof(Value)> m_value_buffer; + Key key_ = KeyInfo::get_empty(); + TypedBuffer<Value> value_buffer_; public: IntrusiveMapSlot() = default; ~IntrusiveMapSlot() { - if (KeyInfo::is_not_empty_or_removed(m_key)) { - this->value()->~Value(); + if (KeyInfo::is_not_empty_or_removed(key_)) { + value_buffer_.ref().~Value(); } } - IntrusiveMapSlot(const IntrusiveMapSlot &other) : m_key(other.m_key) + IntrusiveMapSlot(const IntrusiveMapSlot &other) : key_(other.key_) { - if (KeyInfo::is_not_empty_or_removed(m_key)) { - new (this->value()) Value(*other.value()); + if (KeyInfo::is_not_empty_or_removed(key_)) { + new (&value_buffer_) Value(*other.value_buffer_); } } - IntrusiveMapSlot(IntrusiveMapSlot &&other) noexcept : m_key(other.m_key) + IntrusiveMapSlot(IntrusiveMapSlot &&other) noexcept : key_(other.key_) { - if (KeyInfo::is_not_empty_or_removed(m_key)) { - new (this->value()) Value(std::move(*other.value())); + if (KeyInfo::is_not_empty_or_removed(key_)) { + new (&value_buffer_) Value(std::move(*other.value_buffer_)); } } Key *key() { - return &m_key; + return &key_; } const Key *key() const { - return &m_key; + return &key_; } Value *value() { - return (Value *)m_value_buffer.ptr(); + return value_buffer_; } const Value *value() const { - return (const Value *)m_value_buffer.ptr(); + return value_buffer_; } bool is_occupied() const { - return KeyInfo::is_not_empty_or_removed(m_key); + return KeyInfo::is_not_empty_or_removed(key_); } bool is_empty() const { - return KeyInfo::is_empty(m_key); + return KeyInfo::is_empty(key_); } - template<typename Hash> uint32_t get_hash(const Hash &hash) + template<typename Hash> uint64_t get_hash(const Hash &hash) { BLI_assert(this->is_occupied()); - return hash(*this->key()); + return hash(key_); } - void relocate_occupied_here(IntrusiveMapSlot &other, uint32_t UNUSED(hash)) + void relocate_occupied_here(IntrusiveMapSlot &other, uint64_t UNUSED(hash)) { BLI_assert(!this->is_occupied()); BLI_assert(other.is_occupied()); - m_key = std::move(other.m_key); - new (this->value()) Value(std::move(*other.value())); - other.m_key.~Key(); - other.value()->~Value(); + 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 + bool contains(const ForwardKey &key, const IsEqual &is_equal, uint64_t UNUSED(hash)) const { BLI_assert(KeyInfo::is_not_empty_or_removed(key)); - return is_equal(key, m_key); + return is_equal(key, key_); } template<typename ForwardKey, typename ForwardValue> - void occupy(ForwardKey &&key, ForwardValue &&value, uint32_t hash) + void occupy(ForwardKey &&key, ForwardValue &&value, uint64_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 (this->value()) Value(std::forward<ForwardValue>(value)); + new (&value_buffer_) Value(std::forward<ForwardValue>(value)); } - template<typename ForwardKey> void occupy_without_value(ForwardKey &&key, uint32_t UNUSED(hash)) + template<typename ForwardKey> void occupy_without_value(ForwardKey &&key, uint64_t UNUSED(hash)) { BLI_assert(!this->is_occupied()); BLI_assert(KeyInfo::is_not_empty_or_removed(key)); - m_key = std::forward<ForwardKey>(key); + key_ = std::forward<ForwardKey>(key); } void remove() { BLI_assert(this->is_occupied()); - KeyInfo::remove(m_key); - this->value()->~Value(); + KeyInfo::remove(key_); + value_buffer_.ref().~Value(); } }; |