support copy, deepcopy in python for sorted interval lists

This commit is contained in:
Laurent Perron
2025-12-09 16:18:15 +01:00
committed by Mizux Seiha
parent 4a2de332ce
commit 97b64b49f2
2 changed files with 17 additions and 0 deletions

View File

@@ -70,6 +70,9 @@ PYBIND11_MODULE(sorted_interval_list, m) {
[](const Domain& domain) {
return absl::StrCat("Domain(", domain.ToString(), ")");
})
.def("__copy__", [](const Domain& self) { return Domain(self); })
.def("__deepcopy__",
[](const Domain& self, pybind11::dict) { return Domain(self); })
// Compatibility with pre PEP8 APIs.
.def_static("AllValues", &Domain::AllValues,
DOC(operations_research, Domain, AllValues))

View File

@@ -12,6 +12,8 @@
# See the License for the specific language governing permissions and
# limitations under the License.
import copy
from absl.testing import absltest
from ortools.util.python import sorted_interval_list as sil
@@ -111,6 +113,18 @@ class SortedIntervalListTest(absltest.TestCase):
self.assertEqual(str(d1), "[0,5]")
self.assertEqual(repr(d1), "Domain([0,5])")
def testCopy(self):
d1 = sil.Domain(-3, 5)
d2 = copy.copy(d1)
self.assertIsNot(d1, d2)
self.assertEqual(d1.flattened_intervals(), d2.flattened_intervals())
def testDeepCopy(self):
d1 = sil.Domain(-3, 5)
d2 = copy.deepcopy(d1)
self.assertIsNot(d1, d2)
self.assertEqual(d1.flattened_intervals(), d2.flattened_intervals())
if __name__ == "__main__":
absltest.main()