summaryrefslogtreecommitdiff
path: root/fpcsrc/packages/fcl-base/examples/list.pp
blob: 3c3372ff9306df6078b744e19efe9b460ad96912 (plain)
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
Program TestList;

{$mode objfpc}{$h+}

Uses classes;

const a1 : pchar = '0';
      a2 : pchar = '1';
      a3 : pchar = '2';
      a4 : pchar = '3';
      a5 : pchar = '4';
      a6 : pchar = '5';
      a7 : pchar = '6';
      a8 : pchar = '7';
      a9 : pchar = '8';
      a10 : pchar = '9';

Var List : TList;
    StartMem,Runner : longint;

Function ACompare (P1,P2 : Pointer) : Integer;

Type PByte = ^Byte;

begin
  Result:=PByte(p1)^-PByte(P2)^;
end;

Procedure DumpMem;

begin
  Writeln ('    usedbytes : ',getfpcheapstatus.currheapused,' (=',getfpcheapstatus.currheapused-StartMem,' Bytes lost).')
end;

Procedure DumpList;

Var I : longint;

begin
  Write ('Count/Capacity : ',List.Count,'/',List.Capacity);dumpmem;
  If List.Count>0 then
    begin
    For i:=0 to List.Count-1 do
      if assigned(List.items[I]) then write (Pchar(List.items[i])) else write ('*');
    Writeln;
    end;
end;


begin
  StartMem:=getfpcheapstatus.currheapused;
  Writeln ('Creating List');
  List:=TList.Create;
  DumpList;
  Writeln ('Increasing capacity to 10');
  List.Capacity:=10;
  DumpList;
  Writeln ('Setting capacity to zero');
  List.capacity:=0;
  DumpList;
  Writeln ('Adding 10 elements in random sequence.');
  List.add (a2);
  List.add (a1);
  List.add (a3);
  List.add (a8);
  List.add (a5);
  List.add (a9);
  List.add (a4);
  List.Add (a8);
  List.Add (a7);
  List.Add (a6);
  Dumplist;
  Writeln ('Removing Third element.');
  List.Delete(2);
  DumpList;
  Writeln ('Inserting "0" at third place');
  List.Insert (2,a1);
  DumpList;
  Writeln ('Setting elmts 3 to 6 to Nil.');
  For Runner:=2 to 5 do List.Items[Runner]:=Nil;
  Dumplist;
  Writeln ('Packing list');
  List.Pack;
  DumpList;
  Writeln ('Setting capacity to count');
  List.Capacity:=List.Count;
  DumpList;
  Writeln ('Expanding list');
  List.Expand;
  DumpList;
  Writeln ('Index of ',a1,' : ',List.IndexOf(a1));
  Writeln ('Removing "',A1,'" from list.');
  List.Remove (a1);
  DumpList;
  Writeln ('Sorting List.');
  List.Sort (@ACompare);
  DumpList;
  Writeln ('Freeing list.');
  List.Free;
  DumpMem;
end.