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

Suggested by David Blaikie.

llvm-svn: 285024
This commit is contained in:
Vedant Kumar 2016-10-24 20:51:46 +00:00
parent d48b0a3346
commit 26f89df41f
1 changed files with 46 additions and 50 deletions

View File

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