Skip to content

Commit

Permalink
Merge pull request #275 from thomasmoelhave/cpp23
Browse files Browse the repository at this point in the history
Perform nessesary changes to be c++23 compatible
  • Loading branch information
antialize authored Nov 22, 2024
2 parents 648f6fb + a68422c commit 0564adc
Show file tree
Hide file tree
Showing 8 changed files with 68 additions and 80 deletions.
7 changes: 5 additions & 2 deletions test/speed_regression/btree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#include "testtime.h"
#include "testinfo.h"
#include "stat.h"
#include <random>

using namespace tpie;
using namespace tpie::test;
Expand All @@ -51,6 +52,8 @@ void test(size_t times, size_t size) {
test_realtime_t start;
test_realtime_t end;

std::mt19937 rng(42);

for (size_t i = 0; i < times; ++i) {
/*btree_internal_store<int> store;
btree<btree_internal_store<int> > tree(store);*/
Expand All @@ -62,7 +65,7 @@ void test(size_t times, size_t size) {
std::vector<int> x(count);
for(size_t i = 0; i < count; ++i)
x[i] = i;
std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);

// insertion
getTestRealtime(start);
Expand All @@ -81,7 +84,7 @@ void test(size_t times, size_t size) {
s(testRealtimeDiff(start,end));

// deletion
std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);

getTestRealtime(start);
for(size_t i = 0; i < count; ++i) {
Expand Down
8 changes: 3 additions & 5 deletions test/unit/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,17 @@
#include <iomanip>

struct bit_permute {
uint64_t operator()(uint64_t i) const{
constexpr uint64_t operator()(uint64_t i) const noexcept {
return (i & 0xAAAAAAAAAAAAAAAALL) >> 1 | (i & 0x5555555555555555LL) << 1;
}
};

template <typename T=std::less<uint64_t> >
struct bit_pertume_compare: std::binary_function<uint64_t, uint64_t, bool> {
struct bit_pertume_compare {
bit_permute bp;
T c;
typedef uint64_t first_argument_type;
typedef uint64_t second_argument_type;

bool operator()(uint64_t a, uint64_t b) const {
constexpr bool operator()(uint64_t a, uint64_t b) const noexcept {
return c(bp(a), bp(b));
}
};
Expand Down
53 changes: 19 additions & 34 deletions test/unit/test_array.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,15 +295,14 @@ class test_allocator {

private:
typedef tpie::allocator<T> a_t;
typedef std::allocator_traits<a_t> at_t;
a_t a;
public:
typedef typename a_t::size_type size_type;
typedef typename a_t::difference_type difference_type;
typedef typename a_t::pointer pointer;
typedef typename a_t::const_pointer const_pointer;
typedef typename a_t::reference reference;
typedef typename a_t::const_reference const_reference;
typedef typename a_t::value_type value_type;
typedef typename at_t::size_type size_type;
typedef typename at_t::difference_type difference_type;
typedef typename at_t::pointer pointer;
typedef typename at_t::const_pointer const_pointer;
typedef typename at_t::value_type value_type;

test_allocator() throw() {}
test_allocator(const test_allocator & a) throw() {unused(a);}
Expand All @@ -312,42 +311,28 @@ class test_allocator {

template <class U> struct rebind {typedef test_allocator<U> other;};

inline T * allocate(size_t size, const void * hint=0) {
T * allocate(size_t size) {
allocated += size;
return a.allocate(size, hint);
return a.allocate(size);
}

inline void deallocate(T * p, size_t n) {
void deallocate(T * p, size_t n) {
if (p == 0) return;
deallocated += n;
a.deallocate(p, n);
}

inline size_t max_size() const {return a.max_size();}

#ifdef TPIE_CPP_RVALUE_REFERENCE
#ifdef TPIE_CPP_VARIADIC_TEMPLATES
template <typename ...TT>
inline void construct(T * p, TT &&...x) {a.construct(p, x...);}
#else
template <typename TT>
inline void construct(T * p, TT && val) {a.construct(p, val);}
#endif
#endif
inline void construct(T * p) {
#ifdef WIN32
#pragma warning( push )
#pragma warning(disable: 4345)
#endif
new(p) T();
#ifdef WIN32
#pragma warning( pop )
#endif
size_t max_size() const {return a.max_size();}

template <typename U, typename ...TT>
void construct(U * p, TT &&...x) {
at_t::construct(a, p, std::forward<TT>(x)...);
}

template <typename U>
void destroy(U * p) {
at_t::destroy(a, p);
}
inline void construct(T * p, const T& val) {a.construct(p, val);}
inline void destroy(T * p) {a.destroy(p);}
inline pointer address(reference x) const {return &x;}
inline const_pointer address(const_reference x) const {return &x;}
};

template <typename T> size_t test_allocator<T>::allocated;
Expand Down
26 changes: 16 additions & 10 deletions test/unit/test_btree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
#include <numeric>
#include <filesystem>
#include "tpie_test_paths.h"
#include <random>

#ifdef TPIE_HAS_LZ4
#define SKIP_IF_NO_LZ4 {}
Expand Down Expand Up @@ -94,7 +95,8 @@ bool basic_test(TA<TT...>, A && ... a) {

std::vector<int> x(12); //34);
std::iota(x.begin(), x.end(), 0);
std::random_shuffle(x.begin(), x.end());
std::mt19937 rng(42);
std::shuffle(x.begin(), x.end(), rng);

for (size_t v: x) {
tree.insert(v);
Expand All @@ -103,7 +105,7 @@ bool basic_test(TA<TT...>, A && ... a) {
TEST_ENSURE_EQUALITY(tree2.size(), tree.size(), "The tree has the wrong size during insert stage.");
}

std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);
for (size_t v: x) {
tree.erase(v);
tree2.erase(v);
Expand Down Expand Up @@ -156,7 +158,8 @@ bool dynamic_iterator_test(TA<TT...> ta, A && ... a) {
for (int i=0; i < 1234; ++i) {
x.push_back(i);
}
std::random_shuffle(x.begin(), x.end());
std::mt19937 rng(42);
std::shuffle(x.begin(), x.end(), rng);

for (size_t i=0; i < x.size(); ++i) {
tree.insert(x[i]);
Expand Down Expand Up @@ -225,10 +228,11 @@ bool key_and_comparator_test(TA<TT...>, A && ... a) {
it.key.value = i;
x.push_back(it);
}
std::random_shuffle(x.begin(), x.end());
std::mt19937 rng(42);
std::shuffle(x.begin(), x.end(), rng);
for (size_t i=0; i < x.size(); ++i)
x[i].value = i;
std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);

for (size_t i=0; i < x.size(); ++i) {
tree.insert(x[i]);
Expand All @@ -245,7 +249,7 @@ bool key_and_comparator_test(TA<TT...>, A && ... a) {
++i2;
}

std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);
for (size_t i=0; i < x.size(); ++i) {
tree.erase(x[i].key);
}
Expand Down Expand Up @@ -363,7 +367,8 @@ bool augment_test(TA<TT...> ta, A && ... a) {
auto tree = get_btree(ta, c, au, std::forward<A>(a)...);
std::vector<int> x;
for (int i=0; i < 1234; ++i) x.push_back(i);
std::random_shuffle(x.begin(), x.end());
std::mt19937 rng(42);
std::shuffle(x.begin(), x.end(), rng);
for (size_t i=0; i < x.size(); ++i) {
tree.insert(x[i]);
auto n=tree.root();
Expand All @@ -379,7 +384,7 @@ bool augment_test(TA<TT...> ta, A && ... a) {
}

size_t e=x.size()/2;
std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);
for (size_t i=e; i < x.size(); ++i)
tree.erase(x[i]);
x.resize(e);
Expand Down Expand Up @@ -448,7 +453,8 @@ bool bound_test(TA<TT...>, A && ... a) {
for (int i=0; i < 1234; ++i) {
x.push_back(i);
}
std::random_shuffle(x.begin(), x.end());
std::mt19937 rng(42);
std::shuffle(x.begin(), x.end(), rng);

for (size_t i=0; i < x.size(); ++i) {
tree.insert(x[i]);
Expand All @@ -464,7 +470,7 @@ bool bound_test(TA<TT...>, A && ... a) {
TEST_ENSURE(tree.upper_bound(r2) == tree.end() || *tree.upper_bound(r2) == *tree2.upper_bound(r2), "Upper bound compare failed during insert stage.");
}

std::random_shuffle(x.begin(), x.end());
std::shuffle(x.begin(), x.end(), rng);

for (size_t i=0; i < x.size(); ++i) {
tree.erase(x[i]);
Expand Down
11 changes: 5 additions & 6 deletions test/unit/test_freespace_collection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
#include <set>
#include <tpie/tempname.h>
#include <tpie/blocks/freespace_collection.h>
#include <random>

using namespace tpie;
using namespace tpie::blocks;
Expand All @@ -44,10 +45,6 @@ memory_size_type random(memory_size_type seed, memory_size_type min, memory_size
return (seed * 1009) % (max - min) + min;
}

int random_generator(int i) {
return 10007 % i;
}

bool alloc_test(memory_size_type size, memory_size_type block_size) {
typedef std::vector<block_handle> handles_t;
temp_file file;
Expand Down Expand Up @@ -96,8 +93,10 @@ bool size_test(memory_size_type size, memory_size_type block_size) {
}

// the two arrays are shuffled the same way
std::random_shuffle(handles.begin(), handles.end(), random_generator);
std::random_shuffle(sizes.begin(), sizes.end(), random_generator);
std::mt19937 rng1(42);
std::shuffle(handles.begin(), handles.end(), rng1);
std::mt19937 rng2(42);
std::shuffle(sizes.begin(), sizes.end(), rng2);

for(memory_size_type i = 0; i < size; ++i) {
TEST_ENSURE(collection.size() >= minimum_size, "The space used is too small.");
Expand Down
2 changes: 1 addition & 1 deletion test/unit/test_stream.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -621,7 +621,7 @@ static bool user_data_test() {
movable_file_stream openstream(tpie::temp_file & file) {
tpie::uncompressed_stream<uint64_t> fs;
fs.open(file.path());
return fs;
return movable_file_stream(fs);
}

bool swap_test() {
Expand Down
6 changes: 4 additions & 2 deletions tpie/execution_time_predictor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -246,8 +246,9 @@ execution_time_predictor::~execution_time_predictor() {

time_type execution_time_predictor::estimate_execution_time(stream_size_type n, double & confidence) {
#ifndef TPIE_EXECUTION_TIME_PREDICTOR
confidence=0.0;
return -1;
unused(n);
confidence=0.0;
return -1;
#else
if (m_id == std::hash<std::string>()("")) {
confidence=0.0;
Expand Down Expand Up @@ -315,6 +316,7 @@ std::string execution_time_predictor::estimate_remaining_time(double progress) {
s << (int)remaining << " days";
return s.str();
#else
unused(progress);
return "No estimation";
#endif
}
Expand Down
35 changes: 15 additions & 20 deletions tpie/memory.h
Original file line number Diff line number Diff line change
Expand Up @@ -363,17 +363,16 @@ template <class T>
class allocator {
private:
typedef std::allocator<T> a_t;
typedef std::allocator_traits<a_t> at_t;
a_t a;
public:
memory_bucket_ref bucket;

typedef typename a_t::size_type size_type;
typedef typename a_t::difference_type difference_type;
typedef typename a_t::pointer pointer;
typedef typename a_t::const_pointer const_pointer;
typedef typename a_t::reference reference;
typedef typename a_t::const_reference const_reference;
typedef typename a_t::value_type value_type;
typedef typename at_t::size_type size_type;
typedef typename at_t::difference_type difference_type;
typedef typename at_t::pointer pointer;
typedef typename at_t::const_pointer const_pointer;
typedef typename at_t::value_type value_type;

typedef std::true_type propagate_on_container_copy_assignment;
typedef std::true_type propagate_on_container_move_assignment;
Expand All @@ -397,10 +396,10 @@ class allocator {

template <class U> struct rebind {typedef allocator<U> other;};

T * allocate(size_t size, const void * hint=0) {
T * allocate(size_t size) {
get_memory_manager().register_allocation(size * sizeof(T), typeid(T));
if (bucket) bucket->count += size * sizeof(T);
T * res = a.allocate(size, hint);
T * res = a.allocate(size);
return res;
}

Expand All @@ -410,22 +409,18 @@ class allocator {
get_memory_manager().register_deallocation(n * sizeof(T), typeid(T));
return a.deallocate(p, n);
}
size_t max_size() const noexcept {return a.max_size();}
size_t max_size() const noexcept {return at_t::max_size(a);}

template <typename U, typename ...TT>
void construct(U * p, TT &&...x) {a.construct(p, std::forward<TT>(x)...);}
void construct(U * p, TT &&...x) {
at_t::construct(a, p, std::forward<TT>(x)...);
}


// void construct(T * p) {
// new(p) T();
// }
// void construct(T * p, const T& val) {a.construct(p, val);}
template <typename U>
void destroy(U * p) {
p->~U();
}
pointer address(reference x) const noexcept {return &x;}
const_pointer address(const_reference x) const noexcept {return &x;}

at_t::destroy(a, p);
}

friend bool operator==(const allocator & l, const allocator & r) noexcept {return l.bucket == r.bucket;}
friend bool operator!=(const allocator & l, const allocator & r) noexcept {return l.bucket != r.bucket;}
Expand Down

0 comments on commit 0564adc

Please sign in to comment.