Skip to content

Commit

Permalink
Better return type for find
Browse files Browse the repository at this point in the history
  • Loading branch information
BrainStone committed Oct 29, 2023
1 parent da64dcc commit ccfa141
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 12 deletions.
14 changes: 8 additions & 6 deletions src/static_string_map.hpp
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include <algorithm>
#include <array>
#include <optional>
#include <string_view>
#include <utility>

Expand All @@ -10,6 +11,7 @@ class static_string_map {
using entry_t = std::pair<str_t, str_t>;
using map_t = std::array<entry_t, N>;
using it_t = typename map_t::const_iterator;
using find_t = std::optional<str_t>;

// Mimic std::map's size functions
static constexpr std::size_t size = N;
Expand All @@ -29,22 +31,22 @@ class static_string_map {
constexpr explicit static_string_map(const map_t& map) : sorted_map{sort_map(map)} {}
constexpr explicit static_string_map(const entry_t (&&entries)[N]) : static_string_map{std::to_array(entries)} {}

[[nodiscard]] constexpr std::pair<bool, it_t> find(str_t key) const {
[[nodiscard]] constexpr find_t find(str_t key) const {
const it_t begin = sorted_map.cbegin();
const it_t end = sorted_map.end();

const it_t result =
std::lower_bound(begin, end, key, [](const entry_t& a, const std::string_view& b) { return a.first < b; });

return std::make_pair((result != end) && (result->first == key), result);
return (result != end) && (result->first == key) ? std::make_optional(result->second) : std::nullopt;
}

[[nodiscard]] constexpr str_t at(str_t key) const {
const std::pair<bool, it_t> search_result = find(key);
const find_t search_result = find(key);

if (search_result.first) [[likely]] {
if (search_result) [[likely]] {
// Found key
return search_result.second->second;
return *search_result;
} else {
using namespace std::literals::string_literals;
// Key not found
Expand All @@ -53,7 +55,7 @@ class static_string_map {
}

[[nodiscard]] constexpr bool contains(str_t key) const {
return find(key).first;
return find(key).has_value();
}

[[nodiscard]] constexpr str_t operator[](str_t key) const {
Expand Down
6 changes: 3 additions & 3 deletions test/test_constexpr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ TEST(TestConstexpr, Find) {
constexpr auto hello = map.find("hello");
constexpr auto hi = map.find("hi");

EXPECT_TRUE(hello.first);
EXPECT_EQ(hello.second->second, "world");
EXPECT_TRUE(hello);
EXPECT_EQ(*hello, "world");

EXPECT_FALSE(hi.first);
EXPECT_FALSE(hi);
}
6 changes: 3 additions & 3 deletions test/test_dynamic.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ TEST(TestDynamic, Find) {
const auto hello = map.find("hello");
const auto hi = map.find("hi");

EXPECT_TRUE(hello.first);
EXPECT_EQ(hello.second->second, "world");
EXPECT_TRUE(hello);
EXPECT_EQ(*hello, "world");

EXPECT_FALSE(hi.first);
EXPECT_FALSE(hi);
}

0 comments on commit ccfa141

Please sign in to comment.