Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sampling/include/ebpps_sketch_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@ void ebpps_sketch<T, A>::merge(const ebpps_sketch<T, A>& sk) {
// need to swap this with sk to merge, so make a copy, swap,
// and use that to merge
ebpps_sketch sk_copy(sk);
swap(*this, sk_copy);
std::swap(*this, sk_copy);
internal_merge(sk_copy);
} else {
internal_merge(sk);
Expand Down
33 changes: 33 additions & 0 deletions sampling/test/ebpps_allocation_test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,26 @@

#include <sstream>

// An allocator outside the datasketches namespace, as an application's would be: argument-dependent
// lookup finds no swap() in its namespace.
namespace ebpps_test_alloc {

template<typename T>
struct plain_allocator {
using value_type = T;
plain_allocator() = default;
template<typename U>
plain_allocator(const plain_allocator<U>&) {}
T* allocate(std::size_t n) { return static_cast<T*>(::operator new(n * sizeof(T))); }
void deallocate(T* p, std::size_t) { ::operator delete(p); }
};
template<typename T, typename U>
bool operator==(const plain_allocator<T>&, const plain_allocator<U>&) { return true; }
template<typename T, typename U>
bool operator!=(const plain_allocator<T>&, const plain_allocator<U>&) { return false; }

} // namespace ebpps_test_alloc

namespace datasketches {

using ebpps_test_sketch = ebpps_sketch<test_type, test_allocator<test_type>>;
Expand Down Expand Up @@ -93,4 +113,17 @@ TEST_CASE( "ebpps merge", "[ebpps_sketch][test_type]") {
REQUIRE(test_allocator_net_allocations == 0);
}

TEST_CASE("ebpps merge heavier sketch by const reference with an application allocator", "[ebpps_sketch]") {
using sketch = ebpps_sketch<int, ebpps_test_alloc::plain_allocator<int>>;
sketch light(5);
sketch heavy(5);
for (int i = 0; i < 10; ++i) light.update(i);
for (int i = 0; i < 100; ++i) heavy.update(i, 2.0);
const sketch& heavy_ref = heavy;
light.merge(heavy_ref);
REQUIRE(light.get_n() == 110);
REQUIRE(light.get_cumulative_weight() == Approx(210.0));
REQUIRE(heavy.get_n() == 100);
}

} // namespace datasketches
Loading