Salam,

My mission is to program a cube(given its dimensions (x,y,z)).
The cube will be represented in a struct, which I'll choose what data members it should contain.
I need to implement the allocation function(which actually creates the matrix) and some other functions.
The most important function is the setter function, which receives the bounds of an inner cube, and change the value of the inner cube to a given value.

I did an implementation of all of this, but I don't think it is efficient.
because I have this function:

void Setter(Struct Matrix * matrix, int xfirst, int xlast,
        int yfirst, int ylast, int zfirst, int zlast, int value) {
    int i,j,k;
    for(i=xfirst;i<=xlast;i++)
        for(j=yfirst;j<=ylast;j++)
            for(k=zfirst;k<=zlast;k++)
                matrix->m[i][j][k]=value; //m is defined in the struct: int ***m;
}

I will be very happy to get some ideas about some data structures or any tips that improves my program.

Dani AI

Generated

Brief recap: wants a 3D cube, initialize it to zero and be able to set an arbitrary sub-cube to a value. rightly noted a 1D backing store is an option, but pointed out the asymptotic work stays the same. The practical win is not in lowering Big-O for a full-volume write (you must touch every element), but in improving memory layout and the cost per element written.

A recommended approach is one contiguous allocation (one malloc/calloc) with simple index arithmetic so Z is the fastest-varying axis. That makes each Z-run contiguous and lets the inner write be a single memcpy/memset instead of per-element assignment. Example pattern:

typedef struct { int x,y,z; int *data; } Cube;

static inline size_t idx(const Cube *c, int i, int j, int k) {
    return (size_t)i*c->y*c->z + (size_t)j*c->z + (size_t)k;
}

/* allocate with calloc to get zeros */
Cube *alloc_cube(int x,int y,int z);

/* fill sub-cube by copying a prefilled Z-row buffer with memcpy */
void fill_subcube(Cube *c, int x0,int x1,int y0,int y1,int z0,int z1,int val);

Practical tips: use calloc (or memset) for zero init. If val==0, use memset over the whole contiguous region when possible. For arbitrary val, build a temporary Z-row filled with val once and memcpy it into each (i,j) position; memcpy is typically vectorized and much faster than a per-int loop. Avoid int*** with many small mallocs — pointer indirection and fragmentation kill cache performance.

If your workload has many large range-assignments and few point reads, consider a hierarchical structure (octree / region compression / 3D segment tree with lazy propagation) so a whole uniform block can be marked in O(1) instead of touching every element. That adds complexity but can reduce work drastically for sparse or blocky updates.

Recommended Answers

All 3 Replies

According to what i understood, you have a huge cube and you need to initialise the smaller cubes inside it, with their (x, y, z). Am i right?

A more elaborate explanation would be helpful.

Ok.. I'll try to better explain it.
I built a struct which represents a 3D array, lets say:

typedef struct MyCube {
    int ***array;
    int x;
    int y;
    int z;
} MyCube;

and I want to allocate memory for a cube, fill it with zeros.
Then I may need to change a "sub-cube" in it. The user will give me the sub cube and a value.. my mission is to set this sub-cube with the given value.

I did the implementation using a for, for, for loops.
I think this is very not efficient.
So I'm looking for another way to represent the cube.
By 2D array.. another fields.. don't know what. :(

Well, you can use a one dimensional array. But the number of computations would remain the same.

struct Cube {
    int x;
    int y;
    int z;
    int *subCubes;
};

for (i = 0 to Number_of_SubCubes) {
   /* initialize values */
}

eg: 3x3x3 cube, i varies from 0 to 26. But, the number of computations would remain the same. So, this in no way is a better solution, except that you can use a single loop that does the same job.(in the same time)

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.