GLBasic forum

Main forum => GLBasic - en => Topic started by: S.O.P.M. on 2015-Jan-18

Title: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: S.O.P.M. on 2015-Jan-18
Good morning,

I wonder how to use DIMPUSH in the following way, if possible. I've a 128 x 24 two dimensional array and I'm looking for an easy way (as less code as possible and standard basic only, of course) to add emtpy rows and columns to it.

To add these rows/columns below and on the right side I simply can REDIM that array - everybody knows that. But I also want to add on top and on the left side! Then I have to shift the array contents element by element which could be quite slow and inefficient. How can I use DIMPUSH for that?

What I also want to know is, how do professional programmers handle this situation high efficient with any array they use in their application? With the same code/function? Working with lists maybe?

The perfect solution for me would be to have one single function where I can pass every array and add a row or columns on every position. For example something like AddColumnRow(Array[], [Row][Column], Position)

Thanks for your attention, have a good day.

-S.O.P.M.-
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: kanonet on 2015-Jan-19
I think you already mentioned what could be done using GLBasic. Of cause when oyu know how much space you will later need on top/left you could simply leave out that room and fill it later when needed.

When you would also consider using INLINE, I would go with memcpy and things like that. But also reconsider the structure of your data, maybe the same result could be achived, when you rearrange your data differently.
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Schranz0r on 2015-Jan-19
INLINE and List<whatever_type_you_need> should work...

you can hold a 2D array in a 1D array as well.


You can do it like this:

http://stackoverflow.com/questions/1817631/iterating-one-dimension-array-as-two-dimension-array
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: S.O.P.M. on 2015-Jan-19
That sounds like a great idea to save 2D data in a one dimensional array. I've a borderless operating level editor where the user can add objects at any position as long as the width and height of the entire structure doesn't exceed set limits. So I always have to shift data within the whole array. But now as I realized that DIMPUSH also doesn't do the job because it doesn't matter if I work with one or two dimensions I... :S
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: kanonet on 2015-Jan-19
Actually internally GLBasic does always use a 1-dim array, all other dimensions are only simulated on top of that.
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Marmor on 2015-Jan-19
why not use a second array what handle the order ?

every insert creates a new fieldnumber , so you can grow your array with redim but the real order
is stored in the second array.

no insert needed and for drawing use the second array in combination with your playfield.

mfg
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: kanonet on 2015-Jan-19
I think this could be considered GLBasics version of a linked list, but its a good idea.
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Schranz0r on 2015-Jan-20
You can use DGArray by INLINE...
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Moru on 2015-Jan-20
If this is an editor that needs copy/paste functions, you might as well just make a move function that redim and then move the content as you suggested in the first post. What is wrong with this solution? Less lookups and more speed later when drawing. If this array only contains indexes into your block data anyway, this is move can be done in real-time every screen frame without even noticing anything.

I'm sure my Atari would handle an array with 128 x 24 integers without breaking out in a sweat even :-)

What I'm trying to say is: Don't optimize one-time operations, make sure that draw routines that will be called 60 times per second runs as lean as possible instead. (Now I don't know if this resize you plan is a one-time thing but it sounds like it is something the user can do with a button-click so it's nothing that needs to be done 60 times per second.)
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Marmor on 2015-Jan-20
But the Problem itself is interesting imho.
My first idea was to make a big array and create a center (0,0) in the middle.
But this solution are bounding also.

Moving elements in an editor to create
a easy brainfriendly array is also ok imho
Because u need no new arrangement in the
Game , so this is a speedup.

If you use inline i would go with memcopy
Because its faster als a for next.
I think also about an cartesian coordinate arranged
Array imho the easyest way for an editor.
U can add the 3. Dimension also easy with using a second array.

Mfg
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: S.O.P.M. on 2015-Jan-20
Thank you very much, it becomes really interesting here but have to say not able to follow in any aspect. Actually my game (or better, the editor of it) runs smooth at 60 fps on my SGS3 phone at this time. I think the best way to continue is to first show you in detail what I'm speaking about. So please wait for a short video demonstration I'll upload till tomorrow.
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Moru on 2015-Jan-20
It's always hard to give advice if you don't have all the details so that would be nice :-)
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: S.O.P.M. on 2015-Jan-21
Ok, here is some video to show the editor in action. You can see at the end, borders will be shown as soon as objects are placed in the array. Dependent on the overall width and height of all objects, borders will change. So it's always possible to place new objects at any position as long as the overall size remains legit.

http://youtu.be/mM8h8qBdWEg
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: erico on 2015-Jan-21
funky editor! :) me likes it!
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Kitty Hello on 2015-Jan-21
You must move the contents if each cell. No way around that. Lists (pointer liszs) of that size are WAY slower than arrays.

Gesendet von meinem GT-N7100 mit Tapatalk

Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Marmor on 2015-Jan-21
its time for transfer (arrayx[],arrayy[],start,len) to kill the basic overhead  :noggin:
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: S.O.P.M. on 2015-Jan-21
Now I've written a new shifting procedure that uses less code and hopyfully will work for any given array but if it will run with similar speed that's the next question. At first I copied element by element inside two loops. Now I copy the whole array to another array with ArrCop[] = Arr[] and then paste the copy to the original with the given x and y offset. Still clearing the remaining elements with a new loop would be stupid. The following code is untested, please take a look on it and tell me what do you think it's the best way to do the task by using basic only. To remember, I need to shift the contents of any array with a single procedure to any extension - using basic code only, as less as possible and fast.

Code (glbasic) Select
FUNCTION ShiftArray: Arr%[], XExt%, YExt%

LOCAL ArrCop%[]
LOCAL ACX%, ACY%
LOCAL AX%, AY%
LOCAL W%, H%

IF XExt < 0
    ACX = ABS(XExt)
ELSE
    AX = XExt
ENDIF
IF YExt < 0
    ACY = ABS(YExt)
ELSE
    AY = YExt
ENDIF
W = BOUNDS(Arr[], 0)
H = BOUNDS(Arr[], 1)
ArrCop[] = Arr[]
FOR i = AX TO H - AY - 1
    FOR j = AY TO W - AX - 1
        Arr[AX + j][AY + i] = ArrCop[ACX + j][ACY + i]
    NEXT
NEXT

ENDFUNCTION


Zeroization is still not in there. XExt and YExt are the given movements, positive values shift right, negative ones left. ACX and ACY are the start coordinates where the copy will read, AX and AY the coordinates to start writing on the origin array. Would like to replace the first two IF-ENDIF's by formulas to set the right values but can't figure out how. Finally want to build the zeroization inside the double loop not using another one for that.
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: Hemlos on 2015-Mar-13
My lib uses arrays is a few ways, maybe you can use some of this code, or use the lib itself.
You can see in the image here, a function list for controlling arrays.
In your case here, you would be looking at the pop del and shift functions.

Feel free to use the whole library, its all compatible to each object type...some depend on each other.
By type i mean; strings, lists, files, time, and terminals.
You can get it from my download page.

http://silver.binhoster.com/downloads.html (http://silver.binhoster.com/downloads.html)

PS. i will be updating that library again this year i hope. Im thinking about the 3d particle engine as part of the lib too.
Title: Re: Adding emtpy rows and columns to a two dimensional array with DIMPUSH
Post by: S.O.P.M. on 2015-Mar-21
Seems to be some fantastic and useful work, many thanks. That's fine  :)