Blender V5.0
BLI_vector_set_slots.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2023 Blender Authors
2 *
3 * SPDX-License-Identifier: GPL-2.0-or-later */
4
5#pragma once
6
26
27#include <cstdint>
28#include <type_traits>
29
30namespace blender {
31
36template<typename Key, typename IndexT = int64_t> class SimpleVectorSetSlot {
37 static_assert(std::is_integral_v<IndexT> && std::is_signed_v<IndexT>);
38
39 private:
40#define s_is_empty -1
41#define s_is_removed -2
42
46 IndexT state_ = s_is_empty;
47
48 public:
52 bool is_occupied() const
53 {
54 return state_ >= 0;
55 }
56
60 bool is_empty() const
61 {
62 return state_ == s_is_empty;
63 }
64
68 IndexT index() const
69 {
70 BLI_assert(this->is_occupied());
71 return state_;
72 }
73
78 template<typename ForwardKey, typename IsEqual>
79 bool contains(const ForwardKey &key,
80 const IsEqual &is_equal,
81 uint64_t /*hash*/,
82 const Key *keys) const
83 {
84 if (state_ >= 0) {
85 return is_equal(key, keys[state_]);
86 }
87 return false;
88 }
89
94 void occupy(IndexT index, uint64_t /*hash*/)
95 {
96 BLI_assert(!this->is_occupied());
97 state_ = index;
98 }
99
104 void update_index(IndexT index)
105 {
106 BLI_assert(this->is_occupied());
107 state_ = index;
108 }
109
113 void remove()
114 {
115 BLI_assert(this->is_occupied());
116 state_ = s_is_removed;
117 }
118
122 bool has_index(IndexT index) const
123 {
124 return state_ == index;
125 }
126
131 template<typename Hash> uint64_t get_hash(const Key &key, const Hash &hash) const
132 {
133 BLI_assert(this->is_occupied());
134 return hash(key);
135 }
136
137#undef s_is_empty
138#undef s_is_removed
139};
140
141template<typename Key> struct DefaultVectorSetSlot;
142
143template<typename Key> struct DefaultVectorSetSlot {
145};
146
147} // namespace blender
#define BLI_assert(a)
Definition BLI_assert.h:46
#define s_is_removed
#define s_is_empty
unsigned long long int uint64_t
bool contains(const ForwardKey &key, const IsEqual &is_equal, uint64_t, const Key *keys) const
bool has_index(IndexT index) const
void occupy(IndexT index, uint64_t)
uint64_t get_hash(const Key &key, const Hash &hash) const
#define hash
Definition noise_c.cc:154
SimpleVectorSetSlot< Key > type