Janilabo

Janilabo | TSAMove() [SCAR Divi]

Apr 27th, 2013
76
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Pascal 1.00 KB | None | 0 0
  1. {==============================================================================]  
  2.   Explanation: Moves oldIndex to newIndex in TSA. Returns true, if movement was succesfully done!              
  3. [==============================================================================}
  4. function TSAMove(var TSA: TStrArray; oldIndex, newIndex: Integer): Boolean;
  5. var
  6.   h, i: Integer;
  7. begin
  8.   h := High(TSA);
  9.   Result := ((h > 0) and (oldIndex <> newIndex) and InRange(oldIndex, 0, h) and InRange(newIndex, 0, h));
  10.   if Result then
  11.   case (oldIndex > newIndex) of
  12.     True:
  13.     for i := oldIndex downto (newIndex + 1) do
  14.       Swap(TSA[i], TSA[(i - 1)]);
  15.     False:
  16.     for i := oldIndex to (newIndex - 1) do
  17.       Swap(TSA[i], TSA[(i + 1)]);
  18.   end;
  19. end;
  20.  
  21. var
  22.   s: string;
  23.   h, i: Integer;
  24.   a: TStrArray;
  25.  
  26. begin
  27.   ClearDebug;
  28.   a := ['Test1', 'Test2', 'Test3', 'Test4', 'Test0'];
  29.   TSAMove(a, 4, 0);
  30.   h := High(a);
  31.   for i := 0 to h do
  32.     s := (s + '"' +  a[i] + '" ');
  33.   WriteLn('a: ' + s);
  34. end.
Advertisement
Add Comment
Please, Sign In to add comment