Browse Source

Improvements on AbstractMem

PascalCoin 3 years ago
parent
commit
15eb3cd98c

+ 17 - 9
src/libraries/abstractmem/UAbstractMemBTree.pas

@@ -113,8 +113,9 @@ type
     constructor Create(AAbstractMem : TAbstractMem; const AInitialZone: TAMZone; AAllowDuplicates : Boolean; AOrder : Integer; const AOnCompareAbstractMemDataMethod: TComparison<TBTreeData>);
     constructor Create(AAbstractMem : TAbstractMem; const AInitialZone: TAMZone; AAllowDuplicates : Boolean; AOrder : Integer; const AOnCompareAbstractMemDataMethod: TComparison<TBTreeData>);
     procedure Add(); reintroduce;
     procedure Add(); reintroduce;
     procedure Delete(); reintroduce;
     procedure Delete(); reintroduce;
-    function FindData(const AData: TBTreeData; out APosition : TAbstractMemPosition; out AFoundData : TBTreeData) : Boolean; overload;
-    function FindData(const AData: TBTreeData; out APosition : TAbstractMemPosition) : Boolean; overload;
+    function FindData(const AData: TBTreeData; out APosition : TAbstractMemPosition; var AFoundData : TBTreeData) : Boolean; overload;
+    function FindData(const AData: TBTreeData; var AFoundData : TBTreeData) : Boolean; overload;
+    function FindDataPos(const AData: TBTreeData; out APosition : TAbstractMemPosition) : Boolean;
     function FindDataPrecessor(const AData : TBTreeData; var APrecessor : TBTreeData) : Boolean;
     function FindDataPrecessor(const AData : TBTreeData; var APrecessor : TBTreeData) : Boolean;
     function FindDataSuccessor(const AData : TBTreeData; var ASuccessor : TBTreeData) : Boolean;
     function FindDataSuccessor(const AData : TBTreeData; var ASuccessor : TBTreeData) : Boolean;
     function FindDataLowest(out ALowest : TBTreeData) : Boolean;
     function FindDataLowest(out ALowest : TBTreeData) : Boolean;
@@ -559,9 +560,9 @@ begin
 end;
 end;
 
 
 function TAbstractMemBTreeDataAbstract<TBTreeData>.FindData(const AData: TBTreeData;
 function TAbstractMemBTreeDataAbstract<TBTreeData>.FindData(const AData: TBTreeData;
-  out APosition: TAbstractMemPosition; out AFoundData : TBTreeData): Boolean;
+  out APosition: TAbstractMemPosition; var AFoundData : TBTreeData): Boolean;
 begin
 begin
-  if FindData(AData,APosition) then begin
+  if FindDataPos(AData,APosition) then begin
     Result := True;
     Result := True;
     AFoundData := LoadData(APosition);
     AFoundData := LoadData(APosition);
   end else begin
   end else begin
@@ -572,6 +573,13 @@ begin
 end;
 end;
 
 
 function TAbstractMemBTreeDataAbstract<TBTreeData>.FindData(
 function TAbstractMemBTreeDataAbstract<TBTreeData>.FindData(
+  const AData: TBTreeData; var AFoundData: TBTreeData): Boolean;
+var LPos : TAbstractMemPosition;
+begin
+  Result := FindData(AData,LPos,AFoundData);
+end;
+
+function TAbstractMemBTreeDataAbstract<TBTreeData>.FindDataPos(
   const AData: TBTreeData; out APosition: TAbstractMemPosition): Boolean;
   const AData: TBTreeData; out APosition: TAbstractMemPosition): Boolean;
 begin
 begin
   FAbstractBTreeLock.Acquire;
   FAbstractBTreeLock.Acquire;
@@ -657,7 +665,7 @@ begin
   while (Result) and (i<FIndexes.Count) do begin
   while (Result) and (i<FIndexes.Count) do begin
     LBTreeIndex := TAbstractMemBTreeDataIndex<TBTreeData>(FIndexes.Items[i]);
     LBTreeIndex := TAbstractMemBTreeDataIndex<TBTreeData>(FIndexes.Items[i]);
     if (Not LBTreeIndex.AllowDuplicates) then begin
     if (Not LBTreeIndex.AllowDuplicates) then begin
-      Result :=  Not (LBTreeIndex.FindData(AData,LIndexPosition));
+      Result :=  Not (LBTreeIndex.FindDataPos(AData,LIndexPosition));
     end;
     end;
     inc(i);
     inc(i);
   end;
   end;
@@ -696,12 +704,12 @@ begin
   while (Result) and (i<FIndexes.Count) do begin
   while (Result) and (i<FIndexes.Count) do begin
     LBTreeIndex := TAbstractMemBTreeDataIndex<TBTreeData>(FIndexes.Items[i]);
     LBTreeIndex := TAbstractMemBTreeDataIndex<TBTreeData>(FIndexes.Items[i]);
     if (Not LBTreeIndex.AllowDuplicates) then begin
     if (Not LBTreeIndex.AllowDuplicates) then begin
-      Result :=  Not (LBTreeIndex.FindData(AData,LIndexPosition));
+      Result :=  Not (LBTreeIndex.FindDataPos(AData,LIndexPosition));
     end;
     end;
     inc(i);
     inc(i);
   end;
   end;
   if (Result) And (Not AllowDuplicates) then begin
   if (Result) And (Not AllowDuplicates) then begin
