20 #ifndef QUANTILES_SORTED_VIEW_IMPL_HPP_
21 #define QUANTILES_SORTED_VIEW_IMPL_HPP_
29 template<
typename T,
typename C,
typename A>
30 quantiles_sorted_view<T, C, A>::quantiles_sorted_view(uint32_t num,
const C& comparator,
const A& allocator):
31 comparator_(comparator),
35 entries_.reserve(num);
38 template<
typename T,
typename C,
typename A>
39 template<
typename Iterator>
40 void quantiles_sorted_view<T, C, A>::add(Iterator first, Iterator last, uint64_t weight) {
41 const size_t size_before = entries_.size();
42 for (
auto it = first; it != last; ++it) entries_.push_back(Entry(ref_helper(*it), weight));
43 if (size_before > 0) {
44 Container tmp(entries_.get_allocator());
45 tmp.reserve(entries_.capacity());
47 entries_.begin(), entries_.begin() + size_before,
48 entries_.begin() + size_before, entries_.end(),
49 std::back_inserter(tmp), compare_pairs_by_first(comparator_)
51 std::swap(tmp, entries_);
55 template<
typename T,
typename C,
typename A>
56 void quantiles_sorted_view<T, C, A>::convert_to_cummulative() {
57 for (
auto& entry: entries_) {
58 total_weight_ += entry.second;
59 entry.second = total_weight_;
63 template<
typename T,
typename C,
typename A>
65 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
67 std::upper_bound(entries_.begin(), entries_.end(),
Entry(ref_helper(item), 0), compare_pairs_by_first(comparator_))
68 : std::lower_bound(entries_.begin(), entries_.end(),
Entry(ref_helper(item), 0), compare_pairs_by_first(comparator_));
70 if (it == entries_.begin())
return 0;
72 return static_cast<double>(it->second) / total_weight_;
75 template<
typename T,
typename C,
typename A>
77 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
78 uint64_t weight =
static_cast<uint64_t
>(inclusive ? std::ceil(rank * total_weight_) : rank * total_weight_);
80 std::lower_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second())
81 : std::upper_bound(entries_.begin(), entries_.end(), make_dummy_entry<T>(weight), compare_pairs_by_second());
82 if (it == entries_.end())
return deref_helper(entries_[entries_.size() - 1].first);
83 return deref_helper(it->first);
86 template<
typename T,
typename C,
typename A>
88 if (entries_.empty())
throw std::runtime_error(
"operation is undefined for an empty sketch");
89 vector_double buckets(entries_.get_allocator());
90 if (entries_.size() == 0)
return buckets;
91 check_split_points(split_points, size);
92 buckets.reserve(size + 1);
93 for (uint32_t i = 0; i < size; ++i) buckets.push_back(get_rank(split_points[i], inclusive));
98 template<
typename T,
typename C,
typename A>
100 auto buckets = get_CDF(split_points, size, inclusive);
101 if (buckets.size() == 0)
return buckets;
102 for (uint32_t i = size; i > 0; --i) {
103 buckets[i] -= buckets[i - 1];
108 template<
typename T,
typename C,
typename A>
113 template<
typename T,
typename C,
typename A>
118 template<
typename T,
typename C,
typename A>
120 return entries_.
size();
Definition: quantiles_sorted_view.hpp:212
Definition: quantiles_sorted_view.hpp:38
typename std::conditional< std::is_arithmetic< T >::value, std::pair< T, uint64_t >, std::pair< const T *, uint64_t > >::type Entry
Entry type.
Definition: quantiles_sorted_view.hpp:41
typename std::conditional< std::is_arithmetic< T >::value, T, const T & >::type quantile_return_type
Definition: quantiles_sorted_view.hpp:93
size_t size() const
Definition: quantiles_sorted_view_impl.hpp:119
DataSketches namespace.
Definition: common_defs.hpp:32