[llvm] r285024 - [pbqp] unique_ptr-ify (Vector|Matrix)::Data, NFC.

Vedant Kumar via llvm-commits llvm-commits at lists.llvm.org
Mon Oct 24 13:51:47 PDT 2016


Author: vedantk
Date: Mon Oct 24 15:51:46 2016
New Revision: 285024

URL: http://llvm.org/viewvc/llvm-project?rev=285024&view=rev
Log:
[pbqp] unique_ptr-ify (Vector|Matrix)::Data, NFC.

Suggested by David Blaikie.

Modified:
    llvm/trunk/include/llvm/CodeGen/PBQP/Math.h

Modified: llvm/trunk/include/llvm/CodeGen/PBQP/Math.h
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/include/llvm/CodeGen/PBQP/Math.h?rev=285024&r1=285023&r2=285024&view=diff
==============================================================================
--- llvm/trunk/include/llvm/CodeGen/PBQP/Math.h (original)
+++ llvm/trunk/include/llvm/CodeGen/PBQP/Math.h Mon Oct 24 15:51:46 2016
@@ -27,81 +27,78 @@ public:
 
   /// \brief Construct a PBQP vector of the given size.
   explicit Vector(unsigned Length)
-    : Length(Length), Data(new PBQPNum[Length]) {}
+    : Length(Length), Data(llvm::make_unique<PBQPNum []>(Length)) {}
 
   /// \brief Construct a PBQP vector with initializer.
   Vector(unsigned Length, PBQPNum InitVal)
