Changeset 312 for Generics/TemplateGenerics/Generic
- Timestamp:
- Jan 9, 2012, 2:22:31 PM (13 years ago)
- Location:
- Generics/TemplateGenerics/Generic
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/Generic/GenericList.inc
r308 r312 1 1 {$IFDEF INTERFACE} 2 3 // TGList implemented using templates 4 // - item operations (Add, Insert, ReplaceArray, Get, Set, IndexOf, 5 // Extract, Delete, Exchange) 6 // - item range operations (DeleteItems, InsertItems, ReplaceItems, 7 // Move, Fill) 8 // - other TGList operations (AddList, InsertList, 9 // ReplaceList, GetList, IndexOfList) 10 // - dynamic array operations (AddArray, InsertArray, 11 // ReplaceArray, GetArray, IndexOfArray) 12 // - all items operations (Clear, Reverse, Sort) 2 13 3 14 TGList = class; … … 41 52 property First: TGListItem read GetFirst write SetFirst; 42 53 procedure Fill(Start, Count: TGListIndex; Value: TGListItem); 43 function GetArray: TGListItemArray; 54 function GetArray(Index, ACount: TGListIndex): TGListItemArray; 55 procedure GetList(List: TGList; Index, ACount: TGListIndex); 44 56 function Implode(Separator: string; Converter: TGListToStringConverter): string; 45 57 function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex; virtual; 46 58 function IndexOfList(List: TGList; Start: TGListIndex = 0): TGListIndex; 59 function IndexOfArray(Values: array of TGListItem; Start: TGListIndex = 0): TGListIndex; 47 60 procedure Insert(Index: TGListIndex; Item: TGListItem); 48 61 procedure InsertList(Index: TGListIndex; List: TGList); 49 62 procedure InsertArray(Index: TGListIndex; Values: array of TGListItem); 63 procedure InsertCount(Index: TGListIndex; ACount: TGListIndex); 50 64 procedure Move(CurIndex, NewIndex: TGListIndex); 51 65 procedure MoveItems(CurIndex, NewIndex, Count: TGListIndex); 52 66 function Remove(Item: TGListItem): TGListIndex; 53 67 procedure Reverse; 68 procedure ReplaceArray(Index: TGListIndex; Values: array of TGListItem); 54 69 procedure ReplaceList(Index: TGListIndex; Source: TGList); 55 70 procedure ReplaceListPart(Index: TGListIndex; Source: TGList; … … 83 98 end; 84 99 100 procedure TGList.ReplaceArray(Index: TGListIndex; Values: array of TGListItem); 101 var 102 I: TGListIndex; 103 begin 104 I := 0; 105 while I < Length(Values) do begin 106 Items[Index + I] := Values[I]; 107 I := I + 1; 108 end; 109 end; 110 85 111 procedure TGList.ReplaceList(Index: TGListIndex; Source: TGList); 86 112 var … … 165 191 end; 166 192 167 function TGList.GetArray : TGListItemArray;193 function TGList.GetArray(Index, ACount: TGListIndex): TGListItemArray; 168 194 var 169 195 I: Integer; 170 196 begin 171 SetLength(Result, Count);197 SetLength(Result, ACount); 172 198 I := 0; 173 199 while I < Count do begin 174 Result[I] := FItems[I]; 175 I := I + 1; 176 end; 200 Result[I] := FItems[Index + I]; 201 I := I + 1; 202 end; 203 end; 204 205 procedure TGList.GetList(List: TGList; Index, ACount: TGListIndex); 206 begin 207 List.Clear; 208 List.AddListPart(Self, Index, ACount); 177 209 end; 178 210 … … 258 290 procedure TGList.Insert(Index: TGListIndex; Item: TGListItem); 259 291 begin 260 if (Index < 0) or (Index > FCount 292 if (Index < 0) or (Index > FCount) then 261 293 raise EListError.CreateFmt(SListIndexError, [Index]); 262 if FCount = Capacity then SetCapacityOptimized(Capacity + 1); 294 InsertCount(Index, 1); 295 FItems[Index] := Item; 296 end; 297 298 procedure TGList.InsertList(Index: TGListIndex; List: TGList); 299 begin 300 if (Index < 0) or (Index > FCount) then 301 raise EListError.CreateFmt(SListIndexError, [Index]); 302 InsertCount(Index, List.Count); 303 ReplaceList(Index, List); 304 end; 305 306 procedure TGList.InsertArray(Index: TGListIndex; Values: array of TGListItem); 307 begin 308 if (Index < 0) or (Index > FCount) then 309 raise EListError.CreateFmt(SListIndexError, [Index]); 310 InsertCount(Index, Length(Values)); 311 ReplaceArray(Index, Values); 312 end; 313 314 procedure TGList.InsertCount(Index: TGListIndex; ACount: TGListIndex); 315 begin 316 if (Index < 0) or (Index > FCount) then 317 raise EListError.CreateFmt(SListIndexError, [Index]); 318 Count := Count + ACount; 263 319 if Index < FCount then 264 System.Move(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TGListItem)); 265 FItems[Index] := Item; 266 FCount := FCount + 1; 267 end; 268 269 procedure TGList.InsertList(Index: TGListIndex; List: TGList); 270 var 271 I: TGListIndex; 272 begin 273 I := 0; 274 while (I < List.Count) do begin 275 Insert(Index + I, List[I]); 276 I := I + 1; 277 end; 320 System.Move(FItems[Index], FItems[Index + ACount], (FCount - ACount - Index) * SizeOf(TGListItem)); 278 321 end; 279 322 … … 297 340 end; 298 341 342 function TGList.IndexOfArray(Values: array of TGListItem; Start: TGListIndex): TGListIndex; 343 var 344 I: TGListIndex; 345 begin 346 if Length(Values) > 0 then begin 347 Result := IndexOf(Values[0], Start); 348 if Result <> -1 then begin 349 I := 1; 350 while I < Length(Values) do begin 351 if not CompareMem(Addr(FItems[Result + I]), Addr(Values[I]), SizeOf(TGListItem)) then begin 352 Result := -1; 353 Break; 354 end; 355 I := I + 1; 356 end; 357 end; 358 end else Result := -1; 359 end; 360 299 361 function TGList.GetLast: TGListItem; 300 362 begin … … 438 500 end; 439 501 440 procedure TGList.InsertArray(Index: TGListIndex; Values: array of TGListItem);441 var442 I: TGListIndex;443 begin444 I := 0;445 while I <= High(Values) do begin446 Insert(Index + I, Values[I]);447 I := I + 1;448 end;449 end;450 451 502 function TGList.Implode(Separator: string; Converter: TGListToStringConverter): string; 452 503 var -
Generics/TemplateGenerics/Generic/GenericListObject.inc
r257 r312 22 22 procedure Clear; override; 23 23 procedure Assign(Source: TGList); override; 24 constructor Create; 24 constructor Create; override; 25 25 destructor Destroy; override; 26 26 end; -
Generics/TemplateGenerics/Generic/GenericListString.inc
r258 r312 19 19 procedure Assign(Source: TGList); override; 20 20 function IndexOf(Item: TGListItem; Start: TGListIndex = 0): TGListIndex; override; 21 constructor Create; 21 constructor Create; override; 22 22 destructor Destroy; override; 23 23 end; -
Generics/TemplateGenerics/Generic/GenericQueue.inc
r112 r312 11 11 {$I 'GenericList.inc'} 12 12 13 // TGQueue<T SetIndex, TSetItem> = class(TGList)13 // TGQueue<TQueueIndex, TQueueItem> = class(TGList) 14 14 TGQueue = class 15 15 private 16 16 FList: TGList; 17 function GetCount: TGQueueIndex; 17 18 public 18 19 procedure Enqueue(Value: TGQueueItem); 20 procedure EnqueueArray(Values: array of TGQueueItem); 21 procedure EnqueueList(List: TGList); 19 22 function Dequeue: TGQueueItem; 20 23 function Peek: TGQueueItem; … … 22 25 destructor Destroy; override; 23 26 property List: TGList read FList; 27 property Count: TGQueueIndex read GetCount; 24 28 end; 25 29 … … 54 58 end; 55 59 60 procedure TGQueue.EnqueueArray(Values: array of TGQueueItem); 61 begin 62 FList.AddArray(Values); 63 end; 64 65 procedure TGQueue.EnqueueList(List: TGList); 66 begin 67 FList.AddList(List); 68 end; 69 56 70 function TGQueue.Peek: TGQueueItem; 57 71 begin … … 75 89 end; 76 90 91 function TGQueue.GetCount: TGQueueIndex; 92 begin 93 Result := FList.Count; 94 end; 95 77 96 {$UNDEF IMPLEMENTATION} 78 97 {$ENDIF} -
Generics/TemplateGenerics/Generic/GenericStream.inc
r226 r312 1 1 {$IFDEF INTERFACE} 2 2 3 TGStream DataEvent = procedure (Item: TGStreamItem) of object;3 TGStreamItemArray = array of TGStreamItem; 4 4 5 // TGStream<T StreamItem> = class5 // TGStream<TGStreamIndex, TGStreamItem> = class 6 6 TGStream = class 7 FOnData: TGStreamDataEvent; 8 procedure Write(Item: TStreamItem); 9 property OnData: TGStreamDataEvent read FOnData write FOnData; 7 procedure SetSize(AValue: TGStreamIndex); 8 function GetSize: TGStreamIndex; 9 procedure SetPosition(AValue: TGStreamIndex); 10 function GetPosition: TGStreamIndex; 11 public 12 procedure Assign(Source: TGStream); virtual; 13 procedure Write(Item: TGStreamItem); virtual; abstract; 14 procedure WriteArray(Item: array of TGStreamItem); virtual; abstract; 15 function Read: TGStreamItem; virtual; abstract; 16 function ReadArray(Count: TGStreamIndex): TGStreamItemArray; virtual; abstract; 17 function Insert(Count: TGStreamIndex): TGStreamIndex; virtual; abstract; 18 function Remove(Count: TGStreamIndex): TGStreamIndex; virtual; abstract; 19 function Seek(Offset: TGStreamIndex; Origin: TSeekOrigin = soCurrent): 20 TGStreamIndex; virtual; abstract; 21 constructor Create; virtual; 22 property Position: TGStreamIndex read GetPosition write SetPosition; 23 property Size: TGStreamIndex read GetSize write SetSize; 10 24 end; 11 25 … … 15 29 {$IFDEF IMPLEMENTATION} 16 30 17 procedure TGStream. Write(Item: TGStreamItem);31 procedure TGStream.Assign(Source: TGStream); 18 32 begin 19 if Assigned(FOnData) then20 FOnData(Item);21 33 end; 22 34 35 procedure TGStream.SetPosition(AValue: TGStreamIndex); 36 begin 37 Seek(AValue, soBegin); 38 end; 39 40 function TGStream.GetPosition: TGStreamIndex; 41 begin 42 Result := Seek(0, soCurrent); 43 end; 44 45 procedure TGStream.SetSize(AValue: TGStreamIndex); 46 var 47 StreamSize: TGStreamIndex; 48 OldPosition: TGStreamIndex; 49 begin 50 OldPosition := Seek(0, soCurrent); 51 StreamSize := Size; 52 if AValue > StreamSize then begin 53 Seek(StreamSize, soBegin); 54 Insert(AValue - StreamSize); 55 end else 56 if AValue < StreamSize then begin 57 Seek(AValue, soBegin); 58 Remove(StreamSize - AValue); 59 end; 60 Position := OldPosition; 61 end; 62 63 function TGStream.GetSize: TGStreamIndex; 64 var 65 OldPosition: Integer; 66 begin 67 OldPosition := Position; 68 Result := Seek(0, soEnd); 69 Position := OldPosition; 70 end; 71 72 constructor TGStream.Create; 73 begin 74 inherited; 75 end; 23 76 24 77 {$UNDEF IMPLEMENTATION}
Note:
See TracChangeset
for help on using the changeset viewer.