Ignore:
Timestamp:
Jul 20, 2018, 10:25:06 AM (6 years ago)
Author:
chronos
Message:
Location:
trunk
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk

    • Property svn:ignore
      •  

        old new  
        66compiled
        77heaptrclog.trc
         8LazFuck.dbg
  • trunk/Packages/TemplateGenerics/Generic/GenericMatrix.inc

    r72 r93  
     1// Work in progress...
     2
    13{$IFDEF INTERFACE}
    2 
    3   TGMatrix = class;
    4 
    5   TGMatrixSortCompare = function(const Item1, Item2: TGMatrixItem): Integer of object;
    6   TGMatrixToStringConverter = function(Item: TGMatrixItem): string;
    7   TGMatrixFromStringConverter = function(Text: string): TGMatrixItem;
    8   TGMatrixRow = array of TGMatrixItem;
    9   TGMatrixMerge = function(Item1, Item2: TGMatrixItem): TGMatrixItem of object;
    10 
    11   TGMatrixIndex = record
    12     X: TGMatrixIndexX;
    13     Y: TGMatrixIndexY;
    14   end;
    154
    165  // TGMatrix<TGMatrixIndex, TGMatrixIndex, TGMatrixItem> = class
    176  TGMatrix = class
     7  public
     8    type
     9      TSortCompare = function(const Item1, Item2: TGMatrixItem): Integer of object;
     10      TToStringConverter = function(Item: TGMatrixItem): string;
     11      TFromStringConverter = function(Text: string): TGMatrixItem;
     12      TRow = array of TGMatrixItem;
     13      TMerge = function(Item1, Item2: TGMatrixItem): TGMatrixItem of object;
     14
     15      TIndex = record
     16        X: TGMatrixIndexX;
     17        Y: TGMatrixIndexY;
     18      end;
    1819  private
    1920    FItems: array of array of TGMatrixItem;
    20     FCount: TGMatrixIndex;
     21    FCount: TIndex;
    2122    function GetItemXY(X: TGMatrixIndexX; Y: TGMatrixIndexY): TGMatrixItem;
    22     function GetItem(Index: TGMatrixIndex): TGMatrixItem;
    23     function GetCapacity: TGMatrixIndex;
     23    function GetItem(Index: TIndex): TGMatrixItem;
     24    function GetCapacity: TIndex;
    2425    function GetLast: TGMatrixItem;
    2526    function GetFirst: TGMatrixItem;
    26     procedure SetCapacity(const AValue: TGMatrixIndex);
     27    procedure SetCapacity(const AValue: TIndex);
    2728    procedure SetLast(AValue: TGMatrixItem);
    2829    procedure SetFirst(AValue: TGMatrixItem);
    2930    procedure PutItemXY(X: TGMatrixIndexX; Y: TGMatrixIndexY; const AValue: TGMatrixItem); virtual;
    30     procedure PutItem(Index: TGMatrixIndex; const AValue: TGMatrixItem); virtual;
    31     procedure SetCount(const AValue: TGMatrixIndex);
     31    procedure PutItem(Index: TIndex; const AValue: TGMatrixItem); virtual;
     32    procedure SetCount(const AValue: TIndex);
    3233  public
    33     function Add(Item: TGMatrixItem): TGMatrixIndex;
    34     procedure AddMatrix(Values: array of TGMatrixRow);
     34    function Add(Item: TGMatrixItem): TIndex;
     35    procedure AddMatrix(Values: array of TRow);
    3536    procedure AddList(List: TGMatrix);
    3637    procedure Assign(Source: TGMatrix);
    3738    procedure Clear; virtual;
    3839    procedure Contract;
    39     function CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TGMatrixIndex;
    40     procedure Delete(Index: TGMatrixIndex); virtual;
    41     procedure DeleteItems(Index, Count: TGMatrixIndex);
     40    function CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TIndex;
     41    procedure Delete(Index: TIndex); virtual;
     42    procedure DeleteItems(Index, Count: TIndex);
    4243    function EqualTo(List: TGMatrix): Boolean;
    4344    procedure Expand;
    4445    function Extract(Item: TGMatrixItem): TGMatrixItem;
    45     procedure Exchange(Index1, Index2: TGMatrixIndex);
     46    procedure Exchange(Index1, Index2: TIndex);
    4647    property First: TGMatrixItem read GetFirst write SetFirst;
    4748    procedure FillAll(Value: TGMatrixItem);
    48     procedure Fill(Start, Count: TGMatrixIndex; Value: TGMatrixItem);
    49     function Implode(RowSeparator, ColSeparator: string; Converter: TGMatrixToStringConverter): string;
    50     procedure Explode(Text, Separator: string; Converter: TGMatrixFromStringConverter; SlicesCount: Integer = -1);
    51     function IndexOf(Item: TGMatrixItem; Start: TGMatrixIndex): TGMatrixIndex;
    52     function IndexOfList(List: TGMatrix; Start: TGMatrixIndex): TGMatrixIndex;
    53     procedure Insert(Index: TGMatrixIndex; Item: TGMatrixItem);
    54     procedure InsertList(Index: TGMatrixIndex; List: TGMatrix);
    55     procedure InsertArray(Index: TGMatrixIndex; Values: array of TGMatrixItem);
    56     procedure Move(CurIndex, NewIndex: TGMatrixIndex);
    57     procedure MoveItems(CurIndex, NewIndex, Count: TGMatrixIndex);
    58     procedure Merge(Index: TGMatrixIndex; Source: TGMatrix; Proc: TGMatrixMerge);
    59     procedure Replace(Index: TGMatrixIndex; Source: TGMatrix);
    60     function Remove(Item: TGMatrixItem): TGMatrixIndex;
     49    procedure Fill(Start, Count: TIndex; Value: TGMatrixItem);
     50    function Implode(RowSeparator, ColSeparator: string; Converter: TToStringConverter): string;
     51    procedure Explode(Text, Separator: string; Converter: TFromStringConverter; SlicesCount: Integer = -1);
     52    function IndexOf(Item: TGMatrixItem; Start: TIndex): TIndex;
     53    function IndexOfList(List: TGMatrix; Start: TIndex): TIndex;
     54    procedure Insert(Index: TIndex; Item: TGMatrixItem);
     55    procedure InsertList(Index: TIndex; List: TGMatrix);
     56    procedure InsertArray(Index: TIndex; Values: array of TGMatrixItem);
     57    procedure Move(CurIndex, NewIndex: TIndex);
     58    procedure MoveItems(CurIndex, NewIndex, Count: TIndex);
     59    procedure Merge(Index: TIndex; Source: TGMatrix; Proc: TMerge);
     60    procedure Replace(Index: TIndex; Source: TGMatrix);
     61    function Remove(Item: TGMatrixItem): TIndex;
    6162    procedure Reverse;
    6263    procedure ReverseHorizontal;
    6364    procedure ReverseVertical;
    64     procedure Sort(Compare: TGMatrixSortCompare);
     65    procedure Sort(Compare: TSortCompare);
    6566    procedure SetArray(Values: array of TGMatrixItem);
    66     property Count: TGMatrixIndex read FCount write SetCount;
    67     property Capacity: TGMatrixIndex read GetCapacity write SetCapacity;
     67    property Count: TIndex read FCount write SetCount;
     68    property Capacity: TIndex read GetCapacity write SetCapacity;
    6869    property ItemsXY[X: TGMatrixIndexX; Y: TGMatrixIndexY]: TGMatrixItem
    6970      read GetItemXY write PutItemXY; default;
    70     property Items[Index: TGMatrixIndex]: TGMatrixItem
     71    property Items[Index: TIndex]: TGMatrixItem
    7172      read GetItem write PutItem;
    7273    property Last: TGMatrixItem read GetLast write SetLast;
     
    9192{ TGMatrix }
    9293
    93 procedure TGMatrix.Replace(Index: TGMatrixIndex; Source: TGMatrix);
     94procedure TGMatrix.Replace(Index: TIndex; Source: TGMatrix);
    9495var
    9596  X: TGMatrixIndexX;
     
    107108end;
    108109
    109 procedure TGMatrix.Merge(Index: TGMatrixIndex; Source: TGMatrix; Proc: TGMatrixMerge);
     110procedure TGMatrix.Merge(Index: TIndex; Source: TGMatrix; Proc: TMerge);
    110111var
    111112  X: TGMatrixIndexX;
     
    123124end;
    124125
    125 function TGMatrix.CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TGMatrixIndex;
     126function TGMatrix.CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TIndex;
    126127begin
    127128  Result.X := X;
     
    129130end;
    130131
    131 function TGMatrix.GetCapacity: TGMatrixIndex;
     132function TGMatrix.GetCapacity: TIndex;
    132133begin
    133134  Result.Y := Length(FItems);
     
    135136end;
    136137
    137 procedure TGMatrix.SetCapacity(const AValue: TGMatrixIndex);
     138procedure TGMatrix.SetCapacity(const AValue: TIndex);
    138139var
    139140  Y: TGMatrixIndexY;
     
    160161end;
    161162
    162 function TGMatrix.GetItem(Index: TGMatrixIndex): TGMatrixItem;
     163function TGMatrix.GetItem(Index: TIndex): TGMatrixItem;
    163164begin
    164165  if (Index.X < 0) or (Index.X >= Count.X) or
     
    176177end;
    177178
    178 procedure TGMatrix.PutItem(Index: TGMatrixIndex; const AValue: TGMatrixItem);
     179procedure TGMatrix.PutItem(Index: TIndex; const AValue: TGMatrixItem);
    179180begin
    180181  if (Index.X < 0) or (Index.X >= Count.X) or
     
    184185end;
    185186
    186 procedure TGMatrix.SetCount(const AValue: TGMatrixIndex);
     187procedure TGMatrix.SetCount(const AValue: TIndex);
    187188begin
    188189  Capacity := AValue;
     
    192193procedure TGMatrix.Assign(Source: TGMatrix);
    193194var
    194   Index: TGMatrixIndex;
     195  Index: TIndex;
    195196begin
    196197  Count := Source.Count;
     
    208209procedure TGMatrix.Expand;
    209210var
    210   IncSize: TGMatrixIndex;
    211   NewCapacity: TGMatrixIndex;
     211  IncSize: TIndex;
     212  NewCapacity: TIndex;
    212213begin
    213214  if (FCount.X = Capacity.X) then begin
     
    230231procedure TGMatrix.Contract;
    231232var
    232   NewCapacity: TGMatrixIndex;
     233  NewCapacity: TIndex;
    233234begin
    234235  if (Capacity.X > 256) and (FCount.X < Capacity.X shr 2) then
     
    245246function TGMatrix.Extract(Item: TGMatrixItem): TGMatrixItem;
    246247var
    247   I: TGMatrixIndex;
     248  I: TIndex;
    248249begin
    249250(*  I := IndexOf(Item);
     
    256257end;
    257258
    258 function TGMatrix.IndexOf(Item: TGMatrixItem; Start: TGMatrixIndex): TGMatrixIndex;
     259function TGMatrix.IndexOf(Item: TGMatrixItem; Start: TIndex): TIndex;
    259260begin
    260261(*  Result := Start;
     
    266267end;
    267268
    268 procedure TGMatrix.Insert(Index: TGMatrixIndex; Item: TGMatrixItem);
     269procedure TGMatrix.Insert(Index: TIndex; Item: TGMatrixItem);
    269270begin
    270271(*  if (Index < 0) or (Index > FCount ) then
     
    278279end;
    279280
    280 procedure TGMatrix.InsertList(Index: TGMatrixIndex; List: TGMatrix);
    281 var
    282   I: TGMatrixIndex;
     281procedure TGMatrix.InsertList(Index: TIndex; List: TGMatrix);
     282var
     283  I: TIndex;
    283284begin
    284285(*  I := 0;
     
    290291end;
    291292
    292 function TGMatrix.IndexOfList(List: TGMatrix; Start: TGMatrixIndex): TGMatrixIndex;
    293 var
    294   I: TGMatrixIndex;
     293function TGMatrix.IndexOfList(List: TGMatrix; Start: TIndex): TIndex;
     294var
     295  I: TIndex;
    295296begin
    296297(*  if List.Count > 0 then begin
     
    346347end;
    347348
    348 procedure TGMatrix.Move(CurIndex, NewIndex: TGMatrixIndex);
     349procedure TGMatrix.Move(CurIndex, NewIndex: TIndex);
    349350var
    350351  Temp: TGMatrixItem;
     
    366367end;
    367368
    368 procedure TGMatrix.MoveItems(CurIndex, NewIndex, Count: TGMatrixIndex);
     369procedure TGMatrix.MoveItems(CurIndex, NewIndex, Count: TIndex);
    369370var
    370371  S: Integer;
     
    391392end;
    392393
    393 function TGMatrix.Remove(Item: TGMatrixItem): TGMatrixIndex;
     394function TGMatrix.Remove(Item: TGMatrixItem): TIndex;
    394395begin
    395396(*  Result := IndexOf(Item);
     
    400401function TGMatrix.EqualTo(List: TGMatrix): Boolean;
    401402var
    402   I: TGMatrixIndex;
     403  I: TIndex;
    403404begin
    404405(*  Result := Count = List.Count;
     
    463464end;
    464465
    465 procedure TGMatrix.Sort(Compare: TGMatrixSortCompare);
     466procedure TGMatrix.Sort(Compare: TSortCompare);
    466467begin
    467468(*  if FCount > 1 then
     
    469470end;
    470471
    471 procedure TGMatrix.AddMatrix(Values: array of TGMatrixRow);
    472 var
    473   I: TGMatrixIndex;
     472procedure TGMatrix.AddMatrix(Values: array of TRow);
     473var
     474  I: TIndex;
    474475begin
    475476(*  I := 0;
     
    482483procedure TGMatrix.SetArray(Values: array of TGMatrixItem);
    483484var
    484   I: TGMatrixIndex;
     485  I: TIndex;
    485486begin
    486487(*  Clear;
     
    492493end;
    493494
    494 procedure TGMatrix.InsertArray(Index: TGMatrixIndex; Values: array of TGMatrixItem);
    495 var
    496   I: TGMatrixIndex;
     495procedure TGMatrix.InsertArray(Index: TIndex; Values: array of TGMatrixItem);
     496var
     497  I: TIndex;
    497498begin
    498499(*  I := 0;
     
    503504end;
    504505
    505 function TGMatrix.Implode(RowSeparator, ColSeparator: string; Converter: TGMatrixToStringConverter): string;
     506function TGMatrix.Implode(RowSeparator, ColSeparator: string; Converter: TToStringConverter): string;
    506507var
    507508  Y: TGMatrixIndexY;
     
    524525end;
    525526
    526 procedure TGMatrix.Explode(Text, Separator: string; Converter: TGMatrixFromStringConverter; SlicesCount: Integer = -1);
     527procedure TGMatrix.Explode(Text, Separator: string; Converter: TFromStringConverter; SlicesCount: Integer = -1);
    527528begin
    528529(*  Clear;
     
    535536end;
    536537
    537 function TGMatrix.Add(Item: TGMatrixItem): TGMatrixIndex;
     538function TGMatrix.Add(Item: TGMatrixItem): TIndex;
    538539begin
    539540(*  if FCount = Capacity then
     
    546547procedure TGMatrix.AddList(List: TGMatrix);
    547548var
    548   I: TGMatrixIndex;
     549  I: TIndex;
    549550begin
    550551(*  I := 0;
     
    561562end;
    562563
    563 procedure TGMatrix.Delete(Index: TGMatrixIndex);
     564procedure TGMatrix.Delete(Index: TIndex);
    564565begin
    565566(*  if (Index < 0) or (Index >= FCount) then
     
    571572end;
    572573
    573 procedure TGMatrix.DeleteItems(Index, Count: TGMatrixIndex);
    574 var
    575   I: TGMatrixIndex;
     574procedure TGMatrix.DeleteItems(Index, Count: TIndex);
     575var
     576  I: TIndex;
    576577begin
    577578(*  I := Index;
     
    583584end;
    584585
    585 procedure TGMatrix.Fill(Start, Count: TGMatrixIndex; Value: TGMatrixItem);
     586procedure TGMatrix.Fill(Start, Count: TIndex; Value: TGMatrixItem);
    586587var
    587588  X: TGMatrixIndexX;
     
    604605end;
    605606
    606 procedure TGMatrix.Exchange(Index1, Index2: TGMatrixIndex);
     607procedure TGMatrix.Exchange(Index1, Index2: TIndex);
    607608var
    608609  Temp: TGMatrixItem;
Note: See TracChangeset for help on using the changeset viewer.