-    : Length(Length), Data(new PBQPNum[Length]) {
-    std::fill(Data, Data + Length, InitVal);
+    : Length(Length), Data(llvm::make_unique<PBQPNum []>(Length)) {
+    std::fill(Data.get(), Data.get() + Length, InitVal);
   }
 
   /// \brief Copy construct a PBQP vector.
   Vector(const Vector &V)
-    : Length(V.Length), Data(new PBQPNum[Length]) {
-    std::copy(V.Data, V.Data + Length, Data);
+    : Length(V.Length), Data(llvm::make_unique<PBQPNum []>(Length)) {
+    std::copy(V.Data.get(), V.Data.get() + Length, Data.get());
   }
 
   /// \brief Move construct a PBQP vector.
   Vector(Vector &&V)
-    : Length(V.Length), Data(V.Data) {
+    : Length(V.Length), Data(std::move(V.Data)) {
     V.Length = 0;
-    V.Data = nullptr;
   }
 
-  /// \brief Destroy this vector, return its memory.
-  ~Vector() { delete[] Data; }
-
   /// \brief Comparison operator.
   bool operator==(const Vector &V) const {
-    assert(Length != 0 && Data != nullptr && "Invalid vector");
+    assert(Length != 0 && Data && "Invalid vector");
     if (Length != V.Length)
       return false;
-    return std::equal(Data, Data + Length, V.Data);
+    return std::equal(Data.get(), Data.get() + Length, V.Data.get());
   }
 
   /// \brief Return the length of the vector
   unsigned getLength() const {
-    assert(Length != 0 && Data != nullptr && "Invalid vector");
+    assert(Length != 0 && Data && "Invalid vector");
     return Length;
   }
 
   /// \brief Element access.
   PBQPNum& operator[](unsigned Index) {
-    assert(Length != 0 && Data != nullptr && "Invalid vector");
+    assert(Length != 0 && Data && "Invalid vector");
     assert(Index < Length && "Vector element access out of bounds.");
     return Data[Index];
   }
 
   /// \brief Const element access.
   const PBQPNum& operator[](unsigned Index) const {
-    assert(Length != 0 && Data != nullptr && "Invalid vector");
+    assert(Length != 0 && Data && "Invalid vector");
     assert(Index < Length && "Vector element access out of bounds.");
     return Data[Index];
   }
 
   /// \brief Add another vector to this one.
   Vector& operator+=(const Vector &V) {
-    assert(Length != 0 && Data != nullptr && "Invalid vector");
+    assert(Length != 0 && Data && "Invalid vector");
     assert(Length == V.Length && "Vector length mismatch.");
-    std::transform(Data, Data + Length, V.Data, Data, std::plus<PBQPNum>());
+    std::transform(Data.get(), Data.get() + Length, V.Data.get(), Data.get(),
+                   std::plus<PBQPNum>());
     return *this;
   }
 
   /// \brief Returns the index of the minimum value in this vector
   unsigned minIndex() const {
-    assert(Length != 0 && Data != nullptr && "Invalid vector");
-    return std::min_element(Data, Data + Length) - Data;
+    assert(Length != 0 && Data && "Invalid vector");
+    return std::min_element(Data.get(), Data.get() + Length) - Data.get();
   }
 
 private:
   unsigned Length;
-  PBQPNum *Data;
+  std::unique_ptr<PBQPNum []> Data;
 };
 
 /// \brief Return a hash_value for the given vector.
 inline hash_code hash_value(const Vector &V) {
-  unsigned *VBegin = reinterpret_cast<unsigned*>(V.Data);
-  unsigned *VEnd = reinterpret_cast<unsigned*>(V.Data + V.Length);
+  unsigned *VBegin = reinterpret_cast<unsigned*>(V.Data.get());
+  unsigned *VEnd = reinterpret_cast<unsigned*>(V.Data.get() + V.Length);
   return hash_combine(V.Length, hash_combine_range(VBegin, VEnd));
 }
 
@@ -127,69 +124,67 @@ public:
 
   /// \brief Construct a PBQP Matrix with the given dimensions.
   Matrix(unsigned Rows, unsigned Cols) :
-    Rows(Rows), Cols(Cols), Data(new PBQPNum[Rows * Cols]) {
+    Rows(Rows), Cols(Cols), Data(llvm::make_unique<PBQPNum []>(Rows * Cols)) {
   }
 
   /// \brief Construct a PBQP Matrix with the given dimensions and initial
   /// value.
   Matrix(unsigned Rows, unsigned Cols, PBQPNum InitVal)
-    : Rows(Rows), Cols(Cols), Data(new PBQPNum[Rows * Cols]) {
-    std::fill(Data, Data + (Rows * Cols), InitVal);
+    : Rows(Rows), Cols(Cols),
+      Data(llvm::make_unique<PBQPNum []>(Rows * Cols)) {
+    std::fill(Data.get(), Data.get() + (Rows * Cols), InitVal);
   }
 
   /// \brief Copy construct a PBQP matrix.
   Matrix(const Matrix &M)
-    : Rows(M.Rows), Cols(M.Cols), Data(new PBQPNum[Rows * Cols]) {
-    std::copy(M.Data, M.Data + (Rows * Cols), Data);
+    : Rows(M.Rows), Cols(M.Cols),
+      Data(llvm::make_unique<PBQPNum []>(Rows * Cols)) {
+    std::copy(M.Data.get(), M.Data.get() + (Rows * Cols), Data.get());
   }
 
   /// \brief Move construct a PBQP matrix.
   Matrix(Matrix &&M)
-    : Rows(M.Rows), Cols(M.Cols), Data(M.Data) {
+    : Rows(M.Rows), Cols(M.Cols), Data(std::move(M.Data)) {
     M.Rows = M.Cols = 0;
-    M.Data = nullptr;
   }
 
-  /// \brief Destroy this matrix, return its memory.
-  ~Matrix() { delete[] Data; }
-
   /// \brief Comparison operator.
   bool operator==(const Matrix &M) const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     if (Rows != M.Rows || Cols != M.Cols)
       return false;
-    return std::equal(Data, Data + (Rows * Cols), M.Data);
+    return std::equal(Data.get(), Data.get() + (Rows * Cols), M.Data.get());
   }
 
   /// \brief Return the number of rows in this matrix.
   unsigned getRows() const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     return Rows;
   }
 
   /// \brief Return the number of cols in this matrix.
   unsigned getCols() const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     return Cols;
   }
 
   /// \brief Matrix element access.
   PBQPNum* operator[](unsigned R) {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     assert(R < Rows && "Row out of bounds.");
-    return Data + (R * Cols);
+    return Data.get() + (R * Cols);
   }
 
   /// \brief Matrix element access.
   const PBQPNum* operator[](unsigned R) const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     assert(R < Rows && "Row out of bounds.");
-    return Data + (R * Cols);
+    return Data.get() + (R * Cols);
   }
 
   /// \brief Returns the given row as a vector.
   Vector getRowAsVector(unsigned R) const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     Vector V(Cols);
     for (unsigned C = 0; C < Cols; ++C)
       V[C] = (*this)[R][C];
@@ -198,7 +193,7 @@ public:
 
   /// \brief Returns the given column as a vector.
   Vector getColAsVector(unsigned C) const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     Vector V(Rows);
     for (unsigned R = 0; R < Rows; ++R)
       V[R] = (*this)[R][C];
@@ -207,7 +202,7 @@ public:
 
   /// \brief Matrix transpose.
   Matrix transpose() const {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     Matrix M(Cols, Rows);
     for (unsigned r = 0; r < Rows; ++r)
       for (unsigned c = 0; c < Cols; ++c)
@@ -217,16 +212,16 @@ public:
 
   /// \brief Add the given matrix to this one.
   Matrix& operator+=(const Matrix &M) {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     assert(Rows == M.Rows && Cols == M.Cols &&
            "Matrix dimensions mismatch.");
-    std::transform(Data, Data + (Rows * Cols), M.Data, Data,
-                   std::plus<PBQPNum>());
+    std::transform(Data.get(), Data.get() + (Rows * Cols), M.Data.get(),
+                   Data.get(), std::plus<PBQPNum>());
     return *this;
   }
 
   Matrix operator+(const Matrix &M) {
-    assert(Rows != 0 && Cols != 0 && Data != nullptr && "Invalid matrix");
+    assert(Rows != 0 && Cols != 0 && Data && "Invalid matrix");
     Matrix Tmp(*this);
     Tmp += M;
     return Tmp;
@@ -234,13 +229,14 @@ public:
 
 private:
   unsigned Rows, Cols;
-  PBQPNum *Data;
+  std::unique_ptr<PBQPNum []> Data;
 };
 
 /// \brief Return a hash_code for the given matrix.
 inline hash_code hash_value(const Matrix &M) {
-  unsigned *MBegin = reinterpret_cast<unsigned*>(M.Data);
-  unsigned *MEnd = reinterpret_cast<unsigned*>(M.Data + (M.Rows * M.Cols));
+  unsigned *MBegin = reinterpret_cast<unsigned*>(M.Data.get());
+  unsigned *MEnd =
+    reinterpret_cast<unsigned*>(M.Data.get() + (M.Rows * M.Cols));
   return hash_combine(M.Rows, M.Cols, hash_combine_range(MBegin, MEnd));
 }
 




More information about the llvm-commits mailing list