Changeset 175


Ignore:
Timestamp:
Mar 6, 2011, 6:44:23 PM (14 years ago)
Author:
george
Message:
  • Added: Generic Matrix class methods enhacement.
Location:
Generics/TemplateGenerics
Files:
5 edited

Legend:

Unmodified
Added
Removed
  • Generics/TemplateGenerics/Generic

    • Property svn:ignore set to
      backup
  • Generics/TemplateGenerics/Generic/GenericList.inc

    r129 r175  
    4949    function Remove(Item: TGListItem): TGListIndex;
    5050    procedure Reverse;
     51    procedure Replace(Index: TGListIndex; Source: TGList);
    5152    procedure Sort(Compare: TGListSortCompare);
    5253    procedure SetArray(Values: TGListItemArray);
     
    7172
    7273{ TGList }
     74
     75procedure TGList.Replace(Index: TGListIndex; Source: TGList);
     76var
     77  I: TGListIndex;
     78begin
     79  I := 0;
     80  while I < Source.Count do begin
     81    Items[Index + I] := Source[I];
     82    I := I + 1;
     83  end;
     84end;
    7385
    7486function TGList.GetCapacity: TGListIndex;
  • Generics/TemplateGenerics/Generic/GenericMatrix.inc

    r109 r175  
    77  TGMatrixFromStringConverter = function(Text: string): TGMatrixItem;
    88  TGMatrixRow = array of TGMatrixItem;
     9  TGMatrixMerge = function(Item1, Item2: TGMatrixItem): TGMatrixItem of object;
    910
    1011  TGMatrixIndex = record
     
    1819    FItems: array of array of TGMatrixItem;
    1920    FCount: TGMatrixIndex;
    20     function Get(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem;
     21    function GetXY(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem;
     22    function Get(Index: TGMatrixIndex): TGMatrixItem;
    2123    function GetCapacity: TGMatrixIndex;
    2224    function GetLast: TGMatrixItem;
     
    2527    procedure SetLast(AValue: TGMatrixItem);
    2628    procedure SetFirst(AValue: TGMatrixItem);
    27     procedure Put(Y: TGMatrixIndexY; X: TGMatrixIndexX; const AValue: TGMatrixItem); virtual;
     29    procedure PutXY(Y: TGMatrixIndexY; X: TGMatrixIndexX; const AValue: TGMatrixItem); virtual;
     30    procedure Put(Index: TGMatrixIndex; const AValue: TGMatrixItem); virtual;
    2831    procedure SetCount(const AValue: TGMatrixIndex);
    2932  public
     
    3437    procedure Clear; virtual;
    3538    procedure Contract;
    36     function CreateIndex(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixIndex;
     39    function CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TGMatrixIndex;
    3740    procedure Delete(Index: TGMatrixIndex); virtual;
    3841    procedure DeleteItems(Index, Count: TGMatrixIndex);
     
    5255    procedure Move(CurIndex, NewIndex: TGMatrixIndex);
    5356    procedure MoveItems(CurIndex, NewIndex, Count: TGMatrixIndex);
     57    procedure Merge(Index: TGMatrixIndex; Source: TGMatrix; Proc: TGMatrixMerge);
     58    procedure Replace(Index: TGMatrixIndex; Source: TGMatrix);
    5459    function Remove(Item: TGMatrixItem): TGMatrixIndex;
    5560    procedure Reverse;
     61    procedure ReverseHorizontal;
     62    procedure ReverseVertical;
    5663    procedure Sort(Compare: TGMatrixSortCompare);
    5764    procedure SetArray(Values: array of TGMatrixItem);
    5865    property Count: TGMatrixIndex read FCount write SetCount;
    5966    property Capacity: TGMatrixIndex read GetCapacity write SetCapacity;
    60     property Items[Y: TGMatrixIndexY; X: TGMatrixIndexX]: TGMatrixItem read Get write Put; default;
     67    property ItemsXY[Y: TGMatrixIndexY; X: TGMatrixIndexX]: TGMatrixItem read GetXY write PutXY; default;
     68    property Items[Index: TGMatrixIndex]: TGMatrixItem read Get write Put;
    6169    property Last: TGMatrixItem read GetLast write SetLast;
    6270  end;
     
    7179
    7280resourcestring
    73   SMatrixIndexError = 'Matrix index error [%d, %d]';
     81  SMatrixIndexError = 'Matrix index error [X: %d, Y: %d]';
    7482
    7583{$UNDEF IMPLEMENTATION_USES}
     
    8088{ TGMatrix }
    8189
    82 function TGMatrix.CreateIndex(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixIndex;
     90procedure TGMatrix.Replace(Index: TGMatrixIndex; Source: TGMatrix);
     91var
     92  X: TGMatrixIndexX;
     93  Y: TGMatrixIndexY;
     94begin
     95  Y := 0;
     96  while Y < Source.Count.Y do begin
     97    X := 0;
     98    while X < Source.Count.X do begin
     99      ItemsXY[Index.Y + Y, Index.X + X] := Source.ItemsXY[Y, X];
     100      X := X + 1;
     101    end;
     102    Y := Y + 1;
     103  end;
     104end;
     105
     106procedure TGMatrix.Merge(Index: TGMatrixIndex; Source: TGMatrix; Proc: TGMatrixMerge);
     107var
     108  X: TGMatrixIndexX;
     109  Y: TGMatrixIndexY;
     110begin
     111  Y := 0;
     112  while Y < Source.Count.Y do begin
     113    X := 0;
     114    while X < Source.Count.X do begin
     115      ItemsXY[Index.Y + Y, Index.X + X] := Proc(ItemsXY[Index.Y + Y, Index.X + X], Source.ItemsXY[Y, X]);
     116      X := X + 1;
     117    end;
     118    Y := Y + 1;
     119  end;
     120end;
     121
     122function TGMatrix.CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TGMatrixIndex;
    83123begin
    84124  Result.X := X;
     
    97137begin
    98138  if (Capacity.X <> AValue.X) and (Capacity.Y <> AValue.Y) then begin
    99     SetLength(FItems, AValue.Y);
     139(*    SetLength(FItems, AValue.Y);
    100140    Y := 0;
    101141    while Y < Length(FItems) do begin
     
    104144    end;
    105145  end;
    106 end;
    107 
    108 function TGMatrix.Get(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem;
    109 begin
     146  *)
     147    SetLength(FItems, AValue.Y, AValue.X);
     148  end;
     149end;
     150
     151function TGMatrix.GetXY(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem;
     152begin
     153  if (X < 0) or (X >= Count.X) or
     154    (Y < 0) or (Y >= Count.Y) then
     155    raise EListError.CreateFmt(SMatrixIndexError, [X, Y]);
    110156  Result := FItems[Y, X];
    111157end;
    112158
    113 procedure TGMatrix.Put(Y: TGMatrixIndexY; X: TGMatrixIndexX; const AValue: TGMatrixItem);
    114 begin
     159function TGMatrix.Get(Index: TGMatrixIndex): TGMatrixItem;
     160begin
     161  if (Index.X < 0) or (Index.X >= Count.X) or
     162    (Index.Y < 0) or (Index.Y >= Count.Y) then
     163    raise EListError.CreateFmt(SMatrixIndexError, [Index.X, Index.Y]);
     164  Result := FItems[Index.Y, Index.X];
     165end;
     166
     167procedure TGMatrix.PutXY(Y: TGMatrixIndexY; X: TGMatrixIndexX; const AValue: TGMatrixItem);
     168begin
     169  if (X < 0) or (X >= Count.X) or
     170    (Y < 0) or (Y >= Count.Y) then
     171    raise EListError.CreateFmt(SMatrixIndexError, [X, Y]);
    115172  FItems[Y, X] := AValue;
    116173end;
    117174
     175procedure TGMatrix.Put(Index: TGMatrixIndex; const AValue: TGMatrixItem);
     176begin
     177  if (Index.X < 0) or (Index.X >= Count.X) or
     178    (Index.Y < 0) or (Index.Y >= Count.Y) then
     179    raise EListError.CreateFmt(SMatrixIndexError, [Index.X, Index.Y]);
     180  FItems[Index.Y, Index.X] := AValue;
     181end;
     182
    118183procedure TGMatrix.SetCount(const AValue: TGMatrixIndex);
    119184begin
    120   Capacity := AValue;;
     185  Capacity := AValue;
    121186  FCount := AValue;
    122187end;
     
    124189procedure TGMatrix.Assign(Source: TGMatrix);
    125190var
    126   X: TGMatrixIndexX;
    127   Y: TGMatrixIndexY;
     191  Index: TGMatrixIndex;
    128192begin
    129193  Count := Source.Count;
    130   Y := 0;
    131   while Y < Count.Y do begin
    132     while X < Count.X do begin
    133       Items[Y, X] := Source[Y, X];
    134       X := X + 1;
    135     end;
    136     Y := Y + 1;
     194  Index.Y := 0;
     195  while Index.Y < Count.Y do begin
     196    Index.X := 0;
     197    while Index.X < Count.X do begin
     198      Items[Index] := Source.Items[Index];
     199      Index.X := Index.X + 1;
     200    end;
     201    Index.Y := Index.Y + 1;
    137202  end;
    138203end;
     
    349414procedure TGMatrix.Reverse;
    350415var
    351   I: TGMatrixIndex;
    352 begin
    353 (*  I := 0;
    354   while I < (Count div 2) do begin
    355     Exchange(I, Count - 1 - I);
    356     I := I + 1;
    357   end; *)
     416  X: TGMatrixIndexX;
     417  Y: TGMatrixIndexY;
     418begin
     419  Y := 0;
     420  while Y < (Count.Y - 1) do begin
     421    X := 1 + Y;
     422    while X < Count.X do begin
     423      Exchange(CreateIndex(X, Y), CreateIndex(Y, X));
     424      X := X + 1;
     425    end;
     426    Y := Y + 1;
     427  end;
     428end;
     429
     430procedure TGMatrix.ReverseHorizontal;
     431var
     432  X: TGMatrixIndexX;
     433  Y: TGMatrixIndexY;
     434begin
     435  Y := 0;
     436  while Y < Count.Y do begin
     437    X := 0;
     438    while X < (Count.X div 2) do begin
     439      Exchange(CreateIndex(X, Y), CreateIndex(Count.X - 1 - X, Y));
     440      X := X + 1;
     441    end;
     442    Y := Y + 1;
     443  end;
     444end;
     445
     446procedure TGMatrix.ReverseVertical;
     447var
     448  X: TGMatrixIndexX;
     449  Y: TGMatrixIndexY;
     450begin
     451  X := 0;
     452  while X < Count.X do begin
     453    Y := 0;
     454    while Y < (Count.Y div 2) do begin
     455      Exchange(CreateIndex(X, Y), CreateIndex(X, Count.Y - 1 - Y));
     456      Y := Y + 1;
     457    end;
     458    X := X + 1;
     459  end;
    358460end;
    359461
     
    408510    X := 0;
    409511    while X < Count.X do begin
    410       Result := Result + Converter(Items[Y, X]);
     512      Result := Result + Converter(ItemsXY[Y, X]);
    411513      if X < (Count.X - 1) then
    412514        Result := Result + ColSeparator;
     
    487589    X := Start.X;
    488590    while X < Count.X do begin
    489       Items[Y, X] := Value;
     591      ItemsXY[Y, X] := Value;
    490592      X := X + 1;
    491593    end;
     
    498600  Temp: TGMatrixItem;
    499601begin
    500   if ((Index1.Y >= FCount.Y) or (Index1.Y < 0)) then
    501     raise EListError.CreateFmt(SMatrixIndexError, [Index1.Y, Index1.X]);
    502   if ((Index2.Y >= FCount.Y) or (Index2.Y < 0)) then
    503     raise EListError.CreateFmt(SMatrixIndexError, [Index2.Y, Index2.X]);
     602  if (Index1.X < 0) or (Index1.X >= Count.X) or
     603    (Index1.Y < 0) or (Index1.Y >= Count.Y) then
     604    raise EListError.CreateFmt(SMatrixIndexError, [Index1.X, Index1.Y]);
     605  if (Index2.X < 0) or (Index2.X >= Count.X) or
     606    (Index2.Y < 0) or (Index2.Y >= Count.Y) then
     607    raise EListError.CreateFmt(SMatrixIndexError, [Index2.X, Index2.Y]);
    504608  Temp := FItems[Index1.Y, Index1.X];
    505   FItems[Index1.Y, Index1.X] := FItems[Index2.Y, Index1.X];
     609  FItems[Index1.Y, Index1.X] := FItems[Index2.Y, Index2.X];
    506610  FItems[Index2.Y, Index2.X] := Temp;
    507611end;
  • Generics/TemplateGenerics/Specialized

    • Property svn:ignore set to
      backup
  • Generics/TemplateGenerics/Specialized/SpecializedMatrix.pas

    r109 r175  
    2121{$DEFINE TGMatrixToStringConverter := TMatrixIntegerToStringConverter}
    2222{$DEFINE TGMatrixFromStringConverter := TMatrixIntegerFromStringConverter}
     23{$DEFINE TGMatrixMerge := TMatrixIntegerMerge}
     24{$DEFINE INTERFACE}
     25{$I 'GenericMatrix.inc'}
     26
     27// TMatrixByte<Integer, Integer, Byte>
     28{$DEFINE TGMatrixIndexX := Integer}
     29{$DEFINE TGMatrixIndexY := Integer}
     30{$DEFINE TGMatrixItem := Byte}
     31{$DEFINE TGMatrixIndex := TMatrixByteIndex}
     32{$DEFINE TGMatrixRow := TMatrixByteRow}
     33{$DEFINE TGMatrix := TMatrixByte}
     34{$DEFINE TGMatrixSortCompare := TMatrixByteSortCompare}
     35{$DEFINE TGMatrixToStringConverter := TMatrixByteToStringConverter}
     36{$DEFINE TGMatrixFromStringConverter := TMatrixByteFromStringConverter}
     37{$DEFINE TGMatrixMerge := TMatrixByteMerge}
    2338{$DEFINE INTERFACE}
    2439{$I 'GenericMatrix.inc'}
     
    3449{$DEFINE TGMatrixToStringConverter := TMatrixObjectToStringConverter}
    3550{$DEFINE TGMatrixFromStringConverter := TMatrixObjectFromStringConverter}
     51{$DEFINE TGMatrixMerge := TMatrixStringMerge}
    3652{$DEFINE INTERFACE}
    3753{$I 'GenericMatrix.inc'}
     
    5268{$DEFINE TGMatrixToStringConverter := TMatrixIntegerToStringConverter}
    5369{$DEFINE TGMatrixFromStringConverter := TMatrixIntegerFromStringConverter}
     70{$DEFINE TGMatrixMerge := TMatrixIntegerMerge}
     71{$DEFINE IMPLEMENTATION}
     72{$I 'GenericMatrix.inc'}
     73
     74// TMatrixByte<Integer, Integer, Byte>
     75{$DEFINE TGMatrixIndexX := Integer}
     76{$DEFINE TGMatrixIndexY := Integer}
     77{$DEFINE TGMatrixItem := Byte}
     78{$DEFINE TGMatrixIndex := TMatrixByteIndex}
     79{$DEFINE TGMatrixRow := TMatrixByteRow}
     80{$DEFINE TGMatrix := TMatrixByte}
     81{$DEFINE TGMatrixSortCompare := TMatrixByteSortCompare}
     82{$DEFINE TGMatrixToStringConverter := TMatrixByteToStringConverter}
     83{$DEFINE TGMatrixFromStringConverter := TMatrixByteFromStringConverter}
     84{$DEFINE TGMatrixMerge := TMatrixByteMerge}
    5485{$DEFINE IMPLEMENTATION}
    5586{$I 'GenericMatrix.inc'}
     
    6596{$DEFINE TGMatrixToStringConverter := TMatrixObjectToStringConverter}
    6697{$DEFINE TGMatrixFromStringConverter := TMatrixObjectFromStringConverter}
     98{$DEFINE TGMatrixMerge := TMatrixStringMerge}
    6799{$DEFINE IMPLEMENTATION}
    68100{$I 'GenericMatrix.inc'}
Note: See TracChangeset for help on using the changeset viewer.