utils: Add utils::Transform
An element-wise vector transformation utility function.
Similar to JS/TS's map() method on arrays.
Change-Id: I4baf52daa918f2e7bf5f9b4af13894fe66826f7c
Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/69103
Kokoro: Kokoro <noreply+kokoro@google.com>
Commit-Queue: Ben Clayton <bclayton@chromium.org>
Reviewed-by: Antonio Maiorano <amaiorano@google.com>
diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt
index 021c606..d7f8178 100644
--- a/src/CMakeLists.txt
+++ b/src/CMakeLists.txt
@@ -729,6 +729,7 @@
utils/reverse_test.cc
utils/scoped_assignment_test.cc
utils/string_test.cc
+ utils/transform_test.cc
utils/unique_vector_test.cc
writer/append_vector_test.cc
writer/float_to_string_test.cc
diff --git a/src/utils/transform.h b/src/utils/transform.h
new file mode 100644
index 0000000..cd31ec5
--- /dev/null
+++ b/src/utils/transform.h
@@ -0,0 +1,62 @@
+// Copyright 2021 The Tint Authors
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#ifndef SRC_UTILS_TRANSFORM_H_
+#define SRC_UTILS_TRANSFORM_H_
+
+#include <algorithm>
+#include <type_traits>
+#include <utility>
+#include <vector>
+
+#include "src/traits.h"
+
+namespace tint {
+namespace utils {
+
+/// Transform performs an element-wise transformation of a vector.
+/// @param in the input vector.
+/// @param transform the transformation function with signature: `OUT(IN)`
+/// @returns a new vector with each element of the source vector transformed by
+/// `transform`.
+template <typename IN, typename TRANSFORMER>
+auto Transform(const std::vector<IN>& in, TRANSFORMER&& transform)
+ -> std::vector<decltype(transform(in[0]))> {
+ std::vector<decltype(transform(in[0]))> result(in.size());
+ for (size_t i = 0; i < result.size(); ++i) {
+ result[i] = transform(in[i]);
+ }
+ return result;
+}
+
+/// Transform performs an element-wise transformation of a vector.
+/// @param in the input vector.
+/// @param transform the transformation function with signature:
+/// `OUT(IN, size_t)`
+/// @returns a new vector with each element of the source vector transformed by
+/// `transform`.
+template <typename IN, typename TRANSFORMER>
+auto Transform(const std::vector<IN>& in, TRANSFORMER&& transform)
+ -> std::vector<decltype(transform(in[0], 1u))> {
+ std::vector<decltype(transform(in[0], 1u))> result(in.size());
+ for (size_t i = 0; i < result.size(); ++i) {
+ result[i] = transform(in[i], i);
+ }
+ return result;
+}
+
+} // namespace utils
+} // namespace tint
+
+#endif // SRC_UTILS_TRANSFORM_H_
diff --git a/src/utils/transform_test.cc b/src/utils/transform_test.cc
new file mode 100644
index 0000000..32498be
--- /dev/null
+++ b/src/utils/transform_test.cc
@@ -0,0 +1,94 @@
+// Copyright 2021 The Tint Authors.
+//
+// Licensed under the Apache License, Version 2.0 (the "License");
+// you may not use this file except in compliance with the License.
+// You may obtain a copy of the License at
+//
+// http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing, software
+// distributed under the License is distributed on an "AS IS" BASIS,
+// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+// See the License for the specific language governing permissions and
+// limitations under the License.
+
+#include "src/utils/transform.h"
+
+#include <string>
+#include <type_traits>
+
+#include "gmock/gmock.h"
+
+#define CHECK_ELEMENT_TYPE(vector, expected) \
+ static_assert(std::is_same<decltype(vector)::value_type, expected>::value, \
+ "unexpected result vector element type")
+
+namespace tint {
+namespace utils {
+namespace {
+
+TEST(TransformTest, Empty) {
+ const std::vector<int> empty{};
+ {
+ auto transformed = Transform(empty, [](int) -> int {
+ [] { FAIL() << "Transform should not be called for empty vector"; }();
+ return 0;
+ });
+ CHECK_ELEMENT_TYPE(transformed, int);
+ EXPECT_EQ(transformed.size(), 0u);
+ }
+ {
+ auto transformed = Transform(empty, [](int, size_t) -> int {
+ [] { FAIL() << "Transform should not be called for empty vector"; }();
+ return 0;
+ });
+ CHECK_ELEMENT_TYPE(transformed, int);
+ EXPECT_EQ(transformed.size(), 0u);
+ }
+}
+
+TEST(TransformTest, Identity) {
+ const std::vector<int> input{1, 2, 3, 4};
+ {
+ auto transformed = Transform(input, [](int i) { return i; });
+ CHECK_ELEMENT_TYPE(transformed, int);
+ EXPECT_THAT(transformed, testing::ElementsAre(1, 2, 3, 4));
+ }
+ {
+ auto transformed = Transform(input, [](int i, size_t) { return i; });
+ CHECK_ELEMENT_TYPE(transformed, int);
+ EXPECT_THAT(transformed, testing::ElementsAre(1, 2, 3, 4));
+ }
+}
+
+TEST(TransformTest, Index) {
+ const std::vector<int> input{10, 20, 30, 40};
+ {
+ auto transformed = Transform(input, [](int, size_t idx) { return idx; });
+ CHECK_ELEMENT_TYPE(transformed, size_t);
+ EXPECT_THAT(transformed, testing::ElementsAre(0u, 1u, 2u, 3u));
+ }
+}
+
+TEST(TransformTest, TransformSameType) {
+ const std::vector<int> input{1, 2, 3, 4};
+ {
+ auto transformed = Transform(input, [](int i) { return i * 10; });
+ CHECK_ELEMENT_TYPE(transformed, int);
+ EXPECT_THAT(transformed, testing::ElementsAre(10, 20, 30, 40));
+ }
+}
+
+TEST(TransformTest, TransformDifferentType) {
+ const std::vector<int> input{1, 2, 3, 4};
+ {
+ auto transformed =
+ Transform(input, [](int i) { return std::to_string(i); });
+ CHECK_ELEMENT_TYPE(transformed, std::string);
+ EXPECT_THAT(transformed, testing::ElementsAre("1", "2", "3", "4"));
+ }
+}
+
+} // namespace
+} // namespace utils
+} // namespace tint
diff --git a/test/BUILD.gn b/test/BUILD.gn
index f6d093a..6325c34 100644
--- a/test/BUILD.gn
+++ b/test/BUILD.gn
@@ -346,6 +346,7 @@
"../src/utils/reverse_test.cc",
"../src/utils/scoped_assignment_test.cc",
"../src/utils/string_test.cc",
+ "../src/utils/transform_test.cc",
"../src/utils/unique_vector_test.cc",
]
}