Skip to content

Commit ac5d49b

Browse files
committed
add copy dunders to some objects
1 parent d67b694 commit ac5d49b

5 files changed

Lines changed: 46 additions & 0 deletions

File tree

src/common_edge_properties.hpp

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include <reticula/network_concepts.hpp>
33

44
#include "bind_core.hpp"
5+
#include "nanobind/operators.h"
56
#include "type_str/scalars.hpp"
67
#include "type_str/edges.hpp"
78
#include "type_utils.hpp"
@@ -46,6 +47,11 @@ nb::class_<EdgeT> define_basic_edge_concept(nb::module_& m) {
4647
nb::call_guard<nb::gil_scoped_release>())
4748
.def(nb::hash(nb::self),
4849
nb::call_guard<nb::gil_scoped_release>())
50+
.def("__copy__", [](const EdgeT& self) {
51+
return EdgeT(self);
52+
}).def("__deepcopy__", [](const EdgeT& self, nb::dict) {
53+
return EdgeT(self);
54+
}, "memo"_a)
4955
.def("__repr__", [](const EdgeT& a) {
5056
return fmt::format("{}", a);
5157
}).def_static("__class_repr__", []() {

src/components.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,11 @@ struct declare_component_types {
6464
.def("__contains__", &Component::contains,
6565
"vertex"_a,
6666
nb::call_guard<nb::gil_scoped_release>())
67+
.def("__copy__", [](const Component& self) {
68+
return Component(self);
69+
}).def("__deepcopy__", [](const Component& self, nb::dict) {
70+
return Component(self);
71+
}, "memo"_a)
6772
.def("__repr__", [](const Component& c) {
6873
return fmt::format("{}", c);
6974
}).def_static("vertex_type", []() {
@@ -84,6 +89,11 @@ struct declare_component_types {
8489
python_type_str<ComponentSize>().c_str())
8590
.def("size", &ComponentSize::size,
8691
nb::call_guard<nb::gil_scoped_release>())
92+
.def("__copy__", [](const ComponentSize& self) {
93+
return ComponentSize(self);
94+
}).def("__deepcopy__", [](const ComponentSize& self, nb::dict) {
95+
return ComponentSize(self);
96+
}, "memo"_a)
8797
.def("__repr__", [](const ComponentSize& c) {
8898
return fmt::format("{}", c);
8999
}).def_static("vertex_type", []() {
@@ -102,6 +112,11 @@ struct declare_component_types {
102112
.def("size_estimate",
103113
&ComponentSizeEstimate::size_estimate,
104114
nb::call_guard<nb::gil_scoped_release>())
115+
.def("__copy__", [](const ComponentSizeEstimate& self) {
116+
return ComponentSizeEstimate(self);
117+
}).def("__deepcopy__", [](const ComponentSizeEstimate& self, nb::dict) {
118+
return ComponentSizeEstimate(self);
119+
}, "memo"_a)
105120
.def("__repr__", [](const ComponentSizeEstimate& c) {
106121
return fmt::format("{}", c);
107122
})

src/networks.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ struct declare_network_class {
9393
nb::call_guard<nb::gil_scoped_release>())
9494
.def(nb::self != nb::self,
9595
nb::call_guard<nb::gil_scoped_release>())
96+
.def("__copy__", [](const Net& self) {
97+
return Net(self);
98+
}).def("__deepcopy__", [](const Net& self, nb::dict) {
99+
return Net(self);
100+
}, "memo"_a)
96101
.def("__repr__", [](const Net& a) {
97102
return fmt::format("{}", a);
98103
}).def_static("__class_repr__", []() {

src/random_state.cpp

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,11 @@ void declare_random_states(nb::module_& m) {
1515
}, nb::call_guard<nb::gil_scoped_release>())
1616
.def(nb::init<std::mt19937_64::result_type>(),
1717
"seed"_a, nb::call_guard<nb::gil_scoped_release>())
18+
.def("__copy__", [](const std::mt19937_64& self) {
19+
return std::mt19937_64(self);
20+
}).def("__deepcopy__", [](const std::mt19937_64& self, nb::dict) {
21+
return std::mt19937_64(self);
22+
}, "memo"_a)
1823
.def("__call__", [](std::mt19937_64& self) { return self(); })
1924
.def_static("__class_repr__", []() {
2025
return fmt::format("<class '{}'>", type_str<std::mt19937_64>{}());

src/temporal_clusters.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,11 @@ struct declare_temporal_cluster_types {
7171
&Cluster::contains,
7272
"event"_a,
7373
nb::call_guard<nb::gil_scoped_release>())
74+
.def("__copy__", [](const Cluster& self) {
75+
return Cluster(self);
76+
}).def("__deepcopy__", [](const Cluster& self, nb::dict) {
77+
return Cluster(self);
78+
}, "memo"_a)
7479
.def("__repr__", [](const Cluster& c) {
7580
return fmt::format("{}", c);
7681
})
@@ -102,6 +107,11 @@ struct declare_temporal_cluster_types {
102107
nb::call_guard<nb::gil_scoped_release>())
103108
.def("__len__", &ClusterSize::size,
104109
nb::call_guard<nb::gil_scoped_release>())
110+
.def("__copy__", [](const ClusterSize& self) {
111+
return ClusterSize(self);
112+
}).def("__deepcopy__", [](const ClusterSize& self, nb::dict) {
113+
return ClusterSize(self);
114+
}, "memo"_a)
105115
.def("__repr__", [](const ClusterSize& c) {
106116
return fmt::format("{}", c);
107117
})
@@ -130,6 +140,11 @@ struct declare_temporal_cluster_types {
130140
.def("mass_estimate",
131141
&ClusterSizeEstimate::mass_estimate,
132142
nb::call_guard<nb::gil_scoped_release>())
143+
.def("__copy__", [](const ClusterSizeEstimate& self) {
144+
return ClusterSizeEstimate(self);
145+
}).def("__deepcopy__", [](const ClusterSizeEstimate& self, nb::dict) {
146+
return ClusterSizeEstimate(self);
147+
}, "memo"_a)
133148
.def("__repr__", [](const ClusterSizeEstimate& c) {
134149
return fmt::format("{}", c);
135150
})

0 commit comments

Comments
 (0)