Changeset 39 for trunk/Demos/Generics
- Timestamp:
- Feb 25, 2012, 7:41:42 PM (13 years ago)
- Location:
- trunk/Demos/Generics
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Demos/Generics/Generics.pas
r21 r39 5 5 6 6 type 7 TListInteger = T GList<Integer,Integer>;7 TListInteger = TList<Integer>; 8 8 9 9 var 10 List: TList Integer;10 List: TList<Integer>; 11 11 I: Integer; 12 12 begin 13 13 try 14 List := TList Integer.Create;14 List := TList<Integer>.Create; 15 15 List.Add(100); 16 16 finally -
trunk/Demos/Generics/List.pas
r21 r39 4 4 5 5 type 6 TGList<TGListIndex, TGListItem> = class 7 private 8 FItems: array of TGListItem; 9 FCount: TGListIndex; 10 function Get(Index: TGListIndex): TGListItem; 11 function GetCapacity: TGListIndex; 12 function GetLast: TGListItem; 13 function GetFirst: TGListItem; 14 procedure SetCapacity(const AValue: TGListIndex); 15 procedure SetLast(AValue: TGListItem); 16 procedure SetFirst(AValue: TGListItem); 17 procedure Put(Index: TGListIndex; const AValue: TGListItem); virtual; 18 procedure SetCount(const AValue: TGListIndex); 19 procedure QuickSort(L, R : TGListIndex; Compare: TGListSortCompare); 6 TList<TItem, TIndex = NativeInt> = class 20 7 public 21 8 type 22 TGListSortCompare = function(const Item1, Item2: TGListItem): Integer of object; 23 TGListStringConverter = function(Item: TGListItem): string; 24 function Add(Item: TGListItem): TGListIndex; 25 procedure AddArray(Values: array of TGListItem); 26 procedure AddList(List: TGList); 27 procedure Assign(List: TGList); 9 TSortCompare = function(const Item1, Item2: TItem): Integer of object; 10 TStringConverter = function(Item: TItem): string; 11 private 12 FItems: array of TItem; 13 FCount: TIndex; 14 function Get(Index: TIndex): TItem; 15 function GetCapacity: TIndex; 16 function GetLast: TItem; 17 function GetFirst: TItem; 18 procedure SetCapacity(const AValue: TIndex); 19 procedure SetLast(AValue: TItem); 20 procedure SetFirst(AValue: TItem); 21 procedure Put(Index: TIndex; const AValue: TItem); virtual; 22 procedure SetCount(const AValue: TIndex); 23 procedure QuickSort(L, R : TIndex; Compare: TSortCompare); 24 public 25 function Add(Item: TItem): TIndex; 26 procedure AddArray(Values: array of TItem); 27 procedure AddList(List: TList); 28 procedure Assign(List: TList); 28 29 procedure Clear; virtual; 29 30 procedure Contract; 30 procedure Delete(Index: T GListIndex); virtual;31 procedure DeleteItems(Index, Count: T GListIndex);32 function Equals(List: T GList): Boolean;31 procedure Delete(Index: TIndex); virtual; 32 procedure DeleteItems(Index, Count: TIndex); 33 function Equals(List: TList): Boolean; 33 34 procedure Expand; 34 function Extract(Item: T GListItem): TGListItem;35 procedure Exchange(Index1, Index2: T GListIndex);36 property First: T GListItem read GetFirst write SetFirst;37 procedure Fill(Start, Count: T GListIndex; Value: TGListItem);38 function Implode(Separator: string; Converter: T GListStringConverter): string;39 function IndexOf(Item: T GListItem; Start: TGListIndex = 0): TGListIndex;40 function IndexOfList(List: T GList; Start: TGListIndex = 0): TGListIndex;41 procedure Insert(Index: T GListIndex; Item: TGListItem);42 procedure InsertList(Index: T GListIndex; List: TGList);43 procedure InsertArray(Index: T GListIndex; Values: array of TGListItem);44 procedure Move(CurIndex, NewIndex: T GListIndex);45 procedure MoveItems(CurIndex, NewIndex, Count: T GListIndex);46 function Remove(Item: T GListItem): TGListIndex;35 function Extract(Item: TItem): TItem; 36 procedure Exchange(Index1, Index2: TIndex); 37 property First: TItem read GetFirst write SetFirst; 38 procedure Fill(Start, Count: TIndex; Value: TItem); 39 function Implode(Separator: string; Converter: TStringConverter): string; 40 function IndexOf(Item: TItem; Start: TIndex = 0): TIndex; 41 function IndexOfList(List: TList; Start: TIndex = 0): TIndex; 42 procedure Insert(Index: TIndex; Item: TItem); 43 procedure InsertList(Index: TIndex; List: TList); 44 procedure InsertArray(Index: TIndex; Values: array of TItem); 45 procedure Move(CurIndex, NewIndex: TIndex); 46 procedure MoveItems(CurIndex, NewIndex, Count: TIndex); 47 function Remove(Item: TItem): TIndex; 47 48 procedure Reverse; 48 procedure Sort(Compare: T GListSortCompare);49 procedure SetArray(Values: array of T GListItem);50 property Count: T GListIndex read FCount write SetCount;51 property Capacity: T GListIndex read GetCapacity write SetCapacity;52 property Items[Index: T GListIndex]: TGListItem read Get write Put; default;53 property Last: T GListItem read GetLast write SetLast;49 procedure Sort(Compare: TSortCompare); 50 procedure SetArray(Values: array of TItem); 51 property Count: TIndex read FCount write SetCount; 52 property Capacity: TIndex read GetCapacity write SetCapacity; 53 property Items[Index: TIndex]: TItem read Get write Put; default; 54 property Last: TItem read GetLast write SetLast; 54 55 end; 55 56 56 57 implementation 57 58 58 function T GList.GetCapacity: TGListIndex;59 function TList.GetCapacity: TIndex; 59 60 begin 60 61 Result := Length(FItems); 61 62 end; 62 63 63 procedure T GList.SetCapacity(const AValue: TGListIndex);64 procedure TList.SetCapacity(const AValue: TIndex); 64 65 begin 65 66 SetLength(FItems, AValue); 66 67 end; 67 68 68 function T GList.Get(Index: TGListIndex): TGListItem;69 function TList.Get(Index: TIndex): TItem; 69 70 begin 70 71 Result := FItems[Index]; 71 72 end; 72 73 73 procedure T GList.Put(Index: TGListIndex; const AValue: TGListItem);74 procedure TList.Put(Index: TIndex; const AValue: TItem); 74 75 begin 75 76 FItems[Index] := AValue; 76 77 end; 77 78 78 procedure T GList.SetCount(const AValue: TGListIndex);79 procedure TList.SetCount(const AValue: TIndex); 79 80 begin 80 81 SetLength(FItems, AValue); … … 82 83 end; 83 84 84 procedure T GList.QuickSort(L, R: TGListIndex; Compare: TGListSortCompare);85 var 86 I, J: T GListIndex;87 P, Q: T GListItem;85 procedure TList.QuickSort(L, R: TIndex; Compare: TSortCompare); 86 var 87 I, J: TIndex; 88 P, Q: TItem; 88 89 begin 89 90 repeat … … 111 112 end; 112 113 113 procedure T GList.Assign(List: TGList);114 procedure TList.Assign(List: TList); 114 115 var 115 116 I: Integer; … … 123 124 end; 124 125 125 procedure T GList.Expand;126 var 127 IncSize: T GListIndex;126 procedure TList.Expand; 127 var 128 IncSize: TIndex; 128 129 begin 129 130 if FCount = Capacity then begin … … 136 137 end; 137 138 138 procedure T GList.Contract;139 procedure TList.Contract; 139 140 begin 140 141 if (Capacity > 256) and (FCount < Capacity shr 2) then … … 144 145 end; 145 146 146 function T GList.Extract(Item: TGListItem): TGListItem;147 var 148 I: T GListIndex;147 function TList.Extract(Item: TItem): TItem; 148 var 149 I: TIndex; 149 150 begin 150 151 I := IndexOf(Item); … … 156 157 end; 157 158 158 function T GList.IndexOf(Item: TGListItem; Start: TGListIndex): TGListIndex;159 function TList.IndexOf(Item: TItem; Start: TIndex): TIndex; 159 160 begin 160 161 Result := Start; 161 162 while (Result < FCount) and 162 not CompareMem(Addr(FItems[Result]), Addr(Item), SizeOf(T GListItem)) do163 not CompareMem(Addr(FItems[Result]), Addr(Item), SizeOf(TItem)) do 163 164 Result := Result + 1; 164 165 if Result = FCount then Result := -1; 165 166 end; 166 167 167 procedure T GList.Insert(Index: TGListIndex; Item: TGListItem);168 procedure TList.Insert(Index: TIndex; Item: TItem); 168 169 begin 169 170 if (Index < 0) or (Index > FCount ) then … … 171 172 if FCount = Capacity then Expand; 172 173 if Index < FCount then 173 System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(T GListItem));174 System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TItem)); 174 175 FItems[Index] := Item; 175 176 FCount := FCount + 1; 176 177 end; 177 178 178 procedure T GList.InsertList(Index: TGListIndex; List: TGList);179 var 180 I: T GListIndex;179 procedure TList.InsertList(Index: TIndex; List: TList); 180 var 181 I: TIndex; 181 182 begin 182 183 I := 0; … … 187 188 end; 188 189 189 function T GList.IndexOfList(List: TGList; Start: TGListIndex): TGListIndex;190 var 191 I: T GListIndex;190 function TList.IndexOfList(List: TList; Start: TIndex): TIndex; 191 var 192 I: TIndex; 192 193 begin 193 194 if List.Count > 0 then begin … … 196 197 I := 1; 197 198 while I < List.Count do begin 198 if not CompareMem(Addr(FItems[Result + I]), Addr(List.FItems[I]), SizeOf(T GListItem)) then begin199 if not CompareMem(Addr(FItems[Result + I]), Addr(List.FItems[I]), SizeOf(TItem)) then begin 199 200 Result := -1; 200 201 Break; … … 206 207 end; 207 208 208 function T GList.GetLast: TGListItem;209 function TList.GetLast: TItem; 209 210 begin 210 211 if FCount = 0 then … … 214 215 end; 215 216 216 procedure T GList.SetLast(AValue: TGListItem);217 procedure TList.SetLast(AValue: TItem); 217 218 begin 218 219 if FCount = 0 then … … 222 223 end; 223 224 224 function T GList.GetFirst: TGListItem;225 function TList.GetFirst: TItem; 225 226 begin 226 227 if FCount = 0 then … … 230 231 end; 231 232 232 procedure T GList.SetFirst(AValue: TGListItem);233 procedure TList.SetFirst(AValue: TItem); 233 234 begin 234 235 if FCount = 0 then … … 238 239 end; 239 240 240 procedure T GList.Move(CurIndex, NewIndex: TGListIndex);241 var 242 Temp: T GListItem;241 procedure TList.Move(CurIndex, NewIndex: TIndex); 242 var 243 Temp: TItem; 243 244 begin 244 245 if ((CurIndex < 0) or (CurIndex > Count - 1)) then … … 248 249 Temp := FItems[CurIndex]; 249 250 if NewIndex > CurIndex then begin 250 System.Move(FItems[CurIndex + 1], FItems[CurIndex], (NewIndex - CurIndex) * SizeOf(T GListItem));251 System.Move(FItems[CurIndex + 1], FItems[CurIndex], (NewIndex - CurIndex) * SizeOf(TItem)); 251 252 end else 252 253 if NewIndex < CurIndex then begin 253 System.Move(FItems[NewIndex], FItems[NewIndex + 1], (CurIndex - NewIndex) * SizeOf(T GListItem));254 System.Move(FItems[NewIndex], FItems[NewIndex + 1], (CurIndex - NewIndex) * SizeOf(TItem)); 254 255 end; 255 256 FItems[NewIndex] := Temp; … … 258 259 end; 259 260 260 procedure T GList.MoveItems(CurIndex, NewIndex, Count: TGListIndex);261 procedure TList.MoveItems(CurIndex, NewIndex, Count: TIndex); 261 262 var 262 263 S: Integer; … … 283 284 end; 284 285 285 function T GList.Remove(Item: TGListItem): TGListIndex;286 function TList.Remove(Item: TItem): TIndex; 286 287 begin 287 288 Result := IndexOf(Item); … … 290 291 end; 291 292 292 function T GList.Equals(List: TGList): Boolean;293 var 294 I: T GListIndex;293 function TList.Equals(List: TList): Boolean; 294 var 295 I: TIndex; 295 296 begin 296 297 Result := Count = List.Count; … … 298 299 I := 0; 299 300 while I < Count do begin 300 if not CompareMem(Addr(FItems[I]), Addr(List.FItems[I]), SizeOf(T GListItem)) then begin301 if not CompareMem(Addr(FItems[I]), Addr(List.FItems[I]), SizeOf(TItem)) then begin 301 302 Result := False; 302 303 Break; … … 307 308 end; 308 309 309 procedure T GList.Reverse;310 var 311 I: T GListIndex;310 procedure TList.Reverse; 311 var 312 I: TIndex; 312 313 begin 313 314 I := 0; … … 318 319 end; 319 320 320 procedure T GList.Sort(Compare: TGListSortCompare);321 procedure TList.Sort(Compare: TSortCompare); 321 322 begin 322 323 if FCount > 1 then … … 324 325 end; 325 326 326 procedure T GList.AddArray(Values: array of TGListItem);327 var 328 I: T GListIndex;327 procedure TList.AddArray(Values: array of TItem); 328 var 329 I: TIndex; 329 330 begin 330 331 I := 0; … … 335 336 end; 336 337 337 procedure T GList.SetArray(Values: array of TGListItem);338 var 339 I: T GListIndex;338 procedure TList.SetArray(Values: array of TItem); 339 var 340 I: TIndex; 340 341 begin 341 342 Clear; … … 347 348 end; 348 349 349 procedure T GList.InsertArray(Index: TGListIndex; Values: array of TGListItem);350 var 351 I: T GListIndex;350 procedure TList.InsertArray(Index: TIndex; Values: array of TItem); 351 var 352 I: TIndex; 352 353 begin 353 354 I := 0; … … 358 359 end; 359 360 360 function T GList.Implode(Separator: string; Converter: TGListStringConverter): string;361 var 362 I: T GListIndex;361 function TList.Implode(Separator: string; Converter: TStringConverter): string; 362 var 363 I: TIndex; 363 364 begin 364 365 Result := ''; … … 372 373 end; 373 374 374 function T GList.Add(Item: TGListItem): TGListIndex;375 function TList.Add(Item: TItem): TIndex; 375 376 begin 376 377 if FCount = Capacity then … … 381 382 end; 382 383 383 procedure T GList.AddList(List: TGList);384 var 385 I: T GListIndex;384 procedure TList.AddList(List: TList); 385 var 386 I: TIndex; 386 387 begin 387 388 I := 0; … … 392 393 end; 393 394 394 procedure T GList.Clear;395 procedure TList.Clear; 395 396 begin 396 397 Count := 0; … … 398 399 end; 399 400 400 procedure T GList.Delete(Index: TGListIndex);401 procedure TList.Delete(Index: TIndex); 401 402 begin 402 403 if (Index < 0) or (Index >= FCount) then 403 404 raise EListError.CreateFmt(SListIndexError, [Index]); 404 405 FCount := FCount - 1; 405 System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(T GListItem));406 System.Move(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TItem)); 406 407 Contract; 407 408 end; 408 409 409 procedure T GList.DeleteItems(Index, Count: TGListIndex);410 var 411 I: T GListIndex;410 procedure TList.DeleteItems(Index, Count: TIndex); 411 var 412 I: TIndex; 412 413 begin 413 414 I := Index; … … 418 419 end; 419 420 420 procedure T GList.Fill(Start, Count: TGListIndex; Value: TGListItem);421 procedure TList.Fill(Start, Count: TIndex; Value: TItem); 421 422 begin 422 423 while Count > 0 do begin … … 427 428 end; 428 429 429 procedure T GList.Exchange(Index1, Index2: TGListIndex);430 var 431 Temp: T GListItem;430 procedure TList.Exchange(Index1, Index2: TIndex); 431 var 432 Temp: TItem; 432 433 begin 433 434 if ((Index1 >= FCount) or (Index1 < 0)) then
Note:
See TracChangeset
for help on using the changeset viewer.