home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 5 Edit / 05-Edit.zip / ppwizard.zip / ppwsort.h < prev    next >
Text File  |  1999-10-02  |  10KB  |  188 lines

  1. ;----------------------------------------------------------------------------
  2. ;     MODULE NAME:   PPWSORT.H
  3. ;
  4. ;         $Author:   Dennis_Bareis  $
  5. ;       $Revision:   1.2  $
  6. ;           $Date:   02 Oct 1999 18:40:58  $
  7. ;        $Logfile:   E:/DB/PVCS.IT/OS2/PPWIZARD/PPWSORT.H_V  $
  8. ;
  9. ;     DESCRIPTION:   PPWIZARD has a "SortArray()" routine which can be used
  10. ;                    to sort a single array.  It does have some flexability
  11. ;                    in how it compares entries.
  12. ;
  13. ;                    This header file would be used to handle sort
  14. ;                    requirements that are not possible using "SortArray()".
  15. ;
  16. ;                    This header allows you to:
  17. ;
  18. ;                         * Imbed the sort routine between PPWIZARD commands.
  19. ;
  20. ;                         * Imbed the sort routine between rexx     commands.
  21. ;
  22. ;                         * Sort more than one array at a time, for example
  23. ;                           you may have multiple arrays storing associated
  24. ;                           information that should all be sorted together.
  25. ;
  26. ;                         * Do any sort of compare, no matter how complex.
  27. ;
  28. ;
  29. ;                     EXAMPLE1 - Inline Rexx code (use "SortArray" for inline
  30. ;                                PPWIZARD code). Array 'A' is being sorted.
  31. ;                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  32. ;                     ArrayName = arg(1);      ;;Caller passed "A"
  33. ;                     <$GenRexx2Sort                                                                                      \
  34. ;                          COUNT=^_valueG(ArrayName || '.0')^                                                             \
  35. ;                        GREATER=^SrtGreater = _valueG(ArrayName || '.SrtIndex1') >> _valueG(ArrayName || '.SrtIndex2')^  \
  36. ;                           SWAP=^SrtTemp = _valueG(ArrayName || '.SrtIndex1');                                          -\
  37. ;                                 call _valueS ArrayName || '.SrtIndex1', _valueG(ArrayName || '.SrtIndex2');            -\
  38. ;                                 call _valueS ArrayName || '.SrtIndex2', SrtTemp                                        -\
  39. ;                                ^                                                                                       -\
  40. ;                     >
  41. ;
  42. ;                     Note ">>" being used in the compare, if the data being
  43. ;                     sorted is numeric use '>' instead (or else)!!!!!!
  44. ;
  45. ;                     EXAMPLE2 - Exactly same as Example 1
  46. ;                                hardcoded - can't handle other arrays).
  47. ;                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  48. ;                     ArrayName = translate(arg(1));      ;;Caller passed "A" (handle possible value() bug!)
  49. ;                     <$GenRexx2Sort1ArrayIndirect NamedIn="ArrayName">
  50. ;
  51. ;                     EXAMPLE3 - In this case the name of the array ("A" is
  52. ;                                hardcoded - can't handle other arrays).
  53. ;                     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  54. ;                     <$GenRexx2Sort1Array ARRAY="A">
  55. ;
  56. ;----------------------------------------------------------------------------
  57.  
  58.  
  59. ;--- Only include once ------------------------------------------------------
  60. #ifndef VERSION_PPWSORT_H
  61.  
  62.  
  63. ;--- Define the version number of this header file --------------------------
  64. #define   VERSION_PPWSORT_H    99.269
  65. #require  99.269
  66.  
  67.  
  68. ;--- For use within PPWIZARD for immediate sorting of array (PPWIZARD Command!) ---
  69. #define SortArray    \
  70.         #evaluate '' ^<$GenRexx2Sort COUNT={$COUNT} GREATER={$GREATER} SWAP={$SWAP}>^
  71.  
  72. ;--- Generates the rexx code to perform sorting (sort one "NAMED" array) ----
  73. #define GenRexx2Sort1Array                                        \
  74.         <$GenRexx2Sort                                            \
  75.              COUNT=^{$Count="{$Array}.0"}^                        \
  76.             PREFIX=^{$Prefix="Srt"}^                              \
  77.            GREATER=^SrtGreater = {$Array}.SrtIndex1 {$Compare=">"} {$Array}.SrtIndex2^  \
  78.               SWAP=^SrtTemp = {$Array}.SrtIndex1;                -\
  79.                     {$Array}.SrtIndex1 = {$Array}.SrtIndex2;     -\
  80.                     {$Array}.SrtIndex2 = SrtTemp                 -\
  81.                    ^                                             -\
  82.         >
  83.  
  84. ;--- Generates the rexx code to perform sorting (sort 2 "NAMED" arrays) -----
  85. #define GenRexx2Sort2Arrays                                                    \
  86.         <$GenRexx2Sort                                                         \
  87.              COUNT=^{$Count="{$Array1}.0"}^                                    \
  88.             PREFIX=^{$Prefix="Srt"}^                                           \
  89.            GREATER=^{$Prefix}Greater = {$Array1}.{$Prefix}Index1 {$Compare=">"} {$Array1}.{$Prefix}Index2^ \
  90.               SWAP=^{$Prefix}Temp = {$Array1}.{$Prefix}Index1;                -\
  91.                     {$Array1}.{$Prefix}Index1 = {$Array1}.{$Prefix}Index2;    -\
  92.                     {$Array1}.{$Prefix}Index2 = {$Prefix}Temp;                -\
  93.                     {$Prefix}Temp = {$Array2}.{$Prefix}Index1;                -\
  94.                     {$Array2}.{$Prefix}Index1 = {$Array2}.{$Prefix}Index2;    -\
  95.                     {$Array2}.{$Prefix}Index2 = {$Prefix}Temp                 -\
  96.                    ^                                                          -\
  97.         >
  98.  
  99.  
  100. ;--- Generates the rexx code to perform sorting (array name not hard coded) -
  101. #define GenRexx2Sort1ArrayIndirect                                                                             \
  102.         <$GenRexx2Sort                                                                                         \
  103.              COUNT=^{$Count="_valueG({$NamedIn} || '.0')"}^                                                    \
  104.             PREFIX=^{$Prefix="SRT"}^                                                                           \
  105.            GREATER=^{$Prefix}Greater = _valueG({$NamedIn} || '.{$Prefix}INDEX1') {$Compare=">"} _valueG({$NamedIn} || '.{$Prefix}INDEX2')^  \
  106.               SWAP=^{$Prefix}Temp = _valueG({$NamedIn} || '.{$Prefix}INDEX1');                                -\
  107.                     call _valueS {$NamedIn} || '.{$Prefix}INDEX1', _valueG({$NamedIn} || '.{$Prefix}INDEX2'); -\
  108.                     call _valueS {$NamedIn} || '.{$Prefix}INDEX2', {$Prefix}Temp                              -\
  109.                    ^                                                                                          -\
  110.         >
  111.  
  112. ;--- Generates the rexx code to perform sorting (GENERIC) -------------------
  113. #DefineRexx GenRexx2Sort
  114.            {$Prefix="SRT"}M = 1
  115.            {$Prefix}Count   = {$COUNT};
  116.            do  while (9 * {$Prefix}M + 4) < {$Prefix}Count
  117.                {$Prefix}M = {$Prefix}M * 3 + 1
  118.            end /* do while */
  119.            do  while {$Prefix}M > 0
  120.                {$Prefix}K = {$Prefix}Count - {$Prefix}M
  121.                do  {$Prefix}J = 1 to {$Prefix}K
  122.                    {$Prefix}Index1 = {$Prefix}J
  123.                    do while {$Prefix}Index1 > 0
  124.                        ;--- Work out what to compare it with ----------------
  125.                        {$Prefix}Index2 = {$Prefix}Index1 + {$Prefix}M;
  126.  
  127.                        ;--- User user supplied logic to check for greater ---
  128.                        {$Greater};
  129.  
  130.                        ;--- Swap if required --------------------------------
  131.                        if {$Prefix}Greater then
  132.                        do
  133.                            {$Swap};
  134.                        end;
  135.                        else
  136.                            leave;
  137.  
  138.                        ;--- Prepare for the next loop -----------------------
  139.                        {$Prefix}Index1 = {$Prefix}Index1 - {$Prefix}M
  140.  
  141.                    end /* do while {$Prefix}Index1 > 0 */
  142.                end     /* do {$Prefix}J = 1 to {$Prefix}K */
  143.                {$Prefix}M = {$Prefix}M % 3
  144.            end         /* while {$Prefix}M > 0 */
  145. #DefineRexx
  146.  
  147.  
  148.  
  149.  
  150. #ifdef   CommentBlock  /* (Saturday 07/08/1999, 20:32:20, by Dennis_Bareis) */
  151. **+--------------------------------------------------------------------------
  152. **|;--- Generates the rexx code to perform sorting (GENERIC) -------------------
  153. **|#DefineRexx GenRexx2Sort
  154. **|        {$Prefix="Srt"}Count    = {$COUNT};
  155. **|        {$Prefix}Distance = {$Prefix}Count % 2;
  156. **|        do  while {$Prefix}Distance > 0
  157. **|            do  until {$Prefix}Finished;      /* start of mini-bubblesort loop */
  158. **|                {$Prefix}Finished = 1;
  159. **|                do  {$Prefix}Index1 = 1 to {$Prefix}Count - {$Prefix}Distance
  160. **|                    ;--- We now compare and swap items {$Prefix}Index1 and {$Prefix}Index1+{$Prefix}Distance ---
  161. **|                    {$Prefix}Index2 = {$Prefix}Index1 + {$Prefix}Distance;
  162. **|
  163. **|                    ;--- User user supplied logic to check for greater ---
  164. **|                    {$GREATER};
  165. **|
  166. **|                    ;--- Swap if required --------------------------------
  167. **|                    if {$Prefix}Greater then
  168. **|                    do
  169. **|                        ;--- User user supplied logic to swap items ------
  170. **|                        {$Swap};
  171. **|                        {$Prefix}Finished = 0;
  172. **|                    end;
  173. **|                end;
  174. **|            end;                       /* end of mini-bubblesort loop */
  175. **|            {$Prefix}Distance = {$Prefix}Distance % 2;
  176. **|        end;
  177. **|#DefineRexx
  178. **+--------------------------------------------------------------------------
  179. #endif                 /* (Saturday 07/08/1999, 20:32:20, by Dennis_Bareis) */
  180.  
  181.  
  182. #endif
  183.  
  184.  
  185.  
  186.  
  187.  
  188.