Commit 9d109700 by CJ Carey Committed by Copybara-Service

Internal change

PiperOrigin-RevId: 432516918
parent 5b334624
...@@ -154,7 +154,7 @@ Supported exactly the same way pybind11 supports `std::string_view`. ...@@ -154,7 +154,7 @@ Supported exactly the same way pybind11 supports `std::string_view`.
Supported exactly the same way pybind11 supports `std::optional`. Supported exactly the same way pybind11 supports `std::optional`.
## absl::flat_hash_map ## absl::flat_hash_map and absl::btree_map
Supported exactly the same way pybind11 supports `std::map`. Supported exactly the same way pybind11 supports `std::map`.
......
...@@ -13,6 +13,7 @@ pybind_library( ...@@ -13,6 +13,7 @@ pybind_library(
hdrs = ["absl_casters.h"], hdrs = ["absl_casters.h"],
deps = [ deps = [
"@com_google_absl//absl/cleanup", "@com_google_absl//absl/cleanup",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_map", "@com_google_absl//absl/container:flat_hash_map",
"@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
......
...@@ -39,6 +39,7 @@ ...@@ -39,6 +39,7 @@
#include <vector> #include <vector>
#include "absl/cleanup/cleanup.h" #include "absl/cleanup/cleanup.h"
#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_map.h" #include "absl/container/flat_hash_map.h"
#include "absl/container/flat_hash_set.h" #include "absl/container/flat_hash_set.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
...@@ -366,6 +367,11 @@ template <typename Key, typename Hash, typename Equal, typename Alloc> ...@@ -366,6 +367,11 @@ template <typename Key, typename Hash, typename Equal, typename Alloc>
struct type_caster<absl::flat_hash_set<Key, Hash, Equal, Alloc>> struct type_caster<absl::flat_hash_set<Key, Hash, Equal, Alloc>>
: set_caster<absl::flat_hash_set<Key, Hash, Equal, Alloc>, Key> {}; : set_caster<absl::flat_hash_set<Key, Hash, Equal, Alloc>, Key> {};
// Convert between absl::btree_map and python dict.
template <typename Key, typename Value, typename Compare, typename Alloc>
struct type_caster<absl::btree_map<Key, Value, Compare, Alloc>>
: map_caster<absl::btree_map<Key, Value, Compare, Alloc>, Key, Value> {};
// Convert between absl::string_view and python. // Convert between absl::string_view and python.
// //
// pybind11 supports std::string_view, and absl::string_view is meant to be a // pybind11 supports std::string_view, and absl::string_view is meant to be a
......
...@@ -10,6 +10,7 @@ pybind_extension( ...@@ -10,6 +10,7 @@ pybind_extension(
srcs = ["absl_example.cc"], srcs = ["absl_example.cc"],
deps = [ deps = [
"//pybind11_abseil:absl_casters", "//pybind11_abseil:absl_casters",
"@com_google_absl//absl/container:btree",
"@com_google_absl//absl/container:flat_hash_set", "@com_google_absl//absl/container:flat_hash_set",
"@com_google_absl//absl/strings", "@com_google_absl//absl/strings",
"@com_google_absl//absl/time", "@com_google_absl//absl/time",
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include <cstddef> #include <cstddef>
#include <vector> #include <vector>
#include "absl/container/btree_map.h"
#include "absl/container/flat_hash_set.h" #include "absl/container/flat_hash_set.h"
#include "absl/strings/str_cat.h" #include "absl/strings/str_cat.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
...@@ -163,6 +164,29 @@ bool CheckSet(const absl::flat_hash_set<int>& set, ...@@ -163,6 +164,29 @@ bool CheckSet(const absl::flat_hash_set<int>& set,
return set == check; return set == check;
} }
absl::btree_map<int, int> MakeBtreeMap(
const std::vector<std::pair<int, int>>& keys_and_values) {
absl::btree_map<int, int> map;
for (const auto& kvp : keys_and_values) {
map.insert(kvp);
}
return map;
}
bool CheckBtreeMap(const absl::btree_map<int, int>& map,
const std::vector<std::pair<int, int>>& keys_and_values) {
for (const auto& kvp : keys_and_values) {
auto found = map.find(kvp.first);
if (found == map.end()) {
return false;
}
if (found->second != kvp.second) {
return false;
}
}
return true;
}
// Span // Span
bool CheckSpan(absl::Span<const int> span, const std::vector<int>& values) { bool CheckSpan(absl::Span<const int> span, const std::vector<int>& values) {
if (span.size() != values.size()) return false; if (span.size() != values.size()) return false;
...@@ -321,6 +345,10 @@ PYBIND11_MODULE(absl_example, m) { ...@@ -321,6 +345,10 @@ PYBIND11_MODULE(absl_example, m) {
m.def("make_set", &MakeSet, arg("values")); m.def("make_set", &MakeSet, arg("values"));
m.def("check_set", &CheckSet, arg("set"), arg("values")); m.def("check_set", &CheckSet, arg("set"), arg("values"));
// absl::btree_map bindings
m.def("make_btree_map", &MakeBtreeMap, arg("keys_and_values"));
m.def("check_btree_map", &CheckBtreeMap, arg("map"), arg("keys_and_values"));
// absl::variant // absl::variant
class_<A>(m, "A").def(init<int>()).def_readonly("a", &A::a); class_<A>(m, "A").def(init<int>()).def_readonly("a", &A::a);
class_<B>(m, "B").def(init<int>()).def_readonly("b", &B::b); class_<B>(m, "B").def(init<int>()).def_readonly("b", &B::b);
......
...@@ -357,6 +357,18 @@ class AbslFlatHashSetTest(absltest.TestCase): ...@@ -357,6 +357,18 @@ class AbslFlatHashSetTest(absltest.TestCase):
self.assertTrue(absl_example.check_set(set(expected), expected)) self.assertTrue(absl_example.check_set(set(expected), expected))
class AbslBTreeMapTest(absltest.TestCase):
def test_return_map(self):
keys_and_values = [(1, 2), (3, 4), (5, 6)]
expected = dict(keys_and_values)
self.assertEqual(expected, absl_example.make_btree_map(keys_and_values))
def test_pass_map(self):
expected = [(10, 20), (30, 40)]
self.assertTrue(absl_example.check_btree_map(dict(expected), expected))
class AbslOptionalTest(absltest.TestCase): class AbslOptionalTest(absltest.TestCase):
def test_pass_default_nullopt(self): def test_pass_default_nullopt(self):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment