Janilabo

Janilabo | TSAToParts() [SCAR Divi]

Apr 28th, 2013
74
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.46 KB | None | 0 0
  1. const
  2.   COUNT = 2; // Used as the "x" variable for testing - Change to see the effect (1-9).
  3.  
  4. {==============================================================================]  
  5.   Explanation: Breaks TSA to parts (TSA => ATSA). Contains 2 methods:
  6.                -pm_PartSize (Breaks TSA to ATSA by size of the parts) [x = size]
  7.                -pm_PartAmount (Breaks TSA to ATSA by amount of the parts) [x = amount]                
  8. [==============================================================================}
  9. function TSAToParts(TSA: TStrArray; method: (pm_PartSize, pm_PartAmount); x: Integer): T2DStrArray;
  10. var
  11.   a, e, h, h2, i, i2, p, l, z: Integer;
  12.   f: Boolean;
  13. begin
  14.   h := High(TSA);
  15.   case ((h > -1) and (x > 0)) of
  16.     True:  
  17.     begin
  18.       case method of
  19.         pm_PartSize:
  20.         if (x <= h) then
  21.         begin
  22.           Inc(h);
  23.           p := (h div x);
  24.           if ((p * x) < h) then
  25.             Inc(p);
  26.           SetLength(Result, p);
  27.           for i := 0 to (p - 1) do
  28.             for i2 := 0 to (x - 1) do
  29.             begin
  30.               SetLength(Result[i], x);
  31.               if (a < h) then
  32.               begin
  33.                 Result[i][i2] := string(TSA[a]);
  34.                 Inc(a);
  35.               end else
  36.               begin
  37.                 SetLength(Result[i], i2);
  38.                 Exit;
  39.               end;
  40.             end;
  41.         end else
  42.           f := True;
  43.         pm_PartAmount:
  44.         case (h <= 0) of
  45.           False:
  46.           begin
  47.             if (h < (x - 1)) then
  48.               x := (h + 1);
  49.             p := Floor((h + 1) / x);
  50.             if (p = 0) then
  51.               p := 1;
  52.             e := ((h + 1) - (p * x));
  53.             if (e >= (h + 1)) then
  54.               e := 0;
  55.             SetLength(Result, x);
  56.             for i := 0 to (x - 1) do
  57.             begin
  58.               if ((e >= (i + 1)) and (e > 0)) then
  59.                 SetLength(Result[i], (p + 1))
  60.               else
  61.                 if (i <= h) then
  62.                   SetLength(Result[i], p);
  63.               h2 := High(Result[i]);
  64.               for i2 := 0 to h2 do
  65.               begin
  66.                 Result[i][i2] := string(TSA[a]);
  67.                 Inc(a);
  68.               end;
  69.             end;      
  70.           end;
  71.           True: f := True;
  72.         end;
  73.       end;  
  74.       if f then
  75.       begin                    
  76.         SetLength(Result, 1);
  77.         l := Length(TSA);
  78.         SetLength(Result[0], l);
  79.         for z := 0 to (l - 1) do
  80.           Result[0][z] := string(TSA[z]);
  81.       end;          
  82.     end;  
  83.     False: SetLength(Result, 0);          
  84.   end;
  85. end;
  86.  
  87. var
  88.   TSA: TStrArray;
  89.   ATSA: T2DStrArray;
  90.   h, i, c: Integer;
  91.   s: string;
  92.  
  93. begin
  94.   ClearDebug;  
  95.   TSA := ['Test1', 'Test2', 'Test3', 'Test4', 'Test5', 'Test6', 'Test7', 'Test8', 'Test9'];
  96.   ATSA := TSAToParts(TSA, pm_PartAmount, COUNT);
  97.   WriteLn('TSAToParts(TSA, pm_PartAmount, ' + IntToStr(COUNT) + '):');
  98.   h := High(ATSA);
  99.   for i := 0 to h do
  100.   begin
  101.     for c := 0 to High(ATSA[i]) do
  102.       s := (s + '"' + ATSA[i][c] + '" ');
  103.     WriteLn('ATSA[' + IntToStr(i) + ']: ' + Trim(s));
  104.     s := '';
  105.   end;  
  106.   WriteLn('');
  107.   ATSA := TSAToParts(TSA, pm_PartSize, COUNT);
  108.   WriteLn('TSAToParts(TSA, pm_PartSize, ' + IntToStr(COUNT) + '):');
  109.   h := High(ATSA);
  110.   for i := 0 to h do
  111.   begin
  112.     for c := 0 to High(ATSA[i]) do
  113.       s := (s + '"' + ATSA[i][c] + '" ');
  114.     WriteLn('ATSA[' + IntToStr(i) + ']: ' + Trim(s));
  115.     s := '';
  116.   end;
  117. end.
Advertisement
Add Comment
Please, Sign In to add comment