Skip to content

[CodeGen] Use OwningArrayRef in PBQP::Vector (NFC) #137548

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Apr 27, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 21 additions & 29 deletions llvm/include/llvm/CodeGen/PBQP/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#ifndef LLVM_CODEGEN_PBQP_MATH_H
#define LLVM_CODEGEN_PBQP_MATH_H

#include "llvm/ADT/ArrayRef.h"
#include "llvm/ADT/Hashing.h"
#include "llvm/ADT/STLExtras.h"
#include "llvm/Support/InterleavedRange.h"
Expand All @@ -28,83 +29,74 @@ class Vector {

public:
/// Construct a PBQP vector of the given size.
explicit Vector(unsigned Length)
: Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {}
explicit Vector(unsigned Length) : Data(Length) {}

/// Construct a PBQP vector with initializer.
Vector(unsigned Length, PBQPNum InitVal)
: Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {
Vector(unsigned Length, PBQPNum InitVal) : Data(Length) {
std::fill(begin(), end(), InitVal);
}

/// Copy construct a PBQP vector.
Vector(const Vector &V)
: Length(V.Length), Data(std::make_unique<PBQPNum []>(Length)) {
llvm::copy(V, begin());
}
Vector(const Vector &V) : Data(ArrayRef<PBQPNum>(V.Data)) {}

/// Move construct a PBQP vector.
Vector(Vector &&V)
: Length(V.Length), Data(std::move(V.Data)) {
V.Length = 0;
}
Vector(Vector &&V) : Data(std::move(V.Data)) {}

// Iterator-based access.
const PBQPNum *begin() const { return Data.get(); }
const PBQPNum *end() const { return Data.get() + Length; }
PBQPNum *begin() { return Data.get(); }
PBQPNum *end() { return Data.get() + Length; }
const PBQPNum *begin() const { return Data.begin(); }
const PBQPNum *end() const { return Data.end(); }
PBQPNum *begin() { return Data.begin(); }
PBQPNum *end() { return Data.end(); }

/// Comparison operator.
bool operator==(const Vector &V) const {
assert(Length != 0 && Data && "Invalid vector");
assert(!Data.empty() && "Invalid vector");
return llvm::equal(*this, V);
}

/// Return the length of the vector
unsigned getLength() const {
assert(Length != 0 && Data && "Invalid vector");
return Length;
assert(!Data.empty() && "Invalid vector");
return Data.size();
}

/// Element access.
PBQPNum& operator[](unsigned Index) {
assert(Length != 0 && Data && "Invalid vector");
assert(Index < Length && "Vector element access out of bounds.");
assert(!Data.empty() && "Invalid vector");
assert(Index < Data.size() && "Vector element access out of bounds.");
return Data[Index];
}

/// Const element access.
const PBQPNum& operator[](unsigned Index) const {
assert(Length != 0 && Data && "Invalid vector");
assert(Index < Length && "Vector element access out of bounds.");
assert(!Data.empty() && "Invalid vector");
assert(Index < Data.size() && "Vector element access out of bounds.");
return Data[Index];
}

/// Add another vector to this one.
Vector& operator+=(const Vector &V) {
assert(Length != 0 && Data && "Invalid vector");
assert(Length == V.Length && "Vector length mismatch.");
assert(!Data.empty() && "Invalid vector");
assert(Data.size() == V.Data.size() && "Vector length mismatch.");
std::transform(begin(), end(), V.begin(), begin(), std::plus<PBQPNum>());
return *this;
}

/// Returns the index of the minimum value in this vector
unsigned minIndex() const {
assert(Length != 0 && Data && "Invalid vector");
assert(!Data.empty() && "Invalid vector");
return llvm::min_element(*this) - begin();
}

private:
unsigned Length;
std::unique_ptr<PBQPNum []> Data;
OwningArrayRef<PBQPNum> Data;
};

/// Return a hash_value for the given vector.
inline hash_code hash_value(const Vector &V) {
const unsigned *VBegin = reinterpret_cast<const unsigned *>(V.begin());
const unsigned *VEnd = reinterpret_cast<const unsigned *>(V.end());
return hash_combine(V.Length, hash_combine_range(VBegin, VEnd));
return hash_combine(V.Data.size(), hash_combine_range(VBegin, VEnd));
}

/// Output a textual representation of the given vector on the given
Expand Down
Loading