Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Newcomers DBPro Corner / Fast solution to insert and shuffle data in an typed array?

Author
Message
Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 1st May 2011 01:33
Iam working on an z depth buffer for my engine and currently have this!

if Zbuffer(1,t).Size>0
for z=Zdepth to 2 step -1
Zbuffer(z,t).Size=Zbuffer(z-1,t).Size
Zbuffer(z,t).Texture=Zbuffer(z-1,t).Texture
next z
endif

Is there an faster way to simply insert my new data in to index 2 and shuffle the other data down?
This is extremely slow the way i do it
mr_d
DBPro Tool Maker
17
Years of Service
User Offline
Joined: 26th Mar 2007
Location: Somewhere In Australia
Posted: 1st May 2011 10:53 Edited at: 1st May 2011 11:07
you could maybe implement a linked list (LL)?
i.e. have an 3 extra values in your Zbuffer UDT like .ThisIndex, .NextIndex, and PrevIndex
this means that when you add entries to your array, you would need to manually manage these values as well.
i.e. first item would have .ThisIndex=1, .NextIndex=2, .PrevIndex=0; second item would have .ThisIndex=2, .NextIndex=3, .PrevIndex=1, etc.
Then when you what to insert an entry you just change the .NextIndex and .PrevIndex values of the current, next and previous items.

Note:The values for .ThisIndex, .NextIndex, and .PrevIndex are not really indexes but are really unique identifiers to uniquely identify each entry of the LL.
i.e. although it's easy to start at 1 and go 1,2,3,4,5 initially, when you start using the LL properly and add and delete entries, the actual sequential values may end up being for example 1,2,6,3,8,4,5, etc.

Hope this makes sense and helps you out.

Grog Grueslayer
Valued Member
19
Years of Service
User Offline
Joined: 30th May 2005
Playing: Green Hell
Posted: 1st May 2011 11:48
Quote: "Is there an faster way to simply insert my new data in to index 2 and shuffle the other data down?
This is extremely slow the way i do it "


Not with a 2D array like you have. If you used a 1D array you could use ARRAY INSERT AT ELEMENT to add a new element to the middle of the array.



Cliff Mellangard 3DEGS
Developer
18
Years of Service
User Offline
Joined: 20th Feb 2006
Location: Sweden
Posted: 1st May 2011 12:03
Quote: "Hope this makes sense and helps you out"

All input is good input

Quote: "Not with a 2D array like you have. If you used a 1D array you could use ARRAY INSERT AT ELEMENT to add a new element to the middle of the array."

Thanks iam going to test this as i simply nead to make two typed arrays so should it work the way i want it?
Time to try it!
BatVink
Moderator
21
Years of Service
User Offline
Joined: 4th Apr 2003
Location: Gods own County, UK
Posted: 2nd May 2011 00:42
ARRAY INSERT results in your entire array being shifted in memory. You save a bit of code, but not any time.

Linked Lists is a better way to go. You can combine this with a second array that holds a reference to all of your empty array elements in the main array. This way you can have a fairly constant array size and keep reusing elements as they become available.

Login to post a reply

Server time is: 2024-11-22 23:27:47
Your offset time is: 2024-11-22 23:27:47