7日目 動的配列を使ったリスト

これまでのインスタンスをTListで管理する方法が iOS ではエラーになる。しっかり型変換を行えば動作するとは思うが、ソースがごちゃごちゃするので、動的配列を使ったリストに変更する。
delete や insert も時間があったら作ろう。
しかし、ファイルの読み込みにすごく時間がかかるようになった気がする。

type
TWordRecord = Record
  private
    FWord: string;
    FJapanese: string;
  public
    procedure Assign(aWordRecord: TWordRecord);
    property Word: string read FWord write FWord;
    property Japanese: string read FJapanese write FJapanese;
end;

type
  TWordList = class
  private
    function getCount: Integer;
  public
    Items: Array of TWordRecord;
    constructor Create();
    destructor Destroy; override;
    procedure Load(aPathFileName: string);
    procedure Assign(aWordList: TWordList);
    procedure AddItem(aWordRecord: TWordRecord);
    procedure Clear();
    property Count: Integer read getCount;
  end;

const
  ESCAPE_COMMA_CHR = 35;   // #
  ESCAPE_SPACE_CHR = 36;   // $
  DEMILITR_CHR = 9;        // TAB

implementation

procedure TWordRecord.Assign(aWordRecord: TWordRecord);
begin
  Self.FWord     := aWordRecord.Word;
  Self.FJapanese := aWordRecord.Japanese;
end;

{ TWordList }

constructor TWordList.Create;
begin
  Self.Clear;
end;

destructor TWordList.Destroy;
begin
  Self.Clear;
  inherited;
end;

procedure TWordList.AddItem(aWordRecord: TWordRecord);
begin
  SetLength(Self.Items, Length(Self.Items) + 1);
  Self.Items[High(Items)].Assign(aWordRecord);
end;

procedure TWordList.Assign(aWordList: TWordList);
var
  i: Integer;
begin
  SetLength(Self.Items, Length(aWordList.Items));
  for i := Low(aWordList.Items) to High(aWordList.Items) do
    Self.Items[i].Assign(aWordList.Items[i]);
end;

function TWordList.getCount: Integer;
begin
  Result := Length(Items);
end;

procedure TWordList.Clear;
begin
  SetLength(Self.Items, 0);
end;

procedure TWordList.Load(aPathFileName: string);
var
  i: Integer;
  tmpWordRecord: TWordRecord;
  tmpRecordList: TStringList;
  tmpFieldList: TStringList;
  tmpString: String;
begin

  self.Clear;

  if FileExists(aPathFileName) then
  begin
    tmpFieldList  := TStringList.Create;
    tmpRecordList := TStringList.Create;

    tmpRecordList.LoadFromFile(aPathFileName);

    for i := 0 to tmpRecordList.Count - 1 do
    begin

      tmpFieldList.Delimiter := chr(DEMILITR_CHR);
      tmpFieldList.StrictDelimiter := False;

      tmpString := tmpRecordList[i];
      tmpFieldList.DelimitedText := tmpString;

      tmpWordRecord.Word := trim(tmpFieldList[0]);
      tmpWordRecord.Japanese := trim(tmpFieldList[1]);
      tmpWordRecord.Japanese := StringReplace(tmpWordRecord.Japanese, chr(ESCAPE_COMMA_CHR), ',', [rfReplaceAll]);
      tmpWordRecord.Japanese := StringReplace(tmpWordRecord.Japanese, chr(ESCAPE_SPACE_CHR), ' ', [rfReplaceAll]);

      self.AddItem(tmpWordRecord);

    end;
    tmpRecordList.Free;
    tmpFieldList.Free;
  end;

end;

コメントを残す

メールアドレスが公開されることはありません。


三 − 1 =

次のHTML タグと属性が使えます: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>