From 7de3052fbbd0c8005f4e911dda497b477072b554 Mon Sep 17 00:00:00 2001 From: Joshua Haberman Date: Sun, 9 Jan 2022 13:24:49 -0800 Subject: [PATCH] Implemented reverse(). --- .../pb_unit_tests/reflection_test_wrapper.py | 4 ---- python/repeated.c | 24 ++++++++++++++----- 2 files changed, 18 insertions(+), 10 deletions(-) diff --git a/python/pb_unit_tests/reflection_test_wrapper.py b/python/pb_unit_tests/reflection_test_wrapper.py index 99011e906c..f2e0bfad27 100644 --- a/python/pb_unit_tests/reflection_test_wrapper.py +++ b/python/pb_unit_tests/reflection_test_wrapper.py @@ -32,11 +32,7 @@ reflection_test.Proto2ReflectionTest.testExtensionIter.__unittest_expecting_fail reflection_test.Proto2ReflectionTest.testIsInitialized.__unittest_expecting_failure__ = True reflection_test.Proto2ReflectionTest.testListFieldsAndExtensions.__unittest_expecting_failure__ = True -reflection_test.Proto2ReflectionTest.testRepeatedCompositeReverse_Empty.__unittest_expecting_failure__ = True -reflection_test.Proto2ReflectionTest.testRepeatedCompositeReverse_NonEmpty.__unittest_expecting_failure__ = True reflection_test.Proto2ReflectionTest.testRepeatedListExtensions.__unittest_expecting_failure__ = True -reflection_test.Proto2ReflectionTest.testRepeatedScalarsReverse_Empty.__unittest_expecting_failure__ = True -reflection_test.Proto2ReflectionTest.testRepeatedScalarsReverse_NonEmpty.__unittest_expecting_failure__ = True reflection_test.Proto2ReflectionTest.testSingularListExtensions.__unittest_expecting_failure__ = True reflection_test.Proto2ReflectionTest.testStringUTF8Serialization.__unittest_expecting_failure__ = True reflection_test.Proto2ReflectionTest.testTopLevelExtensionsForOptionalMessage.__unittest_expecting_failure__ = True diff --git a/python/repeated.c b/python/repeated.c index b758bef402..0b25f4c3ef 100644 --- a/python/repeated.c +++ b/python/repeated.c @@ -545,6 +545,20 @@ static PyObject* PyUpb_RepeatedContainer_Sort(PyObject* pself, PyObject* args, return ret; } +static PyObject* PyUpb_RepeatedContainer_Reverse(PyObject* _self) { + upb_array* arr = PyUpb_RepeatedContainer_EnsureReified(_self); + size_t n = upb_array_size(arr); + size_t half = n / 2; // Rounds down. + for (size_t i = 0; i < half; i++) { + size_t i2 = n - i - 1; + upb_msgval val1 = upb_array_get(arr, i); + upb_msgval val2 = upb_array_get(arr, i2); + upb_array_set(arr, i, val2); + upb_array_set(arr, i2, val1); + } + Py_RETURN_NONE; +} + static PyObject* PyUpb_RepeatedContainer_MergeFrom(PyObject* _self, PyObject* args) { return PyUpb_RepeatedContainer_Extend(_self, args); @@ -650,9 +664,8 @@ static PyMethodDef PyUpb_RepeatedCompositeContainer_Methods[] = { "Removes an object from the repeated container."}, {"sort", (PyCFunction)PyUpb_RepeatedContainer_Sort, METH_VARARGS | METH_KEYWORDS, "Sorts the repeated container."}, - // TODO(https://github.com/protocolbuffers/upb/issues/459) - //{"reverse", reinterpret_cast(Reverse), METH_NOARGS, - // "Reverses elements order of the repeated container."}, + {"reverse", (PyCFunction)PyUpb_RepeatedContainer_Reverse, METH_NOARGS, + "Reverses elements order of the repeated container."}, {"MergeFrom", PyUpb_RepeatedContainer_MergeFrom, METH_O, "Adds objects to the repeated container."}, {NULL, NULL}}; @@ -734,9 +747,8 @@ static PyMethodDef PyUpb_RepeatedScalarContainer_Methods[] = { "Removes an object from the repeated container."}, {"sort", (PyCFunction)PyUpb_RepeatedContainer_Sort, METH_VARARGS | METH_KEYWORDS, "Sorts the repeated container."}, - // TODO(https://github.com/protocolbuffers/upb/issues/459) - // {"reverse", reinterpret_cast(Reverse), METH_NOARGS, - // "Reverses elements order of the repeated container."}, + {"reverse", (PyCFunction)PyUpb_RepeatedContainer_Reverse, METH_NOARGS, + "Reverses elements order of the repeated container."}, {"MergeFrom", PyUpb_RepeatedContainer_MergeFrom, METH_O, "Merges a repeated container into the current container."}, {NULL, NULL}};