- Timestamp:
- Oct 27, 2010, 9:28:58 PM (14 years ago)
- Location:
- Generics/TemplateGenerics
- Files:
-
- 2 added
- 13 edited
Legend:
- Unmodified
- Added
- Removed
-
Generics/TemplateGenerics/List/GenericListImplementation.tpl
r68 r69 10 10 { TGList } 11 11 12 function TGList.GetCapacity: T IndexType;12 function TGList.GetCapacity: TListIndex; 13 13 begin 14 14 Result := Length(FItems); 15 15 end; 16 16 17 procedure TGList.SetCapacity(const AValue: T IndexType);17 procedure TGList.SetCapacity(const AValue: TListIndex); 18 18 begin 19 19 SetLength(FItems, AValue); 20 20 end; 21 21 22 function TGList.Get(Index: T IndexType): TItemType;22 function TGList.Get(Index: TListIndex): TListItem; 23 23 begin 24 24 Result := FItems[Index]; 25 25 end; 26 26 27 function TGList.GetCount: T IndexType;27 function TGList.GetCount: TListIndex; 28 28 begin 29 29 Result := FCount; 30 30 end; 31 31 32 procedure TGList.Put(Index: T IndexType; const AValue: TItemType);32 procedure TGList.Put(Index: TListIndex; const AValue: TListItem); 33 33 begin 34 34 FItems[Index] := AValue; 35 35 end; 36 36 37 procedure TGList.SetCount(const AValue: T IndexType);37 procedure TGList.SetCount(const AValue: TListIndex); 38 38 begin 39 39 SetLength(FItems, AValue); … … 41 41 end; 42 42 43 procedure TGList.QuickSort(L, R: T IndexType; Compare: TGListSortCompare);44 var 45 I, J: T IndexType;46 P, Q: T ItemType;43 procedure TGList.QuickSort(L, R: TListIndex; Compare: TGListSortCompare); 44 var 45 I, J: TListIndex; 46 P, Q: TListItem; 47 47 begin 48 48 repeat … … 84 84 procedure TGList.Expand; 85 85 var 86 IncSize: T IndexType;86 IncSize: TListIndex; 87 87 begin 88 88 if FCount = Capacity then begin … … 95 95 end; 96 96 97 function TGList.Extract(Item: T ItemType): TItemType;98 var 99 I: T IndexType;97 function TGList.Extract(Item: TListItem): TListItem; 98 var 99 I: TListIndex; 100 100 begin 101 101 I := IndexOf(Item); … … 107 107 end; 108 108 109 function TGList.ExtractList(Item: T ItemType; Count: TIndexType): TItemType;109 function TGList.ExtractList(Item: TListItem; Count: TListIndex): TListItem; 110 110 begin 111 111 raise Exception.Create(SNotImplemented); 112 112 end; 113 113 114 function TGList.First: T ItemType;114 function TGList.First: TListItem; 115 115 begin 116 116 if FCount = 0 then … … 120 120 end; 121 121 122 function TGList.IndexOf(Item: T ItemType): TIndexType;122 function TGList.IndexOf(Item: TListItem): TListIndex; 123 123 begin 124 124 Result := 0; … … 128 128 end; 129 129 130 procedure TGList.Insert(Index: T IndexType; Item: TItemType);130 procedure TGList.Insert(Index: TListIndex; Item: TListItem); 131 131 begin 132 132 if (Index < 0) or (Index > FCount ) then … … 134 134 if FCount = Capacity then Expand; 135 135 if Index < FCount then 136 SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(T ItemType));136 SystemMove(FItems[Index], FItems[Index + 1], (FCount - Index) * SizeOf(TListItem)); 137 137 FItems[Index] := Item; 138 138 FCount := FCount + 1; 139 139 end; 140 140 141 procedure TGList.InsertList(Index: T IndexType; List: TGList);142 var 143 I: T IndexType;141 procedure TGList.InsertList(Index: TListIndex; List: TGList); 142 var 143 I: TListIndex; 144 144 begin 145 145 I := 0; … … 150 150 end; 151 151 152 function TGList.Last: T ItemType;152 function TGList.Last: TListItem; 153 153 begin 154 154 if FCount = 0 then … … 158 158 end; 159 159 160 procedure TGList.Move(CurIndex, NewIndex: T IndexType);161 var 162 Temp: T ItemType;160 procedure TGList.Move(CurIndex, NewIndex: TListIndex); 161 var 162 Temp: TListItem; 163 163 begin 164 164 if ((CurIndex < 0) or (CurIndex > Count - 1)) then … … 171 171 end; 172 172 173 procedure TGList.MoveItems(CurIndex, NewIndex, Count: T IndexType);173 procedure TGList.MoveItems(CurIndex, NewIndex, Count: TListIndex); 174 174 begin 175 175 raise Exception.Create(SNotImplemented); 176 176 end; 177 177 178 function TGList.Remove(Item: TItemType): TIndexType; 178 procedure TGList.Swap(Index1, Index2: TListIndex); 179 var 180 Temp: TListItem; 181 begin 182 Temp := Items[Index1]; 183 Items[Index1] := Items[Index2]; 184 Items[Index2] := Temp; 185 end; 186 187 function TGList.Remove(Item: TListItem): TListIndex; 179 188 begin 180 189 Result := IndexOf(Item); … … 183 192 end; 184 193 185 function TGList.Equals(Obj: TObject): Boolean;186 var 187 I: T IndexType;194 (*function TGList.Equals(Obj: TObject): Boolean; 195 var 196 I: TListIndex; 188 197 begin 189 198 Result := Count = (Obj as TGList).Count; … … 198 207 end; 199 208 end; 200 end; 209 end;*) 201 210 202 211 procedure TGList.Reverse; 203 begin 204 raise Exception.Create(SNotImplemented); 212 var 213 I: TListIndex; 214 begin 215 I := 0; 216 while I < (Count div 2) do begin 217 Swap(I, Count - 1 - I); 218 I := I + 1; 219 end; 205 220 end; 206 221 … … 211 226 end; 212 227 213 procedure TGList.SetArray(Values: array of T ItemType);214 var 215 I: T IndexType;228 procedure TGList.SetArray(Values: array of TListItem); 229 var 230 I: TListIndex; 216 231 begin 217 232 Clear; … … 223 238 end; 224 239 225 function TGList.Implode(Separator: string ): string;226 var 227 I: T IndexType;240 function TGList.Implode(Separator: string; Converter: TGListStringConverter): string; 241 var 242 I: TListIndex; 228 243 begin 229 244 Result := ''; 230 245 I := 0; 231 246 while I < Count do begin 232 Result := Result + string(Items[I]);247 Result := Result + Converter(Items[I]); 233 248 if I < (Count - 1) then 234 249 Result := Result + Separator; … … 237 252 end; 238 253 239 function TGList.Add(Item: T ItemType): TIndexType;254 function TGList.Add(Item: TListItem): TListIndex; 240 255 begin 241 256 if FCount = Capacity then … … 248 263 procedure TGList.AddList(List: TGList); 249 264 var 250 I: T IndexType;265 I: TListIndex; 251 266 begin 252 267 I := 0; … … 263 278 end; 264 279 265 procedure TGList.Delete(Index: T IndexType);280 procedure TGList.Delete(Index: TListIndex); 266 281 begin 267 282 if (Index < 0) or (Index >= FCount) then 268 283 raise EListError.CreateFmt(SListIndexError, [Index]); 269 284 FCount := FCount - 1; 270 SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(T ItemType));285 SystemMove(FItems[Index + 1], FItems[Index], (FCount - Index) * SizeOf(TListItem)); 271 286 // Shrink the list if appropriate 272 287 if (Capacity > 256) and (FCount < Capacity shr 2) then … … 276 291 end; 277 292 278 procedure TGList.DeleteItems(Index, Count: T IndexType);279 var 280 I: T IndexType;293 procedure TGList.DeleteItems(Index, Count: TListIndex); 294 var 295 I: TListIndex; 281 296 begin 282 297 I := 0; … … 287 302 end; 288 303 289 procedure TGList.Exchange(Index1, Index2: T IndexType);290 var 291 Temp: T ItemType;304 procedure TGList.Exchange(Index1, Index2: TListIndex); 305 var 306 Temp: TListItem; 292 307 begin 293 308 if ((Index1 >= FCount) or (Index1 < 0)) then -
Generics/TemplateGenerics/List/GenericListInterface.tpl
r68 r69 4 4 5 5 type 6 TGListSortCompare = function(const Item1, Item2: TItemType): Integer; 6 TGListSortCompare = function(const Item1, Item2: TListItem): Integer of object; 7 TGListStringConverter = function(Item: TListItem): string; 7 8 //TGListNotification = (lnAdded, lnExtracted, lnDeleted); 8 9 9 // TGList<T IndexType, TItemType> = class10 // TGList<TListIndex, TListItem> = class 10 11 TGList = class 11 12 private 12 FItems: array of T ItemType;13 FCount: T IndexType;14 function Get(Index: T IndexType): TItemType;15 function GetCount: T IndexType;16 function GetCapacity: T IndexType;17 procedure SetCapacity(const AValue: T IndexType);18 procedure Put(Index: T IndexType; const AValue: TItemType);19 procedure SetCount(const AValue: T IndexType);20 procedure QuickSort(L, R : T IndexType; Compare: TGListSortCompare);21 property Capacity: T IndexTyperead GetCapacity write SetCapacity;13 FItems: array of TListItem; 14 FCount: TListIndex; 15 function Get(Index: TListIndex): TListItem; 16 function GetCount: TListIndex; 17 function GetCapacity: TListIndex; 18 procedure SetCapacity(const AValue: TListIndex); 19 procedure Put(Index: TListIndex; const AValue: TListItem); 20 procedure SetCount(const AValue: TListIndex); 21 procedure QuickSort(L, R : TListIndex; Compare: TGListSortCompare); 22 property Capacity: TListIndex read GetCapacity write SetCapacity; 22 23 public 23 24 // All items … … 26 27 procedure Expand; 27 28 procedure Sort(Compare: TGListSortCompare); 28 function Implode(Separator: string ): string;29 function Implode(Separator: string; Converter: TGListStringConverter): string; 29 30 // Many items 30 procedure MoveItems(CurIndex, NewIndex, Count: TIndexType); 31 procedure MoveItems(CurIndex, NewIndex, Count: TListIndex); 32 procedure Swap(Index1, Index2: TListIndex); 31 33 // One item 32 function Add(Item: T ItemType): TIndexType;33 procedure Delete(Index: T IndexType);34 procedure Exchange(Index1, Index2: T IndexType);35 function Extract(Item: T ItemType): TItemType;36 function First: T ItemType;37 function IndexOf(Item: T ItemType): TIndexType;38 procedure Insert(Index: T IndexType; Item: TItemType);39 function Last: T ItemType;40 procedure Move(CurIndex, NewIndex: T IndexType);41 function Remove(Item: T ItemType): TIndexType;42 property Items[Index: T IndexType]: TItemTyperead Get write Put; default;34 function Add(Item: TListItem): TListIndex; 35 procedure Delete(Index: TListIndex); 36 procedure Exchange(Index1, Index2: TListIndex); 37 function Extract(Item: TListItem): TListItem; 38 function First: TListItem; 39 function IndexOf(Item: TListItem): TListIndex; 40 procedure Insert(Index: TListIndex; Item: TListItem); 41 function Last: TListItem; 42 procedure Move(CurIndex, NewIndex: TListIndex); 43 function Remove(Item: TListItem): TListIndex; 44 property Items[Index: TListIndex]: TListItem read Get write Put; default; 43 45 // List 44 46 procedure AddList(List: TGList); 45 47 procedure Assign(List: TGList); 46 procedure DeleteItems(Index, Count: T IndexType);47 function Equals(Obj: TObject): Boolean; override;48 function ExtractList(Item: T ItemType; Count: TIndexType): TItemType;49 procedure InsertList(Index: T IndexType; List: TGList);48 procedure DeleteItems(Index, Count: TListIndex); 49 //function Equals(Obj: TObject): Boolean; override; 50 function ExtractList(Item: TListItem; Count: TListIndex): TListItem; 51 procedure InsertList(Index: TListIndex; List: TGList); 50 52 // Other 51 property Count: T IndexTyperead GetCount write SetCount;53 property Count: TListIndex read GetCount write SetCount; 52 54 // Additional 53 procedure SetArray(Values: array of T ItemType);55 procedure SetArray(Values: array of TListItem); 54 56 end; -
Generics/TemplateGenerics/List/IntegerList.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= Integer;11 TListIndex = Integer; 12 TListItem = Integer; 13 13 {$INCLUDE 'GenericListInterface.tpl'} 14 14 15 15 type 16 TInteger GList = class(TGList)16 TIntegerList = class(TGList) 17 17 end; 18 18 -
Generics/TemplateGenerics/List/ObjectList.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= TObject;11 TListIndex = Integer; 12 TListItem = TObject; 13 13 {$INCLUDE 'GenericListInterface.tpl'} 14 14 15 15 type 16 TObjectGList = class(TGList) 16 17 { TObjectList } 18 19 TObjectList = class(TGList) 20 //OwnObjects: Boolean; 21 destructor Destroy; override; 17 22 end; 18 23 … … 22 27 23 28 29 { TObjectList } 30 31 destructor TObjectList.Destroy; 32 begin 33 inherited Destroy; 34 end; 35 24 36 end. -
Generics/TemplateGenerics/List/PointerList.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= Pointer;11 TListIndex = Integer; 12 TListItem = Pointer; 13 13 {$INCLUDE 'GenericListInterface.tpl'} 14 14 15 15 type 16 TPointer GList = class(TGList)16 TPointerList = class(TGList) 17 17 end; 18 18 -
Generics/TemplateGenerics/List/StringList.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= string;11 TListIndex = Integer; 12 TListItem = string; 13 13 {$INCLUDE 'GenericListInterface.tpl'} 14 14 15 15 type 16 TString GList = class(TGList)16 TStringList = class(TGList) 17 17 end; 18 18 -
Generics/TemplateGenerics/TemplateGenerics.lpk
r68 r69 8 8 <PathDelim Value="\"/> 9 9 <SearchPaths> 10 <OtherUnitFiles Value="List ;Tree"/>10 <OtherUnitFiles Value="List\;Tree\"/> 11 11 <UnitOutputDirectory Value="lib\$(TargetCPU)-$(TargetOS)"/> 12 12 </SearchPaths> … … 15 15 </Other> 16 16 </CompilerOptions> 17 <Files Count="1 2">17 <Files Count="13"> 18 18 <Item1> 19 19 <Filename Value="List\StringList.pas"/> … … 64 64 <UnitName Value="PointerTree"/> 65 65 </Item12> 66 <Item13> 67 <Filename Value="List\DoubleList.pas"/> 68 <UnitName Value="DoubleList"/> 69 </Item13> 66 70 </Files> 67 71 <Type Value="RunAndDesignTime"/> -
Generics/TemplateGenerics/TemplateGenerics.pas
r68 r69 9 9 uses 10 10 StringList, IntegerList, ObjectList, PointerList, StringTree, IntegerTree, 11 ObjectTree, PointerTree, LazarusPackageIntf;11 ObjectTree, PointerTree, DoubleList, LazarusPackageIntf; 12 12 13 13 implementation -
Generics/TemplateGenerics/Tree/GenericTreeInterface.tpl
r68 r69 1 // TGTreeNode<TIndexType, TItemType> = class 1 2 3 type 4 TListIndex = TTreeIndex; 5 TListItem = TTreeItem; 6 //{$INCLUDE 'GenericTreeInterface.tpl'} 7 8 // TGTreeNode<TTreeIndex, TTreeItem> = class 2 9 TGTreeNode = class 3 10 4 11 end; 5 12 6 // TGTree<T IndexType, TItemType> = class13 // TGTree<TTreeIndex, TTreeItem> = class 7 14 TGTree = class 8 15 TopItem: TGTreeNode; -
Generics/TemplateGenerics/Tree/IntegerTree.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= Integer;11 TTreeIndex = Integer; 12 TTreeItem = Integer; 13 13 {$INCLUDE 'GenericTreeInterface.tpl'} 14 14 15 15 type 16 TInteger GTree = class(TGTree)16 TIntegerTree = class(TGTree) 17 17 end; 18 18 19 TInteger GTreeNode = class(TGTreeNode)19 TIntegerTreeNode = class(TGTreeNode) 20 20 end; 21 21 -
Generics/TemplateGenerics/Tree/ObjectTree.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= TObject;11 TTreeIndex = Integer; 12 TTreeItem = TObject; 13 13 {$INCLUDE 'GenericTreeInterface.tpl'} 14 14 … … 17 17 end; 18 18 19 TObject GTreeNode = class(TGTreeNode)19 TObjectTreeNode = class(TGTreeNode) 20 20 end; 21 21 -
Generics/TemplateGenerics/Tree/PointerTree.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= Pointer;11 TTreeIndex = Integer; 12 TTreeItem = Pointer; 13 13 {$INCLUDE 'GenericTreeInterface.tpl'} 14 14 … … 17 17 end; 18 18 19 TPointer GTreeNode = class(TGTreeNode)19 TPointerTreeNode = class(TGTreeNode) 20 20 end; 21 21 -
Generics/TemplateGenerics/Tree/StringTree.pas
r68 r69 9 9 10 10 type 11 T IndexType= Integer;12 T ItemType= string;11 TTreeIndex = Integer; 12 TTreeItem = string; 13 13 {$INCLUDE 'GenericTreeInterface.tpl'} 14 14 15 15 type 16 TString GTree = class(TGTree)16 TStringTree = class(TGTree) 17 17 end; 18 18 19 TString GTreeNode = class(TGTreeNode)19 TStringTreeNode = class(TGTreeNode) 20 20 end; 21 21
Note:
See TracChangeset
for help on using the changeset viewer.