[RFC,v2,03/12] ipa: libipa: vector: Add copy constructor and assignment operator
diff mbox series

Message ID 20241118000738.18977-4-laurent.pinchart@ideasonboard.com
State New
Headers show
Series
  • Improve linear algebra helpers in libipa
Related show

Commit Message

Laurent Pinchart Nov. 18, 2024, 12:07 a.m. UTC
It is useful to assign a value to an existing vector. Define a copy
constructor and a copy assignment operator.

Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
---
 src/ipa/libipa/vector.cpp | 13 +++++++++++++
 src/ipa/libipa/vector.h   | 13 +++++++++++++
 2 files changed, 26 insertions(+)

Comments

Milan Zamazal Nov. 18, 2024, 12:23 p.m. UTC | #1
Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:

> It is useful to assign a value to an existing vector. Define a copy
> constructor and a copy assignment operator.
>
> Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> ---
>  src/ipa/libipa/vector.cpp | 13 +++++++++++++
>  src/ipa/libipa/vector.h   | 13 +++++++++++++
>  2 files changed, 26 insertions(+)
>
> diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> index f14f155216f3..df089f8aed9f 100644
> --- a/src/ipa/libipa/vector.cpp
> +++ b/src/ipa/libipa/vector.cpp
> @@ -40,6 +40,19 @@ namespace ipa {
>   * The size of \a data must be equal to the dimension size Rows of the vector.
>   */
>  
> +/**
> + * \fn Vector::Vector(const Vector &other)
> + * \brief Construct a Vector by copying \a other
> + * \param[in] other The other Vector value
> + */
> +
> +/**
> + * \fn Vector &Vector::operator=(const Vector &other)
> + * \brief Replace the content of the Vector with a copy of the content of \a other
> + * \param[in] other The other Vector value
> + * \return This Vector value
> + */
> +
>  /**
>   * \fn T Vector::operator[](size_t i) const
>   * \brief Index to an element in the vector
> diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> index b72ab9851aa3..50e16250acfd 100644
> --- a/src/ipa/libipa/vector.h
> +++ b/src/ipa/libipa/vector.h
> @@ -6,6 +6,7 @@
>   */
>  #pragma once
>  
> +#include <algorithm>

Is this needed for copying the array?

Reviewed-by: Milan Zamazal <mzamazal@redhat.com>

>  #include <array>
>  #include <cmath>
>  #include <optional>
> @@ -41,6 +42,18 @@ public:
>  			data_[i] = data[i];
>  	}
>  
> +	Vector(const Vector &other)
> +		: data_(other.data_)
> +	{
> +	}
> +
> +	Vector &operator=(const Vector &other)
> +	{
> +		data_ = other.data_;
> +
> +		return *this;
> +	}
> +
>  	const T &operator[](size_t i) const
>  	{
>  		ASSERT(i < data_.size());
Laurent Pinchart Nov. 18, 2024, 1:09 p.m. UTC | #2
On Mon, Nov 18, 2024 at 01:23:06PM +0100, Milan Zamazal wrote:
> Laurent Pinchart <laurent.pinchart@ideasonboard.com> writes:
> 
> > It is useful to assign a value to an existing vector. Define a copy
> > constructor and a copy assignment operator.
> >
> > Signed-off-by: Laurent Pinchart <laurent.pinchart@ideasonboard.com>
> > ---
> >  src/ipa/libipa/vector.cpp | 13 +++++++++++++
> >  src/ipa/libipa/vector.h   | 13 +++++++++++++
> >  2 files changed, 26 insertions(+)
> >
> > diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
> > index f14f155216f3..df089f8aed9f 100644
> > --- a/src/ipa/libipa/vector.cpp
> > +++ b/src/ipa/libipa/vector.cpp
> > @@ -40,6 +40,19 @@ namespace ipa {
> >   * The size of \a data must be equal to the dimension size Rows of the vector.
> >   */
> >  
> > +/**
> > + * \fn Vector::Vector(const Vector &other)
> > + * \brief Construct a Vector by copying \a other
> > + * \param[in] other The other Vector value
> > + */
> > +
> > +/**
> > + * \fn Vector &Vector::operator=(const Vector &other)
> > + * \brief Replace the content of the Vector with a copy of the content of \a other
> > + * \param[in] other The other Vector value
> > + * \return This Vector value
> > + */
> > +
> >  /**
> >   * \fn T Vector::operator[](size_t i) const
> >   * \brief Index to an element in the vector
> > diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
> > index b72ab9851aa3..50e16250acfd 100644
> > --- a/src/ipa/libipa/vector.h
> > +++ b/src/ipa/libipa/vector.h
> > @@ -6,6 +6,7 @@
> >   */
> >  #pragma once
> >  
> > +#include <algorithm>
> 
> Is this needed for copying the array?

No, it's not needed. It belongs to patch 05/12. I'll move it there.

> Reviewed-by: Milan Zamazal <mzamazal@redhat.com>
> 
> >  #include <array>
> >  #include <cmath>
> >  #include <optional>
> > @@ -41,6 +42,18 @@ public:
> >  			data_[i] = data[i];
> >  	}
> >  
> > +	Vector(const Vector &other)
> > +		: data_(other.data_)
> > +	{
> > +	}
> > +
> > +	Vector &operator=(const Vector &other)
> > +	{
> > +		data_ = other.data_;
> > +
> > +		return *this;
> > +	}
> > +
> >  	const T &operator[](size_t i) const
> >  	{
> >  		ASSERT(i < data_.size());

Patch
diff mbox series

diff --git a/src/ipa/libipa/vector.cpp b/src/ipa/libipa/vector.cpp
index f14f155216f3..df089f8aed9f 100644
--- a/src/ipa/libipa/vector.cpp
+++ b/src/ipa/libipa/vector.cpp
@@ -40,6 +40,19 @@  namespace ipa {
  * The size of \a data must be equal to the dimension size Rows of the vector.
  */
 
+/**
+ * \fn Vector::Vector(const Vector &other)
+ * \brief Construct a Vector by copying \a other
+ * \param[in] other The other Vector value
+ */
+
+/**
+ * \fn Vector &Vector::operator=(const Vector &other)
+ * \brief Replace the content of the Vector with a copy of the content of \a other
+ * \param[in] other The other Vector value
+ * \return This Vector value
+ */
+
 /**
  * \fn T Vector::operator[](size_t i) const
  * \brief Index to an element in the vector
diff --git a/src/ipa/libipa/vector.h b/src/ipa/libipa/vector.h
index b72ab9851aa3..50e16250acfd 100644
--- a/src/ipa/libipa/vector.h
+++ b/src/ipa/libipa/vector.h
@@ -6,6 +6,7 @@ 
  */
 #pragma once
 
+#include <algorithm>
 #include <array>
 #include <cmath>
 #include <optional>
@@ -41,6 +42,18 @@  public:
 			data_[i] = data[i];
 	}
 
+	Vector(const Vector &other)
+		: data_(other.data_)
+	{
+	}
+
+	Vector &operator=(const Vector &other)
+	{
+		data_ = other.data_;
+
+		return *this;
+	}
+
 	const T &operator[](size_t i) const
 	{
 		ASSERT(i < data_.size());