7 Replies Latest reply on Oct 6, 2009 11:20 AM by Flex harUI

    sparse arrays: array of arrays

    timo888 Level 1

      Let's say I have a hypothetical requirement to build a structure in memory mapping to selected pizza shops, organized by city, and each city and each pizza shop has a unique integer identifier. So I won't be using string keys but integer keys.

       

      Question #1. in Flex, can an array of arrays use [ ][ ] syntax to reference an item?

       

      PizzaShop (aPizzaShop) = myPizzaShopsByCityArray[1275422][382];    //  e.g. Chicago=1275422, Vesuvio Pizza = 382.

       

      There would be only a dozen or so cities in the array with perhaps 10-20 shops in each city, so the total number of items would count in the hundreds.

       

      Question #2: The documentation says that arrays in Flex are sparse; is it correct to assume there's not going to be a performance penalty here with the very large integer?[1175422 Is there  overhead for spare arrays that I should be taking into account?

       

      Thanks

        • 1. Re: sparse arrays: array of arrays
          Gregory Lafrance Level 6

          This app compiles to a SWF of 286K, so the answer is yes on both counts, you can index arrays as you indicated, and they are sparse.

           

          The Flex framework is slightly heavy, which is why this app is even 286K.

           

          If this post answers your question or helps, please mark it as such.

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
            creationComplete="init();" width="100%" height="100%">
            <mx:Script>
              <![CDATA[
                private var arr:Array = new Array();
               
                private function init():void{
                  arr[10000000] = "Greg";
                  printArray();
                }
               
                private function printArray():void{
                  for each(var item:Object in arr){
                    txt.text += item.toString() + "\n";
                  }
                } 
              ]]>
            </mx:Script>
            <mx:TextArea width="100%" height="100%" id="txt"/>
          </mx:Application>

          1 person found this helpful
          • 2. Re: sparse arrays: array of arrays
            Flex harUI Adobe Employee

            If there were to be an issue with sparse arrays, it would show up in runtime memory, not in SWF size.

             

            Array might be slightly heavier than Objects with string keys.  I doubt you'll notice the difference, even for every pizza shop in the US.  But I would still use Objects.  You won't be tempted to use Array methods, and you'll still have to use Object methods to loop through all of the shops.  Did you try using integer keys on Object?  Actionscript might do the coercion to String for you.

             

            Alex Harui

            Flex SDK Developer

            Adobe Systems Inc.

            Blog: http://blogs.adobe.com/aharui

            • 3. Re: sparse arrays: array of arrays
              timo888 Level 1

              Thanks both of you for the helpful answers.

               

              I chose Array over Object() with string keys because I need to sort the elements of the PizzaShopsByCity array, that is, the pizza shops within a given city sometimes have to be alpha-sorted and sometimes they have to be ranked (numeric sort).  I forgot to add that requirement to the description of the hypothetical. My bad.  That's why I used array with numeric keys--sorting arrays requires numeric keys, IIRC.

              • 4. Re: sparse arrays: array of arrays
                Flex harUI Adobe Employee

                Did you test sorting the sparse array to make sure it doesn't alter the keys/indices?

                 

                Alex Harui

                Flex SDK Developer

                Adobe Systems Inc.

                Blog: http://blogs.adobe.com/aharui

                • 5. Re: sparse arrays: array of arrays
                  timo888 Level 1

                  harUI wrote:

                   

                  Did you test sorting the sparse array to make sure it doesn't alter the keys/indices?

                   


                  Alex,  I haven't gotten to the testing phase yet (and at the moment I'm content when my A/S simply compiles  ), but truth to tell, it did not even cross my mind to test for mutating numeric indices during a sort. Is such mutation a  typical feature of sparse arrays, i.e. the index value, like a PK value in a relational database, is subject to change?

                  • 6. Re: sparse arrays: array of arrays
                    Flex harUI Adobe Employee

                    I would think so.  In a simple sort, the index of the items change to reflect sort order.

                     

                    Alex Harui

                    Flex SDK Developer

                    Adobe Systems Inc.

                    Blog: http://blogs.adobe.com/aharui

                    • 7. Re: sparse arrays: array of arrays
                      Flex harUI Adobe Employee

                      I ran a test:

                       

                      Sparse Array

                      0 Zable

                      40 Bofa

                      3 Ghair

                      101 Nink

                      2005 Zower

                      32303 Nupboards

                       

                      After Sorting

                      0 Bofa

                      1 Ghair

                      2 Nink

                      3 Nupboards

                      4 Zable

                      5 Zower

                       

                      Alex Harui

                      Flex SDK Developer

                      Adobe Systems Inc.

                      Blog: http://blogs.adobe.com/aharui