Skip to content

Commit 61b202a

Browse files
Merge pull request #3473 from hannes-steffenhagen-diffblue/feature-optional_map_lookup
Add optional_lookup utility function
2 parents 5b66cd3 + fa39d19 commit 61b202a

File tree

3 files changed

+73
-0
lines changed

3 files changed

+73
-0
lines changed

src/util/optional_utils.h

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
/*******************************************************************\
2+
3+
Module: functions that are useful with optionalt
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#ifndef CPROVER_UTIL_OPTIONAL_UTILS_H
10+
#define CPROVER_UTIL_OPTIONAL_UTILS_H
11+
12+
#include "optional.h"
13+
14+
/// Lookup a key in a map, if found return the associated value,
15+
/// nullopt otherwise
16+
template <typename map_like_collectiont, typename keyt>
17+
auto optional_lookup(const map_like_collectiont &map, const keyt &key)
18+
-> optionalt<decltype(map.find(key)->second)>
19+
{
20+
auto const it = map.find(key);
21+
if(it != map.end())
22+
{
23+
return it->second;
24+
}
25+
return nullopt;
26+
}
27+
28+
#endif // CPROVER_UTIL_OPTIONAL_UTILS_H

unit/Makefile

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,7 @@ SRC += analyses/ai/ai.cpp \
4141
util/irep_sharing.cpp \
4242
util/message.cpp \
4343
util/optional.cpp \
44+
util/optional_utils.cpp \
4445
util/pointer_offset_size.cpp \
4546
util/range.cpp \
4647
util/replace_symbol.cpp \

unit/util/optional_utils.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
/*******************************************************************\
2+
3+
Module: optional_utils unit tests
4+
5+
Author: Diffblue Ltd.
6+
7+
\*******************************************************************/
8+
9+
#include <testing-utils/catch.hpp>
10+
11+
#include <util/optional_utils.h>
12+
13+
#include <map>
14+
#include <string>
15+
#include <unordered_map>
16+
17+
namespace
18+
{
19+
template <typename map_like_collectiont>
20+
void do_optional_lookup_test(map_like_collectiont &map)
21+
{
22+
map.insert({"hello", "world"});
23+
map.insert({"pwd", "/home"});
24+
auto const hello_result = optional_lookup(map, "hello");
25+
REQUIRE(hello_result.has_value());
26+
REQUIRE(hello_result.value() == "world");
27+
auto const pwd_result = optional_lookup(map, "pwd");
28+
REQUIRE(pwd_result.has_value());
29+
REQUIRE(pwd_result.value() == "/home");
30+
REQUIRE_FALSE(optional_lookup(map, "does not exit").has_value());
31+
}
32+
} // namespace
33+
34+
TEST_CASE("Using optional_lookup with std::map")
35+
{
36+
auto map = std::map<std::string, std::string>{};
37+
do_optional_lookup_test(map);
38+
}
39+
40+
TEST_CASE("Using optional_lookup with std::unordered_map")
41+
{
42+
auto map = std::unordered_map<std::string, std::string>{};
43+
do_optional_lookup_test(map);
44+
}

0 commit comments

Comments
 (0)