Blender V5.3
BLI_bit_group_vector.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
8
9#pragma once
10
11#include "BLI_bit_span_ops.hh"
12#include "BLI_bit_vector.hh"
13
14namespace blender {
15
16namespace bits {
17
25template<int64_t InlineBufferCapacity = 64, typename Allocator = GuardedAllocator>
27 private:
31 int64_t group_size_ = 0;
36 int64_t aligned_group_size_ = 0;
38
39 static int64_t align_group_size(const int64_t group_size)
40 {
41 if (group_size < 64) {
42 /* Align to next power of two so that a single group never spans across two ints. */
44 }
45 /* Align to multiple of BitsPerInt. */
46 return (group_size + BitsPerInt - 1) & ~(BitsPerInt - 1);
47 }
48
49 public:
50 BitGroupVector(Allocator allocator = {}) noexcept : data_(allocator) {}
51
52 BitGroupVector(NoExceptConstructor, Allocator allocator = {}) noexcept
53 : BitGroupVector(allocator)
54 {
55 }
56
57 BitGroupVector(const int64_t size_in_groups,
58 const int64_t group_size,
59 const bool value = false,
60 Allocator allocator = {})
61 : group_size_(group_size),
62 aligned_group_size_(align_group_size(group_size)),
63 data_(size_in_groups * aligned_group_size_, value, allocator)
64 {
66 BLI_assert(size_in_groups >= 0);
67 }
68
70 : group_size_(other.group_size_),
71 aligned_group_size_(other.aligned_group_size_),
72 data_(other.data_)
73 {
74 }
75
77 : group_size_(other.group_size_),
78 aligned_group_size_(other.aligned_group_size_),
79 data_(std::move(other.data_))
80 {
81 }
82
84 {
85 return copy_assign_container(*this, other);
86 }
87
89 {
90 return move_assign_container(*this, std::move(other));
91 }
92
95 {
96 BLI_assert(this->index_range().contains(i));
97 const int64_t offset = aligned_group_size_ * i;
98 return {data_.data() + (offset >> BitToIntIndexShift),
99 IndexRange(offset & BitIndexMask, group_size_)};
100 }
101
104 {
105 BLI_assert(this->index_range().contains(i));
106 const int64_t offset = aligned_group_size_ * i;
107 return {data_.data() + (offset >> BitToIntIndexShift),
108 IndexRange(offset & BitIndexMask, group_size_)};
109 }
110
112 int64_t size() const
113 {
114 return aligned_group_size_ == 0 ? 0 : data_.size() / aligned_group_size_;
115 }
116
117 bool is_empty() const
118 {
119 return this->size() == 0;
120 }
121
124 {
125 return group_size_;
126 }
127
129 {
130 return IndexRange{this->size()};
131 }
132
138 {
139 return data_;
140 }
141
143 {
144 return data_;
145 }
146
151 {
152 /* This can still be optimized due to the additional knowledge we have how consecutive groups
153 * are laid out in memory. It is possible to updated multiple small groups at once. */
154 BLI_assert(bits.size() == group_size_);
155 for (const int64_t i : this->index_range()) {
156 MutableBoundedBitSpan group = (*this)[i];
157 group &= bits;
158 }
159 }
160};
161
162} // namespace bits
163
164using bits::BitGroupVector;
165
166} // namespace blender
BitGroupVector(Allocator allocator={}) noexcept
constexpr IndexRange()=default
void foreach_and(const BoundedBitSpan bits)
MutableBoundedBitSpan operator[](const int64_t i)
BitGroupVector & operator=(const BitGroupVector &other)
BitGroupVector(Allocator allocator={}) noexcept
BitGroupVector(NoExceptConstructor, Allocator allocator={}) noexcept
BitGroupVector(const int64_t size_in_groups, const int64_t group_size, const bool value=false, Allocator allocator={})
BitGroupVector(const BitGroupVector &other)
BitGroupVector & operator=(BitGroupVector &&other)
BitGroupVector(BitGroupVector &&other)
BoundedBitSpan operator[](const int64_t i) const
BMesh Iterators.
BLI_assert(slot->slot_subtype.map==BMO_OP_SLOT_SUBTYPE_MAP_FLT)
__int64 int64_t
Definition BKE_text.h:90
constexpr int64_t power_of_2_max(const int64_t x)
Container & copy_assign_container(Container &dst, const Container &src)
Container & move_assign_container(Container &dst, Container &&src) noexcept(std::is_nothrow_move_constructible_v< Container >)