Ignore:
Timestamp:
May 23, 2024, 10:14:11 PM (6 weeks ago)
Author:
chronos
Message:
  • Modified: Code cleanup.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/AI Template/Pile.pas

    r582 r583  
    88interface
    99
    10 procedure Create(Size: integer);
     10procedure Create(Size: Integer);
    1111procedure Free;
    1212procedure Empty;
    13 function Put(Item, Value: integer): boolean;
    14 function TestPut(Item, Value: integer): boolean;
    15 function Get(var Item, Value: integer): boolean;
     13function Put(Item, Value: Integer): Boolean;
     14function TestPut(Item, Value: Integer): Boolean;
     15function Get(var Item, Value: Integer): Boolean;
    1616
    1717
     
    2323type
    2424  TheapItem = record
    25     Item: integer;
    26     Value: integer;
     25    Item: Integer;
     26    Value: Integer;
    2727  end;
    2828
    2929var
    3030  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}
    3434
    3535
    36 procedure Create(Size: integer);
     36procedure Create(Size: Integer);
    3737begin
    3838  {$IFDEF DEBUG}
    39   assert(not InUse, 'Pile is a single instance class, ' +
     39  Assert(not InUse, 'Pile is a single instance class, ' +
    4040    'no multiple usage possible. Always call Pile.Free after use.');
    41   {$ENDIF}
    42   assert(Size <= MaxSize);
    43   if (n <> 0) or (Size > CurrentSize) then
     41{$ENDIF}
     42  Assert(Size <= MaxSize);
     43  if (N <> 0) or (Size > CurrentSize) then
    4444  begin
    45     FillChar(Ix, Size * sizeOf(integer), 255);
    46     n := 0;
     45    FillChar(Ix, Size * SizeOf(Integer), 255);
     46    N := 0;
    4747  end;
    4848  CurrentSize := Size;
    49   {$IFDEF DEBUG}
     49{$IFDEF DEBUG}
    5050  InUse := True;
    51   {$ENDIF}
     51{$ENDIF}
    5252end;
    5353
    5454procedure Free;
    5555begin
    56   {$IFDEF DEBUG}
    57   assert(InUse);
     56{$IFDEF DEBUG}
     57  Assert(InUse);
    5858  InUse := False;
    59   {$ENDIF}
     59{$ENDIF}
    6060end;
    6161
    6262procedure Empty;
    6363begin
    64   if n <> 0 then
     64  if N <> 0 then
    6565  begin
    66     FillChar(Ix, CurrentSize * sizeOf(integer), 255);
    67     n := 0;
     66    FillChar(Ix, CurrentSize * SizeOf(Integer), 255);
     67    N := 0;
    6868  end;
    6969end;
    7070
    71 //Parent(i) = (i-1)/2.
    72 function Put(Item, Value: integer): boolean; //O(lg(n))
     71// Parent(i) = (i-1)/2.
     72function Put(Item, Value: Integer): Boolean; // O(lg(n))
    7373var
    74   i, j: integer;
     74  I, J: Integer;
    7575begin
    76   assert(Item < CurrentSize);
    77   i := Ix[Item];
    78   if i >= 0 then
     76  Assert(Item < CurrentSize);
     77  I := Ix[Item];
     78  if I >= 0 then
    7979  begin
    80     if bh[i].Value <= Value then
     80    if bh[I].Value <= Value then
    8181    begin
    8282      Result := False;
    83       exit;
     83      Exit;
    8484    end;
    8585  end
    8686  else
    8787  begin
    88     i := n;
    89     Inc(n);
     88    I := N;
     89    Inc(N);
    9090  end;
    9191
    92   while i > 0 do
     92  while I > 0 do
    9393  begin
    94     j := (i - 1) shr 1;  //Parent(i) = (i-1)/2
    95     if Value >= bh[j].Value then  break;
    96     bh[i] := bh[j];
    97     Ix[bh[i].Item] := i;
    98     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;
    99100  end;
    100   //  Insert the new Item at the insertion point found.
    101   bh[i].Value := Value;
    102   bh[i].Item := Item;
    103   Ix[bh[i].Item] := i;
     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;
    104105  Result := True;
    105106end;
    106107
    107 function TestPut(Item, Value: integer): boolean;
     108function TestPut(Item, Value: Integer): Boolean;
    108109var
    109   i: integer;
     110  I: Integer;
    110111begin
    111   assert(Item < CurrentSize);
    112   i := Ix[Item];
    113   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);
    114115end;
    115116
    116117//Left(i) = 2*i+1.
    117118//Right(i) = 2*i+2 => Left(i)+1
    118 function Get(var Item, Value: integer): boolean; //O(lg(n))
     119function Get(var Item, Value: Integer): Boolean; //O(lg(n))
    119120var
    120   i, j: integer;
    121   last: TheapItem;
     121  I, J: Integer;
     122  Last: TheapItem;
    122123begin
    123   if n = 0 then
     124  if N = 0 then
    124125  begin
    125126    Result := False;
    126     exit;
     127    Exit;
    127128  end;
    128129
     
    132133  Ix[Item] := -1;
    133134
    134   Dec(n);
    135   if n > 0 then
     135  Dec(N);
     136  if N > 0 then
    136137  begin
    137     last := bh[n];
    138     i := 0;
    139     j := 1;
    140     while j < n do
     138    Last := bh[N];
     139    I := 0;
     140    J := 1;
     141    while J < N do
    141142    begin
    142143      //  Right(i) = Left(i)+1
    143       if (j < n - 1) and (bh[j].Value > bh[j + 1].Value) then
    144         Inc(j);
    145       if last.Value <= bh[j].Value then  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;
    146148
    147       bh[i] := bh[j];
    148       Ix[bh[i].Item] := i;
    149       i := j;
    150       j := j shl 1 + 1;  //Left(j) = 2*j+1
     149      bh[I] := bh[J];
     150      Ix[bh[I].Item] := I;
     151      I := J;
     152      J := J shl 1 + 1;  //Left(j) = 2*j+1
    151153    end;
    152154
    153155    // Insert the root in the correct place in the heap.
    154     bh[i] := last;
    155     Ix[last.Item] := i;
     156    bh[I] := Last;
     157    Ix[Last.Item] := I;
    156158  end;
    157159  Result := True;
     
    159161
    160162initialization
    161   n := 0;
     163  N := 0;
    162164  CurrentSize := 0;
    163   {$IFDEF DEBUG}
     165{$IFDEF DEBUG}
    164166  InUse := False;
    165   {$ENDIF}
     167{$ENDIF}
    166168end.
Note: See TracChangeset for help on using the changeset viewer.