list.pp 2.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. Program TestList;
  2. {$mode objfpc}{$h+}
  3. Uses classes;
  4. const a1 : pchar = '0';
  5. a2 : pchar = '1';
  6. a3 : pchar = '2';
  7. a4 : pchar = '3';
  8. a5 : pchar = '4';
  9. a6 : pchar = '5';
  10. a7 : pchar = '6';
  11. a8 : pchar = '7';
  12. a9 : pchar = '8';
  13. a10 : pchar = '9';
  14. Var List : TList;
  15. StartMem,Runner : longint;
  16. Function ACompare (P1,P2 : Pointer) : Integer;
  17. Type PByte = ^Byte;
  18. begin
  19. Result:=PByte(p1)^-PByte(P2)^;
  20. end;
  21. Procedure DumpMem;
  22. begin
  23. Writeln (' usedbytes : ',getfpcheapstatus.currheapused,' (=',getfpcheapstatus.currheapused-StartMem,' Bytes lost).')
  24. end;
  25. Procedure DumpList;
  26. Var I : longint;
  27. begin
  28. Write ('Count/Capacity : ',List.Count,'/',List.Capacity);dumpmem;
  29. If List.Count>0 then
  30. begin
  31. For i:=0 to List.Count-1 do
  32. if assigned(List.items[I]) then write (Pchar(List.items[i])) else write ('*');
  33. Writeln;
  34. end;
  35. end;
  36. begin
  37. StartMem:=getfpcheapstatus.currheapused;
  38. Writeln ('Creating List');
  39. List:=TList.Create;
  40. DumpList;
  41. Writeln ('Increasing capacity to 10');
  42. List.Capacity:=10;
  43. DumpList;
  44. Writeln ('Setting capacity to zero');
  45. List.capacity:=0;
  46. DumpList;
  47. Writeln ('Adding 10 elements in random sequence.');
  48. List.add (a2);
  49. List.add (a1);
  50. List.add (a3);
  51. List.add (a8);
  52. List.add (a5);
  53. List.add (a9);
  54. List.add (a4);
  55. List.Add (a8);
  56. List.Add (a7);
  57. List.Add (a6);
  58. Dumplist;
  59. Writeln ('Removing Third element.');
  60. List.Delete(2);
  61. DumpList;
  62. Writeln ('Inserting "0" at third place');
  63. List.Insert (2,a1);
  64. DumpList;
  65. Writeln ('Setting elmts 3 to 6 to Nil.');
  66. For Runner:=2 to 5 do List.Items[Runner]:=Nil;
  67. Dumplist;
  68. Writeln ('Packing list');
  69. List.Pack;
  70. DumpList;
  71. Writeln ('Setting capacity to count');
  72. List.Capacity:=List.Count;
  73. DumpList;
  74. Writeln ('Expanding list');
  75. List.Expand;
  76. DumpList;
  77. Writeln ('Index of ',a1,' : ',List.IndexOf(a1));
  78. Writeln ('Removing "',A1,'" from list.');
  79. List.Remove (a1);
  80. DumpList;
  81. Writeln ('Sorting List.');
  82. List.Sort (@ACompare);
  83. DumpList;
  84. Writeln ('Freeing list.');
  85. List.Free;
  86. DumpMem;
  87. end.