poolmm2.pp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. {
  2. $Id: header,v 1.1 2000/07/13 06:33:45 michael Exp $
  3. This file is part of the Free Component Library (FCL)
  4. Copyright (c) 1999-2000 by the Free Pascal development team
  5. See the file COPYING.FPC, included in this distribution,
  6. for details about the copyright.
  7. This program is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
  10. **********************************************************************}
  11. program poolmm2;
  12. {$mode objfpc}
  13. uses
  14. pooledmm;
  15. type
  16. // basic tree node
  17. PNode = ^TNode;
  18. TNode = record
  19. l, r: PNode;
  20. Value: Longint;
  21. end;
  22. { TTree }
  23. TTree = class
  24. private
  25. FMM: TNonFreePooledMemManager;
  26. FRoot: PNode;
  27. function CreateNode(l2, r2: PNode; const AValue: Integer): PNode;
  28. function Make(AValue, depth: Integer): PNode;
  29. public
  30. constructor Create(AValue, depth: Integer);
  31. destructor Destroy;override;
  32. function Check: Integer;
  33. end;
  34. function CheckNode(ANode: PNode): Integer;
  35. begin
  36. if ANode^.l = nil then
  37. Result:=ANode^.Value
  38. else
  39. Result:=CheckNode(ANode^.l) + ANode^.Value - CheckNode(ANode^.r);
  40. end;
  41. { TTree }
  42. constructor TTree.Create(AValue, depth: Integer);
  43. begin
  44. FMM := TNonFreePooledMemManager.Create(SizeOf(TNode));
  45. FRoot := Make(AValue, depth);
  46. end;
  47. destructor TTree.Destroy;
  48. begin
  49. FMM.Free; // frees all nodes, so no need to free the nodes recursively
  50. inherited Destroy;
  51. end;
  52. function TTree.Check: Integer;
  53. begin
  54. Result := CheckNode(FRoot);
  55. end;
  56. function TTree.CreateNode(l2, r2: PNode; const AValue: Integer): PNode;
  57. begin
  58. // Normally one would do something like this:
  59. // Result := GetMem(Sizeof(TNode));
  60. // But now we ask the a new item from the NonFree memory manager.
  61. Result := FMM.NewItem();
  62. Result^.l:=l2;
  63. Result^.r:=r2;
  64. Result^.Value:=AValue;
  65. end;
  66. function TTree.Make(AValue, depth: Integer): PNode;
  67. begin
  68. if depth = 0 then
  69. Result:=CreateNode(nil, nil, AValue)
  70. else
  71. Result:=CreateNode(Make(2 * AValue - 1, depth - 1), Make(2 * AValue, depth - 1), AValue);
  72. end;
  73. const
  74. MinDepth = 10;
  75. var
  76. MaxDepth : Integer;
  77. c, i : Integer;
  78. aa, bb, llt: TTree;
  79. begin
  80. MaxDepth := 12;
  81. if ParamCount = 1 then
  82. Val(ParamStr(1), MaxDepth);
  83. if (MinDepth + 2) > MaxDepth then
  84. MaxDepth := MinDepth + 2;
  85. // Create a tree of certain depth
  86. llt:=TTree.Create(0, MaxDepth);
  87. c := 0;
  88. for i:=1 to 100 do begin
  89. aa:=TTree.Create(i, MinDepth);
  90. bb:=TTree.Create(-i, MinDepth);
  91. c:=c + aa.Check + bb.Check;
  92. aa.Free;
  93. bb.Free;
  94. end;
  95. llt.Free;
  96. end.