diff --git a/src/BUILD.gn b/src/BUILD.gn index b87783b662..7fdc018e7b 100644 --- a/src/BUILD.gn +++ b/src/BUILD.gn @@ -485,8 +485,8 @@ libtint_source_set("libtint_core_all_src") { "transform/zero_init_workgroup_memory.cc", "transform/zero_init_workgroup_memory.h", "utils/enum_set.h", - "utils/get_or_create.h", "utils/hash.h", + "utils/map.h", "utils/math.h", "utils/scoped_assignment.h", "utils/string.h", diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt index 6c901a4bb6..d605d49309 100644 --- a/src/CMakeLists.txt +++ b/src/CMakeLists.txt @@ -397,8 +397,8 @@ set(TINT_LIB_SRCS sem/void_type.cc sem/void_type.h utils/enum_set.h - utils/get_or_create.h utils/hash.h + utils/map.h utils/math.h utils/scoped_assignment.h utils/string.h @@ -730,10 +730,10 @@ if(${TINT_BUILD_TESTS}) transform/transform_test.cc utils/defer_test.cc utils/enum_set_test.cc - utils/get_or_create_test.cc utils/hash_test.cc utils/io/command_test.cc utils/io/tmpfile_test.cc + utils/map_test.cc utils/math_test.cc utils/reverse_test.cc utils/scoped_assignment_test.cc diff --git a/src/clone_context.cc b/src/clone_context.cc index e56f754190..bf021c3178 100644 --- a/src/clone_context.cc +++ b/src/clone_context.cc @@ -17,7 +17,7 @@ #include #include "src/program_builder.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" TINT_INSTANTIATE_TYPEINFO(tint::Cloneable); diff --git a/src/intrinsic_table.cc b/src/intrinsic_table.cc index cb7c02f987..7c0f13d933 100644 --- a/src/intrinsic_table.cc +++ b/src/intrinsic_table.cc @@ -28,8 +28,8 @@ #include "src/sem/pipeline_stage_set.h" #include "src/sem/sampled_texture_type.h" #include "src/sem/storage_texture_type.h" -#include "src/utils/get_or_create.h" #include "src/utils/hash.h" +#include "src/utils/map.h" #include "src/utils/math.h" #include "src/utils/scoped_assignment.h" diff --git a/src/reader/spirv/parser_type.cc b/src/reader/spirv/parser_type.cc index 90f250bb87..63d51238d9 100644 --- a/src/reader/spirv/parser_type.cc +++ b/src/reader/spirv/parser_type.cc @@ -19,7 +19,7 @@ #include #include "src/program_builder.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/hash.h" TINT_INSTANTIATE_TYPEINFO(tint::reader::spirv::Type); diff --git a/src/resolver/resolver.cc b/src/resolver/resolver.cc index 156174c588..b075644caa 100644 --- a/src/resolver/resolver.cc +++ b/src/resolver/resolver.cc @@ -74,7 +74,7 @@ #include "src/sem/type_conversion.h" #include "src/sem/variable.h" #include "src/utils/defer.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/math.h" #include "src/utils/reverse.h" #include "src/utils/scoped_assignment.h" diff --git a/src/resolver/resolver_constants.cc b/src/resolver/resolver_constants.cc index d2ba745481..0386239ae0 100644 --- a/src/resolver/resolver_constants.cc +++ b/src/resolver/resolver_constants.cc @@ -16,7 +16,7 @@ #include "src/sem/constant.h" #include "src/sem/type_constructor.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" namespace tint { namespace resolver { diff --git a/src/resolver/resolver_validation.cc b/src/resolver/resolver_validation.cc index a9f4013cb9..7b648f114c 100644 --- a/src/resolver/resolver_validation.cc +++ b/src/resolver/resolver_validation.cc @@ -74,7 +74,7 @@ #include "src/sem/type_conversion.h" #include "src/sem/variable.h" #include "src/utils/defer.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/math.h" #include "src/utils/reverse.h" #include "src/utils/scoped_assignment.h" diff --git a/src/transform/calculate_array_length.cc b/src/transform/calculate_array_length.cc index 82f8fcb85d..05076850ad 100644 --- a/src/transform/calculate_array_length.cc +++ b/src/transform/calculate_array_length.cc @@ -25,7 +25,7 @@ #include "src/sem/statement.h" #include "src/sem/struct.h" #include "src/sem/variable.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/hash.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::CalculateArrayLength); diff --git a/src/transform/decompose_memory_access.cc b/src/transform/decompose_memory_access.cc index bd40a6fe80..1281ccb521 100644 --- a/src/transform/decompose_memory_access.cc +++ b/src/transform/decompose_memory_access.cc @@ -35,7 +35,7 @@ #include "src/sem/statement.h" #include "src/sem/struct.h" #include "src/sem/variable.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/hash.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::DecomposeMemoryAccess); diff --git a/src/transform/decompose_strided_matrix.cc b/src/transform/decompose_strided_matrix.cc index 23f98aed45..0b89315b94 100644 --- a/src/transform/decompose_strided_matrix.cc +++ b/src/transform/decompose_strided_matrix.cc @@ -23,7 +23,7 @@ #include "src/sem/member_accessor_expression.h" #include "src/transform/inline_pointer_lets.h" #include "src/transform/simplify.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/hash.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::DecomposeStridedMatrix); diff --git a/src/transform/pad_array_elements.cc b/src/transform/pad_array_elements.cc index a14ac7b77a..b732af5fff 100644 --- a/src/transform/pad_array_elements.cc +++ b/src/transform/pad_array_elements.cc @@ -22,7 +22,7 @@ #include "src/sem/call.h" #include "src/sem/expression.h" #include "src/sem/type_constructor.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::PadArrayElements); diff --git a/src/transform/remove_phonies.cc b/src/transform/remove_phonies.cc index fe8b75f4b5..ddf2697282 100644 --- a/src/transform/remove_phonies.cc +++ b/src/transform/remove_phonies.cc @@ -25,7 +25,7 @@ #include "src/sem/function.h" #include "src/sem/statement.h" #include "src/sem/variable.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/scoped_assignment.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::RemovePhonies); diff --git a/src/transform/vertex_pulling.cc b/src/transform/vertex_pulling.cc index 3403fccfce..40dd564c54 100644 --- a/src/transform/vertex_pulling.cc +++ b/src/transform/vertex_pulling.cc @@ -23,7 +23,7 @@ #include "src/ast/variable_decl_statement.h" #include "src/program_builder.h" #include "src/sem/variable.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/math.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::VertexPulling); diff --git a/src/transform/wrap_arrays_in_structs.cc b/src/transform/wrap_arrays_in_structs.cc index f034e33441..62d18ac45d 100644 --- a/src/transform/wrap_arrays_in_structs.cc +++ b/src/transform/wrap_arrays_in_structs.cc @@ -21,7 +21,7 @@ #include "src/sem/call.h" #include "src/sem/expression.h" #include "src/sem/type_constructor.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/transform.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::WrapArraysInStructs); diff --git a/src/transform/zero_init_workgroup_memory.cc b/src/transform/zero_init_workgroup_memory.cc index 66176ef4d2..86013562fc 100644 --- a/src/transform/zero_init_workgroup_memory.cc +++ b/src/transform/zero_init_workgroup_memory.cc @@ -25,7 +25,7 @@ #include "src/sem/atomic_type.h" #include "src/sem/function.h" #include "src/sem/variable.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/unique_vector.h" TINT_INSTANTIATE_TYPEINFO(tint::transform::ZeroInitWorkgroupMemory); diff --git a/src/utils/get_or_create.h b/src/utils/map.h similarity index 65% rename from src/utils/get_or_create.h rename to src/utils/map.h index dd5e151cd8..db1b153e24 100644 --- a/src/utils/get_or_create.h +++ b/src/utils/map.h @@ -12,14 +12,30 @@ // See the License for the specific language governing permissions and // limitations under the License. -#ifndef SRC_UTILS_GET_OR_CREATE_H_ -#define SRC_UTILS_GET_OR_CREATE_H_ +#ifndef SRC_UTILS_MAP_H_ +#define SRC_UTILS_MAP_H_ #include namespace tint { namespace utils { +/// Lookup is a utility function for fetching a value from an unordered map if +/// it exists, otherwise returning the `if_missing` argument. +/// @param map the unordered_map +/// @param key the map key of the item to query +/// @param if_missing the value to return if the map does not contain the given +/// key. Defaults to the zero-initializer for the value type. +/// @return the map item value, or `if_missing` if the map does not contain the +/// given key +template +V Lookup(std::unordered_map& map, + const KV& key, + const KV& if_missing = {}) { + auto it = map.find(key); + return it != map.end() ? it->second : if_missing; +} + /// GetOrCreate is a utility function for lazily adding to an unordered map. /// If the map already contains the key `key` then this is returned, otherwise /// `create()` is called and the result is added to the map and is returned. @@ -43,4 +59,4 @@ V GetOrCreate(std::unordered_map& map, } // namespace utils } // namespace tint -#endif // SRC_UTILS_GET_OR_CREATE_H_ +#endif // SRC_UTILS_MAP_H_ diff --git a/src/utils/get_or_create_test.cc b/src/utils/map_test.cc similarity index 77% rename from src/utils/get_or_create_test.cc rename to src/utils/map_test.cc index ae0c499fa5..11f3ba8db1 100644 --- a/src/utils/get_or_create_test.cc +++ b/src/utils/map_test.cc @@ -12,7 +12,7 @@ // See the License for the specific language governing permissions and // limitations under the License. -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include @@ -22,6 +22,15 @@ namespace tint { namespace utils { namespace { +TEST(Lookup, Test) { + std::unordered_map map; + map.emplace(10, 1); + EXPECT_EQ(Lookup(map, 10, 0), 1); // exists, with if_missing + EXPECT_EQ(Lookup(map, 10), 1); // exists, without if_missing + EXPECT_EQ(Lookup(map, 20, 50), 50); // missing, with if_missing + EXPECT_EQ(Lookup(map, 20), 0); // missing, without if_missing +} + TEST(GetOrCreateTest, NewKey) { std::unordered_map map; EXPECT_EQ(GetOrCreate(map, 1, [&] { return 2; }), 2); diff --git a/src/writer/glsl/generator_impl.cc b/src/writer/glsl/generator_impl.cc index 805fcbb304..b2eb4f4946 100644 --- a/src/writer/glsl/generator_impl.cc +++ b/src/writer/glsl/generator_impl.cc @@ -47,7 +47,7 @@ #include "src/transform/calculate_array_length.h" #include "src/transform/glsl.h" #include "src/utils/defer.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/scoped_assignment.h" #include "src/writer/append_vector.h" #include "src/writer/float_to_string.h" diff --git a/src/writer/hlsl/generator_impl.cc b/src/writer/hlsl/generator_impl.cc index 76b46c1a20..664ca0a852 100644 --- a/src/writer/hlsl/generator_impl.cc +++ b/src/writer/hlsl/generator_impl.cc @@ -61,7 +61,7 @@ #include "src/transform/simplify.h" #include "src/transform/zero_init_workgroup_memory.h" #include "src/utils/defer.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/scoped_assignment.h" #include "src/writer/append_vector.h" #include "src/writer/float_to_string.h" diff --git a/src/writer/msl/generator_impl.cc b/src/writer/msl/generator_impl.cc index 4cfc22e9fa..73cd51fcac 100644 --- a/src/writer/msl/generator_impl.cc +++ b/src/writer/msl/generator_impl.cc @@ -71,7 +71,7 @@ #include "src/transform/wrap_arrays_in_structs.h" #include "src/transform/zero_init_workgroup_memory.h" #include "src/utils/defer.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/utils/scoped_assignment.h" #include "src/writer/float_to_string.h" diff --git a/src/writer/spirv/builder.cc b/src/writer/spirv/builder.cc index c17cb4de1c..1894795e2f 100644 --- a/src/writer/spirv/builder.cc +++ b/src/writer/spirv/builder.cc @@ -51,7 +51,7 @@ #include "src/transform/vectorize_scalar_matrix_constructors.h" #include "src/transform/zero_init_workgroup_memory.h" #include "src/utils/defer.h" -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" #include "src/writer/append_vector.h" namespace tint { diff --git a/src/writer/text_generator.cc b/src/writer/text_generator.cc index 05a76c65fe..405407bded 100644 --- a/src/writer/text_generator.cc +++ b/src/writer/text_generator.cc @@ -17,7 +17,7 @@ #include #include -#include "src/utils/get_or_create.h" +#include "src/utils/map.h" namespace tint { namespace writer { diff --git a/test/BUILD.gn b/test/BUILD.gn index 5bb1335904..010013a8c2 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -339,10 +339,10 @@ tint_unittests_source_set("tint_unittests_utils_src") { sources = [ "../src/utils/defer_test.cc", "../src/utils/enum_set_test.cc", - "../src/utils/get_or_create_test.cc", "../src/utils/hash_test.cc", "../src/utils/io/command_test.cc", "../src/utils/io/tmpfile_test.cc", + "../src/utils/map_test.cc", "../src/utils/math_test.cc", "../src/utils/reverse_test.cc", "../src/utils/scoped_assignment_test.cc",