Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- {==============================================================================]
- Explanation: Breaks TIA to parts (TIA => ATIA). Contains 2 methods:
- -pm_PartSize (Breaks TIA to ATIA by size of the parts) [x = size]
- -pm_PartAmount (Breaks TIA to ATIA by amount of the parts) [x = amount]
- [==============================================================================}
- function TIAToParts(TIA: TIntArray; method: (pm_PartSize, pm_PartAmount); x: Integer): T2DIntArray;
- var
- a, e, h, h2, i, i2, p: Integer;
- begin
- h := High(TIA);
- case method of
- pm_PartSize:
- if ((h >= 0) and (x > 0)) then
- if (x <= h) then
- begin
- Inc(h);
- p := (h div x);
- if ((p * x) < h) then
- Inc(p);
- SetLength(Result, p);
- for i := 0 to (p - 1) do
- for i2 := 0 to (x - 1) do
- begin
- SetLength(Result[i], x);
- if (a < h) then
- begin
- Result[i][i2] := TIA[a];
- Inc(a);
- end else
- begin
- SetLength(Result[i], i2);
- Exit;
- end;
- end;
- end else
- Result := [TIA];
- pm_PartAmount:
- begin
- if ((h <= 0) or (x < 2)) then
- begin
- if (x = 1) then
- Result := [TIA]
- else
- if (x > 0) then
- SetLength(Result, x);
- Exit;
- end;
- if (h < (x - 1)) then
- x := (h + 1);
- p := Floor((h + 1) / x);
- if (p = 0) then
- p := 1;
- e := ((h + 1) - (p * x));
- if (e >= (h + 1)) then
- e := 0;
- SetLength(Result, x);
- for i := 0 to (x - 1) do
- begin
- if ((e >= (i + 1)) and (e > 0)) then
- SetLength(Result[i], (p + 1))
- else
- if (i <= h) then
- SetLength(Result[i], p);
- h2 := High(Result[i]);
- for i2 := 0 to h2 do
- begin
- Result[i][i2] := TIA[a];
- Inc(a);
- end;
- end;
- end;
- end;
- end;
- function TIAByRange(aStart, aFinish: Integer): TIntArray;
- var
- i: Integer;
- begin
- if (aStart <> aFinish) then
- begin
- SetLength(Result, (IAbs(aStart - aFinish) + 1));
- if (aStart > aFinish) then
- for i := aStart downto aFinish do
- Result[(aStart - i)] := i
- else
- for i := aStart to aFinish do
- Result[(i - aStart)] := i;
- end else
- Result := [aStart];
- end;
- var
- h, i: Integer;
- TIA: TIntArray;
- ATIA: T2DIntArray;
- begin
- ClearDebug;
- TIA := TIAByRange(0, 10);
- WriteLn('TIA: ' + TIAToStr(TIA));
- WriteLn('');
- WriteLn('TIAToParts(TIA, pm_PartSize, 5):');
- ATIA := TIAToParts(TIA, pm_PartSize, 5);
- h := High(ATIA);
- for i := 0 to h do
- WriteLn('ATIA[' + IntToStr(i) + ']: ' + TIAToStr(ATIA[i]));
- SetLength(ATIA, 0);
- WriteLn('');
- WriteLn('TIAToParts(TIA, pm_PartAmount, 5):');
- ATIA := TIAToParts(TIA, pm_PartAmount, 5);
- h := High(ATIA);
- for i := 0 to h do
- WriteLn('ATIA[' + IntToStr(i) + ']: ' + TIAToStr(ATIA[i]));
- SetLength(ATIA, 0);
- SetLength(TIA, 0);
- end.
Advertisement
Add Comment
Please, Sign In to add comment