Workspace.h
Go to the documentation of this file.
1 /* Copyright 2020 CNRS-AIST JRL */
2 
3 #pragma once
4 
5 #include <Eigen/Core>
6 
7 #include <jrl-qp/api.h>
8 
9 namespace jrl::qp::internal
10 {
12 struct NotConst
13 {
14 };
15 
17 template<typename Scalar = double>
18 class Workspace
19 {
20 public:
22  Workspace() : buffer_(0) {}
27  Workspace(int size) : buffer_(size) {}
29  Workspace(int rows, int cols) : Workspace(rows * cols) {}
30 
37  void resize(int size, bool fit = false)
38  {
39  if(size > buffer_.size() || (size < buffer_.size() && fit))
40  {
41  buffer_.resize(size);
42  }
43  }
44 
46  int size() const
47  {
48  return static_cast<int>(buffer_.size());
49  }
50 
52  void resize(int rows, int cols, bool fit = false)
53  {
54  resize(rows * cols, fit);
55  }
56 
62  void changeLd(int rows, int cols, int oldLd, int newLd)
63  {
64  if(oldLd < newLd)
65  {
66  assert(cols * newLd < buffer_.size());
67  for(int i = cols - 1; i > 0; --i)
68  {
69  void * src = reinterpret_cast<void *>(&buffer_[i * oldLd]);
70  void * dst = reinterpret_cast<void *>(&buffer_[i * newLd]);
71  memmove(dst, src, rows * sizeof(double));
72  }
73  }
74  else if(oldLd > newLd)
75  {
76  for(int i = 1; i < cols; ++i)
77  {
78  void * src = reinterpret_cast<void *>(&buffer_[i * oldLd]);
79  void * dst = reinterpret_cast<void *>(&buffer_[i * newLd]);
80  memmove(dst, src, rows * sizeof(double));
81  }
82  }
83  }
84 
88  auto asVector(int size, NotConst = {})
89  {
90  assert(size <= buffer_.size());
91  return buffer_.head(size);
92  }
93 
97  auto asVector(int size) const
98  {
99  assert(size <= buffer_.size());
100  return buffer_.head(size);
101  }
102 
114  auto asMatrix(int rows, int cols, int ld, NotConst = {})
115  {
116  assert(ld * cols <= buffer_.size());
117  assert(ld >= rows);
118  return Eigen::Map<Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>, 0, Eigen::Stride<Eigen::Dynamic, 1>>(
119  buffer_.data(), rows, cols, Eigen::Stride<Eigen::Dynamic, 1>(ld, 1));
120  }
121 
126  auto asMatrix(int rows, int cols, int ld) const
127  {
128  assert(ld * cols <= buffer_.size());
129  assert(ld >= rows);
130  return Eigen::Map<const Eigen::Matrix<Scalar, Eigen::Dynamic, Eigen::Dynamic>, 0, Eigen::Stride<Eigen::Dynamic, 1>>(
131  buffer_.data(), rows, cols, Eigen::Stride<Eigen::Dynamic, 1>(ld, 1));
132  }
133 
135  void setZero()
136  {
137  buffer_.setZero();
138  }
139 
140 private:
141  Eigen::Matrix<Scalar, Eigen::Dynamic, 1> buffer_;
142 };
143 } // namespace jrl::qp::internal
144 
145 namespace jrl::qp
146 {
148 using WVector = decltype(internal::Workspace<double>().asVector(0));
150 using WConstVector = decltype(std::add_const_t<internal::Workspace<double>>().asVector(0));
151 } // namespace jrl::qp
jrl::qp::internal::Workspace
Definition: Workspace.h:18
jrl::qp::internal::Workspace::asVector
auto asVector(int size, NotConst={})
Definition: Workspace.h:88
jrl::qp::internal::Workspace::asMatrix
auto asMatrix(int rows, int cols, int ld) const
Definition: Workspace.h:126
jrl::qp::internal::Workspace::changeLd
void changeLd(int rows, int cols, int oldLd, int newLd)
Definition: Workspace.h:62
jrl::qp::internal::Workspace::Workspace
Workspace()
Definition: Workspace.h:22
jrl::qp::internal::Workspace::asVector
auto asVector(int size) const
Definition: Workspace.h:97
jrl::qp::internal::Workspace::Workspace
Workspace(int rows, int cols)
Definition: Workspace.h:29
jrl::qp::internal
Definition: ActiveSet.h:11
jrl::qp::internal::NotConst
Definition: Workspace.h:12
jrl::qp
Definition: blockArrowLLT.h:12
jrl::qp::WConstVector
decltype(std::add_const_t< internal::Workspace< double > >().asVector(0)) WConstVector
Definition: Workspace.h:150
jrl::qp::WVector
decltype(internal::Workspace< double >().asVector(0)) WVector
Definition: Workspace.h:148
jrl::qp::internal::Workspace::resize
void resize(int rows, int cols, bool fit=false)
Definition: Workspace.h:52
jrl::qp::internal::Workspace::asMatrix
auto asMatrix(int rows, int cols, int ld, NotConst={})
Definition: Workspace.h:114
jrl::qp::internal::Workspace::resize
void resize(int size, bool fit=false)
Definition: Workspace.h:37
jrl::qp::internal::Workspace::Workspace
Workspace(int size)
Definition: Workspace.h:27
jrl::qp::internal::Workspace::size
int size() const
Definition: Workspace.h:46
jrl::qp::internal::Workspace::setZero
void setZero()
Definition: Workspace.h:135
api.h