Skip to content

Commit

Permalink
✨ Add find method
Browse files Browse the repository at this point in the history
  • Loading branch information
yosh-matsuda committed Feb 12, 2024
1 parent 47f4025 commit 713cc9b
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 1 deletion.
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -696,7 +696,8 @@ yyjson::reader::const_object_iter cbegin() const;
yyjson::reader::const_object_iter begin() const;
yyjson::reader::const_object_iter cend() const;
yyjson::reader::const_object_iter end() const;
yyjson::reader::const_value_ref operator[](std::string_view key) const;
yyjson::reader::const_object_iter find(std::string_view key) const; // Note: O(N)
yyjson::reader::const_value_ref operator[](std::string_view key) const; // Note: O(N)
std::size_t size() const;
bool empty() const;
bool contains(std::string_view key) const; // Note: O(N)
Expand Down Expand Up @@ -1058,8 +1059,10 @@ const_object_iter cbegin() const;
const_object_iter cend() const;
const_object_iter begin() const;
const_object_iter end() const;
const_object_iter find(std::string_view key) const; // Note: O(N)
object_iter begin();
object_iter end();
object_iter find(std::string_view key); // Note: O(N)
std::size_t size() const;
bool empty() const;
void erase(std::string_view);
Expand Down
29 changes: 29 additions & 0 deletions include/cpp_yyjson.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2457,6 +2457,15 @@ namespace yyjson
[[nodiscard]] auto end() const noexcept { return cend(); }
[[nodiscard]] auto size() const noexcept { return object_size(); }
[[nodiscard]] auto empty() const noexcept { return object_empty(); }
[[nodiscard]] auto find(std::string_view key) const noexcept
{
auto it = begin();
for (; it != end(); ++it)
{
if (it->first == key) break;
}
return it;
}
[[nodiscard]] bool contains(std::string_view key) const noexcept { return object_contains(key); }
auto operator[](std::string_view key) const { return const_value_ref(base::doc_, object_get(key)); }

Expand Down Expand Up @@ -2720,6 +2729,16 @@ namespace yyjson
{
return object_iter<DocType>(*this, base::object_iter_end());
}
[[nodiscard]] auto find(std::string_view key) noexcept
requires (!std::is_const_v<std::remove_reference_t<DocType>>)
{
auto it = begin();
for (; it != end(); ++it)
{
if (it->first == key) break;
}
return it;
}
auto operator[](std::string_view key) noexcept
{
for (const auto& v : *this)
Expand All @@ -2729,6 +2748,7 @@ namespace yyjson

[[nodiscard]] auto begin() const noexcept { return base::begin(); }
[[nodiscard]] auto end() const noexcept { return base::end(); }
[[nodiscard]] auto find(std::string_view key) const noexcept { return base::find(key); }
auto operator[](std::string_view key) const { return base::operator[](key); }
};

Expand Down Expand Up @@ -3151,6 +3171,15 @@ namespace yyjson
[[nodiscard]] auto end() const noexcept { return cend(); }
[[nodiscard]] auto size() const noexcept { return object_size(); }
[[nodiscard]] auto empty() const noexcept { return object_empty(); }
[[nodiscard]] auto find(std::string_view key) const noexcept
{
auto it = begin();
for (; it != end(); ++it)
{
if (it->first == key) break;
}
return it;
}
[[nodiscard]] bool contains(std::string_view key) const noexcept { return object_contains(key); }
auto operator[](std::string_view key) const { return const_value_ref(object_get(key)); }

Expand Down
13 changes: 13 additions & 0 deletions test/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1972,6 +1972,12 @@ TEST(Writer, ObjectConversion)
static_assert(std::same_as<decltype(*std::declval<const object_ref&>().cend()), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<const_object_ref&>().cend()), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<const const_object_ref&>().cend()), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<object&>().find("")), key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<const object&>().find("")), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<object_ref&>().find("")), key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<const object_ref&>().find("")), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<const_object_ref&>().find("")), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(*std::declval<const const_object_ref&>().find("")), const_key_value_ref_pair>);
static_assert(std::same_as<decltype(std::declval<object&>()["a"]), value_ref>);
static_assert(std::same_as<decltype(std::declval<const object&>()["a"]), const_value_ref>);
static_assert(std::same_as<decltype(std::declval<object_ref&>()["a"]), value_ref>);
Expand Down Expand Up @@ -2067,8 +2073,12 @@ TEST(Writer, ObjectMethodExample)
const auto& num_obj_const = num_obj;
EXPECT_EQ(num_map.size() + 1, num_obj.size());
EXPECT_FALSE(num_obj.empty());
EXPECT_TRUE(num_obj.contains("0"));
EXPECT_TRUE(num_obj.contains("X"));
EXPECT_FALSE(num_obj.contains("Y"));
EXPECT_EQ(num_obj.find("0")->second.as_int(), num_obj["0"].as_int());
EXPECT_EQ(num_obj.find("X")->second.as_int(), num_obj["X"].as_int());
EXPECT_EQ(num_obj.find("Y"), num_obj.end());

// concepts
static_assert(std::ranges::input_range<object&>);
Expand Down Expand Up @@ -2745,6 +2755,7 @@ TEST(Reader, ObjectConversion)
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().cend()), const_object_iter>);
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().begin()), const_object_iter>);
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().end()), const_object_iter>);
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().find("")), const_object_iter>);
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().size()), std::size_t>);
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().empty()), bool>);
static_assert(std::same_as<decltype(std::declval<const const_object_ref&>().contains("")), bool>);
Expand Down Expand Up @@ -2772,6 +2783,8 @@ TEST(Reader, ObjectExamples)
EXPECT_TRUE(doc.as_object()->contains("first_key"));
EXPECT_TRUE(doc.as_object()->contains("second_key"));
EXPECT_FALSE(doc.as_object()->contains("third_key"));
EXPECT_NE(doc.as_object()->find("first_key"), doc.as_object()->end());
EXPECT_EQ(doc.as_object()->find("thrid_key"), doc.as_object()->end());

auto obj = *doc.as_object();
auto iter = obj.begin();
Expand Down

0 comments on commit 713cc9b

Please sign in to comment.