Blender V5.0
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_vector.hh"
12
13namespace blender::bits {
14
22template<int64_t InlineBufferCapacity = 64, typename Allocator = GuardedAllocator>
24 private:
28 int64_t group_size_ = 0;
33 int64_t aligned_group_size_ = 0;
35
36 static int64_t align_group_size(const int64_t group_size)
37 {
38 if (group_size < 64) {
39 /* Align to next power of two so that a single group never spans across two ints. */
40 return power_of_2_max(group_size);
41 }
42 /* Align to multiple of BitsPerInt. */
43 return (group_size + BitsPerInt - 1) & ~(BitsPerInt - 1);
44 }
45
46 public:
47 BitGroupVector(Allocator allocator = {}) noexcept : data_(allocator) {}
48
49 BitGroupVector(NoExceptConstructor, Allocator allocator = {}) noexcept
50 : BitGroupVector(allocator)
51 {
52 }
53
54 BitGroupVector(const int64_t size_in_groups,
55 const int64_t group_size,
56 const bool value = false,
57 Allocator allocator = {})
58 : group_size_(group_size),
59 aligned_group_size_(align_group_size(group_size)),
60 data_(size_in_groups * aligned_group_size_, value, allocator)
61 {
63 BLI_assert(size_in_groups >= 0);
64 }
65
67 : group_size_(other.group_size_),
68 aligned_group_size_(other.aligned_group_size_),
69 data_(other.data_)
70 {
71 }
72
74 : group_size_(other.group_size_),
75 aligned_group_size_(other.aligned_group_size_),
76 data_(std::move(other.data_))
77 {
78 }
79
81 {
82 return copy_assign_container(*this, other);
83 }
84
86 {
87 return move_assign_container(*this, std::move(other));
88 }
89
92 {
93 const int64_t offset = aligned_group_size_ * i;
94 return {data_.data() + (offset >> BitToIntIndexShift),
95 IndexRange(offset & BitIndexMask, group_size_)};
96 }
97
100 {
101 const int64_t offset = aligned_group_size_ * i;
102 return {data_.data() + (offset >> BitToIntIndexShift),
103 IndexRange(offset & BitIndexMask, group_size_)};
104 }
105
107 int64_t size() const
108 {
109 return aligned_group_size_ == 0 ? 0 : data_.size() / aligned_group_size_;
110 }
111
112 bool is_empty() const
113 {
114 return this->size() == 0;
115 }
116
119 {
120 return group_size_;
121 }
122
124 {
125 return IndexRange{this->size()};
126 }
127
133 {
134 return data_;
135 }
136
138 {
139 return data_;
140 }
141};
142
143} // namespace blender::bits
144
145namespace blender {
146using bits::BitGroupVector;
147}
#define BLI_assert(a)
Definition BLI_assert.h:46
long long int int64_t
BitGroupVector(Allocator allocator={}) noexcept
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 >)
i
Definition text_draw.cc:230