list.pp 2.0 KB

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