Skip to content

Commit 2d2ebca

Browse files
Add an equal_range utility function
This is to make the equal_range method of multimap easier to use. The usage is illustrated in this commit by an example in java_static_initializers which allows use to use range-for. This will be also useful in following commits.
1 parent 3ed8e34 commit 2d2ebca

File tree

2 files changed

+16
-5
lines changed

2 files changed

+16
-5
lines changed

jbmc/src/java_bytecode/java_static_initializers.cpp

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -988,21 +988,21 @@ code_blockt stub_global_initializer_factoryt::get_stub_initializer_body(
988988
// class. Note this is the same invocation used in
989989
// java_static_lifetime_init.
990990

991-
auto class_globals = stub_globals_by_class.equal_range(*class_id);
991+
auto class_globals = equal_range(stub_globals_by_class, *class_id);
992992
INVARIANT(
993-
class_globals.first != class_globals.second,
993+
!class_globals.empty(),
994994
"class with synthetic clinit should have at least one global to init");
995995

996996
java_object_factory_parameterst parameters = object_factory_parameters;
997997
parameters.function_id = function_id;
998998

999-
for(auto it = class_globals.first; it != class_globals.second; ++it)
999+
for(const auto &pair : class_globals)
10001000
{
10011001
const symbol_exprt new_global_symbol =
1002-
symbol_table.lookup_ref(it->second).symbol_expr();
1002+
symbol_table.lookup_ref(pair.second).symbol_expr();
10031003

10041004
parameters.min_null_tree_depth =
1005-
is_non_null_library_global(it->second)
1005+
is_non_null_library_global(pair.second)
10061006
? object_factory_parameters.min_null_tree_depth + 1
10071007
: object_factory_parameters.min_null_tree_depth;
10081008

src/util/range.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -533,4 +533,15 @@ auto make_range(containert &container) -> ranget<decltype(container.begin())>
533533
container.begin(), container.end());
534534
}
535535

536+
/// Utility function to make equal_range method of multimap easier to use by
537+
/// returning a ranget object. For instance, we can write:
538+
/// `for(auto value : equal_range(map, key).filter(...).map(...)) {...}`.
539+
template <typename multimapt>
540+
ranget<typename multimapt::const_iterator>
541+
equal_range(const multimapt &multimap, const typename multimapt::key_type &key)
542+
{
543+
auto iterator_pair = multimap.equal_range(key);
544+
return make_range(iterator_pair.first, iterator_pair.second);
545+
}
546+
536547
#endif // CPROVER_UTIL_RANGE_H

0 commit comments

Comments
 (0)