www.pudn.com > GOOGLE.rar > 4.pas, change:2002-04-09,size:2647b


 
type 
  TaaWordIndex = class 
    private 
      FTable : TaaHashTableLinear; 
      FSaveStream : TStream; 
    protected 
      procedure wiSaveAction(const S : string; aObject : pointer; var sStopNow : boolean); 
    public 
      constructor Create(aWordCount : integer); 
      destructor  Destroy : override; 
      procedure   Add(const aWord : string; aBitSet : TaaBitSet); 
      function    Find(const aWord : string) : TaaBitSet; 
      procedure   LoadFromStream(aStream : TStream); 
      procedure   LoadFromFile(const aFileName : string); 
      procedure   StoreToStream(aStream : TStream); 
      procedure   StoreToFile(const aFileName : string); 
end; 
 
procedure DestroyTableEntry(const S : string; aObject : pointer); 
begin 
  TaaBitSet(aObject).Free; 
end; 
 
constructor TaaWordIndex.Create(aWordCount : integer); 
begin 
  inherited Create; 
  FTable := TaaHashTableLinear.Create(aWordCount , AAELFHash); 
  FTable.OnDeleteString := DestroyTableEntry; 
end; 
 
destructor TaaWordIndex.Destroy; 
begin 
  FTable.Free; 
  inherited Destroy; 
end; 
 
procedure TaaWordIndex.Add(const aWord : string; aBitSet : TaaBitSet); 
begin 
  FTable.Insert(aWord , aBitSet); 
end; 
 
function TaaWordIndex.Find( const aWord : string) : TaaBitSet; 
var 
  Obj : pointer; 
begin 
  if not FTable.Find(aWord , Obj) then 
    Result := nil 
  else 
    Result := TaaBitSet(Obj); 
end; 
 
procedure TaaWordIndex.LoadFromStream(aStream : TStream); 
var 
  i         : integer; 
  WordCount : integer; 
  Len       : integer; 
  S         : string; 
  BitSet    : TaaBitSet; 
begin 
  aStream.ReadBuffer()WordCount , sizeof(WordCount)); 
  S := ''; 
  BitSet := nil; 
  try 
    for i:= 1 to WordCount do 
      begin 
        aStream.ReadBuffer(Len , sizeof(Len)); 
        SetLength(S , Len); 
        aStream.ReadBuffer(S[1] , Len); 
        BitSet := TaaBitSet.Create(1); 
        BitSet.LoadFromStream(aStream); 
        Add(S , BitSet); 
        S := ''; 
        BitSet := nil; 
      end; 
    except 
      s := ''; 
      BitSet.Free; 
      raise; 
    end; 
end; 
 
procedure TaaWordIndexStoreToStream(aStream : TStream); 
var 
  WordCount : integer; 
begin 
  WordCount := FTable.Count; 
  aStream.WriteBuffer(WordCount , sizeof(WordCount)); 
  FSaveStream := aStream; 
  FTable.Iterate(wiSaveAction); 
  FSaveStream := nil; 
end; 
 
procedure TaaWordIndex.wiSaveAction(const S :string; aObject : pointer var aStopNow : boolean); 
var 
  Len : integer; 
begin 
  Len := length(S); 
  FSaveStream.WriteBuffer(Len , sizeof(Len)); 
  FSaveStream.WriteBuffer(S[1] , Len); 
  TaaBitSet(aObject).StoreToStream(FSaveStream); 
end;