Janilabo

Janilabo | TIAToParts()

Dec 13th, 2012
72
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 3.16 KB | None | 0 0
  1. {==============================================================================]  
  2.   Explanation: Breaks TIA to parts (TIA => ATIA). Contains 2 methods:
  3.                -pm_PartSize (Breaks TIA to ATIA by size of the parts) [x = size]
  4.                -pm_PartAmount (Breaks TIA to ATIA by amount of the parts) [x = amount]                
  5. [==============================================================================}
  6. function TIAToParts(TIA: TIntArray; method: (pm_PartSize, pm_PartAmount); x: Integer): T2DIntArray;
  7. var
  8.   a, e, h, h2, i, i2, p: Integer;
  9. begin
  10.   h := High(TIA);
  11.   case method of
  12.     pm_PartSize:
  13.     if ((h >= 0) and (x > 0)) then
  14.       if (x <= h) then
  15.       begin
  16.         Inc(h);
  17.         p := (h div x);
  18.         if ((p * x) < h) then
  19.           Inc(p);
  20.         SetLength(Result, p);
  21.         for i := 0 to (p - 1) do
  22.           for i2 := 0 to (x - 1) do
  23.           begin
  24.             SetLength(Result[i], x);
  25.             if (a < h) then
  26.             begin
  27.               Result[i][i2] := TIA[a];
  28.               Inc(a);
  29.             end else
  30.             begin
  31.               SetLength(Result[i], i2);
  32.               Exit;
  33.             end;
  34.           end;
  35.       end else
  36.         Result := [TIA];      
  37.     pm_PartAmount:
  38.     begin
  39.       if ((h <= 0) or (x < 2)) then
  40.       begin
  41.         if (x = 1) then
  42.           Result := [TIA]
  43.         else
  44.           if (x > 0) then
  45.             SetLength(Result, x);
  46.         Exit;
  47.       end;
  48.       if (h < (x - 1)) then
  49.         x := (h + 1);
  50.       p := Floor((h + 1) / x);
  51.       if (p = 0) then
  52.         p := 1;
  53.       e := ((h + 1) - (p * x));
  54.       if (e >= (h + 1)) then
  55.         e := 0;
  56.       SetLength(Result, x);
  57.       for i := 0 to (x - 1) do
  58.       begin
  59.         if ((e >= (i + 1)) and (e > 0)) then
  60.           SetLength(Result[i], (p + 1))
  61.         else
  62.           if (i <= h) then
  63.             SetLength(Result[i], p);
  64.         h2 := High(Result[i]);
  65.         for i2 := 0 to h2 do
  66.         begin
  67.           Result[i][i2] := TIA[a];
  68.           Inc(a);
  69.         end;
  70.       end;      
  71.     end;
  72.   end;
  73. end;
  74.  
  75. function TIAByRange(aStart, aFinish: Integer): TIntArray;
  76. var
  77.   i: Integer;
  78. begin
  79.   if (aStart <> aFinish) then
  80.   begin  
  81.     SetLength(Result, (IAbs(aStart - aFinish) + 1));
  82.     if (aStart > aFinish) then
  83.       for i := aStart downto aFinish do
  84.         Result[(aStart - i)] := i
  85.     else  
  86.       for i := aStart to aFinish do
  87.         Result[(i - aStart)] := i;
  88.   end else
  89.     Result := [aStart];
  90. end;
  91.  
  92. var
  93.   h, i: Integer;
  94.   TIA: TIntArray;
  95.   ATIA: T2DIntArray;
  96.  
  97. begin
  98.   ClearDebug;
  99.   TIA := TIAByRange(0, 10);
  100.   WriteLn('TIA: ' + TIAToStr(TIA));
  101.   WriteLn('');
  102.   WriteLn('TIAToParts(TIA, pm_PartSize, 5):');              
  103.   ATIA := TIAToParts(TIA, pm_PartSize, 5);
  104.   h := High(ATIA);
  105.   for i := 0 to h do
  106.     WriteLn('ATIA[' + IntToStr(i) + ']: ' + TIAToStr(ATIA[i]));
  107.   SetLength(ATIA, 0);                
  108.   WriteLn('');
  109.   WriteLn('TIAToParts(TIA, pm_PartAmount, 5):');
  110.   ATIA := TIAToParts(TIA, pm_PartAmount, 5);
  111.   h := High(ATIA);
  112.   for i := 0 to h do
  113.     WriteLn('ATIA[' + IntToStr(i) + ']: ' + TIAToStr(ATIA[i]));
  114.   SetLength(ATIA, 0);
  115.   SetLength(TIA, 0);
  116. end.
Advertisement
Add Comment
Please, Sign In to add comment