ex27.pp 937 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  1. Program ex27;
  2. { Program to demonstrate the TCollection.Pack method }
  3. Uses Objects,MyObject; { For TMyObject definition and registration }
  4. Var C : PCollection;
  5. M : PMyObject;
  6. I,cnt : Longint;
  7. begin
  8. Randomize;
  9. C:=New(PCollection,Init(100,10));
  10. For I:=1 to 100 do
  11. begin
  12. M:=New(PMyObject,Init);
  13. M^.SetField(I-1);
  14. C^.Insert(M);
  15. end;
  16. cnt:=0;
  17. For I:=0 to 99 do
  18. begin
  19. If Random<0.1 then
  20. begin
  21. Inc(Cnt);
  22. C^.FreeItem(C^.At(I));
  23. C^.AtPut(I,Nil);
  24. end;
  25. end;
  26. Writeln ('Set ',cnt,' pointers to Nil. Count is ',C^.Count);
  27. Writeln ('Available memory : ',Memavail);
  28. C^.Pack;
  29. Writeln ('Packed collection. Count is ',C^.Count);
  30. cnt:=Memavail;
  31. Writeln ('Available memory : ',Cnt);
  32. C^.SetLimit(C^.Count);
  33. Writeln ('Set limit to ',C^.Count);
  34. Write ('Available memory : ',Memavail,'.');
  35. Writeln (' Gained ',Memavail-cnt,' bytes.');
  36. Dispose(C,Done);
  37. end.