utils: Add Lookup()
A find or return-default utility. Rename `find_or_replace.h` to `map.h` which contains both these utilties that operate on maps. Change-Id: Iaa76ea3f5c5a1210e413c131dd0556c126ee5d0a Reviewed-on: https://dawn-review.googlesource.com/c/tint/+/70521 Kokoro: Kokoro <noreply+kokoro@google.com> Reviewed-by: Antonio Maiorano <amaiorano@google.com> Commit-Queue: Ben Clayton <bclayton@google.com>
This commit is contained in:
parent
5af571bcbc
commit
1185d61648
|
@ -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",
|
||||
|
|
|
@ -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
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include <string>
|
||||
|
||||
#include "src/program_builder.h"
|
||||
#include "src/utils/get_or_create.h"
|
||||
#include "src/utils/map.h"
|
||||
|
||||
TINT_INSTANTIATE_TYPEINFO(tint::Cloneable);
|
||||
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -19,7 +19,7 @@
|
|||
#include <utility>
|
||||
|
||||
#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);
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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 <unordered_map>
|
||||
|
||||
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 <typename K, typename V, typename H, typename C, typename KV = K>
|
||||
V Lookup(std::unordered_map<K, V, H, C>& 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<K, V, H, C>& map,
|
|||
} // namespace utils
|
||||
} // namespace tint
|
||||
|
||||
#endif // SRC_UTILS_GET_OR_CREATE_H_
|
||||
#endif // SRC_UTILS_MAP_H_
|
|
@ -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 <unordered_map>
|
||||
|
||||
|
@ -22,6 +22,15 @@ namespace tint {
|
|||
namespace utils {
|
||||
namespace {
|
||||
|
||||
TEST(Lookup, Test) {
|
||||
std::unordered_map<int, int> 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<int, int> map;
|
||||
EXPECT_EQ(GetOrCreate(map, 1, [&] { return 2; }), 2);
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
|
|
@ -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"
|
||||
|
||||
|
|
|
@ -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 {
|
||||
|
|
|
@ -17,7 +17,7 @@
|
|||
#include <algorithm>
|
||||
#include <limits>
|
||||
|
||||
#include "src/utils/get_or_create.h"
|
||||
#include "src/utils/map.h"
|
||||
|
||||
namespace tint {
|
||||
namespace writer {
|
||||
|
|
|
@ -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",
|
||||
|
|
Loading…
Reference in New Issue