Blender V4.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
5#pragma once
6
7#include "BLI_bit_vector.hh"
8
9namespace blender::bits {
10
18template<int64_t InlineBufferCapacity = 64, typename Allocator = GuardedAllocator>
20 private:
24 int64_t group_size_ = 0;
29 int64_t aligned_group_size_ = 0;
31
32 static int64_t align_group_size(const int64_t group_size)
33 {
34 if (group_size < 64) {
35 /* Align to next power of two so that a single group never spans across two ints. */
36 return power_of_2_max(group_size);
37 }
38 /* Align to multiple of BitsPerInt. */
39 return (group_size + BitsPerInt - 1) & ~(BitsPerInt - 1);
40 }
41
42 public:
43 BitGroupVector(Allocator allocator = {}) noexcept : data_(allocator) {}
44
45 BitGroupVector(NoExceptConstructor, Allocator allocator = {}) noexcept
46 : BitGroupVector(allocator)
47 {
48 }
49
50 BitGroupVector(const int64_t size_in_groups,
51 const int64_t group_size,
52 const bool value = false,
53 Allocator allocator = {})
54 : group_size_(group_size),
55 aligned_group_size_(align_group_size(group_size)),
56 data_(size_in_groups * aligned_group_size_, value, allocator)
57 {
59 BLI_assert(size_in_groups >= 0);
60 }
61
63 : group_size_(other.group_size_),
64 aligned_group_size_(other.aligned_group_size_),
65 data_(other.data_)
66 {
67 }
68
70 : group_size_(other.group_size_),
71 aligned_group_size_(other.aligned_group_size_),
72 data_(std::move(other.data_))
73 {
74 }
75
77 {
78 return copy_assign_container(*this, other);
79 }
80
82 {
83 return move_assign_container(*this, std::move(other));
84 }
85
88 {
89 const int64_t offset = aligned_group_size_ * i;
90 return {data_.data() + (offset >> BitToIntIndexShift),
91 IndexRange(offset & BitIndexMask, group_size_)};
92 }
93
96 {
97 const int64_t offset = aligned_group_size_ * i;
98 return {data_.data() + (offset >> BitToIntIndexShift),
99 IndexRange(offset & BitIndexMask, group_size_)};
100 }
101
103 int64_t size() const
104 {
105 return aligned_group_size_ == 0 ? 0 : data_.size() / aligned_group_size_;
106 }
107
108 bool is_empty() const
109 {
110 return this->size() == 0;
111 }
112
115 {
116 return group_size_;
117 }
118
120 {
121 return IndexRange{this->size()};
122 }
123
129 {
130 return data_;
131 }
132
134 {
135 return data_;
136 }
137};
138
139} // namespace blender::bits
140
141namespace blender {
142using bits::BitGroupVector;
143}
#define BLI_assert(a)
Definition BLI_assert.h:50
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
static constexpr BitInt BitIndexMask
static constexpr int64_t BitsPerInt
static constexpr int64_t BitToIntIndexShift
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 >)
__int64 int64_t
Definition stdint.h:89