Ignore:
Timestamp:
Mar 6, 2019, 8:10:23 AM (5 years ago)
Author:
chronos
Message:
  • Added: StdAI from original game. Previously used only AI dev kit.
File:
1 edited

Legend:

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

    r124 r160  
    1 { single instance priority queue
    2   main parts contributed by Rassim Eminli }
     1{single instance priority queue
     2main parts contributed by Rassim Eminli}
    33
    44{$INCLUDE Switches.inc}
     5
    56unit Pile;
    67
     
    1415function Get(var Item, Value: integer): boolean;
    1516
     17
    1618implementation
    1719
    1820const
    19   MaxSize = 9600;
     21MaxSize=9600;
    2022
    2123type
    22   TheapItem = record
    23     Item: integer;
    24     Value: integer;
    25   end;
     24TheapItem = record
     25        Item:   integer;
     26        Value:  integer;
     27end;
    2628
    2729var
    28   bh: array [0 .. MaxSize - 1] of TheapItem;
    29   Ix: array [0 .. MaxSize - 1] of integer;
    30   n, CurrentSize: integer;
    31 {$IFDEF DEBUG}InUse: boolean; {$ENDIF}
     30bh: array[0..MaxSize-1] of TheapItem;
     31Ix: array[0..MaxSize-1] of integer;
     32n, CurrentSize: integer;
     33{$IFDEF DEBUG}InUse: boolean;{$ENDIF}
     34
    3235
    3336procedure Create(Size: integer);
    3437begin
    35 {$IFDEF DEBUG}assert(not InUse, 'Pile is a single instance class, ' + 'no multiple usage possible. Always call Pile.Free after use.'); {$ENDIF}
    36   assert(Size <= MaxSize);
    37   if (n <> 0) or (Size > CurrentSize) then
    38   begin
    39     FillChar(Ix, Size * sizeOf(integer), 255);
    40     n := 0;
    41   end;
    42   CurrentSize := Size;
    43 {$IFDEF DEBUG}InUse := true; {$ENDIF}
     38        {$IFDEF DEBUG}assert(not InUse, 'Pile is a single instance class, '
     39          +'no multiple usage possible. Always call Pile.Free after use.');{$ENDIF}
     40        assert(Size<=MaxSize);
     41        if (n <> 0) or (Size > CurrentSize) then
     42        begin
     43                FillChar(Ix, Size*sizeOf(integer), 255);
     44                n := 0;
     45        end;
     46        CurrentSize := Size;
     47        {$IFDEF DEBUG}InUse:=true;{$ENDIF}
    4448end;
    4549
    4650procedure Free;
    4751begin
    48 {$IFDEF DEBUG}assert(InUse);
    49   InUse := false; {$ENDIF}
     52        {$IFDEF DEBUG}assert(InUse);InUse:=false;{$ENDIF}
    5053end;
    5154
    5255procedure Empty;
    5356begin
    54   if n <> 0 then
    55   begin
    56     FillChar(Ix, CurrentSize * sizeOf(integer), 255);
    57     n := 0;
    58   end;
     57        if n <> 0 then
     58        begin
     59                FillChar(Ix, CurrentSize*sizeOf(integer), 255);
     60                n := 0;
     61        end;
    5962end;
    6063
    61 // Parent(i) = (i-1)/2.
    62 function Put(Item, Value: integer): boolean; // O(lg(n))
     64//Parent(i) = (i-1)/2.
     65function Put(Item, Value: integer): boolean; //O(lg(n))
    6366var
    64   i, j: integer;
     67        i, j:   integer;
    6568begin
    66   assert(Item < CurrentSize);
    67   i := Ix[Item];
    68   if i >= 0 then
    69   begin
    70     if bh[i].Value <= Value then
    71     begin
    72       result := false;
    73       exit;
    74     end;
    75   end
    76   else
    77   begin
    78     i := n;
    79     Inc(n);
    80   end;
     69        assert(Item<CurrentSize);
     70        i := Ix[Item];
     71        if i >= 0 then
     72        begin
     73                if bh[i].Value <= Value then
     74                begin
     75                        result := False;
     76                        exit;
     77                end;
     78        end
     79        else
     80        begin
     81                i := n;
     82                Inc(n);
     83        end;
    8184
    82   while i > 0 do
    83   begin
    84     j := (i - 1) shr 1; // Parent(i) = (i-1)/2
    85     if Value >= bh[j].Value then
    86       break;
    87     bh[i] := bh[j];
    88     Ix[bh[i].Item] := i;
    89     i := j;
    90   end;
    91   // Insert the new Item at the insertion point found.
    92   bh[i].Value := Value;
    93   bh[i].Item := Item;
    94   Ix[bh[i].Item] := i;
    95   result := true;
     85        while i > 0 do
     86        begin
     87                j := (i-1) shr 1;       //Parent(i) = (i-1)/2
     88                if Value >= bh[j].Value then    break;
     89                bh[i] := bh[j];
     90                Ix[bh[i].Item] := i;
     91                i := j;
     92        end;
     93        //      Insert the new Item at the insertion point found.
     94        bh[i].Value := Value;
     95        bh[i].Item := Item;
     96        Ix[bh[i].Item] := i;
     97        result := True;
    9698end;
    9799
    98100function TestPut(Item, Value: integer): boolean;
    99101var
    100   i: integer;
     102        i: integer;
    101103begin
    102   assert(Item < CurrentSize);
    103   i := Ix[Item];
    104   result := (i < 0) or (bh[i].Value > Value);
     104        assert(Item<CurrentSize);
     105        i := Ix[Item];
     106        result := (i < 0) or (bh[i].Value > Value);
    105107end;
    106108
    107 // Left(i) = 2*i+1.
    108 // Right(i) = 2*i+2 => Left(i)+1
    109 function Get(var Item, Value: integer): boolean; // O(lg(n))
     109//Left(i) = 2*i+1.
     110//Right(i) = 2*i+2 => Left(i)+1
     111function Get(var Item, Value: integer): boolean; //O(lg(n))
    110112var
    111   i, j: integer;
    112   last: TheapItem;
     113        i, j:   integer;
     114        last:   TheapItem;
    113115begin
    114   if n = 0 then
    115   begin
    116     result := false;
    117     exit;
    118   end;
     116        if n = 0 then
     117        begin
     118                result := False;
     119                exit;
     120        end;
    119121
    120   Item := bh[0].Item;
    121   Value := bh[0].Value;
     122        Item := bh[0].Item;
     123        Value := bh[0].Value;
    122124
    123   Ix[Item] := -1;
     125        Ix[Item] := -1;
    124126
    125   dec(n);
    126   if n > 0 then
    127   begin
    128     last := bh[n];
    129     i := 0;
    130     j := 1;
    131     while j < n do
    132     begin
    133       // Right(i) = Left(i)+1
    134       if (j < n - 1) and (bh[j].Value > bh[j + 1].Value) then
    135         Inc(j);
    136       if last.Value <= bh[j].Value then
    137         break;
     127        dec(n);
     128        if n > 0 then
     129        begin
     130                last := bh[n];
     131                i := 0;         j := 1;
     132                while j < n do
     133                begin
     134                                                                                //      Right(i) = Left(i)+1
     135                        if(j < n-1) and (bh[j].Value > bh[j + 1].Value)then
     136                                inc(j);
     137                        if last.Value <= bh[j].Value then               break;
    138138
    139       bh[i] := bh[j];
    140       Ix[bh[i].Item] := i;
    141       i := j;
    142       j := j shl 1 + 1; // Left(j) = 2*j+1
    143     end;
     139                        bh[i] := bh[j];
     140                        Ix[bh[i].Item] := i;
     141                        i := j;
     142                        j := j shl 1+1; //Left(j) = 2*j+1
     143                end;
    144144
    145     // Insert the root in the correct place in the heap.
    146     bh[i] := last;
    147     Ix[last.Item] := i;
    148   end;
    149   result := true
     145                // Insert the root in the correct place in the heap.
     146                bh[i] := last;
     147                Ix[last.Item] := i;
     148        end;
     149        result := True
    150150end;
    151151
    152152initialization
     153        n:=0;
     154        CurrentSize:=0;
     155        {$IFDEF DEBUG}InUse:=false;{$ENDIF}
     156end.
    153157
    154 n := 0;
    155 CurrentSize := 0;
    156 {$IFDEF DEBUG}InUse := false; {$ENDIF}
    157 
    158 end.
Note: See TracChangeset for help on using the changeset viewer.