Skip to content

Commit

Permalink
Merge pull request #1029 from mrapp-ke/base-view
Browse files Browse the repository at this point in the history
Add class BaseView
  • Loading branch information
michael-rapp authored Aug 16, 2024
2 parents bf0dc62 + b2f6d4d commit 01bed30
Show file tree
Hide file tree
Showing 4 changed files with 282 additions and 247 deletions.
79 changes: 57 additions & 22 deletions cpp/subprojects/common/include/mlrl/common/data/view.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
* @tparam T The type of the values, the view provides access to
*/
template<typename T>
class MLRLCOMMON_API View {
class MLRLCOMMON_API BaseView {
public:

/**
Expand All @@ -29,41 +29,31 @@ class MLRLCOMMON_API View {
* provide access to
* @param dimensions The number of elements in each dimension of the view
*/
View(T* array, std::initializer_list<uint32> dimensions) : array(array) {}
BaseView(T* array, std::initializer_list<uint32> dimensions) : array(array) {}

/**
* @param array A pointer to an array of template type `T` that stores the values, the view should provide
* access to
*/
explicit View(T* array) : array(array) {}
explicit BaseView(T* array) : array(array) {}

/**
* @param other A const reference to an object of type `View` that should be copied
*/
View(const View<T>& other) : array(other.array) {}
BaseView(const BaseView<T>& other) : array(other.array) {}

/**
* @param other A reference to an object of type `View` that should be moved
*/
View(View<T>&& other) : array(other.array) {}
BaseView(BaseView<T>&& other) : array(other.array) {}

virtual ~View() {}
virtual ~BaseView() {}

/**
* The type of the values, the view provides access to.
*/
typedef T value_type;

/**
* An iterator that provides read-only access to the elements in the view.
*/
typedef const value_type* const_iterator;

/**
* An iterator that provides access to the elements in the view and allows to modify them.
*/
typedef value_type* iterator;

/**
* Releases the ownership of the array that stores the values, the view provides access to. As a result, the
* behavior of this view becomes undefined and it should not be used anymore. The caller is responsible for
Expand All @@ -76,14 +66,59 @@ class MLRLCOMMON_API View {
array = nullptr;
return ptr;
}
};

/**
* A view that provides random access, as well as access via iterators, to values stored in a pre-allocated array.
*
* @tparam T The type of the values, the view provides access to
*/
template<typename T>
class MLRLCOMMON_API View : public BaseView<T> {
public:

/**
* @param array A pointer to an array of template type `T` that stores the values, the view should
* provide access to
* @param dimensions The number of elements in each dimension of the view
*/
View(T* array, std::initializer_list<uint32> dimensions) : BaseView<T>(array, dimensions) {}

/**
* @param array A pointer to an array of template type `T` that stores the values, the view should provide
* access to
*/
explicit View(T* array) : BaseView<T>(array) {}

/**
* @param other A const reference to an object of type `View` that should be copied
*/
View(const View<T>& other) : BaseView<T>(other) {}

/**
* @param other A reference to an object of type `View` that should be moved
*/
View(View<T>&& other) : BaseView<T>(std::move(other)) {}

virtual ~View() override {}

/**
* An iterator that provides read-only access to the elements in the view.
*/
typedef const typename BaseView<T>::value_type* const_iterator;

/**
* An iterator that provides access to the elements in the view and allows to modify them.
*/
typedef typename BaseView<T>::value_type* iterator;

/**
* Returns a `const_iterator` to the beginning of the view.
*
* @return A `const_iterator` to the beginning
*/
const_iterator cbegin() const {
return array;
return BaseView<T>::array;
}

/**
Expand All @@ -92,7 +127,7 @@ class MLRLCOMMON_API View {
* @return An `iterator` to the beginning
*/
iterator begin() {
return array;
return BaseView<T>::array;
}

/**
Expand All @@ -101,8 +136,8 @@ class MLRLCOMMON_API View {
* @param pos The position of the element
* @return A const reference to the specified element
*/
const value_type& operator[](uint32 pos) const {
return array[pos];
const typename BaseView<T>::value_type& operator[](uint32 pos) const {
return BaseView<T>::array[pos];
}

/**
Expand All @@ -111,8 +146,8 @@ class MLRLCOMMON_API View {
* @param pos The position of the element
* @return A reference to the specified element
*/
value_type& operator[](uint32 pos) {
return array[pos];
typename BaseView<T>::value_type& operator[](uint32 pos) {
return BaseView<T>::array[pos];
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,67 +14,45 @@
* @tparam T The type of the values, the view provides access to
*/
template<typename T>
class MLRLCOMMON_API DenseMatrix : public Matrix {
class MLRLCOMMON_API DenseMatrix : public BaseView<T>,
public Matrix {
public:

/**
* A pointer to the array that stores the values, the view provides access to.
*/
T* array;

/**
* @param array A pointer to an array of template type `T` that stores the values, the view should provide
* access to
* @param numRows The number of rows in the view
* @param numCols The number of columns in the view
*/
DenseMatrix(T* array, uint32 numRows, uint32 numCols) : Matrix(numRows, numCols), array(array) {}
DenseMatrix(T* array, uint32 numRows, uint32 numCols) : BaseView<T>(array), Matrix(numRows, numCols) {}

/**
* @param other A const reference to an object of type `DenseMatrix` that should be copied
*/
DenseMatrix(const DenseMatrix<T>& other) : Matrix(std::move(other)), array(other.array) {}
DenseMatrix(const DenseMatrix<T>& other) : BaseView<T>(other), Matrix(std::move(other)) {}

/**
* @param other A reference to an object of type `DenseMatrix` that should be moved
*/
DenseMatrix(DenseMatrix<T>&& other) : Matrix(std::move(other)), array(other.array) {}
DenseMatrix(DenseMatrix<T>&& other) : BaseView<T>(std::move(other)), Matrix(std::move(other)) {}

virtual ~DenseMatrix() override {}

/**
* The type of the values, the view provides access to.
*/
typedef T value_type;

/**
* An iterator that provides read-only access to the values in the view.
*/
typedef typename View<value_type>::const_iterator value_const_iterator;
typedef const typename BaseView<T>::value_type* value_const_iterator;

/**
* An iterator that provides access to the values in the view and allows to modify them.
*/
typedef typename View<value_type>::iterator value_iterator;
typedef typename BaseView<T>::value_type* value_iterator;

/**
* Sets all values stored in the matrix to zero.
*/
void clear() {
util::setViewToZeros(array, Matrix::numRows * Matrix::numCols);
}

/**
* Releases the ownership of the array that stores the values, the view provides access to. As a result, the
* behavior of this view becomes undefined and it should not be used anymore. The caller is responsible for
* freeing the memory that is occupied by the array.
*
* @return A pointer to the array that stores the values, the view provides access to
*/
value_type* release() {
value_type* ptr = array;
array = nullptr;
return ptr;
util::setViewToZeros(BaseView<T>::array, Matrix::numRows * Matrix::numCols);
}
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ class MLRLCOMMON_API Vector : public View<T> {
* @return A `const_iterator` to the end
*/
typename View<T>::const_iterator cend() const {
return &View<T>::array[numElements];
return &BaseView<T>::array[numElements];
}

/**
Expand All @@ -99,14 +99,14 @@ class MLRLCOMMON_API Vector : public View<T> {
* @return An `iterator` to the end
*/
typename View<T>::iterator end() {
return &View<T>::array[numElements];
return &BaseView<T>::array[numElements];
}

/**
* Sets all values stored in the view to zero.
*/
void clear() {
util::setViewToZeros(View<T>::array, numElements);
util::setViewToZeros(BaseView<T>::array, numElements);
}
};

Expand Down
Loading

0 comments on commit 01bed30

Please sign in to comment.