Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- [SIZE="0"][url=http://villavu.com/forum/showthread.php?t=82205]String Handling Commands[/url] | [url=http://villavu.com/forum/showthread.php?t=104687]Point Commands[/url] | [url=http://villavu.com/forum/showthread.php?t=104649]Box Commands[/url] |[/SIZE] [SIZE="5"][COLOR="red"]Integer Commands[/COLOR][/SIZE]
- Collection of commands for playing around with integers / integer arrays. :sasmokin:
- Added small examples for all commands, just to show how they work (due to missing those code comments), although most of the functions/procedures are pretty much self-explanatory...
- Also most (if not all) will have small explanations included aswell.
- [b]List of [color=blue]Functions[/color] & [color=blue]Procedures[/color]:[/b]
- [code][u]### / Date Added / Command Name[/u]
- 000 / 07-14-2013 / IntDigits()
- 001 / 07-14-2013 / IntSetMin()
- 002 / 07-14-2013 / IntSetMax()
- 003 / 07-14-2013 / IntSetRange()
- 004 / 07-14-2013 / TIASetMin()
- 005 / 07-14-2013 / TIASetMax()
- 006 / 07-14-2013 / TIASetRange()
- 007 / 07-14-2013 / TIACombine()
- 008 / 07-14-2013 / TIAAppend()
- 009 / 07-14-2013 / TIAAdd()
- 010 / 07-14-2013 / TIAInsert()
- 011 / 07-14-2013 / TIAPlant()
- 012 / 07-14-2013 / TIADelete()
- 013 / 07-14-2013 / TIARemove()
- 014 / 07-14-2013 / TIAGet()
- 015 / 07-14-2013 / TIAPickEx()
- 016 / 07-14-2013 / TIAClone()
- 017 / 07-14-2013 / TIACopy()
- 018 / 07-14-2013 / TIACopyEx()
- 019 / 07-14-2013 / TIACreate()
- 020 / 07-14-2013 / TIACreateEx()
- 021 / 07-14-2013 / TIAFill()
- 022 / 07-14-2013 / TIAFillEx()
- 023 / 07-14-2013 / TIAFillIDs()
- 024 / 07-14-2013 / TIAFillIDsEx()
- 025 / 07-14-2013 / TIAReplace()
- 026 / 07-14-2013 / TIAReplaceEx()
- 027 / 07-14-2013 / TIAMove()
- 028 / 07-14-2013 / TIAExtractEveryX()
- 029 / 07-14-2013 / TIAExtractEveryXEx()
- 030 / 07-14-2013 / TIAFilterEveryX()
- 031 / 07-14-2013 / TIAFilterEveryXEx()
- 032 / 07-14-2013 / TIAContains()
- 033 / 07-17-2013 / TIAContainsEx()
- 034 / 07-17-2013 / TIAContainsTIA()
- 035 / 07-14-2013 / TIAPos()
- 036 / 07-14-2013 / TIAPositions()
- 037 / 07-14-2013 / TIAPositionsEx()
- 038 / 07-14-2013 / TIAPositionsMulti()
- 039 / 07-14-2013 / TIABuiltWith()
- 040 / 07-14-2013 / TIADensity()
- 041 / 07-14-2013 / TIANumberline()
- 042 / 07-14-2013 / TIANumberlineSize()
- 043 / 07-14-2013 / TIAByRange()
- 044 / 07-14-2013 / TIAByRange2bit()
- 045 / 07-14-2013 / TIARangeFrom()
- 046 / 07-17-2013 / TIARange()
- 047 / 07-14-2013 / TIAReverse()
- 048 / 07-18-2013 / TIAInvert()
- 048 / 07-14-2013 / TIAUnique()
- 049 / 07-14-2013 / TIASplitEx()
- 050 / 07-14-2013 / TIAGroupEx()
- 051 / 07-17-2013 / TIAMean()
- 052 / 07-17-2013 / TIASum()
- 053 / 07-17-2013 / TIARandomRange()
- 054 / 07-14-2013 / TIAToParts() [*]
- 055 / 07-14-2013 / ATIAMerge()
- 056 / 07-14-2013 / ATIAClone()
- 057 / 07-14-2013 / ATIASetLength()
- [i]* = Amount of custom types required.[/i][/code]
- [spoiler="IntDigits"]
- [simba]{==============================================================================]
- Explanation: Converts integer value (int) to digits of it.
- Example: 1234 => 1,2,3,4, -999 => 9,9,9
- [==============================================================================}
- function IntDigits(int: Int64): TIntegerArray;
- var
- s: string;
- l, i: Integer;
- begin
- {$IFDEF Lape}
- s := IntToStr(Abs(int));
- {$ELSE}
- s := IntToStr(iAbs(int));
- {$ENDIF}
- l := Length(s);
- SetLength(Result, l);
- for i := 0 to (l - 1) do
- Result[i] := StrToInt(s[(i + 1)]);
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(IntDigits(-12345)));
- end.[/simba]
- [/spoiler]
- [spoiler="IntSetMin"]
- [simba]{==============================================================================]
- Explanation: Sets minimum value (x) to val.
- [==============================================================================}
- procedure IntSetMin(var val: Integer; x: Integer);
- begin
- if (val < x) then
- val := Integer(x);
- end;
- var
- test: Integer;
- begin
- test := 1234;
- IntSetMin(test, 1500);
- WriteLn(test);
- end.[/simba]
- [/spoiler]
- [spoiler="IntSetMax"]
- [simba]{==============================================================================]
- Explanation: Sets maximum value (x) to val.
- [==============================================================================}
- procedure IntSetMax(var val: Integer; x: Integer);
- begin
- if (val > x) then
- val := Integer(x);
- end;
- var
- test: Integer;
- begin
- test := 1234;
- IntSetMax(test, 1000);
- WriteLn(test);
- end.[/simba]
- [/spoiler]
- [spoiler="IntSetRange"]
- [simba]{==============================================================================]
- Explanation: Sets val inside range (mn = minimum, mx = maximum)
- [==============================================================================}
- procedure IntSetRange(var val: Integer; mn, mx: Integer);
- begin
- if (mn > mx) then
- Swap(mn, mx);
- if (mn <> mx) then
- begin
- if (val < mn) then
- val := Integer(mn);
- if (val > mx) then
- val := Integer(mx);
- end else
- if ((val < mn) or (val > mx)) then
- val := Integer(mn);
- end;
- var
- test: Integer;
- begin
- test := 1234;
- IntSetRange(test, 1000, 1500);
- WriteLn(test);
- test := 999;
- IntSetRange(test, 1000, 1500);
- WriteLn(test);
- test := 1501;
- IntSetRange(test, 1000, 1500);
- WriteLn(test);
- end.[/simba]
- [/spoiler]
- [spoiler="TIASetMin"]
- [simba]{==============================================================================]
- Explanation: Sets minimum value (x) to TIA items.
- [==============================================================================}
- procedure TIASetMin(var TIA: TIntegerArray; x: Integer);
- var
- h, i: Integer;
- begin
- h := High(TIA);
- for i := 0 to h do
- if (TIA[i] < x) then
- TIA[i] := Integer(x);
- end;
- var
- test: TIntegerArray;
- begin
- test := [-1, 999, 1234, 1501];
- TIASetMin(test, 1000);
- WriteLn(test);
- end.[/simba]
- [/spoiler]
- [spoiler="TIASetMax"]
- [simba]{==============================================================================]
- Explanation: Sets maximum value (x) to TIA items.
- [==============================================================================}
- procedure TIASetMax(var TIA: TIntegerArray; x: Integer);
- var
- h, i: Integer;
- begin
- h := High(TIA);
- for i := 0 to h do
- if (TIA[i] > x) then
- TIA[i] := Integer(x);
- end;
- var
- test: TIntegerArray;
- begin
- test := [-1, 999, 1234, 1501];
- TIASetMax(test, 1000);
- WriteLn(test);
- end.[/simba]
- [/spoiler]
- [spoiler="TIASetRange"]
- [simba]{==============================================================================]
- Explanation: Sets TIA values inside range (mn = minimum, mx = maximum)
- [==============================================================================}
- procedure TIASetRange(var TIA: TIntegerArray; mn, mx: Integer);
- var
- h, i: Integer;
- begin
- if (mn > mx) then
- Swap(mn, mx);
- h := High(TIA);
- for i := 0 to h do
- if (TIA[i] < mn) then
- TIA[i] := Integer(mn)
- else
- if (TIA[i] > mx) then
- TIA[i] := Integer(mx);
- end;
- var
- test: TIntegerArray;
- begin
- test := [-1, 999, 1234, 1501, 9999];
- TIASetRange(test, 1000, 1500);
- WriteLn(test);
- end.[/simba]
- [/spoiler]
- [spoiler="TIACombine"]
- [simba]{==============================================================================]
- Explanation: Returns TIA1 and TIA2 combined together.
- [==============================================================================}
- function TIACombine(TIA1, TIA2: TIntegerArray): TIntegerArray;
- var
- l1, l2, i: Integer;
- begin
- l1 := Length(TIA1);
- l2 := Length(TIA2);
- SetLength(Result, (l1 + l2));
- for i := 0 to (l1 - 1) do
- Result[i] := Integer(TIA1[i]);
- for i := 0 to (l2 - 1) do
- Result[(l1 + i)] := Integer(TIA2[i]);
- end;
- begin
- WriteLn(ToStr(TIACombine([0, 1, 2, 3], [4, 5, 6, 7, 8, 9])));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAAppend"]
- [simba]{==============================================================================]
- Explanation: Appends TIA with x.
- [==============================================================================}
- procedure TIAAppend(var TIA: TIntegerArray; x: Integer);
- var
- aL: Integer;
- begin
- aL := (Length(TIA) + 1);
- SetLength(TIA, aL);
- TIA[(aL - 1)] := Integer(x);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8];
- TIAAppend(TIA, 9);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAAdd"]
- [simba]{==============================================================================]
- Explanation: Adds all addTIA items to TIA. Returns the highest index in the end.
- [==============================================================================}
- function TIAAdd(var TIA: TIntegerArray; addTIA: TIntegerArray): Integer;
- var
- h, l, i: Integer;
- begin
- h := High(addTIA);
- if (h > -1) then
- begin
- l := Length(TIA);
- SetLength(TIA, (l + (h + 1)));
- for i := 0 to h do
- TIA[(i + l)] := Integer(addTIA[i]);
- end;
- Result := High(TIA);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3];
- TIAAdd(TIA, [4, 5, 6, 7, 8, 9]);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAInsert"]
- [simba]{==============================================================================]
- Explanation: Inserts x to index position in TIA.
- [==============================================================================}
- procedure TIAInsert(var TIA: TIntegerArray; index, x: Integer);
- var
- i, l: Integer;
- begin
- l := Length(TIA);
- SetLength(TIA, (l + 1));
- if (index < 0) then
- index := 0;
- if (index > l) then
- index := l;
- if (l > index) then
- for i := (l - 1) downto index do
- TIA[(i + 1)] := Integer(TIA[i]);
- TIA[index] := Integer(x);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 5, 6, 7, 8, 9];
- TIAInsert(TIA, 4, 4);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAPlant"]
- [simba]{==============================================================================]
- Explanation: Plants/places ints to index position in TIA.
- Like TIAInsert(), with an exception that this inserts array of points.
- Returns the highest index from TIA in the end.
- [==============================================================================}
- function TIAPlant(var TIA: TIntegerArray; index: Integer; ints: TIntegerArray): Integer;
- var
- i, l, h: Integer;
- begin
- h := High(ints);
- if (h > -1) then
- begin
- l := Length(TIA);
- SetLength(TIA, (l + (h + 1)));
- if (index < 0) then
- index := 0;
- if (index > l) then
- index := l;
- for i := (l + (h + 1) - 1) downto (index + (h + 1)) do
- TIA[i] := TIA[(i - (h + 1))];
- for i := 0 to h do
- TIA[(i + index)] := Integer(ints[i]);
- end;
- Result := High(TIA);
- end;
- var
- TIA: TIntegerArray;
- begin
- TIA := [0, 1, 8, 9];
- TIAPlant(TIA, 2, [2, 3, 4, 5, 6, 7]);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIADelete"]
- [simba]{==============================================================================]
- Explanation: Deletes a point from TIA, x = index. Returns true with success.
- [==============================================================================}
- function TIADelete(var TIA: TIntegerArray; x: Integer): Boolean;
- var
- i, h: Integer;
- begin
- h := High(TIA);
- Result := ((x <= h) and (x > -1));
- if not Result then
- Exit;
- for i := x to (h - 1) do
- TIA[i] := TIA[(i + 1)];
- SetLength(TIA, h);
- end;
- var
- TIA: TIntegerArray;
- begin
- TIA := [0, 1, 2, 123456789, 3, 4, 5, 6, 7, 8, 9];
- if TIADelete(TIA, 3) then
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIARemove"]
- [simba]{==============================================================================]
- Explanation: Removes integers from TIA, x = array of indexes.
- NOTE: RECURSIVE/DYNAMIC! Pay attention to IDs.. In most cases you'll need to use reversed order.
- Also, being dynamic means that this doesn't pay attention to duplicate indexes in x,
- so this will delete any of those duplicate IDs then multiple times, when it is possible!
- [==============================================================================}
- procedure TIARemove(var TIA: TIntegerArray; x: TIntegerArray);
- var
- i, i2, h, h2: Integer;
- begin
- h := High(TIA);
- h2 := High(x);
- if ((h < 0) or (h2 < 0)) then
- Exit;
- for i2 := 0 to h2 do
- if ((x[i2] <= h) and (x[i2] > -1)) then
- begin
- for i := x[i2] to (h - 1) do
- TIA[i] := TIA[(i + 1)];
- Dec(h);
- end;
- SetLength(TIA, (h + 1));
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [-1, 0, -1, 1, 2, -1, -1, 3, -1, 4, 5, 6, 7, 8, 9];
- TIARemove(TIA, [0, 2, 5, 6, 8]);
- WriteLn(ToStr(TIA) + ' [Without reversed order]');
- SetLength(TIA, 0);
- TIA := [-1, 0, -1, 1, 2, -1, -1, 3, -1, 4, 5, 6, 7, 8, 9];
- TIARemove(TIA, [8, 6, 5, 2, 0]);
- WriteLn(ToStr(TIA) + ' [With reversed order]');
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TIAGet"]
- [simba]{==============================================================================]
- Explanation: Returns array of items from TIA by IDs. Ignores invalid ID's.
- [==============================================================================}
- function TIAGet(TIA: TIntegerArray; IDs: TIntegerArray): TIntegerArray;
- var
- i, h, h2, r: Integer;
- begin
- h := High(TIA);
- h2 := High(IDs);
- if ((h2 > -1) and (h > -1)) then
- begin
- SetLength(Result, (h2 + 1));
- for i := 0 to h2 do
- if ((IDs[i] <= h) and (IDs[i] > -1)) then
- begin
- Result[r] := Integer(TIA[IDs[i]]);
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- var
- TIA: TIntegerArray;
- i: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- WriteLn(TIAGet(TIA, [-111, 0, 1, 2, 3, 4, 5, 6, 7, 8, 1234, 9, 9999, 8, 7, 6, 5, 4, 3, 2, 1, 0, -1]));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAPickEx"]
- [simba]{==============================================================================]
- Explanation: Returns integers (by pick_IDs) from TIA and then deletes em.
- NOTE: Ignores invalid indexes. DYNAMIC (so place indexes in reversed order to get em correctly)!
- [==============================================================================}
- function TIAPickEx(var TIA: TIntegerArray; pick_IDs: TIntegerArray): TIntegerArray;
- var
- h, h2, t, i, r: Integer;
- begin
- h2 := High(TIA);
- h := High(pick_IDs);
- if ((h2 > -1) and (h > -1)) then
- begin
- SetLength(Result, (h2 + 1));
- for i := 0 to h do
- if ((pick_IDs[i] <= h2) and (pick_IDs[i] > -1)) then
- begin
- Result[r] := Integer(TIA[pick_IDs[i]]);
- Inc(r);
- for t := pick_IDs[i] to (h2 - 1) do
- TIA[t] := TIA[(t + 1)];
- SetLength(TIA, h2);
- Dec(h2);
- if (h2 < 0) then
- Break;
- end;
- SetLength(Result, r);
- end else
- SetLength(Result, 0);
- end;
- var
- TIA, TIA2: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- WriteLn('TIA (Before picking): ' + ToStr(TIA));
- TIA2 := TIAPickEx(TIA, [8, 6, 4, 2, 0]);
- WriteLn('TIA (After picking): ' + ToStr(TIA));
- WriteLn('TIA2 (Picked points): ' + ToStr(TIA2));
- SetLength(TIA, 0);
- SetLength(TIA2, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TIAClone"]
- [simba]{==============================================================================]
- Explanation: Returns copy ("clone") of TIA safely
- [==============================================================================}
- function TIAClone(TIA: TIntegerArray): TIntegerArray;
- var
- i, l: Integer;
- begin
- l := Length(TIA);
- SetLength(Result, l);
- for i := 0 to (l - 1) do
- Result[i] := Integer(TIA[i]);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- WriteLn(TIAClone(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIACopy"]
- [simba]{==============================================================================]
- Explanation: Version of Copy() function for TIntegerArrays.
- [==============================================================================}
- function TIACopy(TIA: TIntegerArray; startIndex, count: Integer): TIntegerArray;
- var
- i, l, t: Integer;
- begin
- l := Length(TIA);
- if (startIndex < 0) then
- startIndex := 0;
- if ((l >= startIndex) and (count > 0)) then
- begin
- t := (l - startIndex);
- if (count > t) then
- count := t;
- SetLength(Result, count);
- for i := startIndex to ((startIndex + count) - 1) do
- Result[(i - startIndex)] := Integer(TIA[i]);
- end else
- SetLength(Result, 0);
- end;
- var
- TIA: TIntegerArray;
- begin
- TIA := [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- WriteLn(ToStr(TIACopy(TIA, 1, 10)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIACopyEx"]
- [simba]{==============================================================================]
- Explanation: Copies TIA from pos1 to pos2. Reverse copies with pos1>pos2 (flexible!).
- [==============================================================================}
- function TIACopyEx(TIA: TIntegerArray; pos1, pos2: Integer): TIntegerArray;
- var
- i, l: Integer;
- begin
- l := Length(TIA);
- if (l > 0) then
- begin
- if (pos1 < 0) then
- pos1 := 0;
- if (pos1 > (l - 1)) then
- pos1 := (l - 1);
- if (pos2 < 0) then
- pos2 := 0;
- if (pos2 > (l - 1)) then
- pos2 := (l - 1);
- if (pos1 <> pos2) then
- begin
- {$IFNDEF Lape}
- SetLength(Result, (iAbs(pos1 - pos2) + 1));
- {$ELSE}
- SetLength(Result, Integer(Abs(pos1 - pos2) + 1));
- {$ENDIF}
- if (pos1 < pos2) then
- begin
- for i := pos1 to pos2 do
- Result[(i - pos1)] := Integer(TIA[i]);
- end else
- for i := pos1 downto pos2 do
- Result[(pos1 - i)] := Integer(TIA[i]);
- end else
- Result := [Integer(TIA[pos1])];
- end else
- SetLength(Result, 0);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [-1, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10];
- WriteLn(ToStr(TIACopyEx(TIA, 1, 10)));
- WriteLn(ToStr(TIACopyEx(TIA, 10, 1)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIACreate"]
- [simba]{==============================================================================]
- Explanation: Creates TIA with x (for every index) where size is length of the array.
- [==============================================================================}
- function TIACreate(x: Integer; size: Integer): TIntegerArray;
- var
- i: Integer;
- begin
- if (size > 0) then
- begin
- SetLength(Result, size);
- for i := 0 to (size - 1) do
- Result[i] := Integer(x);
- end else
- SetLength(Result, 0);
- end;
- var
- TIA: TIntegerArray;
- begin
- TIA := TIACreate(123, 3);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIACreateEx"]
- [simba]{==============================================================================]
- Explanation: Creates TIA with ints (for every index) where size is length of the array.
- [==============================================================================}
- function TIACreateEx(ints: TIntegerArray; size: Integer): TIntegerArray;
- var
- x, y: Integer;
- begin
- if (size > 0) then
- begin
- y := Length(ints);
- if (y = 0) then
- SetLength(Result, 0);
- SetLength(Result, size);
- if (y > 0) then
- for x := 0 to (size - 1) do
- Result[x] := Integer(ints[(x mod y)]);
- end else
- SetLength(Result, 0);
- end;
- var
- TIA: TIntegerArray;
- begin
- TIA := TIACreateEx([1, 2, 3, 4], 10);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAFill"]
- [simba]{==============================================================================]
- Explanation: Fills whole TIA with x.
- [==============================================================================}
- procedure TIAFill(var TIA: TIntegerArray; x: Integer);
- var
- h, i: Integer;
- begin
- h := High(TIA);
- for i := 0 to h do
- TIA[i] := Integer(x);
- end;
- var
- TIA: TIntegerArray;
- begin
- SetLength(TIA, 5);
- TIAFill(TIA, 123);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAFillEx"]
- [simba]{==============================================================================]
- Explanation: Fills whole TIA with x.
- [==============================================================================}
- procedure TIAFillEx(var TIA: TIntegerArray; x: TIntegerArray);
- var
- i, h, l: Integer;
- begin
- h := High(TIA);
- l := Length(x);
- for i := 0 to h do
- TIA[i] := Integer(x[(i mod l)]);
- end;
- var
- TIA: TIntegerArray;
- begin
- SetLength(TIA, 25);
- TIAFillEx(TIA, [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAFillIDs"]
- [simba]{==============================================================================]
- Explanation: Fills TIA indexes (IDs) with x.
- [==============================================================================}
- procedure TIAFillIDs(var TIA: TIntegerArray; IDs: TIntegerArray; x: Integer);
- var
- h, l, i: Integer;
- begin
- l := Length(TIA);
- h := High(IDs);
- if ((l > 0) and (h > -1)) then
- for i := 0 to h do
- if ((IDs[i] < l) and (IDs[i] > -1)) then
- TIA[IDs[i]] := Integer(x);
- end;
- var
- TIA: TIntegerArray;
- begin
- SetLength(TIA, 13);
- TIAFillIDs(TIA, [0, 1, 2, 6, 10, 11, 12], 69);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAFillIDsEx"]
- [simba]{==============================================================================]
- Explanation: Fills TIA indexes (IDs) with ints.
- [==============================================================================}
- procedure TIAFillIDsEx(var TIA: TIntegerArray; IDs, ints: TIntegerArray);
- var
- h, l, i, m, r: Integer;
- begin
- l := Length(TIA);
- h := High(IDs);
- m := Length(ints);
- if ((l > 0) and (h > -1) and (m > 0)) then
- for i := 0 to h do
- if ((IDs[i] < l) and (IDs[i] > -1)) then
- begin
- TIA[IDs[i]] := Integer(ints[(r mod m)]);
- Inc(r);
- end;
- end;
- var
- TIA: TIntegerArray;
- begin
- SetLength(TIA, 13);
- TIAFillIDsEx(TIA, [0, 1, 2, 3, 6, 9, 10, 11, 12], [1, 2, 3]);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAReplace"]
- [simba]{==============================================================================]
- Explanation: Replaces all oldValue's in TIA with newValue.
- [==============================================================================}
- procedure TIAReplace(var TIA: TIntegerArray; oldValue, newValue: Integer);
- var
- h, i: Integer;
- begin
- if (oldValue <> newValue) then
- begin
- h := High(TIA);
- for i := 0 to h do
- if (TIA[i] = oldValue) then
- TIA[i] := Integer(newValue);
- end;
- end;
- var
- TIA: TIntegerArray;
- begin
- SetLength(TIA, 10);
- TIA[0] := 1;
- TIA[1] := 2;
- TIA[8] := 3;
- TIA[9] := 4;
- TIAReplace(TIA, 0, 9);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAReplaceEx"]
- [simba]{==============================================================================]
- Explanation: Replaces all oldPoints' in TIA with newPoint.
- [==============================================================================}
- procedure TIAReplaceEx(var TIA: TIntegerArray; oldValues: TIntegerArray; newValue: Integer);
- var
- a, b, x, y: Integer;
- begin
- y := High(oldValues);
- if (y > -1) then
- begin
- b := High(TIA);
- for a := 0 to b do
- for x := 0 to y do
- if (TIA[a] = oldValues[x]) then
- begin
- TIA[a] := Integer(newValue);
- Break;
- end;
- end;
- end;
- var
- TIA: TIntegerArray;
- begin
- SetLength(TIA, 10);
- TIA[0] := 1;
- TIA[2] := 2;
- TIA[4] := 3;
- TIA[6] := 2;
- TIA[8] := 1;
- TIAReplaceEx(TIA, [1, 3, 2], 9);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAMove"]
- [simba]{==============================================================================]
- Explanation: Moves oldIndex to newIndex in TIA. Returns true, if movement was succesfully done!
- [==============================================================================}
- function TIAMove(var TIA: TIntegerArray; oldIndex, newIndex: Integer): Boolean;
- var
- h, i: Integer;
- begin
- h := High(TIA);
- Result := ((h > 0) and (oldIndex <> newIndex) and InRange(oldIndex, 0, h) and InRange(newIndex, 0, h));
- if Result then
- if (oldIndex > newIndex) then
- begin
- for i := oldIndex downto (newIndex + 1) do
- Swap(TIA[i], TIA[(i - 1)]);
- end else
- for i := oldIndex to (newIndex - 1) do
- Swap(TIA[i], TIA[(i + 1)]);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 6, 2, 3, 4, 5, 7, 8, 9];
- TIAMove(TIA, 2, 6);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAExtractEveryX"]
- [simba]{==============================================================================]
- Explanation: Extracts every X point from TIA.
- [==============================================================================}
- procedure TIAExtractEveryX(var TIA: TIntegerArray; X: Integer);
- var
- i, h, t: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- if (X > 0) then
- for i := 0 to h do
- if ((i mod X) = 0) then
- begin
- TIA[t] := TIA[i];
- Inc(t);
- end;
- SetLength(TIA, t);
- end;
- end;
- var
- TIA, tmp: TIntegerArray;
- i, c: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- for i := 0 to 11 do
- begin
- SetLength(tmp, Length(TIA));
- for c := 0 to (Length(TIA) - 1) do
- tmp[c] := TIA[c];
- TIAExtractEveryX(tmp, i);
- WriteLn('TIAExtractEveryX(TIA, ' + IntToStr(i) + '): ' + ToStr(tmp));
- SetLength(tmp, 0);
- end;
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TIAExtractEveryXEx"]
- [simba]{==============================================================================]
- Explanation: Extracts every X point from TIA - starting from offset.
- [==============================================================================}
- procedure TIAExtractEveryXEx(var TIA: TIntegerArray; X, offset: Integer);
- var
- i, h, t: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- if ((X > 0) and ((offset > -1) and (offset <= h))) then
- for i := offset to h do
- if (((i - offset) mod X) = 0) then
- begin
- TIA[t] := TIA[i];
- Inc(t);
- end;
- SetLength(TIA, t);
- end;
- end;
- var
- TIA, tmp: TIntegerArray;
- i, c: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- for i := 0 to 9 do
- begin
- SetLength(tmp, Length(TIA));
- for c := 0 to (Length(TIA) - 1) do
- tmp[c] := TIA[c];
- TIAExtractEveryXEx(tmp, i, 2);
- WriteLn('TIAExtractEveryX(TIA, ' + IntToStr(i) + ', 2): ' + ToStr(tmp));
- SetLength(tmp, 0);
- end;
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TIAFilterEveryX"]
- [simba]{==============================================================================]
- Explanation: Filters every X point from TIA.
- [==============================================================================}
- procedure TIAFilterEveryX(var TIA: TIntegerArray; X: Integer);
- var
- i, h, t: Integer;
- begin
- h := High(TIA);
- if ((h > -1) and (X > 0)) then
- begin
- for i := 0 to h do
- if ((i mod X) <> 0) then
- begin
- TIA[t] := TIA[i];
- Inc(t);
- end;
- SetLength(TIA, t);
- end;
- end;
- var
- TIA, tmp: TIntegerArray;
- i, c: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- for i := 0 to 10 do
- begin
- SetLength(tmp, Length(TIA));
- for c := 0 to (Length(TIA) - 1) do
- tmp[c] := TIA[c];
- TIAFilterEveryX(tmp, i);
- WriteLn('TIAFilterEveryX(TIA, ' + IntToStr(i) + '): ' + ToStr(tmp));
- SetLength(tmp, 0);
- end;
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TIAFilterEveryXEx"]
- [simba]{==============================================================================]
- Explanation: Filters every X point from TIA - starting from offset.
- [==============================================================================}
- procedure TIAFilterEveryXEx(var TIA: TIntegerArray; X, offset: Integer);
- var
- i, h, t: Integer;
- begin
- h := High(TIA);
- if ((h > -1) and (X > 0) and ((offset > -1) and (offset <= h))) then
- begin
- t := offset;
- for i := offset to h do
- if (((i - offset) mod X) <> 0) then
- begin
- TIA[t] := TIA[i];
- Inc(t);
- end;
- SetLength(TIA, t);
- end;
- end;
- var
- TIA, tmp: TIntegerArray;
- i, c: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- for i := 0 to 9 do
- begin
- SetLength(tmp, Length(TIA));
- for c := 0 to (Length(TIA) - 1) do
- tmp[c] := TIA[c];
- TIAFilterEveryXEx(tmp, i, 2);
- WriteLn('TIAFilterEveryXEx(TIA, ' + IntToStr(i) + ', 2): ' + ToStr(tmp));
- SetLength(tmp, 0);
- end;
- SetLength(TIA, 0);
- end.[/simba]
- [/spoiler]
- [spoiler="TIAContains"]
- [simba]{==============================================================================]
- Explanation: Returns true if TIA contains x.
- [==============================================================================}
- function TIAContains(TIA: TIntegerArray; x: Integer): Boolean;
- var
- i, h: Integer;
- begin
- h := High(TIA);
- for i := 0 to h do
- begin
- Result := (x = TIA[i]);
- if Result then
- Exit;
- end;
- Result := False;
- end;
- var
- TIA: TIntegerArray;
- i: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- for i := -1 to 10 do
- if TIAContains(TIA, i) then
- WriteLn(IntToStr(i) + ' exists in TIA!')
- else
- WriteLn(IntToStr(i) + ' DOES NOT exist in TIA!');
- end.[/simba]
- [/spoiler]
- [spoiler="TIAContainsEx"]
- [simba]{==============================================================================]
- Explanation: Returns true if TIA contains ANY Integer from ints.
- [==============================================================================}
- function TIAContainsEx(TIA, ints: TIntegerArray): Boolean;
- var
- a, b, x, y: Integer;
- begin
- b := High(TIA);
- y := High(ints);
- Result := True;
- if ((b > -1) and (y > -1)) then
- for a := 0 to b do
- for x := 0 to y do
- if (TIA[a] = ints[x]) then
- Exit;
- Result := False;
- end;
- begin
- ClearDebug;
- if TIAContainsEx([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [8, 3, 5]) then
- WriteLn('YEP!');
- end.[/simba]
- [/spoiler]
- [spoiler="TIAContainsTIA"]
- [simba]{==============================================================================]
- Explanation: Returns true if TIA contains WHOLE chain of Integers (arr) in it.
- [==============================================================================}
- function TIAContainsTIA(TIA, arr: TIntegerArray): Boolean;
- var
- a, b, x, y, z: Integer;
- begin
- z := High(TIA);
- y := High(arr);
- if ((z >= y) and (z > -1)) then
- begin
- b := (z - y);
- for a := 0 to b do
- begin
- for x := 0 to y do
- if (TIA[(a + x)] <> arr[x]) then
- Break;
- if (x > y) then
- Break;
- end;
- Result := (a <= b);
- end else
- Result := False;
- end;
- begin
- ClearDebug;
- if TIAContainsTIA([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [8, 3, 5]) then
- WriteLn('NOOOO!');
- if TIAContainsTIA([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], [5, 6, 7]) then
- WriteLn('YEEAH!');
- end.[/simba]
- [/spoiler]
- [spoiler="TIAPos"]
- [simba]{==============================================================================]
- Explanation: Returns index position from TIA which matched with x.
- Returns -1 if any of TIA items doesnt match with x!
- [==============================================================================}
- function TIAPos(TIA: TIntegerArray; x: Integer): Integer;
- var
- h: Integer;
- begin
- h := High(TIA);
- for Result := 0 to h do
- if (TIA[Result] = x) then
- Exit;
- Result := -1;
- end;
- var
- TIA: TIntegerArray;
- i, p: Integer;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- for i := -1 to 10 do
- begin
- p := TIAPos(TIA, i);
- if (p > -1) then
- WriteLn(IntToStr(i) + ' index in TIA: ' + IntToStr(p))
- else
- WriteLn(IntToStr(i) + ' DOES NOT exist in TIA!');
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="TIAPositions"]
- [simba]{==============================================================================]
- Explanation: Returns all the TIA positions where x can be found.
- [==============================================================================}
- function TIAPositions(TIA: TIntegerArray; x: Integer): TIntegerArray;
- var
- i, h, r: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- SetLength(Result, (h + 1));
- for i := 0 to h do
- if (TIA[i] = x) then
- begin
- Result[r] := i;
- Inc(r);
- end;
- end;
- SetLength(Result, r);
- end;
- begin
- WriteLn(ToStr(TIAPositions([1, 2, 3, 1, 2, 3, 1], 1)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAPositionsEx"]
- [simba]{==============================================================================]
- Explanation: Returns all the TIA positions where index contains any integer from ints.
- [==============================================================================}
- function TIAPositionsEx(TIA, ints: TIntegerArray): TIntegerArray;
- var
- i, v, h, l, r: Integer;
- begin
- h := High(TIA);
- l := Length(ints);
- if ((l > 0) and (h > -1)) then
- begin
- SetLength(Result, (h + 1));
- for i := 0 to h do
- for v := 0 to (l - 1) do
- if (ints[v] = TIA[i]) then
- begin
- Result[r] := i;
- Inc(r);
- Break;
- end;
- end;
- SetLength(Result, r);
- end;
- var
- a, b: TIntegerArray;
- begin
- a := [0, 1, 0, 1, 2, 3, 2, 3, 4];
- b := [0, 1, 2];
- WriteLn(ToStr(TIAPositionsEx(a, b)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAPositionsMulti"]
- [simba]{==============================================================================]
- Explanation: Returns the TIA positions where index contains any integer from ints.
- NOTE: Doesn't return ALL indexes, like TIAPositionsEx() does.
- [==============================================================================}
- function TIAPositionsMulti(TIA, ints: TIntegerArray): TIntegerArray;
- var
- i, v, h, l: Integer;
- begin
- h := High(ints);
- l := Length(TIA);
- if ((l > 0) and (h > -1)) then
- begin
- SetLength(Result, (h + 1));
- for i := 0 to h do
- begin
- for v := 0 to (l - 1) do
- if (TIA[v] = ints[i]) then
- Break;
- if (v < l) then
- Result[i] := v
- else
- Result[i] := -1;
- end;
- end else
- SetLength(Result, 0);
- end;
- var
- a, b: TIntegerArray;
- begin
- a := [0, 1, 0, 1, 2, 3, 2, 3, 4];
- b := [0, 1, 2];
- WriteLn(ToStr(TIAPositionsMulti(a, b)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIABuiltWith"]
- [simba]{==============================================================================]
- Explanation: Returns true if TIA is built ONLY with allowed values.
- [==============================================================================}
- function TIABuiltWith(TIA, allowed: TIntegerArray): Boolean;
- var
- h, i, x, y: Integer;
- begin
- Result := False;
- h := High(TIA);
- y := High(allowed);
- if ((h > -1) and (y > -1)) then
- begin
- for i := 0 to h do
- begin
- for x := 0 to y do
- if (TIA[i] = allowed[x]) then
- Break;
- if (x > y) then
- Exit;
- end;
- Result := True;
- end;
- end;
- begin
- if TIABuiltWith([1, 2, 3, 4], [0, 1, 2, 3, 4, 5]) then
- WriteLn('YES!');
- if TIABuiltWith([0, 1, 2, 3, 4, 5], [1, 2, 3, 4]) then
- WriteLn('NOOOO!');
- end.[/simba]
- [/spoiler]
- [spoiler="TIADensity"]
- [simba]{==============================================================================]
- Explanation: Returns the calculated density from given TIA.
- [==============================================================================}
- function TIADensity(TIA: TIntegerArray): Extended;
- var
- h, l, i, d, mn, mx, x, y: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- mn := TIA[0];
- mx := TIA[0];
- if (h > 0) then
- for i := (h - d) downto 1 do
- begin
- if (TIA[i] < mn) then
- mn := TIA[i]
- else
- if (TIA[i] > mx) then
- mx := TIA[i];
- for x := (i - 1) downto 0 do
- if (TIA[i] = TIA[x]) then
- begin
- l := High(TIA);
- for y := i to (l - 1) do
- TIA[y] := TIA[(y + 1)];
- SetLength(TIA, l);
- Inc(d);
- Break;
- end;
- end;
- {$IFNDEF Lape}
- Result := (Extended(Length(TIA)) / (iAbs(mn - mx) + 1));
- {$ELSE}
- Result := (Extended(Length(TIA)) / (Abs(mn - mx) + 1));
- {$ENDIF}
- end else
- Result := 0;
- end;
- var
- test: TIntegerArray;
- i, l: Integer;
- begin
- ClearDebug;
- test := [0];
- for i := 5 downto 1 do
- begin
- l := Length(test);
- SetLength(test, (l + 1));
- test[l] := i;
- WriteLn(ToStr(TIADensity(test)) + ' ' + ToStr(test));
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="TIANumberline"]
- [simba]{==============================================================================]
- Explanation: Returns numberline by TIA minimum and maximum values from TIA.
- [==============================================================================}
- function TIANumberline(TIA: TIntegerArray): TIntegerArray;
- var
- i, s, f, mn, mx, x, y: Integer;
- begin
- y := High(TIA);
- if (y > -1) then
- begin
- mn := TIA[0];
- mx := TIA[0];
- if (y > 0) then
- for x := 1 to y do
- if (TIA[x] < mn) then
- mn := TIA[x]
- else
- if (TIA[x] > mx) then
- mx := TIA[x];
- if (mn <> mx) then
- begin
- s := Integer(mn);
- f := Integer(mx);
- {$IFNDEF Lape}
- SetLength(Result, (IAbs(mn - mx) + 1));
- {$ELSE}
- SetLength(Result, (Abs(mn - mx) + 1));
- {$ENDIF}
- for i := s to f do
- Result[(i - s)] := i;
- end else
- Result := [Integer(mn)];
- end else
- SetLength(Result, 0);
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(TIANumberline([10, 5, 2])));
- WriteLn(ToStr(TIANumberline([999, 995, 990, 985, 980])));
- end.[/simba]
- [/spoiler]
- [spoiler="TIANumberlineSize"]
- [simba]{==============================================================================]
- Explanation: Returns the size of numberline by TIA.
- [==============================================================================}
- function TIANumberlineSize(TIA: TIntegerArray): Integer;
- var
- x, y, mn, mx: Integer;
- begin
- y := High(TIA);
- if (y > -1) then
- begin
- mn := TIA[0];
- mx := TIA[0];
- if (y > 0) then
- for x := 1 to y do
- if (TIA[x] < mn) then
- mn := TIA[x]
- else
- if (TIA[x] > mx) then
- mx := TIA[x];
- {$IFNDEF Lape}
- Result := (iAbs(mn - mx) + 1);
- {$ELSE}
- Result := (Abs(mn - mx) + 1);
- {$ENDIF}
- end else
- Result := 0;
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(TIANumberlineSize([10, 5, 2])));
- WriteLn(ToStr(TIANumberlineSize([999, 995, 990, 985, 980])));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAByRange"]
- [simba]{==============================================================================]
- Explanation: Returns a TIA that contains all the value from start value (aStart)
- to finishing value (aFinish)..
- [==============================================================================}
- function TIAByRange(aStart, aFinish: Integer): TIntegerArray;
- var
- i, s, f: Integer;
- begin
- if (aStart <> aFinish) then
- begin
- s := Integer(aStart);
- f := Integer(aFinish);
- {$IFNDEF Lape}
- SetLength(Result, (IAbs(aStart - aFinish) + 1));
- {$ELSE}
- SetLength(Result, (Abs(aStart - aFinish) + 1));
- {$ENDIF}
- if (aStart > aFinish) then
- begin
- for i := s downto f do
- Result[(s - i)] := i;
- end else
- for i := s to f do
- Result[(i - s)] := i;
- end else
- Result := [Integer(aStart)];
- end;
- begin
- WriteLn(ToStr(TIAByRange(10, -10)));
- WriteLn(ToStr(TIAByRange(-10, 10)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAByRange2bit"]
- [simba]{==============================================================================]
- Explanation: Returns a TIA that contains all the value from start value (aStart)
- to finishing value (aFinish)..
- Works with 2-bit method, that cuts loop in half.
- [==============================================================================}
- function TIAByRange2bit(aStart, aFinish: Integer): TIntegerArray;
- var
- g, l, i, s, f: Integer;
- begin
- if (aStart <> aFinish) then
- begin
- s := Integer(aStart);
- f := Integer(aFinish);
- {$IFNDEF Lape}
- l := (IAbs(aStart - aFinish) + 1);
- {$ELSE}
- l := (Abs(aStart - aFinish) + 1);
- {$ENDIF}
- SetLength(Result, l);
- g := ((l - 1) div 2);
- if (aStart < aFinish) then
- begin
- for i := 0 to g do
- begin
- Result[i] := (s + i);
- Result[((l - 1) - i)] := (f - i);
- end;
- if ((l mod 2) <> 0) then
- Result[i] := (s + i);
- end else
- begin
- for i := 0 to g do
- begin
- Result[i] := (s - i);
- Result[((l - 1) - i)] := (f + i);
- end;
- if ((l mod 2) <> 0) then
- Result[i] := (s - i);
- end;
- end else
- Result := [Integer(aStart)];
- end;
- begin
- WriteLn(ToStr(TIAByRange2bit(10, -10)));
- WriteLn(ToStr(TIAByRange2bit(-10, 10)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIARangeFrom"]
- [simba]{==============================================================================]
- Explanation: Returns a TIA from start position, where step is the difference between each range value.
- Count is the size of the result..
- Examples: (3, -1, 3) => [3, 2, 1] and (0, 2, 4) => [0, 2, 4, 6]
- [==============================================================================}
- function TIARangeFrom(start, step, count: Integer): TIntegerArray;
- var
- i: Integer;
- begin
- if (count > 0) then
- begin
- SetLength(Result, count);
- for i := 0 to (count - 1) do
- Result[i] := (start + (i * step));
- end else
- SetLength(Result, 0);
- end;
- const
- START = 2;
- STEP = 2;
- COUNT = 4;
- begin
- WriteLn(ToStr(TIARangeFrom(START, STEP, COUNT)));
- WriteLn(ToStr(TIARangeFrom(START, -STEP, COUNT)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIARange"]
- [simba]{==============================================================================]
- Explanation: Determines the lowest and highest values in TIA and stores em to lo and hi variables.
- [==============================================================================}
- procedure TIARange(TIA: TIntegerArray; var lo, hi: Integer);
- var
- i, h: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- lo := TIA[0];
- hi := TIA[0];
- if (h > 0) then
- for i := 1 to h do
- if (TIA[i] < lo) then
- lo := TIA[i]
- else
- if (TIA[i] > hi) then
- hi := TIA[i];
- end;
- end;
- var
- lowest, highest: Integer;
- begin
- ClearDebug;
- TIARange([3, 1, 5, 6, -1, 9, 5, 6, 4, 9, 0], lowest, highest);
- WriteLn('Lowest: ' + ToStr(lowest) + ', Highest: ' + ToStr(highest));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAReverse"]
- [simba]{==============================================================================]
- Explanation: Reverses TIA.
- [==============================================================================}
- procedure TIAReverse(var TIA: TIntegerArray);
- var
- g, h, i: Integer;
- begin
- h := High(TIA);
- if (h < 1) then
- Exit;
- g := (h div 2);
- for i := 0 to g do
- Swap(TIA[i], TIA[(h - i)]);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- TIAReverse(TIA);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAInvert"]
- [simba]{==============================================================================]
- Explanation: Inverts TIA by replacing all of the integers inside the numberline that weren't defined in the array.
- [==============================================================================}
- procedure TIAInvert(var TIA: TIntegerArray);
- var
- c, l, i, mn, mx, x, y, r: Integer;
- b: TBoolArray;
- begin
- y := High(TIA);
- if (y > 0) then
- begin
- mn := TIA[0];
- mx := TIA[0];
- for x := 1 to y do
- if (TIA[x] < mn) then
- mn := TIA[x]
- else
- if (TIA[x] > mx) then
- mx := TIA[x];
- if (mn <> mx) then
- begin
- {$IFNDEF Lape}
- l := (IAbs(mn - mx) + 1);
- {$ELSE}
- l := (Abs(mn - mx) + 1);
- {$ENDIF}
- SetLength(b, l);
- for x := 0 to y do
- if not b[(TIA[x] - mn)] then
- begin
- b[(TIA[x] - mn)] := True;
- Inc(c);
- end;
- SetLength(TIA, (l - c));
- if (Length(TIA) > 0) then
- for i := 0 to (l - 1) do
- if not b[i] then
- begin
- TIA[r] := (i + mn);
- Inc(r);
- end;
- SetLength(b, 0);
- end else
- SetLength(TIA, 0);
- end else
- SetLength(TIA, 0);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [5, 10, 0];
- TIAInvert(TIA);
- WriteLn(ToStr(TIA));
- TIA := [999, 984, 995, 990, 985, 980];
- TIAInvert(TIA);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAUnique"]
- [simba]{==============================================================================]
- Explanation: Removes all duplicates from TIA.
- [==============================================================================}
- procedure TIAUnique(var TIA: TIntegerArray);
- var
- h, h2, i, i2, i3, d: Integer;
- begin
- h := High(TIA);
- if (h < 1) then
- Exit;
- for i := (h - d) downto 1 do
- for i2 := (i - 1) downto 0 do
- if (TIA[i] = TIA[i2]) then
- begin
- h2 := High(TIA);
- for i3 := i to (h2 - 1) do
- TIA[i3] := TIA[(i3 + 1)];
- SetLength(TIA, h2);
- Inc(d);
- Break;
- end;
- end;
- var
- TIA: TIntegerArray;
- begin
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0];
- TIAUnique(TIA);
- WriteLn(ToStr(TIA));
- end.[/simba]
- [/spoiler]
- [spoiler="TIASplitEx"]
- [simba]{==============================================================================]
- Explanation: Splits given TIntArray (TIA) into T2DIntArray by grouping together the integer values
- that are within a given difference range (minDifference, maxDifference) from each other.
- [==============================================================================}
- function TIASplitEx(TIA: TIntegerArray; minDifference, maxDifference: Integer): T2DIntegerArray;
- var
- a, b, h, l, i, r, d: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- SetLength(Result, (h + 1));
- Result[0] := [Integer(TIA[0])];
- if (h > 0) then
- begin
- r := 1;
- if (minDifference > maxDifference) then
- Swap(minDifference, maxDifference);
- for i := 1 to h do
- begin
- for a := 0 to (r - 1) do
- begin
- l := Length(Result[a]);
- for b := 0 to (l - 1) do
- begin
- {$IFNDEF Lape}
- d := IAbs(TIA[i] - Result[a][b]);
- {$ELSE}
- d := Abs(TIA[i] - Result[a][b]);
- {$ENDIF}
- if ((d >= minDifference) and (d <= maxDifference)) then
- begin
- SetLength(Result[a], (l + 1));
- Result[a][l] := Integer(TIA[i]);
- Break;
- end;
- end;
- if (b < l) then
- Break;
- end;
- if (a >= r) then
- begin
- Result[r] := [Integer(TIA[i])];
- Inc(r);
- end;
- end;
- end;
- end;
- SetLength(Result, r);
- end;
- var
- TIA: TIntegerArray;
- ATIA: T2DIntegerArray;
- i: Integer;
- begin
- ClearDebug;
- TIA := [0, 18, 1, 5, 6, 9, 11, 12, 17, 18, 19, 20, 15, 0, -1, 24];
- for i := 0 to 5 do
- begin
- ATIA := TIASplitEx(TIA, 0, i);
- WriteLn('ATIA = TIASplitEx(TIA, 0, ' + IntToStr(i) + '): ' + ToStr(ATIA));
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="TIAGroupEx"]
- [simba]{==============================================================================]
- Explanation: Splits given TIntArray (TIA) into T2DIntArray by grouping together the integer values
- that are within a given difference range (minDifference, maxDifference) of the first integer value in the sub-array.
- [==============================================================================}
- function TIAGroupEx(TIA: TIntegerArray; minDifference, maxDifference: Integer): T2DIntegerArray;
- var
- a, h, l, i, r, d: Integer;
- begin
- h := High(TIA);
- if (h > -1) then
- begin
- SetLength(Result, (h + 1));
- Result[0] := [Integer(TIA[0])];
- if (h > 0) then
- begin
- r := 1;
- if (minDifference > maxDifference) then
- Swap(minDifference, maxDifference);
- for i := 1 to h do
- begin
- for a := 0 to (r - 1) do
- begin
- {$IFNDEF Lape}
- d := IAbs(TIA[i] - Result[a][0]);
- {$ELSE}
- d := Abs(TIA[i] - Result[a][0]);
- {$ENDIF}
- if ((d >= minDifference) and (d <= maxDifference)) then
- begin
- l := Length(Result[a]);
- SetLength(Result[a], (l + 1));
- Result[a][l] := Integer(TIA[i]);
- Break;
- end;
- end;
- if (a >= r) then
- begin
- Result[r] := [Integer(TIA[i])];
- Inc(r);
- end;
- end;
- end;
- end;
- SetLength(Result, r);
- end;
- var
- TIA: TIntegerArray;
- ATIA: T2DIntegerArray;
- i: Integer;
- begin
- ClearDebug;
- TIA := [0, 18, 1, 5, 6, 9, 11, 12, 17, 18, 19, 20, 15, 0, -1, 24];
- for i := 0 to 24 do
- begin
- ATIA := TIAGroupEx(TIA, 0, i);
- WriteLn('ATIA = TIASplitEx(TIA, 0, ' + IntToStr(i) + '): ' + ToStr(ATIA));
- end;
- end.[/simba]
- [/spoiler]
- [spoiler="TIAMean"]
- [simba]{==============================================================================]
- Explanation: Returns the arithmetic mean of TIA values.
- [==============================================================================}
- function TIAMean(TIA: TIntegerArray): Extended;
- var
- i, h: Integer;
- begin
- Result := 0;
- h:= High(TIA);
- for i:= 0 to h do
- Result := (Result + (Extended(TIA[i]) / (h + 1)));
- end;
- begin
- WriteLn(FloatToStr(TIAMean([1, 5, 8, 3])));
- end.[/simba]
- [/spoiler]
- [spoiler="TIASum"]
- [simba]{==============================================================================]
- Explanation: Returns the sum of all TIA values together.
- [==============================================================================}
- function TIASum(TIA: TIntegerArray): Int64;
- var
- i, h: Integer;
- begin
- Result := 0;
- h:= High(TIA);
- for i:= 0 to h do
- Result := (Result + TIA[i]);
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(TIASum([3, 2, 1, 4])));
- end.[/simba]
- [/spoiler]
- [spoiler="TIARandomRange"]
- [simba]{==============================================================================]
- Explanation: Generates an array of random numbers from aFrom to aTo.
- Amount is the size of the array.
- If duplicates is set as true, the values wont be unique to each other.
- [==============================================================================}
- function TIARandomRange(aFrom, aTo, amount: Integer; duplicates: Boolean): TIntegerArray;
- var
- a, d, e, m, n, r, i, t, x, y: Integer;
- tmp: TIntegerArray;
- b: Boolean;
- begin
- if (amount > 0) then
- if not duplicates then
- begin
- if (aFrom > aTo) then
- Swap(aFrom, aTo);
- {$IFNDEF Lape}
- a := IAbs(aTo - aFrom);
- {$ELSE}
- a := Abs(aTo - aFrom);
- {$ENDIF}
- if (a < amount) then
- amount := a;
- SetLength(Result, amount);
- if (amount > 0) then
- begin
- if (amount < 20000) then
- begin
- for r := 0 to (amount - 1) do
- repeat
- n := RandomRange(aFrom, aTo);
- y := High(Result);
- for x := 0 to y do
- if (Result[x] = n) then
- Break;
- if (x > y) then
- begin
- Result[i] := n;
- Inc(i);
- end;
- until (i > r);
- end else
- begin
- SetLength(Result, amount);
- {$IFNDEF Lape}
- m := IAbs(aFrom - aTo);
- {$ELSE}
- m := Abs(aFrom - aTo);
- {$ENDIF}
- if (m > 10) then
- b := (m > Trunc(amount * 1.1));
- if b then
- begin
- d := (Trunc(m / amount) + 3);
- e := aFrom;
- for i := 0 to (amount - 1) do
- begin
- r := Random(d);
- IncEx(e, r);
- IncEx(t, (r + 1));
- Result[i] := (e + i);
- Swap(Result[Random(i)], Result[Random(i)]);
- d := (Trunc(((m - t) / (amount - i))) * 2);
- end;
- for i := 0 to (amount div 2) do
- Swap(Result[Random(amount)], Result[Random(amount)]);
- end else
- begin
- SetLength(tmp, (aTo - aFrom));
- for i := aFrom to (aTo - 1) do
- tmp[(i - aFrom)] := i;
- for i := 0 to (amount - 1) do
- begin
- r := Random((aTo - aFrom) - i);
- Result[i] := Integer(tmp[r]);
- y := High(tmp);
- if ((r <= y) and (r > -1)) then
- begin
- for x := r to (y - 1) do
- tmp[x] := tmp[(x + 1)];
- SetLength(tmp, y);
- end;
- end;
- SetLength(tmp, 0);
- end;
- end;
- end else
- Result := [aFrom];
- end else
- begin
- SetLength(Result, amount);
- for i := 0 to (amount - 1) do
- Result[i] := RandomRange(aFrom, aTo);
- end;
- end;
- begin
- ClearDebug;
- WriteLn(ToStr(TIARandomRange(1, 10, 9, False)));
- WriteLn('');
- WriteLn(ToStr(TIARandomRange(1, 10, 9, True)));
- end.[/simba]
- [/spoiler]
- [spoiler="TIAToParts"]
- [simba]type
- TPartitionMethod = (pm_PartSize, pm_PartAmount);
- {==============================================================================]
- 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: TIntegerArray; method: TPartitionMethod; x: Integer): T2DIntegerArray;
- var
- a, e, h, h2, i, i2, p, z, l: Integer;
- f: Boolean;
- begin
- h := High(TIA);
- if ((h > -1) and (x > 0)) then
- begin
- case method of
- pm_PartSize:
- 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] := Integer(TIA[a]);
- Inc(a);
- end else
- begin
- SetLength(Result[i], i2);
- Exit;
- end;
- end;
- end else
- f := True;
- pm_PartAmount:
- if (h > -1) then
- begin
- 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] := Integer(TIA[a]);
- Inc(a);
- end;
- end;
- end else
- f := True;
- end;
- if f then
- begin
- SetLength(Result, 1);
- l := Length(TIA);
- SetLength(Result[0], l);
- for z := 0 to (l - 1) do
- Result[0][z] := Integer(TIA[z]);
- end;
- end else
- SetLength(Result, 0);
- end;
- var
- TIA: TIntegerArray;
- begin
- ClearDebug;
- TIA := [0, 1, 2, 3, 4, 5, 6, 7, 8, 9];
- WriteLn('TIAToParts(TIA, pm_PartAmount, 3): ' + ToStr(TIAToParts(TIA, pm_PartAmount, 3)));
- WriteLn('TIAToParts(TIA, pm_PartSize, 3): ' + ToStr(TIAToParts(TIA, pm_PartSize, 3)));
- end.[/simba]
- [/spoiler]
- [spoiler="ATIAMerge"]
- [simba]{==============================================================================]
- Explanation: Merges T2DIntegerArray (ATIA) to TIntegerArray.
- [==============================================================================}
- function ATIAMerge(ATIA: T2DIntegerArray): TIntegerArray;
- var
- i, i2, h, h2, r: Integer;
- begin
- h := High(ATIA);
- if (h > -1) then
- begin
- for i := 0 to h do
- IncEx(r, (High(ATIA[i]) + 1));
- SetLength(Result, r);
- r := 0;
- for i := 0 to h do
- begin
- h2 := High(ATIA[i]);
- for i2 := 0 to h2 do
- begin
- Result[r] := Integer(ATIA[i][i2]);
- Inc(r);
- end;
- end;
- end else
- SetLength(Result, 0);
- end;
- var
- ATIA: T2DIntegerArray;
- begin
- SetLength(ATIA, 3);
- ATIA[0] := [0, 1, 2, 3];
- ATIA[1] := [4, 5];
- ATIA[2] := [6, 7, 8, 9];
- WriteLn(ToStr(ATIAMerge(ATIA)));
- end.[/simba]
- [/spoiler]
- [spoiler="ATIAClone"]
- [simba]{==============================================================================]
- Explanation: Returns copy ("clone") of ATIA safely
- [==============================================================================}
- function ATIAClone(ATIA: T2DIntegerArray): T2DIntegerArray;
- var
- i, l, x, y: Integer;
- begin
- l := Length(ATIA);
- SetLength(Result, l);
- for i := 0 to (l - 1) do
- begin
- y := Length(ATIA[i]);
- SetLength(Result[i], y);
- for x := 0 to (y - 1) do
- Result[i][x] := Integer(ATIA[i][x]);
- end;
- end;
- var
- tmp: T2DIntegerArray;
- begin
- SetLength(tmp, 5);
- tmp[0] := [0, 1];
- tmp[1] := [2, 3];
- tmp[2] := [4, 5];
- tmp[3] := [6, 7];
- tmp[4] := [8, 9];
- WriteLn(ToStr(ATIAClone(tmp)));
- end.[/simba]
- [/spoiler]
- [spoiler="ATIASetLength"]
- [simba]{==============================================================================]
- Explanation: Sets T2DIntArray's (arr) 1D and 2D lengths by s1D and s2D.
- Example: (ATIA, 2, 4) => ATIA = [[0,0,0,0],[0,0,0,0]]
- [==============================================================================}
- procedure ATIASetLength(var arr: T2DIntegerArray; s1D, s2D: Integer);
- var
- i: Integer;
- begin
- if (s1D < 0) then
- s1D := 0;
- if (s2D < 0) then
- s2D := 0;
- SetLength(arr, s1D);
- for i := 0 to (s1D - 1) do
- SetLength(arr[i], s2D);
- end;
- var
- tmp: T2DIntegerArray;
- begin
- ATIASetLength(tmp, 3, 2);
- WriteLn(ToStr(tmp));
- end.[/simba]
- [/spoiler]
- Going to add more stuff soon probably. :stirthepot:
- ..oh and, as always:
- Feel free to use/take/improve/anything - it's all open-source!
- Giving credit is always appreciated, but not really necessary for me. :)
- Enjoy,
- -Jani
Advertisement
Add Comment
Please, Sign In to add comment