Changeset 107 for Generics/TemplateGenerics/Generic/GenericList.inc
- Timestamp:
- Jan 1, 2011, 1:34:29 AM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/Generic/GenericList.inc
r101 r107 17 17 function GetFirst: TGListItem; 18 18 procedure SetCapacity(const AValue: TGListIndex); 19 procedure SetCapacityOptimized(const NewCapacity: TGListIndex); 19 20 procedure SetLast(AValue: TGListItem); 20 21 procedure SetFirst(AValue: TGListItem); 21 22 procedure Put(Index: TGListIndex; const AValue: TGListItem); virtual; 22 procedure SetCount(const AValue: TGListIndex); 23 procedure SetCount(const AValue: TGListIndex); virtual; 23 24 procedure QuickSort(L, R : TGListIndex; Compare: TGListSortCompare); 24 25 public … … 28 29 procedure Assign(Source: TGList); virtual; 29 30 procedure Clear; virtual; 30 procedure Contract;31 31 procedure Delete(Index: TGListIndex); virtual; 32 32 procedure DeleteItems(Index, Count: TGListIndex); 33 33 function EqualTo(List: TGList): Boolean; 34 procedure Expand;35 34 function Extract(Item: TGListItem): TGListItem; 36 35 procedure Exchange(Index1, Index2: TGListIndex); … … 78 77 procedure TGList.SetCapacity(const AValue: TGListIndex); 79 78 begin 79 if (AValue < FCount) then 80 raise EListError.CreateFmt(SListCapacityError, [AValue]); 80 81 SetLength(FItems, AValue); 81 82 end; 82 83 84 procedure TGList.SetCapacityOptimized(const NewCapacity: TGListIndex); 85 var 86 IncSize: TGListIndex; 87 begin 88 if NewCapacity > Capacity then begin 89 IncSize := NewCapacity - Capacity; 90 // Expand 91 if FCount = Capacity then begin 92 IncSize := 4; 93 if Capacity > 3 then IncSize := IncSize + 4; 94 if Capacity > 8 then IncSize := IncSize + 8; 95 if Capacity > 63 then IncSize := IncSize + Capacity shr 2; // Grow by one quarter 96 Capacity := Capacity + IncSize; 97 end; 98 end else 99 if NewCapacity < Capacity then begin 100 // Contract 101 if (Capacity > 256) and (FCount < Capacity shr 2) then 102 begin 103 Capacity := Capacity shr 1; 104 end; 105 end; 106 end; 107 83 108 function TGList.Get(Index: TGListIndex): TGListItem; 84 109 begin … … 93 118 procedure TGList.SetCount(const AValue: TGListIndex); 94 119 begin 95 SetLength(FItems, AValue); 120 if (AValue < 0) then 121 raise EListError.CreateFmt(SListCountError, [AValue]); 122 if AValue > Capacity then SetCapacityOptimized(AValue); // Before FCount change 96 123 FCount := AValue; 124 if AValue < Capacity then SetCapacityOptimized(AValue); // After FCount change 97 125 end; 98 126 … … 105 133 I := L; 106 134 J := R; 107 P := FItems[ (L + R) div 2];135 P := FItems[(L + R) div 2]; 108 136 repeat 109 137 while Compare(P, FItems[I]) > 0 do … … 111 139 while Compare(P, FItems[J]) < 0 do 112 140 J := J - 1; 113 If I <= J then141 if I <= J then 114 142 begin 115 143 Q := FItems[I]; … … 128 156 procedure TGList.Assign(Source: TGList); 129 157 var 130 I: Integer;158 I: TGListIndex; 131 159 begin 132 160 Count := Source.Count; … … 135 163 Items[I] := Source[I]; 136 164 I := I + 1; 137 end;138 end;139 140 procedure TGList.Expand;141 var142 IncSize: TGListIndex;143 begin144 if FCount = Capacity then begin145 IncSize := 4;146 if Capacity > 3 then IncSize := IncSize + 4;147 if Capacity > 8 then IncSize := IncSize + 8;148 if Capacity > 63 then IncSize := IncSize + Capacity shr 2;149 Capacity := Capacity + IncSize;150 end;151 end;152 153 procedure TGList.Contract;154 begin155 if (Capacity > 256) and (FCount < Capacity shr 2) then156 begin157 Capacity := Capacity shr 1;158 165 end; 159 166 end; … … 184 191 if (Index < 0) or (Index > FCount ) then 185 192 raise EListError.CreateFmt(SListIndexError, [Index]); 186 if FCount = Capacity then Expand;193 if FCount = Capacity then SetCapacityOptimized(Capacity + 1); 187 194 if Index < FCount then 188 195 System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TGListItem)); … … 400 407 function TGList.Add(Item: TGListItem): TGListIndex; 401 408 begin 402 if FCount = Capacity then 403 Self.Expand; 404 FItems[FCount] := Item; 405 Result := FCount; 406 FCount := FCount + 1; 409 Count := Count + 1; 410 Result := FCount - 1; 411 FItems[Result] := Item; 407 412 end; 408 413 … … 430 435 FCount := FCount - 1; 431 436 System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TGListItem)); 432 Contract;437 SetCapacityOptimized(Capacity - 1); 433 438 end; 434 439 … … 445 450 446 451 procedure TGList.Fill(Start, Count: TGListIndex; Value: TGListItem); 447 begin 448 while Count > 0 do begin 449 Items[Start] := Value; 450 Count := Count - 1; 451 Start := Start + 1; 452 var 453 I: TGListIndex; 454 begin 455 I := Start; 456 while I < Count do begin 457 Items[I] := Value; 458 I := I + 1; 452 459 end; 453 460 end;
Note:
See TracChangeset
for help on using the changeset viewer.