Changeset 447 for trunk/AI/StdAI/Pile.pas
- Timestamp:
- May 19, 2022, 10:39:34 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/AI/StdAI/Pile.pas
r289 r447 8 8 interface 9 9 10 procedure Create(Size: integer);10 procedure Create(Size: Integer); 11 11 procedure Free; 12 12 procedure Empty; 13 function Put(Item, Value: integer): boolean;14 function TestPut(Item, Value: integer): boolean;15 function Get(var Item, Value: integer): boolean;13 function Put(Item, Value: Integer): Boolean; 14 function TestPut(Item, Value: Integer): Boolean; 15 function Get(var Item, Value: Integer): Boolean; 16 16 17 17 … … 23 23 type 24 24 TheapItem = record 25 Item: integer;26 Value: integer;25 Item: Integer; 26 Value: Integer; 27 27 end; 28 28 29 29 var 30 30 bh: array[0..MaxSize - 1] of TheapItem; 31 Ix: array[0..MaxSize - 1] of integer;32 n, CurrentSize: integer;33 {$IFDEF DEBUG}InUse: boolean;{$ENDIF}31 Ix: array[0..MaxSize - 1] of Integer; 32 N, CurrentSize: Integer; 33 {$IFDEF DEBUG}InUse: Boolean;{$ENDIF} 34 34 35 35 36 procedure Create(Size: integer);36 procedure Create(Size: Integer); 37 37 begin 38 38 {$IFDEF DEBUG} 39 assert(not InUse, 'Pile is a single instance class, ' +39 Assert(not InUse, 'Pile is a single instance class, ' + 40 40 'no multiple usage possible. Always call Pile.Free after use.'); 41 41 {$ENDIF} 42 assert(Size <= MaxSize);43 if ( n<> 0) or (Size > CurrentSize) then42 Assert(Size <= MaxSize); 43 if (N <> 0) or (Size > CurrentSize) then 44 44 begin 45 FillChar(Ix, Size * sizeOf( integer), 255);46 n:= 0;45 FillChar(Ix, Size * sizeOf(Integer), 255); 46 N := 0; 47 47 end; 48 48 CurrentSize := Size; … … 55 55 begin 56 56 {$IFDEF DEBUG} 57 assert(InUse);57 Assert(InUse); 58 58 InUse := False; 59 59 {$ENDIF} … … 62 62 procedure Empty; 63 63 begin 64 if n<> 0 then64 if N <> 0 then 65 65 begin 66 FillChar(Ix, CurrentSize * sizeOf( integer), 255);67 n:= 0;66 FillChar(Ix, CurrentSize * sizeOf(Integer), 255); 67 N := 0; 68 68 end; 69 69 end; 70 70 71 71 //Parent(i) = (i-1)/2. 72 function Put(Item, Value: integer): boolean; //O(lg(n))72 function Put(Item, Value: Integer): Boolean; //O(lg(n)) 73 73 var 74 i, j: integer;74 I, J: Integer; 75 75 begin 76 assert(Item < CurrentSize);77 i:= Ix[Item];78 if i>= 0 then76 Assert(Item < CurrentSize); 77 I := Ix[Item]; 78 if I >= 0 then 79 79 begin 80 if bh[ i].Value <= Value then80 if bh[I].Value <= Value then 81 81 begin 82 82 Result := False; 83 exit;83 Exit; 84 84 end; 85 85 end 86 86 else 87 87 begin 88 i := n;89 Inc( n);88 I := N; 89 Inc(N); 90 90 end; 91 91 92 while i> 0 do92 while I > 0 do 93 93 begin 94 j := (i- 1) shr 1; //Parent(i) = (i-1)/295 if Value >= bh[ j].Value then96 break;97 bh[ i] := bh[j];98 Ix[bh[ i].Item] := i;99 i := j;94 J := (I - 1) shr 1; //Parent(i) = (i-1)/2 95 if Value >= bh[J].Value then 96 Break; 97 bh[I] := bh[J]; 98 Ix[bh[I].Item] := I; 99 I := J; 100 100 end; 101 101 // Insert the new Item at the insertion point found. 102 bh[ i].Value := Value;103 bh[ i].Item := Item;104 Ix[bh[ i].Item] := i;102 bh[I].Value := Value; 103 bh[I].Item := Item; 104 Ix[bh[I].Item] := I; 105 105 Result := True; 106 106 end; 107 107 108 function TestPut(Item, Value: integer): boolean;108 function TestPut(Item, Value: Integer): Boolean; 109 109 var 110 i: integer;110 I: Integer; 111 111 begin 112 assert(Item < CurrentSize);113 i:= Ix[Item];114 Result := ( i < 0) or (bh[i].Value > Value);112 Assert(Item < CurrentSize); 113 I := Ix[Item]; 114 Result := (I < 0) or (bh[I].Value > Value); 115 115 end; 116 116 117 117 //Left(i) = 2*i+1. 118 118 //Right(i) = 2*i+2 => Left(i)+1 119 function Get(var Item, Value: integer): boolean; //O(lg(n))119 function Get(var Item, Value: Integer): Boolean; //O(lg(n)) 120 120 var 121 i, j: integer;122 last: TheapItem;121 I, J: Integer; 122 Last: TheapItem; 123 123 begin 124 if n= 0 then124 if N = 0 then 125 125 begin 126 126 Result := False; 127 exit;127 Exit; 128 128 end; 129 129 … … 133 133 Ix[Item] := -1; 134 134 135 Dec( n);136 if n> 0 then135 Dec(N); 136 if N > 0 then 137 137 begin 138 last := bh[n];139 i:= 0;140 j:= 1;141 while j < ndo138 Last := bh[N]; 139 I := 0; 140 J := 1; 141 while J < N do 142 142 begin 143 143 // Right(i) = Left(i)+1 144 if ( j < n - 1) and (bh[j].Value > bh[j+ 1].Value) then145 Inc( j);146 if last.Value <= bh[j].Value then147 break;144 if (J < N - 1) and (bh[J].Value > bh[J + 1].Value) then 145 Inc(J); 146 if Last.Value <= bh[J].Value then 147 Break; 148 148 149 bh[ i] := bh[j];150 Ix[bh[ i].Item] := i;151 i := j;152 j := jshl 1 + 1; //Left(j) = 2*j+1149 bh[I] := bh[J]; 150 Ix[bh[I].Item] := I; 151 I := J; 152 J := J shl 1 + 1; //Left(j) = 2*j+1 153 153 end; 154 154 155 155 // Insert the root in the correct place in the heap. 156 bh[ i] := last;157 Ix[ last.Item] := i;156 bh[I] := Last; 157 Ix[Last.Item] := I; 158 158 end; 159 159 Result := True; … … 161 161 162 162 initialization 163 n:= 0;163 N := 0; 164 164 CurrentSize := 0; 165 165 {$IFDEF DEBUG}
Note:
See TracChangeset
for help on using the changeset viewer.