1

I'm writing a class to hold a three-dimensional array as a one-dimensional.

I have a bug in the code: all writen elements when the size of the array 1x1 (xz) are null.

Tell me where is the mistake and tell me what libraries already for this?

public class Arr3D<T> {

private T[] arr;
private Vector3 min, max;
private Vector3 size;

public Arr3D() 
{
    arr = new T[0];
}

public void AddOrReplace(T obj, Vector3 pos) 
{
    Vector3 newMin = Vector3.Min(min, pos); // ( Min(x1,x2), Min(y1,y2), ...
    Vector3 newMax = Vector3.Max(max, pos+Vector3.one); // +(1,1,1)

    if(newMin != min || newMax != max) 
    {
        Resize(newMin, newMax);
    }

    Set(obj, pos);
}

public void Set(T obj, Vector3 pos) 
{
    Set(obj, pos.x, pos.y, pos.z);
}

public void Set(T obj, int x, int y, int z) 
{
    arr[ (y-min.y)*size.x*size.y  +  (z-min.z)*size.x + (x-min.x) ] = obj;
}

private void Resize(Vector3 newMin, Vector3 newMax) 
{
    Vector3 oldMin = min;
    Vector3 oldMax = max;
    T[] oldList = arr;
    Vector3 oldSize = size;

    min = newMin;
    max = newMax;
    size = newMax - newMin;
    arr = new T[size.z*size.y*size.x];

    for(int x=oldMin.x; x<oldMax.x; x++) 
    {
        for(int y=oldMin.y; y<oldMax.y; y++) 
        {
            for(int z=oldMin.z; z<oldMax.z; z++) 
            {
                T val = oldList[(y-oldMin.y)*oldSize.x*oldSize.y  +  (z-oldMin.z)*oldSize.x + (x-oldMin.x)];
                Set(val, x, y, z);
            }
        }
    }

}

public T Get(int x, int y, int z) 
{
    return arr[(y-min.y)*size.x*size.y  +  (z-min.z)*size.x + (x-min.x)];
}
}
1
  • if oldList references arr and then you do a "new" on arr...oldList will also be empty Commented Dec 6, 2013 at 17:25

1 Answer 1

1

In the Resize method Instead of

T[] oldList = arr;

You should COPY the values of the arr(ay) otherwise you are keeping a reference to arr

T[] oldList ;

Array.Copy(arr,oldList,arr.Length) ;

For more details see Array.Copy method

Sign up to request clarification or add additional context in comments.

7 Comments

It will not work. One-dimensional array stores a three-dimensional. The elements are arranged like a dotted line. That is if we increase X by one, the new elements will not be the end of the array, and in the middle.
@user1164554 but the important idea is that you need to create copies of each of the values because if you keep a reference to arr and then call a "new" on arr you are going to end up with null values
@user1164554 you are asking about your mistake on null values, not about how to implemente 3 dimensional arrays in one-dimensional array
To be honest, until I see no reason to create new instances of objects and create extra garbage collector calls. Yes, transmitted links, but what's the problem?
@user1164554 then feel free to try your own aproach and solution
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.