Blender V5.3
vector.hh
Go to the documentation of this file.
1/* SPDX-FileCopyrightText: 2026 Clement Foucault
2 *
3 * SPDX-License-Identifier: MIT */
4
5#pragma once
6
7#include <cassert>
8#include <cstring>
9#include <memory>
10#ifndef NDEBUG
11# include <span>
12#endif
13
14namespace lexit {
15
16template<typename T> class AlignedArrayPtr {
17 private:
18 T *ptr = nullptr;
19
20 public:
21 AlignedArrayPtr() = default;
22
24 {
25 ptr = static_cast<T *>(operator new[](sizeof(T) * size, std::align_val_t{64}));
26 }
27
28 AlignedArrayPtr(const AlignedArrayPtr &other) = delete;
29
30 AlignedArrayPtr(AlignedArrayPtr &&other) : ptr(other.ptr)
31 {
32 other.ptr = nullptr;
33 }
34
36 {
37 operator delete[](ptr, std::align_val_t{64});
38 }
39
41 {
42 if (this != &other) {
43 operator delete[](ptr, std::align_val_t{64});
44 ptr = other.ptr;
45 other.ptr = nullptr;
46 }
47 return *this;
48 }
49
50 T *get()
51 {
52 return ptr;
53 }
54 const T *get() const
55 {
56 return ptr;
57 }
58 T &operator[](int i)
59 {
60 return ptr[i];
61 }
62 const T &operator[](int i) const
63 {
64 return ptr[i];
65 }
66};
67
68template<typename T> struct Vector {
69 private:
70 std::unique_ptr<T[]> data_;
71 int size_ = 0;
72 int alloc_size_ = 0;
73#ifndef NDEBUG
74 std::span<T> debug_view_;
75#endif
76
77 public:
78 void reserve(int new_size)
79 {
80 if ((alloc_size_ >= new_size) && data_) {
81 return;
82 }
83 std::unique_ptr<T[]> new_ptr(new T[new_size]);
84 if (data_) {
85 std::memcpy(new_ptr.get(), data_.get(), alloc_size_ * sizeof(T));
86 }
87 data_ = std::move(new_ptr);
88 alloc_size_ = new_size;
89#ifndef NDEBUG
90 debug_view_ = std::span<T>(data_.get(), size_);
91#endif
92 }
93
94 void resize(int new_size)
95 {
96 reserve(new_size);
97 size_ = new_size;
98#ifndef NDEBUG
99 debug_view_ = std::span<T>(data_.get(), size_);
100#endif
101 }
102
104 {
105 return data_.get();
106 }
107 const T *data() const
108 {
109 return data_.get();
110 }
111
112 T &operator[](int i)
113 {
114 return data_.get()[i];
115 }
116 const T &operator[](int i) const
117 {
118 return data_.get()[i];
119 }
120
122 {
123 return data_.get() + size_;
124 }
125 const T *end() const
126 {
127 return data_.get() + size_;
128 }
129
130 int size() const
131 {
132 return size_;
133 }
134
135 void increase_size_by_unchecked(int elem_count)
136 {
137 assert(size_ + elem_count <= alloc_size_);
138 size_ += elem_count;
139#ifndef NDEBUG
140 debug_view_ = std::span<T>(data_.get(), size_);
141#endif
142 }
143};
144
145} // namespace lexit
T & operator[](int i)
Definition vector.hh:58
AlignedArrayPtr(int size)
Definition vector.hh:23
AlignedArrayPtr(AlignedArrayPtr &&other)
Definition vector.hh:30
const T & operator[](int i) const
Definition vector.hh:62
AlignedArrayPtr(const AlignedArrayPtr &other)=delete
AlignedArrayPtr & operator=(AlignedArrayPtr &&other)
Definition vector.hh:40
const T * get() const
Definition vector.hh:54
#define assert(assertion)
#define T
const T * end() const
Definition vector.hh:125
void increase_size_by_unchecked(int elem_count)
Definition vector.hh:135
int size() const
Definition vector.hh:130
void reserve(int new_size)
Definition vector.hh:78
void resize(int new_size)
Definition vector.hh:94
const T * data() const
Definition vector.hh:107
T & operator[](int i)
Definition vector.hh:112
const T & operator[](int i) const
Definition vector.hh:116