tarrayprojectsingle.lpr 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. // Generic types for NewPascal.org and FPC!
  2. // Original version by keeper89.blogspot.com, 2011
  3. // FPC version by Maciej Izak (hnb), 2014
  4. program TArrayProjectSingle;
  5. {$MODE DELPHI}
  6. {$APPTYPE CONSOLE}
  7. uses
  8. SysUtils, Math, Types, Generics.Collections, Generics.Defaults;
  9. function CompareIntReverse(constref Left, Right: Integer): Integer;
  10. begin
  11. Result := TCompare.Integer(Right, Left);
  12. end;
  13. type
  14. TForCompare = class
  15. public
  16. function CompareIntReverseMethod(constref Left, Right: Integer): Integer;
  17. end;
  18. function TForCompare.CompareIntReverseMethod(constref Left, Right: Integer): Integer;
  19. begin
  20. Result := TCompare.Integer(Right, Left);
  21. end;
  22. procedure PrintMatrix(A: TIntegerDynArray);
  23. var
  24. item: Integer;
  25. begin
  26. for item in A do
  27. Write(item, ' ');
  28. Writeln; Writeln;
  29. end;
  30. var
  31. A: TIntegerDynArray;
  32. FoundIndex: PtrInt;
  33. ForCompareObj: TForCompare;
  34. begin
  35. WriteLn('Working with TArray - one-dimensional integer array');
  36. WriteLn;
  37. // Fill a one-dimensional array of integers by random numbers [1 .. 10]
  38. A := TIntegerDynArray.Create(1, 6, 3, 2, 9);
  39. // Print out what happened
  40. Writeln('The original array:');
  41. PrintMatrix(A);
  42. // Sort ascending without comparator
  43. TArrayHelper<Integer>.Sort(A);
  44. Writeln('Ascending Sort without parameters:');
  45. PrintMatrix(A);
  46. // ! FPC don't support anonymous methods yet
  47. // Sort descending, the comparator is constructed
  48. // using an anonymous method
  49. //TArray.Sort<Integer>(A, TComparer<Integer>.Construct(
  50. // function (const Left, Right: Integer): Integer
  51. // begin
  52. // Result := Math.CompareValue(Right, Left)
  53. // end));
  54. // Sort descending, the comparator is constructed
  55. // using an method
  56. TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Construct(
  57. ForCompareObj.CompareIntReverseMethod));
  58. Writeln('Descending by TComparer<Integer>.Construct(ForCompareObj.Method):');
  59. PrintMatrix(A);
  60. // Again sort ascending by using defaul
  61. TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Default);
  62. Writeln('Ascending by TComparer<Integer>.Default:');
  63. PrintMatrix(A);
  64. // Again descending using own comparator function
  65. TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Construct(CompareIntReverse));
  66. Writeln('Descending by TComparer<Integer>.Construct(CompareIntReverse):');
  67. PrintMatrix(A);
  68. // Searches for a nonexistent element
  69. Writeln('BinarySearch nonexistent element');
  70. if TArrayHelper<Integer>.BinarySearch(A, 5, FoundIndex) then
  71. Writeln('5 is found, its index ', FoundIndex)
  72. else
  73. Writeln('5 not found!');
  74. Writeln;
  75. // Search for an existing item with default comparer
  76. Writeln('BinarySearch for an existing item ');
  77. if TArrayHelper<Integer>.BinarySearch(A, 6, FoundIndex) then
  78. Writeln('6 is found, its index ', FoundIndex)
  79. else
  80. Writeln('6 not found!');
  81. Writeln;
  82. // Search for an existing item with custom comparer
  83. Writeln('BinarySearch for an existing item with custom comparer');
  84. if TArrayHelper<Integer>.BinarySearch(A, 6, FoundIndex,
  85. TComparer<Integer>.Construct(CompareIntReverse)) then
  86. Writeln('6 is found, its index ', FoundIndex)
  87. else
  88. Writeln('6 not found!');
  89. Writeln;
  90. Readln;
  91. end.