Author Topic: O2 Multidimensional Arrays  (Read 3899 times)

0 Members and 1 Guest are viewing this topic.

Charles Pegge

  • Guest
Re: O2 Multidimensional Arrays
« Reply #15 on: June 01, 2018, 03:18:55 AM »
A regular 2D dynamic array is simpler:

Code: [Select]
'11:10 31/05/2018
'DYNAMIC 2D array

int sy=100, sx=50
redim int a(sy*sx)
'
'ACCESS ARRAY (USING INDEXBASE 1)
=================================
macro aa(iy,ix)
  a( ( (iy)-1)*sx + (ix) )
end macro

'TESTS:
=======
#show aa(2,3)=42
aa(20,30)=100
print aa(2,3)*aa(20,30)
« Last Edit: June 02, 2018, 01:36:06 AM by Mike Lobanovsky »

edcronos

  • Guest
Re: O2 Multidimensional Arrays
« Reply #16 on: June 01, 2018, 04:22:05 AM »
Thank you Charles,

I have no problem working with the array calculation inside the routines, for those who only loop and if gives even a more complex appearance

I wanted to know more about array of pointers, or array inside array to see if it facilitated the use of dynamic indexes, so I would do the whole process in the sub array of the array instead of using, index within the index, of the array in each sub

but you do not have to give me so much attention, I'm just a curious, nor programmer I am,
  Anyway, I always go the way that I adjust
« Last Edit: June 02, 2018, 01:36:17 AM by Mike Lobanovsky »

jalih

  • Guest
Re: O2 Multidimensional Arrays
« Reply #17 on: June 01, 2018, 08:07:34 AM »
A regular 2D dynamic array is simpler:

Code: [Select]
'11:10 31/05/2018
'DYNAMIC 2D array

int sy=100, sx=50
redim int a(sy*sx)
'
'ACCESS ARRAY (USING INDEXBASE 1)
=================================
macro aa(iy,ix)
  a( ( (iy)-1)*sx + (ix) )
end macro

'TESTS:
=======
#show aa(2,3)=42
aa(20,30)=100
print aa(2,3)*aa(20,30)

Seems like O2 can do some things via macros, that PL/I can do with declaration:

To use 1D array as 2D array:
Code: [Select]
   dcl a(100) fixed bin(31);
   dcl b(10,10) fixed bin(31) defined(a((1sub-1) * hbound(b,1) + 2sub));
Now you can just use array b to refer to array a as 2D array (like your macro, but compiler can do error checking).

PL/I supports multidimensional arrays, so easier would be to use:
Code: [Select]
   dcl a(100) fixed bin(31);
   dcl c(10,10) fixed bin(31) based(addr(a));

And if you need to allocate 2D array, just:
Code: [Select]
   dcl p ptr;
   dcl e(10,10) fixed bin(31) based(p);

   allocate e;
I like the concept of a based variable. It removes the need for ugly C-style casting and let compiler do the math.

With a little creativity one can do some fun stuff with O2 macros. Something like this would be possible with O2 macro:
Code: [Select]
   dcl a(11) float init(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 0);
   dcl b(10,10) defined a(1sub*(1sub=2sub) + (1sub^=2sub)*hbound(a)) float;
Guess what it does?
« Last Edit: June 02, 2018, 01:36:27 AM by Mike Lobanovsky »

Arnold

  • Guest
Re: O2 Multidimensional Arrays
« Reply #18 on: June 01, 2018, 10:27:30 AM »
Hi Charles,

maybe it is time to put together the many possibilites to manage multidimensional arrays. I know there is examples\Basics\Arrays.o2bas, but in the meantime you demonstrated more ways.

Btw - Nehe tutorial 11:
http://www.oxygenbasic.org/forum/index.php?topic=1550.msg18193#msg18193

applies a macro for three dimensions (line 65-69), with indexbase 0 which is also based on your solution.

Roland
« Last Edit: June 02, 2018, 01:36:44 AM by Mike Lobanovsky »

JRS

  • Guest
Re: O2 Multidimensional Arrays
« Reply #19 on: June 11, 2018, 10:11:59 PM »
Quote from: CC@JRS
Unfortunately Eduardo,  i only use indices from 0 upwards,  i have never encounter
or use arrays with negative indices.  What is the purpose of your usage ?

What fields of work that would use negative indices in arrays?

Best that you ask Charles on this matter.

Code: Script BASIC
  1. array[-1000000] = "Negative One Million"
  2. array[1000000]  = "Positive One Million"
  3. PRINT "[",LBOUND(array),"] - ",array[-1000000],"\n"
  4. PRINT "[",UBOUND(array),"] - ",array[1000000],"\n"
  5.  


jrs@jrs-laptop:~/sb/examples/test$ scriba negarray.sb
[-1000000] - Negative One Million
[1000000] - Positive One Million
jrs@jrs-laptop:~/sb/examples/test$


With ScriptO2 you will be able to define dynamically any array indices you like without limitations. You can also use associative arrays or a combination of both. The low level access of O2 to these array / matrix structures is hidden from the user by ScriptO2.

Another benefit of ScriptO2 is that it uses the thread safe MyAlloc memory manager with a variant style variable structure.
« Last Edit: June 11, 2018, 11:57:08 PM by John »