你可以使用 Delphi 的流系统来编写专有的流格式。如果大小很重要(与速度相反),你可以对流进行压缩。以下是一些代码:
您可以使用Delphi的流处理系统编写专有的流格式。 如果大小很重要(而不是速度),则可以对流进行压缩。以下是一些代码:
type
TFilePos = TArray<Integer>;
TFileDict = TDictionary<string, TFilePos>;
TWordDict = class (TDictionary<string, TFileDict>)
private
procedure LoadFromStream(stream: TStream);
procedure SaveToStream(stream: TStream);
public
procedure LoadFromZip(const AFileName: string);
procedure LoadFromFile(const AFileName: string);
procedure SaveToZip(const AFileName: string);
procedure SaveToFile(const AFileName: string);
end;
procedure TWordDict.LoadFromZip(const AFileName: string);
var
stream: TStream;
localHeader: TZipHeader;
zipFile: TZipFile;
begin
zipFile := TZipFile.Create;
try
zipFIle.Open(AFIleName, zmRead);
zipFile.Read('worddict', stream, localHeader);
try
LoadFromStream(stream);
finally
stream.Free;
end;
zipFile.Close;
finally
zipFile.Free;
end;
end;
procedure TWordDict.SaveToZip(const AFileName: string);
var
stream: TStream;
zipFile: TZipFile;
begin
stream := TMemoryStream.Create;
try
SaveToStream(stream);
stream.Position := 0;
zipFile := TZipFile.Create;
try
zipFile.Open(AFileName, zmWrite);
zipFile.Add(stream, 'worddict');
zipFile.Close;
finally
zipFile.Free;
end;
finally
stream.Free;
end;
end;
procedure TWordDict.SaveToStream(stream: TStream);
var
posi: System.Generics.Collections.TPair<string, TFilePos>;
i: Integer;
pair: System.Generics.Collections.TPair<string, TFileDict>;
writer: TWriter;
begin
writer := TWriter.Create(stream, 4096);
try
writer.WriteListBegin;
for pair in Self do
begin
writer.WriteString(pair.Key);
writer.WriteListBegin;
for posi in pair.Value do
begin
writer.WriteString(posi.Key);
writer.WriteInteger(Length(posi.Value));
for i in posi.Value do
begin
writer.WriteInteger(i);
end;
end;
writer.WriteListEnd;
end;
writer.WriteListEnd;
finally
writer.Free;
end;
end;
procedure TWordDict.LoadFromStream(stream: TStream);
var
sFiles: TFileDict;
aPosi: TFilePos;
size: Integer;
i: Integer;
sWord: string;
reader: TReader;
sFile: string;
begin
Clear;
reader := TReader.Create(stream, 1024);
try
reader.ReadListBegin;
while not reader.EndOfList do
begin
sWord := reader.ReadString;
sFiles := TFileDict.Create;
reader.ReadListBegin;
while not reader.EndOfList do
begin
sFile := reader.ReadString;
size := reader.ReadInteger;
SetLength(aPosi, size);
for I := 0 to size - 1 do
begin
aPosi[I] := reader.ReadInteger;
end;
sFiles.Add(sFile, Copy(aPosi));
end;
reader.ReadListEnd;
Add(sWord, sFiles);
end;
reader.ReadListEnd;
finally
reader.Free;
end;
end;
procedure TWordDict.LoadFromFile(const AFileName: string);
var
stream: TStream;
begin
stream := TFileStream.Create(AFileName, fmOpenRead);
try
LoadFromStream(stream);
finally
stream.Free;
end;
end;
procedure TWordDict.SaveToFile(const AFileName: string);
var
stream: TStream;
begin
stream := TFileStream.Create(AFileName, fmCreate);
try
SaveToStream(stream);
finally
stream.Free;
end;
end;
TIntegerDynArray
- 最好使用TArray<Integer>
,因为您已经使用了泛型。请参见 https://dev59.com/6mgu5IYBdhLWcg3w2ahm 和 https://dev59.com/vWYq5IYBdhLWcg3wkRUo。 - Arioch 'The