Jump to content
Janilabo

TIAByRange

Recommended Posts

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;

 

TIAByRange2bit:

 

function TIAByRange2bit(aStart, aFinish: Integer): TIntArray;
var
 g, l, i: Integer;
begin
 if (aStart <> aFinish) then
 begin 
   l := (IAbs(aStart - aFinish) + 1);             
   SetLength(Result, l);     
   g := ((l - 1) div 2);  
   case (aStart < aFinish) of
     True:
     begin
       for i := 0 to g do
       begin                       
         Result[i] := (aStart + i);
         Result[((l - 1) - i)] := (aFinish - i);
       end;
       if ((l mod 2) <> 0) then
         Result[i] := (aStart + i);   
     end;
     False:
     begin
       for i := 0 to g do
       begin                       
         Result[i] := (aStart - i);
         Result[((l - 1) - i)] := (aFinish + i);
       end;
       if ((l mod 2) <> 0) then
         Result[i] := (aStart - i);  
     end;  
   end;     
 end else
   Result := [aStart];
end;

 

Timing the methods:

TIAByRange(-999999, 999999): 1999999 items [7441 ms.]
TIAByRange2bit(-999999, 999999): 1999999 items [6942 ms.]
TIAByRange(999999, -999999): 1999999 items [7395 ms.]
TIAByRange2bit(999999, -999999): 1999999 items [7004 ms.]
Successfully executed (28784.3489 ms)

Edited by Janilabo
Functions updated.
Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.



×
  • Create New...