-    Result := Not FindData(AData,LIndexPosition);
+    Result := Not FindDataPos(AData,LIndexPosition);
   end;
   end;
 end;
 end;
 
 
@@ -734,11 +742,11 @@ var LAbstractMemPos, LindexPosition : TAbstractMemPosition;
   i : Integer;
   i : Integer;
   LBTreeIndex : TAbstractMemBTreeDataIndex<TBTreeData>;
   LBTreeIndex : TAbstractMemBTreeDataIndex<TBTreeData>;
 begin
 begin
-  if FindData(AData,LAbstractMemPos) then begin
+  if FindDataPos(AData,LAbstractMemPos) then begin
     // Delete from indexes
     // Delete from indexes
     for i := 0 to FIndexes.Count-1 do begin
     for i := 0 to FIndexes.Count-1 do begin
       LBTreeIndex := TAbstractMemBTreeDataIndex<TBTreeData>(FIndexes.Items[i]);
       LBTreeIndex := TAbstractMemBTreeDataIndex<TBTreeData>(FIndexes.Items[i]);
-      if Not LBTreeIndex.FindData(AData,LindexPosition) then raise EAbstractMemBTree.Create(Format('Fatal error Data not found in index %d/%d to Delete from pos %s',[i+1,Findexes.Count,LAbstractMemPos.ToHexString]));
+      if Not LBTreeIndex.FindDataPos(AData,LindexPosition) then raise EAbstractMemBTree.Create(Format('Fatal error Data not found in index %d/%d to Delete from pos %s',[i+1,Findexes.Count,LAbstractMemPos.ToHexString]));
       if not LBTreeIndex.DeleteInherited(LindexPosition) then raise EAbstractMemBTree.Create(Format('Fatal error Data not deleted in index %d/%d from pos %s at pos %s',[i+1,Findexes.Count,LAbstractMemPos.ToHexString,LindexPosition.ToHexString]));
       if not LBTreeIndex.DeleteInherited(LindexPosition) then raise EAbstractMemBTree.Create(Format('Fatal error Data not deleted in index %d/%d from pos %s at pos %s',[i+1,Findexes.Count,LAbstractMemPos.ToHexString,LindexPosition.ToHexString]));
       FAbstractMem.Dispose(LindexPosition);
       FAbstractMem.Dispose(LindexPosition);
     end;
     end;

+ 1 - 1
src/libraries/abstractmem/UFileMem.pas

@@ -260,8 +260,8 @@ begin
       SaveHeader;
       SaveHeader;
     finally
     finally
       FIsFlushingCache := False;
       FIsFlushingCache := False;
+      FLock.Release;
     end;
     end;
-    FLock.Release;
   end;
   end;
 end;
 end;
 
 

+ 1 - 0
src/libraries/abstractmem/UOrderedList.pas

@@ -69,6 +69,7 @@ type
     Function IndexOf(const AValue: T) : Integer;
     Function IndexOf(const AValue: T) : Integer;
     property AllowDuplicates : Boolean read FAllowDuplicates;
     property AllowDuplicates : Boolean read FAllowDuplicates;
     property OnComparer : TComparison<T> read FOnCompare;
     property OnComparer : TComparison<T> read FOnCompare;
+    property Items[index:Integer] : T read Get; default;
   End;
   End;
 
 
 // Default Ordered functions
 // Default Ordered functions

+ 2 - 1
src/libraries/abstractmem/tests/src/UAbstractMemBTree.Tests.pas

@@ -403,6 +403,7 @@ var
   intValue, nRounds, nAdds, nDeletes, i, intAux : Integer;
   intValue, nRounds, nAdds, nDeletes, i, intAux : Integer;
   LCurr, LnextCurr : String;
   LCurr, LnextCurr : String;
   begin
   begin
+    nRounds := 0; nAdds := 0; nDeletes := 0;
     repeat
     repeat
       inc(nRounds);
       inc(nRounds);
       intValue := Random(AOrder * 100);
       intValue := Random(AOrder * 100);
@@ -420,7 +421,7 @@ var
     // Delete mode
     // Delete mode
     while Lbt.Count>0 do begin
     while Lbt.Count>0 do begin
       if not Lbt.FindDataLowest(LCurr) then raise Exception.Create('Cannot fint lowest but Count>0');
       if not Lbt.FindDataLowest(LCurr) then raise Exception.Create('Cannot fint lowest but Count>0');
-      if not Lbt.FindData(LCurr,LzoneIndex.position) then raise Exception.Create(Format('"%s" Not Found %d',[LCurr,Lbt.Count]));
+      if not Lbt.FindDataPos(LCurr,LzoneIndex.position) then raise Exception.Create(Format('"%s" Not Found %d',[LCurr,Lbt.Count]));
       while (Random(50)>0) do begin
       while (Random(50)>0) do begin
         if Random(3)=0 then begin
         if Random(3)=0 then begin
           if not Lbt.FindDataPrecessor(Lcurr,LnextCurr) then begin
           if not Lbt.FindDataPrecessor(Lcurr,LnextCurr) then begin