Skip to content

Commit 3215fd7

Browse files
[PBQP] Add begin and end to Vector (NFC) (#136454)
This patch adds begin and end to Vector. The new functions will allow us to use llvm::interleaved in operator<< for example.
1 parent e8245d5 commit 3215fd7

File tree

1 file changed

+13
-10
lines changed
  • llvm/include/llvm/CodeGen/PBQP

1 file changed

+13
-10
lines changed

llvm/include/llvm/CodeGen/PBQP/Math.h

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -33,13 +33,13 @@ class Vector {
3333
/// Construct a PBQP vector with initializer.
3434
Vector(unsigned Length, PBQPNum InitVal)
3535
: Length(Length), Data(std::make_unique<PBQPNum []>(Length)) {
36-
std::fill(Data.get(), Data.get() + Length, InitVal);
36+
std::fill(begin(), end(), InitVal);
3737
}
3838

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

4545
/// Move construct a PBQP vector.
@@ -48,12 +48,16 @@ class Vector {
4848
V.Length = 0;
4949
}
5050

51+
// Iterator-based access.
52+
const PBQPNum *begin() const { return Data.get(); }
53+
const PBQPNum *end() const { return Data.get() + Length; }
54+
PBQPNum *begin() { return Data.get(); }
55+
PBQPNum *end() { return Data.get() + Length; }
56+
5157
/// Comparison operator.
5258
bool operator==(const Vector &V) const {
5359
assert(Length != 0 && Data && "Invalid vector");
54-
if (Length != V.Length)
55-
return false;
56-
return std::equal(Data.get(), Data.get() + Length, V.Data.get());
60+
return llvm::equal(*this, V);
5761
}
5862

5963
/// Return the length of the vector
@@ -80,15 +84,14 @@ class Vector {
8084
Vector& operator+=(const Vector &V) {
8185
assert(Length != 0 && Data && "Invalid vector");
8286
assert(Length == V.Length && "Vector length mismatch.");
83-
std::transform(Data.get(), Data.get() + Length, V.Data.get(), Data.get(),
84-
std::plus<PBQPNum>());
87+
std::transform(begin(), end(), V.begin(), begin(), std::plus<PBQPNum>());
8588
return *this;
8689
}
8790

8891
/// Returns the index of the minimum value in this vector
8992
unsigned minIndex() const {
9093
assert(Length != 0 && Data && "Invalid vector");
91-
return std::min_element(Data.get(), Data.get() + Length) - Data.get();
94+
return llvm::min_element(*this) - begin();
9295
}
9396

9497
private:
@@ -98,8 +101,8 @@ class Vector {
98101

99102
/// Return a hash_value for the given vector.
100103
inline hash_code hash_value(const Vector &V) {
101-
unsigned *VBegin = reinterpret_cast<unsigned*>(V.Data.get());
102-
unsigned *VEnd = reinterpret_cast<unsigned*>(V.Data.get() + V.Length);
104+
const unsigned *VBegin = reinterpret_cast<const unsigned *>(V.begin());
105+
const unsigned *VEnd = reinterpret_cast<const unsigned *>(V.end());
103106
return hash_combine(V.Length, hash_combine_range(VBegin, VEnd));
104107
}
105108

0 commit comments

Comments
 (0)