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 <amaiorano@google.com>
Kokoro: Kokoro <noreply+kokoro@google.com>
This commit is contained in:
Ben Clayton 2021-11-04 22:29:22 +00:00
parent 89d8b2b7a5
commit cc26a4166d
7 changed files with 50 additions and 11 deletions

View File

@ -790,8 +790,8 @@ void Inspector::GenerateSamplerTargets() {
return; return;
} }
sampler_targets_ = std::make_unique< sampler_targets_ = std::make_unique<std::unordered_map<
std::unordered_map<std::string, UniqueVector<SamplerTexturePair>>>(); std::string, utils::UniqueVector<SamplerTexturePair>>>();
auto& sem = program_->Sem(); auto& sem = program_->Sem();
@ -873,7 +873,7 @@ void Inspector::GetOriginatingResources(
std::array<const sem::GlobalVariable*, N> globals{}; std::array<const sem::GlobalVariable*, N> globals{};
std::array<const sem::Parameter*, N> parameters{}; std::array<const sem::Parameter*, N> parameters{};
UniqueVector<const ast::CallExpression*> callsites; utils::UniqueVector<const ast::CallExpression*> callsites;
for (size_t i = 0; i < N; i++) { for (size_t i = 0; i < N; i++) {
auto*& expr = exprs[i]; auto*& expr = exprs[i];

View File

@ -141,7 +141,7 @@ class Inspector {
const Program* program_; const Program* program_;
diag::List diagnostics_; diag::List diagnostics_;
std::unique_ptr< std::unique_ptr<
std::unordered_map<std::string, UniqueVector<SamplerTexturePair>>> std::unordered_map<std::string, utils::UniqueVector<SamplerTexturePair>>>
sampler_targets_; sampler_targets_;
/// @param name name of the entry point to find /// @param name name of the entry point to find

View File

@ -876,8 +876,8 @@ bool ParserImpl::RegisterEntryPoints() {
TINT_ASSERT(Reader, !inner_implementation_name.empty()); TINT_ASSERT(Reader, !inner_implementation_name.empty());
TINT_ASSERT(Reader, ep_name != inner_implementation_name); TINT_ASSERT(Reader, ep_name != inner_implementation_name);
tint::UniqueVector<uint32_t> inputs; utils::UniqueVector<uint32_t> inputs;
tint::UniqueVector<uint32_t> outputs; utils::UniqueVector<uint32_t> outputs;
for (unsigned iarg = 3; iarg < entry_point.NumInOperands(); iarg++) { for (unsigned iarg = 3; iarg < entry_point.NumInOperands(); iarg++) {
const uint32_t var_id = entry_point.GetSingleWordInOperand(iarg); const uint32_t var_id = entry_point.GetSingleWordInOperand(iarg);
if (const auto* var_inst = def_use_mgr_->GetDef(var_id)) { if (const auto* var_inst = def_use_mgr_->GetDef(var_id)) {

View File

@ -135,8 +135,8 @@ class Resolver {
const ast::Function* const declaration; const ast::Function* const declaration;
std::vector<VariableInfo*> parameters; std::vector<VariableInfo*> parameters;
UniqueVector<VariableInfo*> referenced_module_vars; utils::UniqueVector<VariableInfo*> referenced_module_vars;
UniqueVector<VariableInfo*> local_referenced_module_vars; utils::UniqueVector<VariableInfo*> local_referenced_module_vars;
std::vector<const ast::ReturnStatement*> return_statements; std::vector<const ast::ReturnStatement*> return_statements;
std::vector<const ast::CallExpression*> callsites; std::vector<const ast::CallExpression*> callsites;
sem::Type* return_type = nullptr; sem::Type* return_type = nullptr;
@ -145,10 +145,10 @@ class Resolver {
std::vector<IntrinsicCallInfo> intrinsic_calls; std::vector<IntrinsicCallInfo> intrinsic_calls;
// List of transitive calls this function makes // List of transitive calls this function makes
UniqueVector<FunctionInfo*> transitive_calls; utils::UniqueVector<FunctionInfo*> transitive_calls;
// List of entry point functions that transitively call this function // List of entry point functions that transitively call this function
UniqueVector<FunctionInfo*> ancestor_entry_points; utils::UniqueVector<FunctionInfo*> ancestor_entry_points;
}; };
/// Structure holding semantic information about an expression. /// Structure holding semantic information about an expression.

View File

@ -74,7 +74,7 @@ struct ZeroInitWorkgroupMemory::State {
}; };
/// A list of unique ArrayIndex /// A list of unique ArrayIndex
using ArrayIndices = UniqueVector<ArrayIndex, ArrayIndex::Hasher>; using ArrayIndices = utils::UniqueVector<ArrayIndex, ArrayIndex::Hasher>;
/// Expression holds information about an expression that is being built for a /// Expression holds information about an expression that is being built for a
/// statement will zero workgroup values. /// statement will zero workgroup values.

View File

@ -19,6 +19,7 @@
#include <vector> #include <vector>
namespace tint { namespace tint {
namespace utils {
/// UniqueVector is an ordered container that only contains unique items. /// UniqueVector is an ordered container that only contains unique items.
/// Attempting to add a duplicate is a no-op. /// Attempting to add a duplicate is a no-op.
@ -27,6 +28,18 @@ struct UniqueVector {
/// The iterator returned by begin() and end() /// The iterator returned by begin() and end()
using ConstIterator = typename std::vector<T>::const_iterator; using ConstIterator = typename std::vector<T>::const_iterator;
/// Constructor
UniqueVector() = default;
/// Constructor
/// @param v the vector to construct this UniqueVector with. Duplicate
/// elements will be removed.
explicit UniqueVector(std::vector<T>&& v) {
for (auto& el : v) {
add(el);
}
}
/// add appends the item to the end of the vector, if the vector does not /// add appends the item to the end of the vector, if the vector does not
/// already contain the given item. /// already contain the given item.
/// @param item the item to append to the end of the vector /// @param item the item to append to the end of the vector
@ -41,6 +54,14 @@ struct UniqueVector {
/// @param item the item /// @param item the item
bool contains(const T& item) const { return set.count(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 /// @returns the number of items in the vector
size_t size() const { return vector.size(); } size_t size() const { return vector.size(); }
@ -58,6 +79,7 @@ struct UniqueVector {
std::unordered_set<T, HASH> set; std::unordered_set<T, HASH> set;
}; };
} // namespace utils
} // namespace tint } // namespace tint
#endif // SRC_UTILS_UNIQUE_VECTOR_H_ #endif // SRC_UTILS_UNIQUE_VECTOR_H_

View File

@ -17,6 +17,7 @@
#include "gtest/gtest.h" #include "gtest/gtest.h"
namespace tint { namespace tint {
namespace utils {
namespace { namespace {
TEST(UniqueVectorTest, Empty) { TEST(UniqueVectorTest, Empty) {
@ -25,6 +26,15 @@ TEST(UniqueVectorTest, Empty) {
EXPECT_EQ(unique_vec.begin(), unique_vec.end()); EXPECT_EQ(unique_vec.begin(), unique_vec.end());
} }
TEST(UniqueVectorTest, MoveConstructor) {
UniqueVector<int> unique_vec(std::vector<int>{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) { TEST(UniqueVectorTest, AddUnique) {
UniqueVector<int> unique_vec; UniqueVector<int> unique_vec;
unique_vec.add(0); unique_vec.add(0);
@ -36,6 +46,9 @@ TEST(UniqueVectorTest, AddUnique) {
EXPECT_EQ(n, i); EXPECT_EQ(n, i);
i++; i++;
} }
EXPECT_EQ(unique_vec[0], 0);
EXPECT_EQ(unique_vec[1], 1);
EXPECT_EQ(unique_vec[2], 2);
} }
TEST(UniqueVectorTest, AddDuplicates) { TEST(UniqueVectorTest, AddDuplicates) {
@ -52,6 +65,9 @@ TEST(UniqueVectorTest, AddDuplicates) {
EXPECT_EQ(n, i); EXPECT_EQ(n, i);
i++; i++;
} }
EXPECT_EQ(unique_vec[0], 0);
EXPECT_EQ(unique_vec[1], 1);
EXPECT_EQ(unique_vec[2], 2);
} }
TEST(UniqueVectorTest, AsVector) { TEST(UniqueVectorTest, AsVector) {
@ -73,4 +89,5 @@ TEST(UniqueVectorTest, AsVector) {
} }
} // namespace } // namespace
} // namespace utils
} // namespace tint } // namespace tint