Advertisement
Guest User

Janilabo | TIAByRange(2bit)

a guest
Sep 29th, 2012
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.59 KB | None | 0 0
  1. const
  2. RANGE1_START = -999999;
  3. RANGE1_FINISH = 999999;
  4.  
  5. function TIAByRange(aStart, aFinish: Integer): TIntArray;
  6. var
  7. i: Integer;
  8. begin
  9. if (aStart <> aFinish) then
  10. begin
  11. SetLength(Result, (IAbs(aStart - aFinish) + 1));
  12. if (aStart > aFinish) then
  13. for i := aStart downto aFinish do
  14. Result[(aStart - i)] := i
  15. else
  16. for i := aStart to aFinish do
  17. Result[(i - aStart)] := i;
  18. end else
  19. Result := [aStart];
  20. end;
  21.  
  22. function TIAByRange2bit(aStart, aFinish: Integer): TIntArray;
  23. var
  24. g, l, i: Integer;
  25. r: Boolean;
  26. begin
  27. if (aStart <> aFinish) then
  28. begin
  29. l := (IAbs(aStart - aFinish) + 1);
  30. SetLength(Result, l);
  31. g := ((l - 1) div 2);
  32. r := (aStart < aFinish);
  33. case r of
  34. True:
  35. begin
  36. for i := 0 to g do
  37. begin
  38. Result[i] := (aStart + i);
  39. Result[((l - 1) - i)] := (aFinish - i);
  40. end;
  41. if ((l mod 2) <> 0) then
  42. Result[i] := (aStart + i);
  43. end;
  44. False:
  45. begin
  46. for i := 0 to g do
  47. begin
  48. Result[i] := (aStart - i);
  49. Result[((l - 1) - i)] := (aFinish + i);
  50. end;
  51. if ((l mod 2) <> 0) then
  52. Result[i] := (aStart - i);
  53. end;
  54. end;
  55. end else
  56. Result := [aStart];
  57. end;
  58.  
  59. const
  60. RANGE2_START = RANGE1_FINISH;
  61. RANGE2_FINISH = RANGE1_START;
  62.  
  63. var
  64. t: Integer;
  65. TIA: TIntArray;
  66.  
  67. begin
  68. ClearDebug;
  69. t := GetSystemTime;
  70. TIA := TIAByRange(RANGE1_START, RANGE1_FINISH);
  71. WriteLn('TIAByRange(' + IntToStr(RANGE1_START) + ', ' + IntToStr(RANGE1_FINISH) + '): ' + IntToStr(Length(TIA)) + ' items [' + IntToStr(GetSystemTime - t) + ' ms.]' );
  72. SetLength(TIA, 0);
  73. t := GetSystemTime;
  74. TIA := TIAByRange2Bit(RANGE1_START, RANGE1_FINISH);
  75. WriteLn('TIAByRange2bit(' + IntToStr(RANGE1_START) + ', ' + IntToStr(RANGE1_FINISH) + '): ' + IntToStr(Length(TIA)) + ' items [' + IntToStr(GetSystemTime - t) + ' ms.]' );
  76. SetLength(TIA, 0);
  77. t := GetSystemTime;
  78. TIA := TIAByRange(RANGE2_START, RANGE2_FINISH);
  79. WriteLn('TIAByRange(' + IntToStr(RANGE2_START) + ', ' + IntToStr(RANGE2_FINISH) + '): ' + IntToStr(Length(TIA)) + ' items [' + IntToStr(GetSystemTime - t) + ' ms.]' );
  80. SetLength(TIA, 0);
  81. t := GetSystemTime;
  82. TIA := TIAByRange2Bit(RANGE2_START, RANGE2_FINISH);
  83. WriteLn('TIAByRange2bit(' + IntToStr(RANGE2_START) + ', ' + IntToStr(RANGE2_FINISH) + '): ' + IntToStr(Length(TIA)) + ' items [' + IntToStr(GetSystemTime - t) + ' ms.]' );
  84. SetLength(TIA, 0);
  85. end.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement