Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is it fine to replace an array with hierarchal structures and classes?

In my implementations, I should use a long array but my problem with array is that its indices do not much make sense to me. Instead I would like to use hierarchal classes. However, sometimes I need to treat them in a bulk way such as when calculating differences and derivatives or averages.

All members are double and seems aligning does not make any problem. Here is an example as follows. This example apparently works fine.

My question is that is this structure of programming prone to failure on different compilers or systems?

#include <iostream>

class Room
{
public:
    double size;
    double temperature;
    double humidity;
    double oxigen_level;
    // etc
};

class Kitchen
{
public:
    double fan_speed;
    double temperature;
};

class Building // a hierarchal class
{
public:
    Room rooms[5];
    double distance;
    Kitchen kitchen;
};

Building diff(
    const Building &b1,
    const Building &b2) // treat as an array
{
    Building r=b2;
    double *p1=(double *)&b1;
    double *pr=(double *)&r;
    for(int i=0;i*sizeof(double)<sizeof(Building);i++)
        pr[i]-=p1[i];
    return r;
}

int main()
{
    Building b1,b2,delta;
    b1.rooms[3].humidity=0.44;
    b2.rooms[3].humidity=0.43;
    delta=diff(b1,b2);
    std::cout
        <<"diff: "
        <<delta.rooms[3].humidity
        <<std::endl;
    return 0;
}
like image 704
ar2015 Avatar asked Dec 05 '25 18:12

ar2015


1 Answers

What you are doing in diff is a nightmare. If you going to do casts like this, it's better to stick with a plain array for your variables.

But I would use your thought of using the structures to help you with your calculations.

Make the classes as wrappers for your array. Then instead of variables, let them be functions ( double size() {... }). Use those functions in your calculations.

As usual always measure before prematurely optimizing.


Edit:

It is a nightmare, because types are built up, than the compiler is cheated into doing something else. The "assumed" underlying structure is used, when it dosesn't have to be as someone would expect it.

Here is a version I would make.

Is it better? It has less assembler instructions(75 vs 86), than the main example. And it has the intended logic visible to the reader. It is easy to debug. ...

The two examples would have to be benchmarked. But I don't think there is much of a diffrence.

EDIT: Actually there is a difference of speed. The below code runs faster on GCC, Clang and MSVC than the code in the main example.

Quick Bench Benchmark

Compiler Explorer example

#include <iostream>

class Room
{
public:
    double size{};
    double temperature{};
    double humidity{};
    double oxigen_level{};
    // etc
    Room& operator-=( const Room& r )
    {
        size -= r.size;
        temperature -= r.temperature;
        humidity -= r.humidity;
        oxigen_level -= r.oxigen_level;

        return *this;
    }
};

class Kitchen
{
public:
    double fan_speed{};
    double temperature{};

    Kitchen& operator-=( const Kitchen& k )
    {
        fan_speed -= k.fan_speed;
        temperature -= k.temperature;

        return *this;
    }
};

class Building // a hierarchal class
{
public:
    static const int room_count{5};

    Room    rooms[ room_count ];
    double  distance{};
    Kitchen kitchen;

    Building operator-( const Building& b )
    {
        Building ret = *this;

        for ( int i = 0; i < room_count; i++ )
            ret.rooms[ i ] -= b.rooms[ i ];

        ret.distance -= b.distance;
        ret.kitchen -= b.kitchen;

        return ret;
    }
};    
int main()
{
    Building b1,b2,delta;
    b1.rooms[3].humidity=0.44;
    b2.rooms[3].humidity=0.43;
    delta=b1-b2;//diff(b1,b2);
    std::cout
        <<"diff: "
        <<delta.rooms[3].humidity
        <<std::endl;
    return 0;
}
like image 54
Robert Andrzejuk Avatar answered Dec 08 '25 07:12

Robert Andrzejuk



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!