Changeset 175 for Generics/TemplateGenerics/Generic
- Timestamp:
- Mar 6, 2011, 6:44:23 PM (14 years ago)
- Location:
- Generics/TemplateGenerics/Generic
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/Generic
-
Property svn:ignore
set to
backup
-
Property svn:ignore
set to
-
Generics/TemplateGenerics/Generic/GenericList.inc
r129 r175 49 49 function Remove(Item: TGListItem): TGListIndex; 50 50 procedure Reverse; 51 procedure Replace(Index: TGListIndex; Source: TGList); 51 52 procedure Sort(Compare: TGListSortCompare); 52 53 procedure SetArray(Values: TGListItemArray); … … 71 72 72 73 { TGList } 74 75 procedure TGList.Replace(Index: TGListIndex; Source: TGList); 76 var 77 I: TGListIndex; 78 begin 79 I := 0; 80 while I < Source.Count do begin 81 Items[Index + I] := Source[I]; 82 I := I + 1; 83 end; 84 end; 73 85 74 86 function TGList.GetCapacity: TGListIndex; -
Generics/TemplateGenerics/Generic/GenericMatrix.inc
r109 r175 7 7 TGMatrixFromStringConverter = function(Text: string): TGMatrixItem; 8 8 TGMatrixRow = array of TGMatrixItem; 9 TGMatrixMerge = function(Item1, Item2: TGMatrixItem): TGMatrixItem of object; 9 10 10 11 TGMatrixIndex = record … … 18 19 FItems: array of array of TGMatrixItem; 19 20 FCount: TGMatrixIndex; 20 function Get(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem; 21 function GetXY(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem; 22 function Get(Index: TGMatrixIndex): TGMatrixItem; 21 23 function GetCapacity: TGMatrixIndex; 22 24 function GetLast: TGMatrixItem; … … 25 27 procedure SetLast(AValue: TGMatrixItem); 26 28 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; 28 31 procedure SetCount(const AValue: TGMatrixIndex); 29 32 public … … 34 37 procedure Clear; virtual; 35 38 procedure Contract; 36 function CreateIndex( Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixIndex;39 function CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TGMatrixIndex; 37 40 procedure Delete(Index: TGMatrixIndex); virtual; 38 41 procedure DeleteItems(Index, Count: TGMatrixIndex); … … 52 55 procedure Move(CurIndex, NewIndex: TGMatrixIndex); 53 56 procedure MoveItems(CurIndex, NewIndex, Count: TGMatrixIndex); 57 procedure Merge(Index: TGMatrixIndex; Source: TGMatrix; Proc: TGMatrixMerge); 58 procedure Replace(Index: TGMatrixIndex; Source: TGMatrix); 54 59 function Remove(Item: TGMatrixItem): TGMatrixIndex; 55 60 procedure Reverse; 61 procedure ReverseHorizontal; 62 procedure ReverseVertical; 56 63 procedure Sort(Compare: TGMatrixSortCompare); 57 64 procedure SetArray(Values: array of TGMatrixItem); 58 65 property Count: TGMatrixIndex read FCount write SetCount; 59 66 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; 61 69 property Last: TGMatrixItem read GetLast write SetLast; 62 70 end; … … 71 79 72 80 resourcestring 73 SMatrixIndexError = 'Matrix index error [ %d,%d]';81 SMatrixIndexError = 'Matrix index error [X: %d, Y: %d]'; 74 82 75 83 {$UNDEF IMPLEMENTATION_USES} … … 80 88 { TGMatrix } 81 89 82 function TGMatrix.CreateIndex(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixIndex; 90 procedure TGMatrix.Replace(Index: TGMatrixIndex; Source: TGMatrix); 91 var 92 X: TGMatrixIndexX; 93 Y: TGMatrixIndexY; 94 begin 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; 104 end; 105 106 procedure TGMatrix.Merge(Index: TGMatrixIndex; Source: TGMatrix; Proc: TGMatrixMerge); 107 var 108 X: TGMatrixIndexX; 109 Y: TGMatrixIndexY; 110 begin 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; 120 end; 121 122 function TGMatrix.CreateIndex(X: TGMatrixIndexY; Y: TGMatrixIndexX): TGMatrixIndex; 83 123 begin 84 124 Result.X := X; … … 97 137 begin 98 138 if (Capacity.X <> AValue.X) and (Capacity.Y <> AValue.Y) then begin 99 SetLength(FItems, AValue.Y);139 (* SetLength(FItems, AValue.Y); 100 140 Y := 0; 101 141 while Y < Length(FItems) do begin … … 104 144 end; 105 145 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; 149 end; 150 151 function TGMatrix.GetXY(Y: TGMatrixIndexY; X: TGMatrixIndexX): TGMatrixItem; 152 begin 153 if (X < 0) or (X >= Count.X) or 154 (Y < 0) or (Y >= Count.Y) then 155 raise EListError.CreateFmt(SMatrixIndexError, [X, Y]); 110 156 Result := FItems[Y, X]; 111 157 end; 112 158 113 procedure TGMatrix.Put(Y: TGMatrixIndexY; X: TGMatrixIndexX; const AValue: TGMatrixItem); 114 begin 159 function TGMatrix.Get(Index: TGMatrixIndex): TGMatrixItem; 160 begin 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]; 165 end; 166 167 procedure TGMatrix.PutXY(Y: TGMatrixIndexY; X: TGMatrixIndexX; const AValue: TGMatrixItem); 168 begin 169 if (X < 0) or (X >= Count.X) or 170 (Y < 0) or (Y >= Count.Y) then 171 raise EListError.CreateFmt(SMatrixIndexError, [X, Y]); 115 172 FItems[Y, X] := AValue; 116 173 end; 117 174 175 procedure TGMatrix.Put(Index: TGMatrixIndex; const AValue: TGMatrixItem); 176 begin 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; 181 end; 182 118 183 procedure TGMatrix.SetCount(const AValue: TGMatrixIndex); 119 184 begin 120 Capacity := AValue; ;185 Capacity := AValue; 121 186 FCount := AValue; 122 187 end; … … 124 189 procedure TGMatrix.Assign(Source: TGMatrix); 125 190 var 126 X: TGMatrixIndexX; 127 Y: TGMatrixIndexY; 191 Index: TGMatrixIndex; 128 192 begin 129 193 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; 137 202 end; 138 203 end; … … 349 414 procedure TGMatrix.Reverse; 350 415 var 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; 418 begin 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; 428 end; 429 430 procedure TGMatrix.ReverseHorizontal; 431 var 432 X: TGMatrixIndexX; 433 Y: TGMatrixIndexY; 434 begin 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; 444 end; 445 446 procedure TGMatrix.ReverseVertical; 447 var 448 X: TGMatrixIndexX; 449 Y: TGMatrixIndexY; 450 begin 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; 358 460 end; 359 461 … … 408 510 X := 0; 409 511 while X < Count.X do begin 410 Result := Result + Converter(Items [Y, X]);512 Result := Result + Converter(ItemsXY[Y, X]); 411 513 if X < (Count.X - 1) then 412 514 Result := Result + ColSeparator; … … 487 589 X := Start.X; 488 590 while X < Count.X do begin 489 Items [Y, X] := Value;591 ItemsXY[Y, X] := Value; 490 592 X := X + 1; 491 593 end; … … 498 600 Temp: TGMatrixItem; 499 601 begin 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]); 504 608 Temp := FItems[Index1.Y, Index1.X]; 505 FItems[Index1.Y, Index1.X] := FItems[Index2.Y, Index 1.X];609 FItems[Index1.Y, Index1.X] := FItems[Index2.Y, Index2.X]; 506 610 FItems[Index2.Y, Index2.X] := Temp; 507 611 end;
Note:
See TracChangeset
for help on using the changeset viewer.