tint/utils: Add Vector::Sort()
Convenience helper Change-Id: I1d9fabcb1a89df1bc23a1e8805e5fd1caa68500b Reviewed-on: https://dawn-review.googlesource.com/c/dawn/+/112285 Kokoro: Ben Clayton <bclayton@google.com> Reviewed-by: Dan Sinclair <dsinclair@chromium.org> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
d205b716ca
commit
205e16de63
|
@ -423,6 +423,19 @@ class Vector {
|
|||
return val;
|
||||
}
|
||||
|
||||
/// Sort sorts the vector in-place using the predicate function @p pred
|
||||
/// @param pred a function that has the signature `bool(const T& a, const T& b)` which returns
|
||||
/// true if `a` is ordered before `b`.
|
||||
template <typename PREDICATE>
|
||||
void Sort(PREDICATE&& pred) {
|
||||
std::sort(begin(), end(), std::forward<PREDICATE>(pred));
|
||||
}
|
||||
|
||||
/// Sort sorts the vector in-place using `T::operator<()`
|
||||
void Sort() {
|
||||
Sort([](auto& a, auto& b) { return a < b; });
|
||||
}
|
||||
|
||||
/// @returns true if the vector is empty.
|
||||
bool IsEmpty() const { return impl_.slice.len == 0; }
|
||||
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include <string>
|
||||
#include <tuple>
|
||||
|
||||
#include "gtest/gtest.h"
|
||||
#include "gmock/gmock.h"
|
||||
|
||||
#include "src/tint/utils/bitcast.h"
|
||||
|
||||
|
@ -2009,6 +2009,18 @@ TEST(TintVectorRefTest, Index) {
|
|||
EXPECT_EQ(vec_ref[1], "two");
|
||||
}
|
||||
|
||||
TEST(TintVectorRefTest, Sort) {
|
||||
Vector vec{1, 5, 3, 4, 2};
|
||||
vec.Sort();
|
||||
EXPECT_THAT(vec, testing::ElementsAre(1, 2, 3, 4, 5));
|
||||
}
|
||||
|
||||
TEST(TintVectorRefTest, SortPredicate) {
|
||||
Vector vec{1, 5, 3, 4, 2};
|
||||
vec.Sort([](int a, int b) { return b < a; });
|
||||
EXPECT_THAT(vec, testing::ElementsAre(5, 4, 3, 2, 1));
|
||||
}
|
||||
|
||||
TEST(TintVectorRefTest, ConstIndex) {
|
||||
Vector<std::string, 2> vec{"one", "two"};
|
||||
const VectorRef<std::string> vec_ref(vec);
|
||||
|
|
Loading…
Reference in New Issue