Array with negative indexes

23,686

Solution 1

replace your arrays with a class:

class MyArray {
    private MyMapType[] myArray = new myMapType[size]
    MyMapType this[index] {
       get{return myArray[index + offset];}
    }

}

you can set the size and the offset in the constructor or even change it at will.

Building on this example here is another version:

class MyArray {
    private MyMapType[] positives = new myMapType[size]
    private MyMapType[] negatives = new myMapType[size-1]
    MyMapType this[index] {
       get{return index >= 0 ? positives[index] : negateves[1-index];}
    }

}

It does not change the fact that you need to set the size for both of them. Honestly I like the first one better

Solution 2

If you want "negative" indexes C# 8 now supports it.

var words = new string[]
{
                // index from start    index from end
    "The",      // 0                   ^9
    "quick",    // 1                   ^8
    "brown",    // 2                   ^7
    "fox",      // 3                   ^6
    "jumped",   // 4                   ^5
    "over",     // 5                   ^4
    "the",      // 6                   ^3
    "lazy",     // 7                   ^2
    "dog"       // 8                   ^1
};              // 9 (or words.Length) ^0

So the to call the negative one would be like this

words[^1]

See this link

So in your case the middle element could be the zero Z

Solution 3

Use the Dictionary class, since you can assign whatever values you want for either the key or value. While I'm not sure how this would work for the 3-dimensional array that you showed above, I can show how this would work if this were a 1-dimensional array, and you can infer how to best make use of it:

MyMapType[] map;

//map is filled with w/e data

Dictionary<int, MyMapType> x = new Dictionary<int, MyMapType>();

x[-1] = //(map data for whatever value is for the negative value);
x[0] = map[0]
//(etc...)

Solution 4

Could you try to store a list of MyMapTime[,] in two lists:

  • one for z values of greater than or equal to 0
  • and second of negative z-values.

The index of the tables would be the value of z. Having this would let you access quickly the xy-values for specific z-level. Of course the question is: what are your z-values? Are there sparse or dense. Even for sparse values you would end up with an array holding null values for [,].

Share:
23,686
Haedrian
Author by

Haedrian

Updated on January 29, 2020

Comments

  • Haedrian
    Haedrian about 4 years

    I have an array which I'm using to store map data for a game I'm working on.

    MyMapType[,,] map; 
    

    The reason I'm using a fixed array instead of a Collection is because fixed arrays work very much faster.

    Now my problem is, I'd like to have support for negative z levels in the game. So I'd like to be able to access a negative index.

    If this is not possible, I thought of a pair of other solutions.

    I was thinking as a possible solution to have ground-level as some arbitrary number (say 10), and anything less than 10 could be considered negative. But wouldn't this make the array 10 times larger for nothing if its not in use?

    Another solution I considered was to 'roll my own' where you have a Dictionary of 2D arrays, with the Z level held in the List as the index. But this is a lot more work and I'm not sure if its slow or not.

    So to summarise - any way of creating an array which supports a negative index? And if there's not - is there a clean way of 'emulating' such behaviour without sacrificing too much CPU time or RAM - noting that these are game maps which could end up large AND need to be accessed constantly.