Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
P
pybind11_abseil
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
open
pybind11_abseil
Commits
9d109700
Commit
9d109700
authored
Mar 04, 2022
by
CJ Carey
Committed by
Copybara-Service
Mar 04, 2022
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Internal change
PiperOrigin-RevId: 432516918
parent
5b334624
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
49 additions
and
1 deletions
+49
-1
README.md
+1
-1
pybind11_abseil/BUILD
+1
-0
pybind11_abseil/absl_casters.h
+6
-0
pybind11_abseil/tests/BUILD
+1
-0
pybind11_abseil/tests/absl_example.cc
+28
-0
pybind11_abseil/tests/absl_test.py
+12
-0
No files found.
README.md
View file @
9d109700
...
@@ -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`
.
...
...
pybind11_abseil/BUILD
View file @
9d109700
...
@@ -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",
...
...
pybind11_abseil/absl_casters.h
View file @
9d109700
...
@@ -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
...
...
pybind11_abseil/tests/BUILD
View file @
9d109700
...
@@ -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",
...
...
pybind11_abseil/tests/absl_example.cc
View file @
9d109700
...
@@ -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
);
...
...
pybind11_abseil/tests/absl_test.py
View file @
9d109700
...
@@ -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
):
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment