Skip to content

[PBQP] Add begin and end to Vector (NFC) #136454

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 3 commits into from
Apr 19, 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
23 changes: 13 additions & 10 deletions llvm/include/llvm/CodeGen/PBQP/Math.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,13 +33,13 @@ class Vector {
/// Construct a PBQP vector with initializer.
Vector(unsigned Length, PBQPNum InitVal)
: Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {
std::fill(Data.get(), Data.get() + Length, InitVal);
std::fill(begin(), end(), InitVal);
}

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

/// Move construct a PBQP vector.
Expand All @@ -48,12 +48,16 @@ class Vector {
V.Length = 0;
}

// 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; }

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

/// Return the length of the vector
Expand All @@ -80,15 +84,14 @@ class Vector {
Vector& operator+=(const Vector &V) {
assert(Length != 0 && Data && "Invalid vector");
assert(Length == V.Length && "Vector length mismatch.");
std::transform(Data.get(), Data.get() + Length, V.Data.get(), Data.get(),
std::plus<PBQPNum>());
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");
return std::min_element(Data.get(), Data.get() + Length) - Data.get();
return llvm::min_element(*this) - begin();
}

private:
Expand All @@ -98,8 +101,8 @@ class Vector {

/// Return a hash_value for the given vector.
inline hash_code hash_value(const Vector &V) {
unsigned *VBegin = reinterpret_cast<unsigned*>(V.Data.get());
unsigned *VEnd = reinterpret_cast<unsigned*>(V.Data.get() + V.Length);
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));
}

Expand Down
Loading