mirror of
https://github.com/wownero/wownero.git
synced 2026-10-27 11:28:36 +01:00
epee: avoid memcpy in rolling median move
This commit is contained in:
@@ -36,6 +36,7 @@
|
|||||||
|
|
||||||
#include "misc_language.h"
|
#include "misc_language.h"
|
||||||
|
|
||||||
|
#include <type_traits>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include <stdint.h>
|
#include <stdint.h>
|
||||||
|
|
||||||
@@ -47,6 +48,9 @@ namespace misc_utils
|
|||||||
template<typename Item>
|
template<typename Item>
|
||||||
struct rolling_median_t
|
struct rolling_median_t
|
||||||
{
|
{
|
||||||
|
static_assert(std::is_trivially_copyable_v<Item>,
|
||||||
|
"rolling_median_t requires trivially copyable Item");
|
||||||
|
|
||||||
private:
|
private:
|
||||||
Item* data; //circular queue of values
|
Item* data; //circular queue of values
|
||||||
int* pos; //index into `heap` for each value
|
int* pos; //index into `heap` for each value
|
||||||
@@ -124,6 +128,27 @@ private:
|
|||||||
return i == 0;
|
return i == 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
void consume_from(rolling_median_t& m) noexcept
|
||||||
|
{
|
||||||
|
data = m.data;
|
||||||
|
pos = m.pos;
|
||||||
|
heap = m.heap;
|
||||||
|
N = m.N;
|
||||||
|
idx = m.idx;
|
||||||
|
minCt = m.minCt;
|
||||||
|
maxCt = m.maxCt;
|
||||||
|
sz = m.sz;
|
||||||
|
|
||||||
|
m.data = nullptr;
|
||||||
|
m.pos = nullptr;
|
||||||
|
m.heap = nullptr;
|
||||||
|
m.N = 0;
|
||||||
|
m.idx = 0;
|
||||||
|
m.minCt = 0;
|
||||||
|
m.maxCt = 0;
|
||||||
|
m.sz = 0;
|
||||||
|
}
|
||||||
|
|
||||||
protected:
|
protected:
|
||||||
rolling_median_t &operator=(const rolling_median_t&) = delete;
|
rolling_median_t &operator=(const rolling_median_t&) = delete;
|
||||||
|
|
||||||
@@ -152,16 +177,19 @@ public:
|
|||||||
sz = other.sz;
|
sz = other.sz;
|
||||||
}
|
}
|
||||||
|
|
||||||
rolling_median_t(rolling_median_t &&m)
|
rolling_median_t(rolling_median_t &&m) noexcept
|
||||||
{
|
{
|
||||||
memcpy(this, &m, sizeof(rolling_median_t));
|
consume_from(m);
|
||||||
m.data = NULL;
|
|
||||||
}
|
}
|
||||||
rolling_median_t &operator=(rolling_median_t &&m)
|
|
||||||
|
rolling_median_t &operator=(rolling_median_t &&m) noexcept
|
||||||
{
|
{
|
||||||
free(data);
|
if (this != &m)
|
||||||
memcpy(this, &m, sizeof(rolling_median_t));
|
{
|
||||||
m.data = NULL;
|
free(data);
|
||||||
|
consume_from(m);
|
||||||
|
}
|
||||||
|
|
||||||
return *this;
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user