HiveBrain v1.2.0
Get Started
← Back to all entries
debugcppMinor

Subdividing intervals that contain the largest error values

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
theerrorcontainsubdividinglargestintervalsthatvalues

Problem

I'm trying to accomplish the following:

Start out with 1 interval on the real line. This interval is processed in some function and out comes an array of errors (size < 10 usually). The maximum element of this array is also stored and the interval is put onto a queue sorted by this maximum error.

Then we proceed by taking the interval with the largest error from the queue and split this interval into 2 equal parts. We then do the same operations as above on both these intervals and then put them on the queue as well.

I will demonstrate how I solved this below. But high performance is essential in this application, so I would very much appreciate your input on how I can speed things up here. Is std::vector a bad choice for example?

Here is my Interval class.

Interval.h

#include 

class Interval
{
public:
    Interval(size_t dim, double a, double b);
    void UpdateInterval(double, double);

    std::vector errors;      // vector of length dim
    double maxError, xmin, xmax, center;    
private:
    friend bool operator<(const Interval& lhs, const Interval& rhs) { return lhs.maxError < rhs.maxError; }    
};


Interval.cpp

#include "Interval.h"

Interval::Interval(size_t fdim, double a, double b)    
{
    xmin = a;
    xmax = b;    
    center = (xmax + xmin) / 2;       

    errors.resize(fdim);
    maxError = std::numeric_limits::max();
}

void Interval::UpdateInterval(double a, double b)
{
    xmin = a;
    xmax = b;    
    center = (xmax + xmin) / 2;
}


In this simplified example I simply populate the vectors with random doubles. I don't care about the performance of this step since I will populate these vectors in a more sophisticated way.

Source.cpp

```
#include "Interval.h"

typedef std::priority_queue , std::less> PriorityQueue;

static void SplitInterval(Interval &i1, Interval&i2)
{
size_t dim = i1.errors.size();
double a = i1.xmin;
double b = i1.xmax;
double center = (a + b) / 2.0;

i1.UpdateInterval(a, center);

Solution

Mmmmm

// A trick because top() returns a const T&
        Interval i1 = std::move(const_cast(queue.top()));


A trick that is illegal.

Also I am not convinced this does not break the underlying priority queue. As the top value when popped is unwound out of a heap using the compare operation that inspects the internal members of the object. Its not until it is removed from the heap that it is removed from the underlying vector.

Really not much else to review.

I am not sure what performance things you expect us to find the code here has very little to do with performance.

Code Snippets

// A trick because top() returns a const T&
        Interval i1 = std::move(const_cast<Interval&>(queue.top()));

Context

StackExchange Code Review Q#128517, answer score: 3

Revisions (0)

No revisions yet.