Ignore:
Timestamp:
Nov 11, 2009, 12:56:32 PM (15 years ago)
Author:
george
Message:
  • Opraveno: Inicializace a uvolnění třídy, které byly přepsány ze struktury record.
File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/Analyzátor gramatiky/UGrammer.pas

    r29 r30  
    2020  end;
    2121
     22  TPathItemList = specialize TFPGObjectList<TPathItem>;
     23
    2224  TGrammerPath = class
    23     Items: array of TPathItem;
     25    Items: TPathItemList;
    2426    procedure Assign(Source: TGrammerPath);
    2527    procedure Next;
     28    constructor Create;
     29    destructor Destroy; override;
    2630  end;
    2731
     
    3236  end;
    3337
     38  TPossibleCharacterList = specialize TFPGObjectList<TPossibleCharacter>;
     39
     40  { TPossibleCharacters }
     41
    3442  TPossibleCharacters = class
    35     Items: array of TPossibleCharacter;
     43    Items: TPossibleCharacterList;
    3644    procedure Assign(Source: TPossibleCharacters);
     45    constructor Create;
     46    destructor Destroy; override;
    3747  end;
    3848
     
    135145  I: Integer;
    136146begin
    137   for I := 0 to Items.Count - 1 do with TGrammerItem(Items[I]) do begin
     147  for I := 0 to Items.Count - 1 do
     148  with Items[I] do begin
    138149    Processed := False;
    139150  end;
     
    160171  TempPath: TGrammerPath;
    161172begin
    162   SetLength(Path.Items, Length(Path.Items) + 1);
    163   with Path.Items[High(Path.Items)] do begin
     173  TempPath := TGrammerPath.Create;
     174  with Path.Items[Path.Items.Add(TPathItem.Create)] do begin
    164175    Rule := Self;
    165176    ItemIndex := UseIndex;
     
    172183      if UseIndex > 0 then begin
    173184        // Forward generation to upper item
    174         SetLength(Path.Items, Length(Path.Items) - 1);
    175         with Path.Items[High(Path.Items)] do begin
     185       with Path.Items[Path.Items.Add(TPathItem.Create)] do begin
    176186          NextItemIndex := ItemIndex;
    177187          NextCharIndex := CharIndex;
    178188          NextRule := Rule;
    179189        end;
    180         SetLength(Path.Items, Length(Path.Items) - 1);
     190        Path.Items.Delete(Path.Items.Count - 1);
    181191        NextRule.GetPossibleCharacters(Path, Characters, NextItemIndex + 1, NextCharIndex);
    182192      end else begin
    183193        // Generate alternatives
    184194        for I := 0 to Items.Count - 1 do begin
    185           Path.Items[High(Path.Items)].ItemIndex := I;
     195          Path.Items[Path.Items.Count - 1].ItemIndex := I;
    186196          //Inc(Path.Items[High(Path.Items)].CharIndex);
    187197          Items[I].GetPossibleCharacters(Path, Characters);
     
    193203      if UseIndex >= Items.Count then begin
    194204        // Forward generation to upper item
    195         SetLength(Path.Items, Length(Path.Items) - 1);
    196         with Path.Items[High(Path.Items)] do begin
     205        Path.Items.Delete(Path.Items.Count - 1);
     206        with Path.Items[Path.Items.Count - 1] do begin
    197207          NextItemIndex := ItemIndex;
    198208          NextCharIndex := CharIndex;
    199209          NextRule := Rule;
    200210        end;
    201         SetLength(Path.Items, Length(Path.Items) - 1);
     211        Path.Items.Delete(Path.Items.Count - 1);
    202212        NextRule.GetPossibleCharacters(Path, Characters, NextItemIndex + 1, NextCharIndex);
    203213      end else begin
    204         Path.Items[High(Path.Items)].ItemIndex := UseIndex;
     214        Path.Items[Path.Items.Count - 1].ItemIndex := UseIndex;
    205215        Items[UseIndex].GetPossibleCharacters(Path, Characters);
    206216      end;
     
    208218      if (UseIndex > 0) and not Items[UseIndex - 1].Processed then
    209219        if Items[UseIndex - 1].Repetition then begin
    210           TempPath.Items[High(TempPath.Items)].ItemIndex := UseIndex - 1;
     220          TempPath.Items[TempPath.Items.Count - 1].ItemIndex := UseIndex - 1;
    211221          Items[UseIndex - 1].GetPossibleCharacters(TempPath, Characters);
    212222        end;
    213223    end;
    214224  end;
     225  TempPath.Destroy;
    215226end;
    216227
     
    225236  I: Integer;
    226237begin
    227   for I := 0 to Rules.Count - 1 do with Rules[I] do begin
     238  for I := 0 to Rules.Count - 1 do
     239  with Rules[I] do begin
    228240    ClearProcessed;
    229241  end;
     
    278290  case ItemType of
    279291    itTerminal: begin
    280       SetLength(Characters.Items, Length(Characters.Items) + 1);
    281       Characters.Items[High(Characters.Items)].Character := Character;
    282       Characters.Items[High(Characters.Items)].RulePath.Assign(Path);
    283       with Characters.Items[High(Characters.Items)].RulePath do begin
    284         Inc(Items[High(Items)].ItemIndex);
     292      Characters.Items.Add(TPossibleCharacter.Create);
     293      Characters.Items[Characters.Items.Count - 1].Character := Character;
     294      Characters.Items[Characters.Items.Count - 1].RulePath.Assign(Path);
     295      with Characters.Items[Characters.Items.Count - 1].RulePath do begin
     296        Inc(Items[Items.Count - 1].ItemIndex);
    285297      end;
    286298    end;
     
    292304        // Forward generation to upper item
    293305        //SetLength(Path.Items, Length(Path.Items) - 1);
    294         with Path.Items[High(Path.Items)] do begin
     306        with Path.Items[Path.Items.Count - 1] do begin
    295307          NextItemIndex := ItemIndex;
    296308          NextCharIndex := CharIndex;
    297309          NextRule := Rule;
    298310        end;
    299         SetLength(Path.Items, Length(Path.Items) - 1);
     311        Path.Items.Count := Path.Items.Count - 1;
    300312        NextRule.GetPossibleCharacters(Path, Characters, NextItemIndex + 1, NextCharIndex);
    301313  end;
     
    308320  I: Integer;
    309321begin
    310   SetLength(Items, Length(Source.Items));
    311   for I := 0 to High(Items) do Items[I].Assign(Source.Items[I]);
     322  for I := 0 to Items.Count - 1 do
     323    Items[I].Destroy;
     324  Items.Count := Source.Items.Count;
     325  for I := 0 to Items.Count - 1 do begin
     326    Items[I] := TPathItem.Create;
     327    Items[I].Assign(Source.Items[I]);
     328  end;
    312329end;
    313330
     
    318335begin
    319336  Success := False;
    320   Index := High(Items);
     337  Index := Items.Count - 1;
    321338  while not Success and (Index >= 0) do begin
    322339    with Items[Index] do if Rule.Items[ItemIndex].Repetition then begin
     
    331348    end;
    332349  end;
    333   SetLength(Items, Index + 1);
     350  Items.Count := Index + 1;
     351end;
     352
     353constructor TGrammerPath.Create;
     354begin
     355  Items := TPathItemList.Create;
     356end;
     357
     358destructor TGrammerPath.Destroy;
     359begin
     360  Items.Destroy;
     361  inherited Destroy;
    334362end;
    335363
     
    358386  I: Integer;
    359387begin
    360   SetLength(Items, Length(Source.Items));
    361   for I := 0 to High(Items) do Items[I].Assign(Source.Items[I]);
     388  Items.Count := Source.Items.Count;
     389  for I := 0 to Items.Count - 1 do
     390    Items[I].Assign(Source.Items[I]);
     391end;
     392
     393constructor TPossibleCharacters.Create;
     394begin
     395  Items := TPossibleCharacterList.Create;
     396end;
     397
     398destructor TPossibleCharacters.Destroy;
     399begin
     400  Items.Destroy;
     401  inherited Destroy;
    362402end;
    363403
Note: See TracChangeset for help on using the changeset viewer.