Jump to content


Photo

N-dimensional Arrays


  • Please log in to reply
5 replies to this topic

#1 margoose

margoose

    GMC Member

  • GMC Member
  • 277 posts

Posted 29 November 2009 - 09:11 AM

NArrayDLL (download here)

Basic Info:
This DLL allows you to use multiple dimensional arrays. I decided to write it as a DLL in order to increase the number of dimensions possible. Each function (script) is commented in detail, so make sure you read each of them and how it is used. I have made a .gmk that uses each of these functions to further display how each function can be used. I doubt you will have many questions about this DLL as it is pretty straight forward. The only thing I want to make note of is to make sure you use the NArray_GetMaxDimensions() and/or NArray_GetMaxDimensionSize() functions (or calculate it yourself) to make sure you are not making the array too large. If you make the array too large then an unexpected error will occur. Also, if you make the array near the max size then it will take some time to create the array.


How It Works:
For those curious how it works, basically I translate the multidimensional coordinates of an element into a single coordinate of a one dimensional array. In other words, if you want a N-Dimensional array then the narray[x,y,z,w,...] is translated to array1D[element]. This is done mathematically by the algorithm:
element = x+y*sizeof(x)+z*sizeof(y)*sizeof(x)+w*sizeof(z)*si
zeof(y)*sizeof(x)+....etc

What You Can Do:
- Create an Array (all values of an array must be same type: real, or string)
- Set an Element
- Get an Element
- Delete an Array (to free memory)
- Get Datatype of Array (real or string)
- Get Size of Array (quantity of elements in array)
- Get Size of Dimension (quantity of elements in dimension)
- Get Max Quantity of Dimensions at given Size (before crash will occur)
- Get Max Size of Each Dimension at Given Quantity of Dimensions (before crash will occur)
- Get Max Size of Array (max quantity of elements in array possible before crash will occur)
- Resize Array (shrinking will create data loss)

Edited by margoose, 29 November 2009 - 09:14 AM.

  • 0
My Creations:


#2 DZiW

DZiW

    GMC Member

  • GMC Member
  • 978 posts
  • Version:Unknown

Posted 29 November 2009 - 10:30 AM

Processing download request...

Already for 5 mins... Sad.

Short pre- evaluational remarks:

*all values of an array must be same type

ALL? IMO it's more classic, yet handicaps GM array system.

*The problem with GM array system is it RESERVES (locks_ the space for maximum index used, e.g. using A[100]=0 will claim mem for 0..99 indices as well. So, I think ReDim or Resize would be enough.

*How about A[-100..100, 'A'..'Z', bool] and like? Or even A[1.75]?


Although it may prove to be useful but I'd rather use dynamic structures.
  • 0

YOU CREATE: WHERE LiFE MAY FAiL, DEAћ SHALL NOT!


#3 posva

posva

    GMC Member

  • New Member
  • 610 posts
  • Version:GM8

Posted 29 November 2009 - 01:34 PM

umm this is a must-try!, i'm looking at it..
  • 0

#4 margoose

margoose

    GMC Member

  • GMC Member
  • 277 posts

Posted 01 December 2009 - 05:21 PM

Although it may prove to be useful but I'd rather use dynamic structures.

It is a dynamic structure, aka resize. It is a C++ STL deque.

How about A[-100..100, 'A'..'Z', bool] and like? Or even A[1.75]?

No there are no such thing as negative or fractional indices for any language i'm aware of, that would be counter to there definition as there is no such thing as a partial element unless you are talking strings in which case there are better ways of approaching it.

As for string based indices it does not do that, however they could easily be implemented because a character is actually a number so you could set them as a base 26 so array1D["ABC"] would actually be something like 1 + 28 + 55 or 3 + 28 + 52 which ever direction you want to look at it (theres possibly a better way with binary operators and using the actual ascii values, but im not sure and I don't feel like thinking about it right now). You could also map each desired string index as a number, so that only those indices work. This is most optimal and most programmer friendly, and if I decide to do it then this is the way I will do it.

Also, Id like to note that in GM all real values are doubles there is no such thing as bool so to answer your question (somewhat) yes 1 and 0 are allowed as indices, but no to the rest. Only integers 0...dimensionSize-1 can be used as an index.

ALL? IMO it's more classic, yet handicaps GM array system.

any necessary reason you would use an array with reals AND strings in the same array you usually really need two arrays and use them as a map which is more of an industry standard way of doing it which is important for serious programmers to understand.

Edited by margoose, 01 December 2009 - 05:41 PM.

  • 0
My Creations:


#5 lethman427

lethman427

    GMC Member

  • New Member
  • 58 posts

Posted 30 September 2011 - 02:38 PM

What's the maximum length of the array, I'm looking for an array that can be 80x60x128=614400
  • 0
Cheers,
~Lethman~

#6 Big J

Big J

    GMC Member

  • GMC Member
  • 2853 posts
  • Version:GM8.1

Posted 05 December 2012 - 03:46 AM

This is what happens when I try to run the example:
Posted Image
You might want to fix that. :)
  • 0

Get your GM 8.1 Anti-Aliasing here!

2712265.png

http://www.youtube.com/Sporkinator