Ignore:
Timestamp:
May 19, 2022, 10:39:34 PM (2 years ago)
Author:
chronos
Message:
  • Modified: Use first capital letter in identifiers.
File:
1 edited

Legend:

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

    r289 r447  
    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.');
    4141{$ENDIF}
    42   assert(Size <= MaxSize);
    43   if (n <> 0) or (Size > CurrentSize) then
     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;
     
    5555begin
    5656        {$IFDEF DEBUG}
    57   assert(InUse);
     57  Assert(InUse);
    5858  InUse := False;
    5959{$ENDIF}
     
    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
    7171//Parent(i) = (i-1)/2.
    72 function Put(Item, Value: integer): boolean; //O(lg(n))
     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
    96       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;
    100100  end;
    101101  //  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;
    105105  Result := True;
    106106end;
    107107
    108 function TestPut(Item, Value: integer): boolean;
     108function TestPut(Item, Value: Integer): Boolean;
    109109var
    110   i: integer;
     110  I: Integer;
    111111begin
    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);
    115115end;
    116116
    117117//Left(i) = 2*i+1.
    118118//Right(i) = 2*i+2 => Left(i)+1
    119 function Get(var Item, Value: integer): boolean; //O(lg(n))
     119function Get(var Item, Value: Integer): Boolean; //O(lg(n))
    120120var
    121   i, j: integer;
    122   last: TheapItem;
     121  I, J: Integer;
     122  Last: TheapItem;
    123123begin
    124   if n = 0 then
     124  if N = 0 then
    125125  begin
    126126    Result := False;
    127     exit;
     127    Exit;
    128128  end;
    129129
     
    133133  Ix[Item] := -1;
    134134
    135   Dec(n);
    136   if n > 0 then
     135  Dec(N);
     136  if N > 0 then
    137137  begin
    138     last := bh[n];
    139     i := 0;
    140     j := 1;
    141     while j < n do
     138    Last := bh[N];
     139    I := 0;
     140    J := 1;
     141    while J < N do
    142142    begin
    143143      //  Right(i) = Left(i)+1
    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;
     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;
    148148
    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
     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
    153153    end;
    154154
    155155    // 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;
    158158  end;
    159159  Result := True;
     
    161161
    162162initialization
    163   n := 0;
     163  N := 0;
    164164  CurrentSize := 0;
    165165        {$IFDEF DEBUG}
Note: See TracChangeset for help on using the changeset viewer.