21template <
typename MatrixType,
int Alignment = Eigen::AlignedMax>
24template <
typename MatrixType,
int Alignment>
25class ArenaMatrix :
public Eigen::Map<std::decay_t<MatrixType>, Alignment> {
27 using Scalar =
typename std::decay_t<MatrixType>::Scalar;
29 using Base = Eigen::Map<PlainObject, Alignment>;
48 , m_allocator(allocator)
56 : Base::Map(_allocate(cols * rows, allocator), cols, rows)
57 , m_allocator(allocator)
58 , m_alloc_size(cols * rows) {}
61 : Base::Map(_allocate(size, allocator), size)
62 , m_allocator(allocator)
63 , m_alloc_size(size) {}
65 template <
typename D, enable_if_eigen_t<D> * =
nullptr>
67 : Base::Map(_allocate(other.size(), alloc), get_rows(other),
70 , m_alloc_size(other.size()) {
71 Base::operator=(other);
78 Base::operator=(other);
82 : Base::Map(other.data(), get_rows(other), get_cols(other))
83 , m_allocator(other.m_allocator)
84 , m_alloc_size(other.m_alloc_size) {
85 other.m_data =
nullptr;
86 other.m_alloc_size = 0;
92 if (m_allocator == other.m_allocator) {
94 new (this) Base(other.data(), get_rows(other), get_cols(other));
95 m_alloc_size = other.m_alloc_size;
98 this->resize(get_rows(other), get_cols(other));
99 Base::operator=(other);
101 other.m_data =
nullptr;
102 other.m_alloc_size = 0;
105 template <
typename D, enable_if_eigen_t<D> * =
nullptr>
107 this->
resize(get_rows(other), get_cols(other));
108 Base::operator=(other);
116 this->
resize(get_rows(other), get_cols(other));
117 Base::operator=(other);
121 using Base::operator=;
127 if (m_allocator == other.m_allocator) {
129 m_alloc_size = other.m_alloc_size;
130 new (
this)
Base(other.data(), get_rows(other), get_cols(other));
132 this->
resize(get_rows(other), get_cols(other));
133 Base::operator=(other);
135 other.m_data =
nullptr;
140 return ConstMapType(this->m_data, this->rows(), this->cols());
147 if (m_alloc_size != size) {
149 Scalar *p = _allocate(size, m_allocator);
151 new (
this)
Base(p, size);
154 new (
this)
Base(this->m_data, size);
160 const Index size = rows * cols;
161 if (m_alloc_size != size) {
163 Scalar *p = _allocate(size, m_allocator);
165 new (
this)
Base(p, rows, cols);
168 new (
this)
Base(this->m_data, rows, cols);
174 if (m_alloc_size < size) {
175 Scalar *p = _allocate(size, m_allocator);
176 std::copy_n(this->m_data, m_alloc_size, p);
179 new (
this)
Base(p, size);
181 new (
this)
Base(this->m_data, size);
186 const Index size = rows * cols;
187 if (m_alloc_size < size) {
188 Scalar *p = _allocate(size, m_allocator);
189 std::copy_n(this->m_data, m_alloc_size, p);
192 new (
this)
Base(p, rows, cols);
194 new (
this)
Base(this->m_data, rows, cols);
205 *
this = MatrixType::Zero(newSize);
210 *
this = MatrixType::Zero(rows, cols);
214 using Base::setIdentity;
217 *
this = MatrixType::Identity(rows, cols);
221 template <
typename T>
constexpr auto get_rows(
const T &x) {
227 template <
typename T>
constexpr auto get_cols(
const T &x) {
235 return alloc.allocate<
Scalar>(size_t(size), Alignment);
240 m_allocator.deallocate(this->m_data,
size_t(this->size()), Alignment);
255struct traits<aligator::ArenaMatrix<T>> : traits<Eigen::Map<T>> {
256 using TraitsBase = traits<Eigen::Map<T>>;
257 using Scalar =
typename TraitsBase::Scalar;
258 using XprKind =
typename Eigen::internal::traits<std::decay_t<T>>::XprKind;
260 typename Eigen::internal::traits<std::decay_t<T>>::StorageKind;
261 static constexpr int RowsAtCompileTime =
262 Eigen::internal::traits<std::decay_t<T>>::RowsAtCompileTime;
263 static constexpr int ColsAtCompileTime =
264 Eigen::internal::traits<std::decay_t<T>>::ColsAtCompileTime;
allocator_type get_allocator() const noexcept
ArenaMatrix(const allocator_type &allocator)
ArenaMatrix & operator=(ArenaMatrix &&other)
ArenaMatrix & operator=(const D &other)
void resize(Index size)
Resize this matrix. This will reallocate.
void resize(Index rows, Index cols)
Resize this matrix. This will reallocate.
ArenaMatrix(ArenaMatrix &&other) noexcept
ArenaMatrix & setZero(Index rows, Index cols)
std::decay_t< MatrixXs > PlainObject
ArenaMatrix & setZero(Index newSize)
ArenaMatrix(Index cols, Index rows, const allocator_type &allocator={})
polymorphic_allocator allocator_type
ArenaMatrix(const D &other, const allocator_type &alloc={})
ArenaMatrix & setIdentity(Index rows, Index cols)
static constexpr int ColsAtCompileTime
ArenaMatrix & operator=(const ArenaMatrix &other)
static constexpr int RowsAtCompileTime
Eigen::Map< PlainObject, Alignment > Base
void conservativeResize(Index size)
Index allocatedSize() const noexcept
Current allocated size for this matrix. This is what allows resizing without reallocation.
ArenaMatrix(ArenaMatrix &&other, const allocator_type &alloc)
Extended move constructor.
ArenaMatrix(const ArenaMatrix &other, const allocator_type &alloc={})
Eigen::Map< const PlainObject, Alignment > ConstMapType
typename std::decay_t< MatrixXs >::Scalar Scalar
ArenaMatrix(Index size, const allocator_type &allocator={})
void conservativeResize(Index rows, Index cols)
A convenience subclass of std::pmr::polymorphic_allocator for bytes.
::aligator::context::Scalar Scalar