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:24 UTC

[datasketches-cpp] 04/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 e604d4e413aaa7cd42228188e0591500d4919840
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Mon Oct 11 18:54:12 2021 -0700

    no sstream in to_string()
---
 req/include/req_sketch.hpp      |  1 +
 req/include/req_sketch_impl.hpp | 53 +++++++++++++++++++++--------------------
 2 files changed, 28 insertions(+), 26 deletions(-)

diff --git a/req/include/req_sketch.hpp b/req/include/req_sketch.hpp
index 779caba..1e2a4ca 100755
--- a/req/include/req_sketch.hpp
+++ b/req/include/req_sketch.hpp
@@ -273,6 +273,7 @@ public:
    * @param print_levels if true include information about levels
    * @param print_items if true include sketch data
    */
+  template<typename ToStr = ostream_based_to_string<T>>
   string<Allocator> to_string(bool print_levels = false, bool print_items = false) const;
 
   class const_iterator;
diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp
index ee6d9e1..238fc66 100755
--- a/req/include/req_sketch_impl.hpp
+++ b/req/include/req_sketch_impl.hpp
@@ -20,7 +20,6 @@
 #ifndef REQ_SKETCH_IMPL_HPP_
 #define REQ_SKETCH_IMPL_HPP_
 
-#include <sstream>
 #include <stdexcept>
 
 namespace datasketches {
@@ -652,48 +651,50 @@ void req_sketch<T, C, S, A>::compress() {
 }
 
 template<typename T, typename C, typename S, typename A>
+template<typename ToStr>
 string<A> req_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items) const {
-  std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
-  os << "### REQ sketch summary:" << std::endl;
-  os << "   K              : " << k_ << std::endl;
-  os << "   High Rank Acc  : " << (hra_ ? "true" : "false") << std::endl;
-  os << "   Empty          : " << (is_empty() ? "true" : "false") << std::endl;
-  os << "   Estimation mode: " << (is_estimation_mode() ? "true" : "false") << std::endl;
-  os << "   Sorted         : " << (compactors_[0].is_sorted() ? "true" : "false") << std::endl;
-  os << "   N              : " << n_ << std::endl;
-  os << "   Levels         : " << compactors_.size() << std::endl;
-  os << "   Retained items : " << num_retained_ << std::endl;
-  os << "   Capacity items : " << max_nom_size_ << std::endl;
+  string<A> str(allocator_);
+  str.append("### REQ sketch summary:\n");
+  str.append("   K              : ").append(std::to_string(k_)).append("\n");
+  str.append("   High Rank Acc  : ").append(hra_ ? "true\n" : "false\n");
+  str.append("   Empty          : ").append(is_empty() ? "true\n" : "false\n");
+  str.append("   Estimation mode: ").append(is_estimation_mode() ? "true\n" : "false\n");
+  str.append("   Sorted         : ").append(compactors_[0].is_sorted() ? "true\n" : "false\n");
+  str.append("   N              : ").append(std::to_string(n_)).append("\n");
+  str.append("   Levels         : ").append(std::to_string(compactors_.size())).append("\n");
+
+  str.append("   Retained items : ").append(std::to_string(num_retained_)).append("\n");
+  str.append("   Capacity items : ").append(std::to_string(max_nom_size_)).append("\n");
   if (!is_empty()) {
-    os << "   Min value      : " << *min_value_ << std::endl;
-    os << "   Max value      : " << *max_value_ << std::endl;
+    str.append("   Min value      : ").append(ToStr()(*min_value_)).append("\n");
+    str.append("   Max value      : ").append(ToStr()(*max_value_)).append("\n");
   }
-  os << "### End sketch summary" << std::endl;
+  str.append("### End sketch summary\n");
 
   if (print_levels) {
-    os << "### REQ sketch levels:" << std::endl;
-    os << "   index: nominal capacity, actual size" << std::endl;
+    str.append("### REQ sketch levels:\n");
+    str.append("   index: nominal capacity, actual size\n");
     for (uint8_t i = 0; i < compactors_.size(); i++) {
-      os << "   " << (unsigned int) i << ": "
-        << compactors_[i].get_nom_capacity() << ", "
-        << compactors_[i].get_num_items() << std::endl;
+      str.append("   ").append(std::to_string(i)).append(": ")
+          .append(std::to_string(compactors_[i].get_nom_capacity())).append(", ")
+          .append(std::to_string(compactors_[i].get_num_items())).append("\n");
     }
-    os << "### End sketch levels" << std::endl;
+    str.append("### End sketch levels\n");
   }
 
   if (print_items) {
-    os << "### REQ sketch data:" << std::endl;
+    str.append("### REQ sketch data:\n");
     unsigned level = 0;
     for (const auto& compactor: compactors_) {
-      os << " level " << level << ": " << std::endl;
+      str.append(" level ").append(std::to_string(level)).append(":\n");
       for (auto it = compactor.begin(); it != compactor.end(); ++it) {
-        os << "   " << *it << std::endl;
+        str.append("   ").append(ToStr()(*it)).append("\n");
       }
       ++level;
     }
-    os << "### End sketch data" << std::endl;
+    str.append("### End sketch data\n");
   }
-  return os.str();
+  return str;
 }
 
 template<typename T, typename C, typename S, typename A>

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