FBIagent
Erfahrenes Mitglied
Hallo Ihr 
Ich verwende std::sort um ein Array auf Pointer zu sortieren. Das Array auf Pointern resultierte hier aus einem Array auf Objekte direkt. Da ich mir sicher war, das std::sort meine Objekte kopiert, habe ich ein zweites Array mit Pointern auf die Objekte erstellt um das Kopieren zu verhindern.
Habt Ihr vieleicht eine bessere Idee wie man das lösen könnte?
Relevant wären Zeile 87 bis 97.

Ich verwende std::sort um ein Array auf Pointer zu sortieren. Das Array auf Pointern resultierte hier aus einem Array auf Objekte direkt. Da ich mir sicher war, das std::sort meine Objekte kopiert, habe ich ein zweites Array mit Pointern auf die Objekte erstellt um das Kopieren zu verhindern.
Habt Ihr vieleicht eine bessere Idee wie man das lösen könnte?
Relevant wären Zeile 87 bis 97.
C++:
#include <iostream>
#include <cstdint>
#include <type_traits>
#include <algorithm>
using namespace std;
template<class T, T V1, T V2>
struct is_arithmetic_greater
{
static_assert(is_arithmetic<T>::value, "Only arithmetic types allowed for T!");
static const bool value = V1 > V2;
};
template<class T>
class LineAxis
{
static_assert(is_integral<T>::value && is_signed<T>::value, "Only signed integral types allowed for T!");
private:
T* _src;
T* _dst;
T _delta;
T _step;
T _error;
public:
LineAxis()
{}
void init(T* src, T* dst)
{
_src = src;
_dst = dst;
_delta = abs(*dst - *src);
_step = *src < *dst ? 1 : -1;
_error = _delta;
}
bool operator<(LineAxis<T>& other) { return _delta > other._delta; }
bool operator>(LineAxis<T>& other) { return _delta < other._delta; }
bool operator==(LineAxis<T>& other) { return _delta == other._delta; }
void tryStep(T greatestDelta)
{
_error += _delta;
if (_error >= greatestDelta)
{
step();
_error -= greatestDelta;
}
}
void step()
{
*_src += _step;
}
bool arrived()
{
return *_src == *_dst;
}
T delta()
{
return _delta;
}
};
// Description:
// Goes through all line points with any given number of AXES and calls the callback for each point
//
// Template Params:
// T TYPE - signed arithmetic type which describes each axis
// AXES VALUE - number of axes for source and destination point
//
// Function Params:
// srcPoint - the source point of the line
// dstPoint - the destination point of the line
// callback - the callback to which each line point is passed
template<class T, size_t AXES>
void for_each_line_point(T srcPoint[AXES], T dstPoint[AXES], void (*callback)(T point[AXES]))
{
static_assert(is_integral<T>::value && is_signed<T>::value, "Only signed integral types allowed for T!");
static_assert(is_arithmetic_greater<size_t, AXES, 0>::value, "AXES must be at least 1!");
LineAxis<T> axes[AXES];
LineAxis<T>* pAxes[AXES];
for (size_t i = 0;i < AXES;++ i)
{
pAxes[i] = &axes[i];
pAxes[i]->init(&srcPoint[i], &dstPoint[i]);
}
// sort, we need the axis with the greatest delta at index 0, the other axes does not matter
sort(pAxes, pAxes + AXES, [](LineAxis<T>* a, LineAxis<T>* b) {return a->delta() > b->delta();});
(*callback)(srcPoint);
while (!pAxes[0]->arrived())
{
pAxes[0]->step();
for (size_t i = 1;i < AXES;++ i)
{
pAxes[i]->tryStep(pAxes[0]->delta());
}
(*callback)(srcPoint);
}
}
int main(int argc, char** argv)
{
int srcPoint[2];
srcPoint[0] = 1;
srcPoint[1] = 1;
int dstPoint[2];
dstPoint[0] = 11;
dstPoint[1] = 5;
for_each_line_point<int, 2>(srcPoint, dstPoint, [](int point[2]){
cout << point[0] << ", " << point[1] << endl;
});
cin.get();
return 0;
}
Zuletzt bearbeitet: