- Timestamp:
- Oct 28, 2010, 4:52:53 PM (14 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/Generic/DictionaryImplementation.tpl
r71 r72 1 uses2 RtlConsts;3 1 4 // Used instead of System.Move form because of error: Identifier "System" not found 5 procedure SystemMove(const Source; var Dest; Count: SizeInt);2 {$INCLUDE 'ListImplementation.tpl'} 3 function TGDictionary.GetKey(Index: TDictionaryIndex): TDictionaryKey; 6 4 begin 7 Move(Source, Dest, Count);5 Result := Items[Index].Key; 8 6 end; 9 7 10 { TGList } 11 12 function TGList.GetCapacity: TListIndex; 8 function TGDictionary.GetValue(Key: TDictionaryKey): TDictionaryValue; 13 9 begin 14 Result := Length(FItems);10 Result := Items[SearchKey(Key)].Value; 15 11 end; 16 12 17 procedure TGList.SetCapacity(const AValue: TListIndex); 13 procedure TGDictionary.PutKey(Index: TDictionaryIndex; 14 const AValue: TDictionaryKey); 15 var 16 Item: TGPair; 18 17 begin 19 SetLength(FItems, AValue); 18 //Items[Index].Key := AValue; 19 Item := Items[Index]; 20 Item.Key := AValue; 21 Items[Index] := Item; 20 22 end; 21 23 22 function TGList.Get(Index: TListIndex): TListItem; 24 procedure TGDictionary.PutValue(Key: TDictionaryKey; 25 const AValue: TDictionaryValue); 26 var 27 Item: TGPair; 28 Index: TDictionaryIndex; 23 29 begin 24 Result := FItems[Index]; 30 //Items[SearchKey(Index)].Value := AValue; 31 Index := SearchKey(Key); 32 Item := Items[Index]; 33 Item.Value := AValue; 34 Items[Index] := Item; 25 35 end; 26 36 27 function TG List.GetCount: TListIndex;37 function TGDictionary.SearchKey(Key: TDictionaryKey): TDictionaryIndex; 28 38 begin 29 Result := FCount; 30 end; 31 32 procedure TGList.Put(Index: TListIndex; const AValue: TListItem); 33 begin 34 FItems[Index] := AValue; 35 end; 36 37 procedure TGList.SetCount(const AValue: TListIndex); 38 begin 39 SetLength(FItems, AValue); 40 FCount := AValue; 41 end; 42 43 procedure TGList.QuickSort(L, R: TListIndex; Compare: TGListSortCompare); 44 var 45 I, J: TListIndex; 46 P, Q: TListItem; 47 begin 48 repeat 49 I := L; 50 J := R; 51 P := FItems[ (L + R) div 2 ]; 52 repeat 53 while Compare(P, FItems[I]) > 0 do 54 I := I + 1; 55 while Compare(P, FItems[J]) < 0 do 56 J := J - 1; 57 If I <= J then 58 begin 59 Q := FItems[I]; 60 FItems[I] := FItems[J]; 61 FItems[J] := Q; 62 I := I + 1; 63 J := J - 1; 64 end; 65 until I > J; 66 if L < J then 67 QuickSort(L, J, Compare); 68 L := I; 69 until I >= R; 70 end; 71 72 procedure TGList.Assign(List: TGList); 73 var 74 I: Integer; 75 begin 76 Count := List.Count; 77 I := 0; 78 while I < Count do begin 79 Items[I] := List[I]; 80 I := I + 1; 39 Result := 0; 40 while Result < Count do begin 41 if Items[Result].Key = Key then begin 42 Break; 43 end; 44 Result := Result + 1; 81 45 end; 82 46 end; 83 47 84 procedure TG List.Expand;48 procedure TGDictionary.Add(Key: TDictionaryKey; Value: TDictionaryValue); 85 49 var 86 IncSize: TListIndex;50 NewPair: TGPair; 87 51 begin 88 if FCount = Capacity then begin 89 IncSize := 4; 90 if Capacity > 3 then IncSize := IncSize + 4; 91 if Capacity > 8 then IncSize := IncSize + 8; 92 if Capacity > 63 then IncSize := IncSize + Capacity shr 2; 93 Capacity := Capacity + IncSize; 94 end; 52 NewPair.Key := Key; 53 NewPair.Value := Value; 54 inherited Add(NewPair); 95 55 end; 96 97 function TGList.Extract(Item: TListItem): TListItem;98 var99 I: TListIndex;100 begin101 I := IndexOf(Item);102 if I >= 0 then begin103 Result := Item;104 Delete(I);105 end else106 raise EListError.CreateFmt(SListIndexError, [0]);107 end;108 109 function TGList.First: TListItem;110 begin111 if FCount = 0 then112 raise EListError.CreateFmt(SListIndexError, [0])113 else114 Result := Items[0];115 end;116 117 function TGList.IndexOf(Item: TListItem): TListIndex;118 begin119 Result := 0;120 while (Result < FCount) and (FItems[Result] <> Item) do121 Result := Result + 1;122 if Result = FCount then Result := -1;123 end;124 125 procedure TGList.Insert(Index: TListIndex; Item: TListItem);126 begin127 if (Index < 0) or (Index > FCount ) then128 raise EListError.CreateFmt(SListIndexError, [Index]);129 if FCount = Capacity then Expand;130 if Index < FCount then131 SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem));132 FItems[Index] := Item;133 FCount := FCount + 1;134 end;135 136 procedure TGList.InsertList(Index: TListIndex; List: TGList);137 var138 I: TListIndex;139 begin140 I := 0;141 while (I < List.Count) do begin142 Insert(Index + I, List[I]);143 I := I + 1;144 end;145 end;146 147 function TGList.Last: TListItem;148 begin149 if FCount = 0 then150 raise EListError.CreateFmt(SListIndexError, [0])151 else152 Result := Items[FCount - 1];153 end;154 155 procedure TGList.Move(CurIndex, NewIndex: TListIndex);156 var157 Temp: TListItem;158 begin159 if ((CurIndex < 0) or (CurIndex > Count - 1)) then160 raise EListError.CreateFmt(SListIndexError, [CurIndex]);161 if ((NewIndex < 0) or (NewIndex > Count -1)) then162 raise EListError.CreateFmt(SlistIndexError, [NewIndex]);163 Temp := FItems[CurIndex];164 Delete(CurIndex);165 Insert(NewIndex, Temp);166 end;167 168 procedure TGList.MoveItems(CurIndex, NewIndex, Count: TListIndex);169 var170 S: Integer;171 D: Integer;172 begin173 if CurIndex < NewIndex then begin174 S := CurIndex + Count - 1;175 D := NewIndex + Count - 1;176 while S >= CurIndex do begin177 Move(S, D);178 S := S - 1;179 D := D - 1;180 end;181 end else182 if CurIndex > NewIndex then begin183 S := CurIndex;184 D := NewIndex;185 while S < (CurIndex + Count) do begin186 Move(S, D);187 S := S + 1;188 D := D + 1;189 end;190 end;191 end;192 193 procedure TGList.Swap(Index1, Index2: TListIndex);194 var195 Temp: TListItem;196 begin197 Temp := Items[Index1];198 Items[Index1] := Items[Index2];199 Items[Index2] := Temp;200 end;201 202 function TGList.Remove(Item: TListItem): TListIndex;203 begin204 Result := IndexOf(Item);205 if Result <> -1 then206 Delete(Result);207 end;208 209 (*function TGList.Equals(Obj: TObject): Boolean;210 var211 I: TListIndex;212 begin213 Result := Count = (Obj as TGList).Count;214 if Result then begin215 I := 0;216 while I < Count do begin217 if Items[I] <> (Obj as TGList)[I] then begin218 Result := False;219 Break;220 end;221 I := I + 1;222 end;223 end;224 end;*)225 226 procedure TGList.Reverse;227 var228 I: TListIndex;229 begin230 I := 0;231 while I < (Count div 2) do begin232 Swap(I, Count - 1 - I);233 I := I + 1;234 end;235 end;236 237 procedure TGList.Sort(Compare: TGListSortCompare);238 begin239 if FCount > 1 then240 QuickSort(0, FCount - 1, Compare);241 end;242 243 procedure TGList.SetArray(Values: array of TListItem);244 var245 I: TListIndex;246 begin247 Clear;248 I := 0;249 while I <= High(Values) do begin250 Add(Values[I]);251 I := I + 1;252 end;253 end;254 255 function TGList.Implode(Separator: string; Converter: TGListStringConverter): string;256 var257 I: TListIndex;258 begin259 Result := '';260 I := 0;261 while I < Count do begin262 Result := Result + Converter(Items[I]);263 if I < (Count - 1) then264 Result := Result + Separator;265 I := I + 1;266 end;267 end;268 269 function TGList.Add(Item: TListItem): TListIndex;270 begin271 if FCount = Capacity then272 Self.Expand;273 FItems[FCount] := Item;274 Result := FCount;275 FCount := FCount + 1;276 end;277 278 procedure TGList.AddList(List: TGList);279 var280 I: TListIndex;281 begin282 I := 0;283 while I < List.Count do begin284 Add(List[I]);285 I := I + 1;286 end;287 end;288 289 procedure TGList.Clear;290 begin291 Count := 0;292 Capacity := 0;293 end;294 295 procedure TGList.Delete(Index: TListIndex);296 begin297 if (Index < 0) or (Index >= FCount) then298 raise EListError.CreateFmt(SListIndexError, [Index]);299 FCount := FCount - 1;300 SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem));301 // Shrink the list if appropriate302 if (Capacity > 256) and (FCount < Capacity shr 2) then303 begin304 Capacity := Capacity shr 1;305 end;306 end;307 308 procedure TGList.DeleteItems(Index, Count: TListIndex);309 var310 I: TListIndex;311 begin312 I := 0;313 while I < Count do begin314 Delete(I);315 I := I + 1;316 end;317 end;318 319 procedure TGList.Exchange(Index1, Index2: TListIndex);320 var321 Temp: TListItem;322 begin323 if ((Index1 >= FCount) or (Index1 < 0)) then324 raise EListError.CreateFmt(SListIndexError, [Index1]);325 if ((Index2 >= FCount) or (Index2 < 0)) then326 raise EListError.CreateFmt(SListIndexError, [Index2]);327 Temp := FItems[Index1];328 FItems[Index1] := FItems[Index2];329 FItems[Index2] := Temp;330 end;
Note:
See TracChangeset
for help on using the changeset viewer.