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 2021/10/12 01:54:21 UTC

[datasketches-cpp] 01/04: no sstream in to_string()

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

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

commit 5bee9963160375e6dd3c49e57a9e6d316e6296a7
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Oct 11 15:01:07 2021 -0700

    no sstream in to_string()
---
 fi/include/frequent_items_sketch.hpp      |  1 +
 fi/include/frequent_items_sketch_impl.hpp | 29 +++++++++++++++--------------
 2 files changed, 16 insertions(+), 14 deletions(-)

diff --git a/fi/include/frequent_items_sketch.hpp b/fi/include/frequent_items_sketch.hpp
index 6efe2b9..b1f966f 100644
--- a/fi/include/frequent_items_sketch.hpp
+++ b/fi/include/frequent_items_sketch.hpp
@@ -269,6 +269,7 @@ public:
    * Returns a human readable summary of this sketch
    * @param print_items if true include the list of items retained by the sketch
    */
+  template<typename ToStr = ostream_based_to_string<T>>
   string<A> to_string(bool print_items = false) const;
 
 private:
diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp
index e7db449..a7e1d1d 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -420,15 +420,16 @@ void frequent_items_sketch<T, W, H, E, S, A>::check_size(uint8_t lg_cur_size, ui
 }
 
 template<typename T, typename W, typename H, typename E, typename S, typename A>
+template<typename ToStr>
 string<A> frequent_items_sketch<T, W, H, E, S, A>::to_string(bool print_items) const {
-  std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
-  os << "### Frequent items sketch summary:" << std::endl;
-  os << "   lg cur map size  : " << (int) map.get_lg_cur_size() << std::endl;
-  os << "   lg max map size  : " << (int) map.get_lg_max_size() << std::endl;
-  os << "   num active items : " << get_num_active_items() << std::endl;
-  os << "   total weight     : " << get_total_weight() << std::endl;
-  os << "   max error        : " << get_maximum_error() << std::endl;
-  os << "### End sketch summary" << std::endl;
+  string<A> str(map.get_allocator());
+  str.append("### Frequent items sketch summary:\n");
+  str.append("   lg cur map size  : ").append(std::to_string(map.get_lg_cur_size())).append("\n");
+  str.append("   lg max map size  : ").append(std::to_string(map.get_lg_max_size())).append("\n");
+  str.append("   num active items : ").append(std::to_string(get_num_active_items())).append("\n");
+  str.append("   total weight     : ").append(std::to_string(get_total_weight())).append("\n");
+  str.append("   max error        : ").append(std::to_string(get_maximum_error())).append("\n");
+  str.append("### End sketch summary\n");
   if (print_items) {
     vector_row items;
     for (auto it: map) {
@@ -436,15 +437,15 @@ string<A> frequent_items_sketch<T, W, H, E, S, A>::to_string(bool print_items) c
     }
     // sort by estimate in descending order
     std::sort(items.begin(), items.end(), [](row a, row b){ return a.get_estimate() > b.get_estimate(); });
-    os << "### Items in descending order by estimate" << std::endl;
-    os << "   item, estimate, lower bound, upper bound" << std::endl;
+    str.append("### Items in descending order by estimate\n");
+    str.append("   item, estimate, lower bound, upper bound");
     for (auto it: items) {
-      os << "   " << it.get_item() << ", " << it.get_estimate() << ", "
-         << it.get_lower_bound() << ", " << it.get_upper_bound() << std::endl;
+      str.append("   ").append(ToStr()(it.get_item())).append(", ").append(std::to_string(it.get_estimate())).append(", ")
+         .append(std::to_string(it.get_lower_bound())).append(", ").append(std::to_string(it.get_upper_bound())).append("\n");
     }
-    os << "### End items" << std::endl;
+    str.append("### End items\n");
   }
-  return os.str();
+  return str;
 }
 
 // version for integral signed type

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