File: TArrayProjectSingle.lpr

package info (click to toggle)
lazarus 2.0.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 214,460 kB
  • sloc: pascal: 1,862,622; xml: 265,709; cpp: 56,595; sh: 3,008; java: 609; makefile: 535; perl: 297; sql: 222; ansic: 137
file content (111 lines) | stat: -rw-r--r-- 3,182 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
// Generic types for FreeSparta.com and FreePascal!
// Original version by keeper89.blogspot.com, 2011
// FPC version by Maciej Izak (hnb), 2014

program TArrayProjectSingle;

{$MODE DELPHI}
{$APPTYPE CONSOLE}

uses
  SysUtils, Math, Types, Generics.Collections, Generics.Defaults;

function CompareIntReverse(constref Left, Right: Integer): Integer;
begin
  Result := TCompare.Integer(Right, Left);
end;

type
  TForCompare = class
  public
    function CompareIntReverseMethod(constref Left, Right: Integer): Integer;
  end;

function TForCompare.CompareIntReverseMethod(constref Left, Right: Integer): Integer;
begin
  Result := TCompare.Integer(Right, Left);
end;

procedure PrintMatrix(A: TIntegerDynArray);
var
  item: Integer;
begin
  for item in A do
    Write(item, ' ');
  Writeln; Writeln;
end;

var
  A: TIntegerDynArray;
  FoundIndex: PtrInt;
  ForCompareObj: TForCompare;
begin
  WriteLn('Working with TArray - one-dimensional integer array');
  WriteLn;

  // Fill a one-dimensional array of integers by random numbers [1 .. 10]
  A := TIntegerDynArray.Create(1, 6, 3, 2, 9);

  // Print out what happened
  Writeln('The original array:');
  PrintMatrix(A);

  // Sort ascending without comparator
  TArrayHelper<Integer>.Sort(A);
  Writeln('Ascending Sort without parameters:');
  PrintMatrix(A);

  // ! FPC don't support anonymous methods yet
  // Sort descending, the comparator is constructed
  // using an anonymous method
  //TArray.Sort<Integer>(A, TComparer<Integer>.Construct(
  //  function (const Left, Right: Integer): Integer
  //  begin
  //    Result := Math.CompareValue(Right, Left)
  //  end));

  // Sort descending, the comparator is constructed
  // using an method
  TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Construct(
    ForCompareObj.CompareIntReverseMethod));
  Writeln('Descending by TComparer<Integer>.Construct(ForCompareObj.Method):');
  PrintMatrix(A);

  // Again sort ascending by using defaul
  TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Default);
  Writeln('Ascending by TComparer<Integer>.Default:');
  PrintMatrix(A);

  // Again descending using own comparator function
  TArrayHelper<Integer>.Sort(A, TComparer<Integer>.Construct(CompareIntReverse));
  Writeln('Descending by TComparer<Integer>.Construct(CompareIntReverse):');
  PrintMatrix(A);

  // Searches for a nonexistent element
  Writeln('BinarySearch nonexistent element');
  if TArrayHelper<Integer>.BinarySearch(A, 5, FoundIndex) then
    Writeln('5 is found, its index ', FoundIndex)
  else
    Writeln('5 not found!');
  Writeln;

  // Search for an existing item with default comparer
  Writeln('BinarySearch for an existing item ');
  if TArrayHelper<Integer>.BinarySearch(A, 6, FoundIndex) then
    Writeln('6 is found, its index ', FoundIndex)
  else
    Writeln('6 not found!');
  Writeln;

  // Search for an existing item with custom comparer
  Writeln('BinarySearch for an existing item with custom comparer');
  if TArrayHelper<Integer>.BinarySearch(A, 6, FoundIndex,
    TComparer<Integer>.Construct(CompareIntReverse)) then
    Writeln('6 is found, its index ', FoundIndex)
  else
    Writeln('6 not found!');
  Writeln;

  Readln;
end.