XML: Bubble Sort utilizing XMLVARs: Difference between revisions

From FSDeveloper Wiki
Jump to navigationJump to search
No edit summary
No edit summary
Line 7: Line 7:
     Code:
     Code:


    <!-- @sort('array_name', array_length) -->
      <!-- @sort('array_name', array_length) -->
    <Macro Name="sort">
      <Macro Name="sort">
      @2 (>L:sort_len, number)
        @2 (>L:sort_len, number)
      :127
        :127
          0 (>L:sort_swapped, bool)
          0 (>L:sort_swapped, bool)
          0 sp0
          0 sp0
          :128
          :128
            l0 ++ s0 (L:sort_len, number) &gt;= if{ g129 }
              l0 ++ s0 (L:sort_len, number) &gt;= if{ g129 }
            @1 l0 -- scat s10 (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s11
              @1 l0 -- scat s10 (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s11
            @1 l0    scat    (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s12 &gt;
              @1 l0    scat    (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s12 &gt;
                if{
                  if{
                  l11 (>C:XMLVARS:NumberValue, number)
                    l11 (>C:XMLVARS:NumberValue, number)
                  l10 (>C:XMLVARS:SearchVarName, string) l12 (>C:XMLVARS:NumberValue, number)
                    l10 (>C:XMLVARS:SearchVarName, string) l12 (>C:XMLVARS:NumberValue, number)
                  1 (>L:sort_swapped, bool)
                    1 (>L:sort_swapped, bool)
                }
                  }
            g128
              g128
          :129
          :129
          (L:sort_swapped, bool) if{ g127 }
          (L:sort_swapped, bool) if{ g127 }
    </Macro>
      </Macro>
 
      <Macro Name="store">
        @1 @2 scat (>C:XMLVARS:StoreVarName, string) (>C:XMLVARS:NumberValue, number)
      </Macro> 
   
   
    <Macro Name="store">
      <Update Frequency="18" Hidden="No">       
      @1 @2 scat (>C:XMLVARS:StoreVarName, string) (>C:XMLVARS:NumberValue, number)
      (L:SortInit, bool) 0 ==
    </Macro> 
          if{
              2 @store('sort_test', 0)
    <Update Frequency="18" Hidden="No">       
              1 @store('sort_test', 1)
    (L:SortInit, bool) 0 ==
              4 @store('sort_test', 2)
        if{
              8 @store('sort_test', 3)
            2 @store('sort_test', 0)
              7 @store('sort_test', 4)
            1 @store('sort_test', 1)
              0 @store('sort_test', 5)
            4 @store('sort_test', 2)
              1 (>L:SortInit, bool)
            8 @store('sort_test', 3)
          }   
            7 @store('sort_test', 4)
            0 @store('sort_test', 5)
            1 (>L:SortInit, bool)
        }   
                      
                      
        'sort_test0' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest0, number)       
          'sort_test0' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest0, number)       
        'sort_test1' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest1, number)   
          'sort_test1' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest1, number)   
        'sort_test2' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest2, number)   
          'sort_test2' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest2, number)   
        'sort_test3' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest3, number)   
          'sort_test3' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest3, number)   
        'sort_test4' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest4, number)   
          'sort_test4' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest4, number)   
        'sort_test5' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest5, number)   
          'sort_test5' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest5, number)   
    </Update>
      </Update>
   
   
    <Area Left="150" Top="130" Width="16" Height="16">
      <Area Left="150" Top="130" Width="16" Height="16">
        <Cursor Type="Hand" />
          <Cursor Type="Hand" />
            <Click Kind="LeftSingle+RightSingle">
              <Click Kind="LeftSingle+RightSingle">
                (L:SortMacroExecuted,enum) ++ (>L:SortMacroExecuted,enum)
                  (L:SortMacroExecuted,enum) ++ (>L:SortMacroExecuted,enum)
                @sort('sort_test', 6)
                  @sort('sort_test', 6)
            </Click>
              </Click>
    </Area>     
      </Area>     


     Notes:
     Notes:

Revision as of 10:43, 17 February 2014


   Recently, a question about sorting has come up. The context was an ICAO Search of Facility types which returns ICAO strings in alphabetical order. However, each ICAO represents a Facility that has a certain distance from the user's aircraft, and a sort according to distance is needed.
   Acknowledgements are owed to Robbie McElrath who hammered out the Bubble Sort XML in a few minutes (thank you, Robbie) and Tom Aguilo, for providing XMLVARs. The sort script that follows makes use of the array-forming capabilities of XMLVARS. Thanks, Tom.
   Code:
     <Macro Name="sort">
       @2 (>L:sort_len, number)
       :127
          0 (>L:sort_swapped, bool)
          0 sp0
          :128
             l0 ++ s0 (L:sort_len, number) >= if{ g129 }
             @1 l0 -- scat s10 (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s11
             @1 l0    scat     (>C:XMLVARS:SearchVarName, string) (C:XMLVARS:NumberValue, number) s12 >
                 if{
                   l11 (>C:XMLVARS:NumberValue, number)
                   l10 (>C:XMLVARS:SearchVarName, string) l12 (>C:XMLVARS:NumberValue, number)
                   1 (>L:sort_swapped, bool)
                 }
             g128
          :129
          (L:sort_swapped, bool) if{ g127 }
     </Macro>
 
     <Macro Name="store">
       @1 @2 scat (>C:XMLVARS:StoreVarName, string) (>C:XMLVARS:NumberValue, number)
     </Macro>  

     <Update Frequency="18" Hidden="No">      
     (L:SortInit, bool) 0 ==
         if{
             2 @store('sort_test', 0)
             1 @store('sort_test', 1)
             4 @store('sort_test', 2)
             8 @store('sort_test', 3)
             7 @store('sort_test', 4)
             0 @store('sort_test', 5)
             1 (>L:SortInit, bool)
         }  
                   
         'sort_test0' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest0, number)       
         'sort_test1' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest1, number)  
         'sort_test2' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest2, number)  
         'sort_test3' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest3, number)  
         'sort_test4' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest4, number)  
         'sort_test5' (>C:XMLVARS:SearchVarName,string) (C:XMLVARS:NumberValue,number) (>L:SortTest5, number)  
     </Update>

     <Area Left="150" Top="130" Width="16" Height="16">
         <Cursor Type="Hand" />
             <Click Kind="LeftSingle+RightSingle">
                 (L:SortMacroExecuted,enum) ++ (>L:SortMacroExecuted,enum)
                 @sort('sort_test', 6)
             </Click>
     </Area>    
   Notes:
       - (L:sort_len, number) is the array length. For example, IcaoSearchMatchedIcaosNumber
       - The SortInit sequence was used for testing purposes only. It stores 2, 1, 4, 8, 7, and 0 into XMLVARs named 'sort_test0' through 'sort_test5'. In a real application, GeoCalcDistance information would actually generate the values to be stored in these XMLVARs and a SortInit sequence would probably never be needed.
       - The bottom 6 lines in the Update section simply read the XMLVARs and store then into traditional L:Vars so that BlackBox can display them (so you can see what is happening before and after the sort is applied)
       - A Mouse Area 'click area' is used to execute the sort macro, @sort('sort_test', 6). The number 6 is the array length to be sorted. In the real example I postulated, it would be written @sort('sort_test',C:fs9gps:IcaoSearchMatchedIcaosNumber)