From cc26a4166dc5dbd22abfc09d66595d85bc780c04 Mon Sep 17 00:00:00 2001 From: Ben Clayton Date: Thu, 4 Nov 2021 22:29:22 +0000 Subject: [PATCH] utils: Add index operators to UniqueVector And put it into the utils namespace. Change-Id: Ib4c6fadc63954196d572148d0e96ffec6e3bbb38 Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/68404 Reviewed-by: Antonio Maiorano Kokoro: Kokoro --- src/inspector/inspector.cc | 6 +++--- src/inspector/inspector.h | 2 +- src/reader/spirv/parser_impl.cc | 4 ++-- src/resolver/resolver.h | 8 ++++---- src/transform/zero_init_workgroup_memory.cc | 2 +- src/utils/unique_vector.h | 22 +++++++++++++++++++++ src/utils/unique_vector_test.cc | 17 ++++++++++++++++ 7 files changed, 50 insertions(+), 11 deletions(-) diff --git a/src/inspector/inspector.cc b/src/inspector/inspector.cc index 19cf595330..8fbd078ace 100644 --- a/src/inspector/inspector.cc +++ b/src/inspector/inspector.cc @@ -790,8 +790,8 @@ void Inspector::GenerateSamplerTargets() { return; } - sampler_targets_ = std::make_unique< - std::unordered_map>>(); + sampler_targets_ = std::make_unique>>(); auto& sem = program_->Sem(); @@ -873,7 +873,7 @@ void Inspector::GetOriginatingResources( std::array globals{}; std::array parameters{}; - UniqueVector callsites; + utils::UniqueVector callsites; for (size_t i = 0; i < N; i++) { auto*& expr = exprs[i]; diff --git a/src/inspector/inspector.h b/src/inspector/inspector.h index 3fca877d8b..c84233b1c1 100644 --- a/src/inspector/inspector.h +++ b/src/inspector/inspector.h @@ -141,7 +141,7 @@ class Inspector { const Program* program_; diag::List diagnostics_; std::unique_ptr< - std::unordered_map>> + std::unordered_map>> sampler_targets_; /// @param name name of the entry point to find diff --git a/src/reader/spirv/parser_impl.cc b/src/reader/spirv/parser_impl.cc index b5ff8fca6f..65a57c9825 100644 --- a/src/reader/spirv/parser_impl.cc +++ b/src/reader/spirv/parser_impl.cc @@ -876,8 +876,8 @@ bool ParserImpl::RegisterEntryPoints() { TINT_ASSERT(Reader, !inner_implementation_name.empty()); TINT_ASSERT(Reader, ep_name != inner_implementation_name); - tint::UniqueVector inputs; - tint::UniqueVector outputs; + utils::UniqueVector inputs; + utils::UniqueVector outputs; for (unsigned iarg = 3; iarg < entry_point.NumInOperands(); iarg++) { const uint32_t var_id = entry_point.GetSingleWordInOperand(iarg); if (const auto* var_inst = def_use_mgr_->GetDef(var_id)) { diff --git a/src/resolver/resolver.h b/src/resolver/resolver.h index 212360bc42..edd8759a05 100644 --- a/src/resolver/resolver.h +++ b/src/resolver/resolver.h @@ -135,8 +135,8 @@ class Resolver { const ast::Function* const declaration; std::vector parameters; - UniqueVector referenced_module_vars; - UniqueVector local_referenced_module_vars; + utils::UniqueVector referenced_module_vars; + utils::UniqueVector local_referenced_module_vars; std::vector return_statements; std::vector callsites; sem::Type* return_type = nullptr; @@ -145,10 +145,10 @@ class Resolver { std::vector intrinsic_calls; // List of transitive calls this function makes - UniqueVector transitive_calls; + utils::UniqueVector transitive_calls; // List of entry point functions that transitively call this function - UniqueVector ancestor_entry_points; + utils::UniqueVector ancestor_entry_points; }; /// Structure holding semantic information about an expression. diff --git a/src/transform/zero_init_workgroup_memory.cc b/src/transform/zero_init_workgroup_memory.cc index f5948f64f2..46fc32c36c 100644 --- a/src/transform/zero_init_workgroup_memory.cc +++ b/src/transform/zero_init_workgroup_memory.cc @@ -74,7 +74,7 @@ struct ZeroInitWorkgroupMemory::State { }; /// A list of unique ArrayIndex - using ArrayIndices = UniqueVector; + using ArrayIndices = utils::UniqueVector; /// Expression holds information about an expression that is being built for a /// statement will zero workgroup values. diff --git a/src/utils/unique_vector.h b/src/utils/unique_vector.h index 1ae751a8c3..8d92bea40a 100644 --- a/src/utils/unique_vector.h +++ b/src/utils/unique_vector.h @@ -19,6 +19,7 @@ #include namespace tint { +namespace utils { /// UniqueVector is an ordered container that only contains unique items. /// Attempting to add a duplicate is a no-op. @@ -27,6 +28,18 @@ struct UniqueVector { /// The iterator returned by begin() and end() using ConstIterator = typename std::vector::const_iterator; + /// Constructor + UniqueVector() = default; + + /// Constructor + /// @param v the vector to construct this UniqueVector with. Duplicate + /// elements will be removed. + explicit UniqueVector(std::vector&& v) { + for (auto& el : v) { + add(el); + } + } + /// add appends the item to the end of the vector, if the vector does not /// already contain the given item. /// @param item the item to append to the end of the vector @@ -41,6 +54,14 @@ struct UniqueVector { /// @param item the item bool contains(const T& item) const { return set.count(item); } + /// @param i the index of the element to retrieve + /// @returns the element at the index `i` + T& operator[](size_t i) { return vector[i]; } + + /// @param i the index of the element to retrieve + /// @returns the element at the index `i` + const T& operator[](size_t i) const { return vector[i]; } + /// @returns the number of items in the vector size_t size() const { return vector.size(); } @@ -58,6 +79,7 @@ struct UniqueVector { std::unordered_set set; }; +} // namespace utils } // namespace tint #endif // SRC_UTILS_UNIQUE_VECTOR_H_ diff --git a/src/utils/unique_vector_test.cc b/src/utils/unique_vector_test.cc index 83f19e5992..8d22525f00 100644 --- a/src/utils/unique_vector_test.cc +++ b/src/utils/unique_vector_test.cc @@ -17,6 +17,7 @@ #include "gtest/gtest.h" namespace tint { +namespace utils { namespace { TEST(UniqueVectorTest, Empty) { @@ -25,6 +26,15 @@ TEST(UniqueVectorTest, Empty) { EXPECT_EQ(unique_vec.begin(), unique_vec.end()); } +TEST(UniqueVectorTest, MoveConstructor) { + UniqueVector unique_vec(std::vector{0, 3, 2, 1, 2}); + EXPECT_EQ(unique_vec.size(), 4u); + EXPECT_EQ(unique_vec[0], 0); + EXPECT_EQ(unique_vec[1], 3); + EXPECT_EQ(unique_vec[2], 2); + EXPECT_EQ(unique_vec[3], 1); +} + TEST(UniqueVectorTest, AddUnique) { UniqueVector unique_vec; unique_vec.add(0); @@ -36,6 +46,9 @@ TEST(UniqueVectorTest, AddUnique) { EXPECT_EQ(n, i); i++; } + EXPECT_EQ(unique_vec[0], 0); + EXPECT_EQ(unique_vec[1], 1); + EXPECT_EQ(unique_vec[2], 2); } TEST(UniqueVectorTest, AddDuplicates) { @@ -52,6 +65,9 @@ TEST(UniqueVectorTest, AddDuplicates) { EXPECT_EQ(n, i); i++; } + EXPECT_EQ(unique_vec[0], 0); + EXPECT_EQ(unique_vec[1], 1); + EXPECT_EQ(unique_vec[2], 2); } TEST(UniqueVectorTest, AsVector) { @@ -73,4 +89,5 @@ TEST(UniqueVectorTest, AsVector) { } } // namespace +} // namespace utils } // namespace tint