Changeset 322 for Generics/NativeGenerics/Units
- Timestamp:
- Feb 8, 2012, 9:47:54 AM (14 years ago)
- Location:
- Generics/NativeGenerics/Units
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/NativeGenerics/Units/GenericList.pas
r321 r322 38 38 procedure AddArray(Values: array of TItem); 39 39 procedure AddList(List: TGList<TItem>); 40 procedure AddListPart(List: TGList<TItem>; ItemIndex, ItemCount: TIndex); 40 41 procedure Assign(Source: TGList<TItem>); virtual; 41 42 procedure Clear; virtual; … … 48 49 property First: TItem read GetFirst write SetFirst; 49 50 procedure Fill(Start, Count: TIndex; Value: TItem); 50 function GetArray: TItemArray; 51 function GetArray(Index, ACount: TIndex): TItemArray; 52 procedure GetList(List: TGList<TItem>; Index, ACount: TIndex); 51 53 function Implode(Separator: string; Converter: TToStringConverter): string; 52 54 function IndexOf(Item: TItem; Start: TIndex = 0): TIndex; … … 161 163 end; 162 164 163 function TGList<TItem>.GetArray : TItemArray;165 function TGList<TItem>.GetArray(Index, ACount: TIndex): TItemArray; 164 166 var 165 167 I: Integer; 166 168 begin 167 SetLength(Result, Count);169 SetLength(Result, ACount); 168 170 I := 0; 169 171 while I < Count do begin 170 Result[I] := FItems[I]; 171 I := I + 1; 172 end; 172 Result[I] := FItems[Index + I]; 173 I := I + 1; 174 end; 175 end; 176 177 procedure TGList<TItem>.GetList(List: TGList; Index, ACount: TIndex); 178 begin 179 List.Clear; 180 List.AddListPart(Self, Index, ACount); 173 181 end; 174 182 … … 506 514 end; 507 515 516 procedure TGList<TItem>.AddListPart(List: TGList; ItemIndex, ItemCount: TIndex); 517 var 518 I: TIndex; 519 J: TIndex; 520 begin 521 I := Count; 522 J := ItemIndex; 523 Count := Count + ItemCount; 524 while I < Count do begin 525 Items[I] := List[J]; 526 I := I + 1; 527 J := J + 1; 528 end; 529 end; 530 508 531 procedure TGList<TItem>.Clear; 509 532 begin -
Generics/NativeGenerics/Units/GenericStream.pas
r320 r322 24 24 procedure Write(Item: TItem); virtual; abstract; 25 25 procedure WriteArray(Item: array of TItem); virtual; abstract; 26 procedure WriteList(List: TGList<TItem>); virtual; abstract; 26 27 function Read: TItem; virtual; abstract; 27 28 function ReadArray(Count: TIndex): TItemArray; virtual; abstract; … … 35 36 end; 36 37 38 TGMemoryStream<TItem> = class(TGStream<TItem>) 39 private 40 FList: TGList<TItem>; 41 FPosition: TIndex; 42 public 43 procedure Assign(Source: TGStream<TItem>); override; 44 procedure Write(Item: TItem); override; 45 procedure WriteArray(Values: array of TItem); override; 46 procedure WriteList(List: TGList<TItem>); override; 47 function Read: TItem; override; 48 function ReadArray(Count: TIndex): TItemArray; override; 49 function ReadList(List: TGList<TItem>; Count: TIndex): TIndex; 50 function Insert(Count: TIndex): Integer; override; 51 function Remove(Count: TIndex): Integer; override; 52 function Seek(Offset: TIndex; Origin: TSeekOrigin = soCurrent): TIndex; override; 53 constructor Create; override; 54 destructor Destroy; override; 55 property List: TGList<TItem> read FList; 56 end; 57 37 58 38 59 implementation 39 60 61 62 { TGStream } 40 63 41 64 procedure TGStream<TItem>.Assign(Source: TGStream<TItem>); … … 85 108 end; 86 109 110 { TMemoryStreamByte } 111 112 procedure TGMemoryStream<TItem>.Assign(Source: TGStream<TItem>); 113 begin 114 inherited; 115 if Source is TGMemoryStream<TItem> then begin 116 FList.Assign(TGMemoryStream<TItem>(Source).FList); 117 FPosition := TGMemoryStream<TItem>(Source).FPosition; 118 end; 119 end; 120 121 procedure TGMemoryStream<TItem>.Write(Item: TItem); 122 begin 123 if FList.Count < (FPosition + 1) then 124 FList.Count := FPosition + 1; 125 FList[FPosition] := Item; 126 Inc(FPosition); 127 end; 128 129 procedure TGMemoryStream<TItem>.WriteArray(Values: array of TItem); 130 begin 131 if FList.Count < (FPosition + Length(Values)) then 132 FList.Count := FPosition + Length(Values); 133 FList.ReplaceArray(FPosition, Values); 134 Inc(FPosition, Length(Values)); 135 end; 136 137 procedure TGMemoryStream<TItem>.WriteList(List: TGList<TItem>); 138 begin 139 FList.ReplaceList(FPosition, List); 140 end; 141 142 function TGMemoryStream<TItem>.Read: TItem; 143 begin 144 Result := FList[FPosition]; 145 Inc(FPosition); 146 end; 147 148 function TGMemoryStream<TItem>.ReadArray(Count: TIndex): TItemArray; 149 begin 150 Result := FList.GetArray(FPosition, Count); 151 end; 152 153 function TGMemoryStream<TItem>.ReadList(List: TGList<TItem>; Count: TIndex): TIndex; 154 begin 155 if (FPosition + Count) > FList.Count then 156 Count := FList.Count - FPosition; 157 FList.GetList(List, FPosition, Count); 158 Result := Count; 159 end; 160 161 function TGMemoryStream<TItem>.Insert(Count: TIndex): TIndex; 162 begin 163 FList.InsertCount(FPosition, Count); 164 Result := Count; 165 end; 166 167 function TGMemoryStream<TItem>.Remove(Count: TIndex): TIndex; 168 begin 169 Result := FList.Count - FPosition; 170 if Count < Result then Result := Count; 171 FList.DeleteItems(FPosition, Count); 172 end; 173 174 function TGMemoryStream<TItem>.Seek(Offset: TIndex; Origin: TSeekOrigin): TIndex; 175 begin 176 case Origin of 177 soBeginning: FPosition := Offset; 178 soCurrent: FPosition := FPosition + Offset; 179 soEnd: FPosition := FList.Count + Offset; 180 end; 181 if FPosition > FList.Count then FPosition := FList.Count; 182 if FPosition < 0 then FPosition := 0; 183 Result := FPosition; 184 end; 185 186 constructor TGMemoryStream<TItem>.Create; 187 begin 188 inherited; 189 FList := TGList<TItem>.Create; 190 end; 191 192 destructor TGMemoryStream<TItem>.Destroy; 193 begin 194 FList.Free; 195 inherited Destroy; 196 end; 197 198 87 199 end.
Note:
See TracChangeset
for help on using the changeset viewer.