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/13 21:27:25 UTC
[datasketches-cpp] 01/01: use default allocator for temporary
ostringstream
This is an automated email from the ASF dual-hosted git repository.
alsay pushed a commit to branch fix_allocation_in_to_string
in repository https://gitbox.apache.org/repos/asf/datasketches-cpp.git
commit 559343b7a5aa8e6bf2aba624cd4c3f7297958d08
Author: AlexanderSaydakov <Al...@users.noreply.github.com>
AuthorDate: Wed Oct 13 14:27:06 2021 -0700
use default allocator for temporary ostringstream
---
cpc/include/cpc_sketch_impl.hpp | 8 +++++---
fi/include/frequent_items_sketch_impl.hpp | 6 ++++--
hll/include/HllSketch-internal.hpp | 6 ++++--
kll/include/kll_sketch_impl.hpp | 6 ++++--
req/include/req_sketch_impl.hpp | 6 ++++--
sampling/include/var_opt_sketch_impl.hpp | 20 +++++++++++++-------
sampling/include/var_opt_union_impl.hpp | 14 ++++++++------
theta/include/theta_sketch.hpp | 9 +++------
theta/include/theta_sketch_impl.hpp | 10 ++++++----
tuple/include/tuple_sketch.hpp | 9 +++------
tuple/include/tuple_sketch_impl.hpp | 10 ++++++----
11 files changed, 60 insertions(+), 44 deletions(-)
diff --git a/cpc/include/cpc_sketch_impl.hpp b/cpc/include/cpc_sketch_impl.hpp
index 2ad977a..f0e14ca 100644
--- a/cpc/include/cpc_sketch_impl.hpp
+++ b/cpc/include/cpc_sketch_impl.hpp
@@ -381,7 +381,9 @@ void cpc_sketch_alloc<A>::refresh_kxp(const uint64_t* bit_matrix) {
template<typename A>
string<A> cpc_sketch_alloc<A>::to_string() const {
- std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### CPC sketch summary:" << std::endl;
os << " lg_k : " << std::to_string(lg_k) << std::endl;
os << " seed hash : " << std::hex << compute_seed_hash(seed) << std::dec << std::endl;
@@ -392,14 +394,14 @@ string<A> cpc_sketch_alloc<A>::to_string() const {
os << " HIP estimate : " << hip_est_accum << std::endl;
os << " kxp : " << kxp << std::endl;
}
- os << " intresting col : " << std::to_string(first_interesting_column) << std::endl;
+ os << " interesting col: " << std::to_string(first_interesting_column) << std::endl;
os << " table entries : " << surprising_value_table.get_num_items() << std::endl;
os << " window : " << (sliding_window.size() == 0 ? "not " : "") << "allocated" << std::endl;
if (sliding_window.size() > 0) {
os << " window offset : " << std::to_string(window_offset) << std::endl;
}
os << "### End sketch summary" << std::endl;
- return os.str();
+ return string<A>(os.str(), sliding_window.get_allocator());
}
template<typename A>
diff --git a/fi/include/frequent_items_sketch_impl.hpp b/fi/include/frequent_items_sketch_impl.hpp
index e7db449..f9b984d 100644
--- a/fi/include/frequent_items_sketch_impl.hpp
+++ b/fi/include/frequent_items_sketch_impl.hpp
@@ -421,7 +421,9 @@ 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>
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;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream 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;
@@ -444,7 +446,7 @@ string<A> frequent_items_sketch<T, W, H, E, S, A>::to_string(bool print_items) c
}
os << "### End items" << std::endl;
}
- return os.str();
+ return string<A>(os.str(), map.get_allocator());
}
// version for integral signed type
diff --git a/hll/include/HllSketch-internal.hpp b/hll/include/HllSketch-internal.hpp
index c048039..16e00e2 100644
--- a/hll/include/HllSketch-internal.hpp
+++ b/hll/include/HllSketch-internal.hpp
@@ -246,7 +246,9 @@ string<A> hll_sketch_alloc<A>::to_string(const bool summary,
const bool detail,
const bool aux_detail,
const bool all) const {
- std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::stringstream os;
if (summary) {
os << "### HLL sketch summary:" << std::endl
<< " Log Config K : " << std::to_string(get_lg_config_k()) << std::endl
@@ -338,7 +340,7 @@ string<A> hll_sketch_alloc<A>::to_string(const bool summary,
}
}
- return os.str();
+ return string<A>(os.str(), sketch_impl->getAllocator());
}
template<typename A>
diff --git a/kll/include/kll_sketch_impl.hpp b/kll/include/kll_sketch_impl.hpp
index 0eb0ae6..b835857 100644
--- a/kll/include/kll_sketch_impl.hpp
+++ b/kll/include/kll_sketch_impl.hpp
@@ -1023,7 +1023,9 @@ void kll_sketch<T, C, S, A>::check_family_id(uint8_t family_id) {
template <typename T, typename C, typename S, typename A>
string<A> kll_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;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### KLL sketch summary:" << std::endl;
os << " K : " << k_ << std::endl;
os << " min K : " << min_k_ << std::endl;
@@ -1069,7 +1071,7 @@ string<A> kll_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items)
}
os << "### End sketch data" << std::endl;
}
- return os.str();
+ return string<A>(os.str(), allocator_);
}
template <typename T, typename C, typename S, typename A>
diff --git a/req/include/req_sketch_impl.hpp b/req/include/req_sketch_impl.hpp
index ee6d9e1..c7a2f81 100755
--- a/req/include/req_sketch_impl.hpp
+++ b/req/include/req_sketch_impl.hpp
@@ -653,7 +653,9 @@ void req_sketch<T, C, S, A>::compress() {
template<typename T, typename C, typename S, typename A>
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;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### REQ sketch summary:" << std::endl;
os << " K : " << k_ << std::endl;
os << " High Rank Acc : " << (hra_ ? "true" : "false") << std::endl;
@@ -693,7 +695,7 @@ string<A> req_sketch<T, C, S, A>::to_string(bool print_levels, bool print_items)
}
os << "### End sketch data" << std::endl;
}
- return os.str();
+ return string<A>(os.str(), allocator_);
}
template<typename T, typename C, typename S, typename A>
diff --git a/sampling/include/var_opt_sketch_impl.hpp b/sampling/include/var_opt_sketch_impl.hpp
index ce0ac40..484e699 100644
--- a/sampling/include/var_opt_sketch_impl.hpp
+++ b/sampling/include/var_opt_sketch_impl.hpp
@@ -731,8 +731,10 @@ void var_opt_sketch<T,S,A>::update(T&& item, double weight) {
template<typename T, typename S, typename A>
string<A> var_opt_sketch<T,S,A>::to_string() const {
- std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
- os << "### VarOpt SUMMARY: " << std::endl;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
+ os << "### VarOpt SUMMARY:" << std::endl;
os << " k : " << k_ << std::endl;
os << " h : " << h_ << std::endl;
os << " r : " << r_ << std::endl;
@@ -740,24 +742,28 @@ string<A> var_opt_sketch<T,S,A>::to_string() const {
os << " Current size : " << curr_items_alloc_ << std::endl;
os << " Resize factor: " << (1 << rf_) << std::endl;
os << "### END SKETCH SUMMARY" << std::endl;
- return os.str();
+ return string<A>(os.str(), allocator_);
}
template<typename T, typename S, typename A>
string<A> var_opt_sketch<T,S,A>::items_to_string() const {
- std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### Sketch Items" << std::endl;
int idx = 0;
for (auto record : *this) {
os << idx << ": " << record.first << "\twt = " << record.second << std::endl;
++idx;
}
- return os.str();
+ return string<A>(os.str(), allocator_);
}
template<typename T, typename S, typename A>
string<A> var_opt_sketch<T,S,A>::items_to_string(bool print_gap) const {
- std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### Sketch Items" << std::endl;
const uint32_t array_length = (n_ < k_ ? n_ : k_ + 1);
for (uint32_t i = 0, display_idx = 0; i < array_length; ++i) {
@@ -774,7 +780,7 @@ string<A> var_opt_sketch<T,S,A>::items_to_string(bool print_gap) const {
++display_idx;
}
}
- return os.str();
+ return string<A>(os.str(), allocator_);
}
template<typename T, typename S, typename A>
diff --git a/sampling/include/var_opt_union_impl.hpp b/sampling/include/var_opt_union_impl.hpp
index fe1ee49..b9274e7 100644
--- a/sampling/include/var_opt_union_impl.hpp
+++ b/sampling/include/var_opt_union_impl.hpp
@@ -295,14 +295,16 @@ void var_opt_union<T,S,A>::reset() {
template<typename T, typename S, typename A>
string<A> var_opt_union<T,S,A>::to_string() const {
- std::basic_ostringstream<char, std::char_traits<char>, AllocChar<A>> os;
- os << "### VarOpt Union SUMMARY: " << std::endl;
- os << " . n : " << n_ << std::endl;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
+ os << "### VarOpt Union SUMMARY:" << std::endl;
+ os << " n : " << n_ << std::endl;
os << " Max k : " << max_k_ << std::endl;
- os << " Gadget Summary: " << std::endl;
+ os << " Gadget Summary:" << std::endl;
os << gadget_.to_string();
- os << "### END VarOpt Union SUMMARY: " << std::endl;
- return os.str();
+ os << "### END VarOpt Union SUMMARY" << std::endl;
+ return string<A>(os.str(), gadget_.allocator_);
}
template<typename T, typename S, typename A>
diff --git a/theta/include/theta_sketch.hpp b/theta/include/theta_sketch.hpp
index 1bca459..53dcce4 100644
--- a/theta/include/theta_sketch.hpp
+++ b/theta/include/theta_sketch.hpp
@@ -131,8 +131,7 @@ public:
virtual const_iterator end() const = 0;
protected:
- using ostrstream = std::basic_ostringstream<char, std::char_traits<char>, AllocChar<Allocator>>;
- virtual void print_specifics(ostrstream& os) const = 0;
+ virtual void print_specifics(std::ostringstream& os) const = 0;
};
// forward declaration
@@ -288,8 +287,7 @@ private:
update_theta_sketch_alloc(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, uint64_t theta,
uint64_t seed, const Allocator& allocator);
- using ostrstream = typename Base::ostrstream;
- virtual void print_specifics(ostrstream& os) const;
+ virtual void print_specifics(std::ostringstream& os) const;
};
// compact sketch
@@ -377,8 +375,7 @@ private:
uint64_t theta_;
std::vector<uint64_t, Allocator> entries_;
- using ostrstream = typename Base::ostrstream;
- virtual void print_specifics(ostrstream& os) const;
+ virtual void print_specifics(std::ostringstream& os) const;
};
template<typename Allocator>
diff --git a/theta/include/theta_sketch_impl.hpp b/theta/include/theta_sketch_impl.hpp
index 1bee921..636df3d 100644
--- a/theta/include/theta_sketch_impl.hpp
+++ b/theta/include/theta_sketch_impl.hpp
@@ -59,7 +59,9 @@ double theta_sketch_alloc<A>::get_upper_bound(uint8_t num_std_devs) const {
template<typename A>
string<A> theta_sketch_alloc<A>::to_string(bool detail) const {
- ostrstream os;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### Theta sketch summary:" << std::endl;
os << " num retained entries : " << get_num_retained() << std::endl;
os << " seed hash : " << get_seed_hash() << std::endl;
@@ -80,7 +82,7 @@ string<A> theta_sketch_alloc<A>::to_string(bool detail) const {
}
os << "### End retained entries" << std::endl;
}
- return os.str();
+ return string<A>(os.str(), get_allocator());
}
// update sketch
@@ -228,7 +230,7 @@ compact_theta_sketch_alloc<A> update_theta_sketch_alloc<A>::compact(bool ordered
}
template<typename A>
-void update_theta_sketch_alloc<A>::print_specifics(ostrstream& os) const {
+void update_theta_sketch_alloc<A>::print_specifics(std::ostringstream& os) const {
os << " lg nominal size : " << static_cast<int>(table_.lg_nom_size_) << std::endl;
os << " lg current size : " << static_cast<int>(table_.lg_cur_size_) << std::endl;
os << " resize factor : " << (1 << table_.rf_) << std::endl;
@@ -321,7 +323,7 @@ auto compact_theta_sketch_alloc<A>::end() const -> const_iterator {
}
template<typename A>
-void compact_theta_sketch_alloc<A>::print_specifics(ostrstream&) const {}
+void compact_theta_sketch_alloc<A>::print_specifics(std::ostringstream&) const {}
template<typename A>
void compact_theta_sketch_alloc<A>::serialize(std::ostream& os) const {
diff --git a/tuple/include/tuple_sketch.hpp b/tuple/include/tuple_sketch.hpp
index 7777606..db3f359 100644
--- a/tuple/include/tuple_sketch.hpp
+++ b/tuple/include/tuple_sketch.hpp
@@ -153,8 +153,7 @@ public:
virtual const_iterator end() const = 0;
protected:
- using ostrstream = std::basic_ostringstream<char, std::char_traits<char>, AllocChar<Allocator>>;
- virtual void print_specifics(ostrstream& os) const = 0;
+ virtual void print_specifics(std::ostringstream& os) const = 0;
static uint16_t get_seed_hash(uint64_t seed);
@@ -344,8 +343,7 @@ protected:
// for builder
update_tuple_sketch(uint8_t lg_cur_size, uint8_t lg_nom_size, resize_factor rf, uint64_t theta, uint64_t seed, const Policy& policy, const Allocator& allocator);
- using ostrstream = typename Base::ostrstream;
- virtual void print_specifics(ostrstream& os) const;
+ virtual void print_specifics(std::ostringstream& os) const;
};
// compact sketch
@@ -473,8 +471,7 @@ protected:
bool destroy_;
};
- using ostrstream = typename Base::ostrstream;
- virtual void print_specifics(ostrstream& os) const;
+ virtual void print_specifics(std::ostringstream& os) const;
};
diff --git a/tuple/include/tuple_sketch_impl.hpp b/tuple/include/tuple_sketch_impl.hpp
index 1eba1e6..f63697a 100644
--- a/tuple/include/tuple_sketch_impl.hpp
+++ b/tuple/include/tuple_sketch_impl.hpp
@@ -53,7 +53,9 @@ double tuple_sketch<S, A>::get_upper_bound(uint8_t num_std_devs) const {
template<typename S, typename A>
string<A> tuple_sketch<S, A>::to_string(bool detail) const {
- ostrstream os;
+ // Using a temporary stream for implementation here does not comply with AllocatorAwareContainer requirements.
+ // The stream does not support passing an allocator instance, and alternatives are complicated.
+ std::ostringstream os;
os << "### Tuple sketch summary:" << std::endl;
os << " num retained entries : " << get_num_retained() << std::endl;
os << " seed hash : " << get_seed_hash() << std::endl;
@@ -74,7 +76,7 @@ string<A> tuple_sketch<S, A>::to_string(bool detail) const {
}
os << "### End retained entries" << std::endl;
}
- return os.str();
+ return string<A>(os.str(), get_allocator());
}
// update sketch
@@ -238,7 +240,7 @@ compact_tuple_sketch<S, A> update_tuple_sketch<S, U, P, A>::compact(bool ordered
}
template<typename S, typename U, typename P, typename A>
-void update_tuple_sketch<S, U, P, A>::print_specifics(ostrstream& os) const {
+void update_tuple_sketch<S, U, P, A>::print_specifics(std::ostringstream& os) const {
os << " lg nominal size : " << (int) map_.lg_nom_size_ << std::endl;
os << " lg current size : " << (int) map_.lg_cur_size_ << std::endl;
os << " resize factor : " << (1 << map_.rf_) << std::endl;
@@ -554,7 +556,7 @@ auto compact_tuple_sketch<S, A>::end() const -> const_iterator {
}
template<typename S, typename A>
-void compact_tuple_sketch<S, A>::print_specifics(ostrstream&) const {}
+void compact_tuple_sketch<S, A>::print_specifics(std::ostringstream&) const {}
// builder
---------------------------------------------------------------------
To unsubscribe, e-mail: commits-unsubscribe@datasketches.apache.org
For additional commands, e-mail: commits-help@datasketches.apache.org