Home Tutorials Forums Articles Blogs Movies Library Employment Press
Old 11-27-2009, 01:01 PM   #1
davidsp
Registered User
 
Join Date: Mar 2009
Posts: 15
Default data management

I have an array (lets call it 'unusedBlocks') with 262144 items. These contain the numbers 0 - 262144 respectively.

I then populate another array ('someBlocks') with a bunch of data, containing a selection of numbers within in that same range.

How can I compare this data 'someBlocks' with the data in the 'unusedBlocks', so that it remove the matching values from 'unusedBlocks' into another array 'usedBlocks' ?

Is using array's the correct way? I will be adding lots more blocks, and continually be needing to check if the location is free or not.

Im really stuck here! thanks.
davidsp is offline   Reply With Quote
Old 11-27-2009, 01:26 PM   #2
davetidman
Senior Member
 
Join Date: May 2007
Posts: 283
Default

you could have a function that compares the arrays, removes matching values and pushes them into usedBlocks array. You could call that function whenever you needed if it was dynamically written.

Sounds like an awful lot of iterations to do though, but give me one min and I'll post write a function that could do it.
-dt
davetidman is offline   Reply With Quote
Old 11-27-2009, 01:42 PM   #3
davetidman
Senior Member
 
Join Date: May 2007
Posts: 283
Default

this does the job:

ActionScript Code:
var arr0:Array = new Array(0, 1, 2, 3, 4, 5, 6, 7, 8, 9); var arr1:Array = new Array(2, 4, 6, 8); var arr2:Array = new Array(); function compareArrays(unusedArr:Array, someArr:Array, usedArr:Array):void {     var unusedLength:Number         = unusedArr.length;     var someLength:Number       = someArr.length;         for(var i:int = 0; i<unusedLength; i++){                 var num:Number = unusedArr[i];         var compareNum:Number;         var newNum:Number;                     for(var j:int = 0; j<someLength; j++){                             compareNum = someArr[j];                         if(num == compareNum){                 newNum = Number(someArr[j]);                 someArr.splice(j, 1)                 usedArr.push(newNum);             }         }     }         trace("UNUSED ARR : " + arr0);     trace("SOME ARR : " + arr1);     trace("USED ARR : " + arr2); } compareArrays(arr0, arr1, arr2);

Hope this is the kind of thing you meant, I still have reservations about the amount of iterations needed to perform the task, may be a bit processor heavy. Let me know if this does the trick

-dt
davetidman is offline   Reply With Quote
Old 11-27-2009, 04:03 PM   #4
davidsp
Registered User
 
Join Date: Mar 2009
Posts: 15
Default

Thanks, that's exactly the kind of thing I'm looking for - lets hope it holds out when I add all of the data.

btw I added

ActionScript Code:
unusedArr.splice(i, 1); i--

so that the items are also removed from the unused Array.
davidsp is offline   Reply With Quote
Old 11-27-2009, 04:11 PM   #5
davetidman
Senior Member
 
Join Date: May 2007
Posts: 283
Default

Cool, glad it helped. Nice addage too.

Interested to see how it holds out so post your findings here when its got all the data in

-dt
davetidman is offline   Reply With Quote
Old 11-27-2009, 06:39 PM   #6
davidsp
Registered User
 
Join Date: Mar 2009
Posts: 15
Default

well its not looking good unfortunately... It times out every time - I tired the only optimizations I could think of:

ActionScript Code:
var num:Number = i;

since the data that would be stored is the same as its index, but with no luck.

maybe I need to change my whole approach?
davidsp is offline   Reply With Quote
Old 11-27-2009, 07:15 PM   #7
BeerOclock
Member
 
Join Date: Feb 2009
Posts: 90
Default

That is such a huge number! If your blocks are displayObjects of some kind, then even if they are only 1 pixel each, then thats still more blocks than could fit on any normal sized flash area.

How much do you know about data structures? I cant be sure because I dont know what youre doing, but probably Arrays arnt the best thing to use.

Im curious what you are tyring to do.

Heres an easy suggestion that may or may not apply:

In your Block class, have an instance variable like so:
public var used:Boolean;

then instead of copying the block from the 'used' array to the 'unused' array, you only need 1 array called blocks. Then you can simply flip that Boolean variable.
BeerOclock is offline   Reply With Quote
Old 11-28-2009, 09:43 AM   #8
davidsp
Registered User
 
Join Date: Mar 2009
Posts: 15
Default

I'm attempting to build a dynamic level generator, based off this map. It will be have a unit size 0f 512 x 512, and so 262144 tiles. Not to be shown all at one but an overview which will be used by other code.

I need to store the status of each pixel, so that I can first draw some random lines across - as paths. Next loop over the map randomly placing objects, and again placing different objects etc, until it reaches a predefined percentage of map coverage.

So basically I need a way to store all of these tiles/pixels, and a further variable for its type, and a method for checking the use of each one when adding more objects? I was going to use a tile 'id' ie 1,2,3, - 262144, rather than storing the xy values as I thought it might be quicker but not sure that's the best way? any advice. thanks!
davidsp is offline   Reply With Quote
Reply


Thread Tools
Display Modes Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT. The time now is 02:10 AM.

///
Follow actionscriptorg on Twitter

 


Powered by vBulletin® Version 3.8.5
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
Ad Management plugin by RedTyger
Copyright 2000-2013 ActionScript.org. All Rights Reserved.
Your use of this site is subject to our Privacy Policy and Terms of Use.