You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@datasketches.apache.org by al...@apache.org on 2022/12/23 20:15:10 UTC

[datasketches-cpp] 01/02: added iterator

This is an automated email from the ASF dual-hosted git repository.

alsay pushed a commit to branch python_wrapper_improvement
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git

commit d1081cf45d99175901f2a69ffc17bc950c41830b
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Fri Dec 23 12:11:40 2022 -0800

    added iterator
---
 python/src/vo_wrapper.cpp | 2 +-
 python/tests/vo_test.py   | 7 +++++++
 2 files changed, 8 insertions(+), 1 deletion(-)

diff --git a/python/src/vo_wrapper.cpp b/python/src/vo_wrapper.cpp
index 32dfb6c..732810b 100644
--- a/python/src/vo_wrapper.cpp
+++ b/python/src/vo_wrapper.cpp
@@ -140,7 +140,7 @@ void bind_vo_sketch(py::module &m, const char* name) {
     .def("serialize", &dspy::vo_sketch_serialize<T>, py::arg("serde"), "Serialize the var opt sketch using the provided serde")
     .def_static("deserialize", &dspy::vo_sketch_deserialize<T>, py::arg("bytes"), py::arg("serde"),
         "Constructs a var opt sketch from the given bytes using the provided serde")
-    ;
+    .def("__iter__", [](const var_opt_sketch<T>& sk) { return py::make_iterator(sk.begin(), sk.end()); });
 }
 
 template<typename T>
diff --git a/python/tests/vo_test.py b/python/tests/vo_test.py
index 7e71d35..4fbca41 100644
--- a/python/tests/vo_test.py
+++ b/python/tests/vo_test.py
@@ -45,6 +45,13 @@ class VoTest(unittest.TestCase):
     items = vo.get_samples()
     self.assertEqual(len(items), k)
 
+    count = 0
+    for tuple in vo:
+      sample = tuple[0]
+      weight = tuple[1]
+      count = count + 1
+    self.assertEqual(count, vo.num_samples)
+
     # we can also apply a predicate to the sketch to get an estimate
     # (with optimally minimal variance) of the subset sum of items
     # matching that predicate among the entire population


---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org