source: branches/Xvcl/Xvcl.Generics.pas

Last change on this file was 24, checked in by chronos, 12 years ago
  • Fixed: Make only one form focused at once.
  • Modified: Enhanced TList generic class to support more methods.
File size: 3.3 KB
Line 
1unit Xvcl.Generics;
2
3interface
4
5uses
6 SysUtils;
7
8type
9 TList<T> = class
10 private
11 FCount: Integer;
12 FItems: array of T;
13 procedure SetCount(const Value: Integer);
14 function GetItem(Index: Integer): T;
15 procedure SetItem(Index: Integer; const Value: T);
16 public
17 type
18 TEnumerator = class
19 private
20 FList: TList<T>;
21 FIndex: Integer;
22 function GetCurrent: T;
23 public
24 function MoveNext: Boolean;
25 property Current: T read GetCurrent;
26 end;
27 function GetEnumerator: TEnumerator;
28 function Add(Item: T): Integer;
29 function Remove(Item: T): Integer;
30 procedure Delete(Index: Integer);
31 procedure Move(FromIndex, ToIndex: Integer);
32 function IndexOf(Item: T): Integer;
33 function Compare(Item1, Item2: T): Integer;
34 procedure Exchange(Index1, Index2: Integer);
35 property Count: Integer read FCount write SetCount;
36 property Items[Index: Integer]: T read GetItem write SetItem; default;
37 end;
38
39implementation
40
41{ TList<T>.TEnumerator }
42
43function TList<T>.TEnumerator.GetCurrent: T;
44begin
45 Result := FList.Items[FIndex];
46end;
47
48function TList<T>.TEnumerator.MoveNext: Boolean;
49begin
50 if FIndex < (FList.Count - 1) then begin
51 Inc(FIndex);
52 Result := True;
53 end else Result := False;
54end;
55
56{ TList<T> }
57
58function TList<T>.Add(Item: T): Integer;
59begin
60 Count := Count + 1;
61 FItems[Count - 1] := Item;
62 Result := Count - 1;
63end;
64
65function TList<T>.Compare(Item1, Item2: T): Integer;
66begin
67 if CompareMem(Pointer(@Item1), Pointer(@Item2), SizeOf(T)) then Result := 0
68 else Result := -1;
69end;
70
71procedure TList<T>.Delete(Index: Integer);
72var
73 I: Integer;
74begin
75 if (Index >= 0) and (Index < Count) then
76 for I := Index to Count - 2 do
77 Items[I] := Items[I + 1];
78end;
79
80procedure TList<T>.Exchange(Index1, Index2: Integer);
81var
82 Temp: T;
83begin
84 Temp := Items[Index2];
85 Items[Index2] := Items[Index1];
86 Items[Index1] := Temp;
87end;
88
89function TList<T>.GetEnumerator: TEnumerator;
90begin
91 Result := TEnumerator.Create;
92 Result.FList := Self;
93 Result.FIndex := -1;
94end;
95
96function TList<T>.GetItem(Index: Integer): T;
97begin
98 Result := FItems[Index];
99end;
100
101function TList<T>.IndexOf(Item: T): Integer;
102var
103 I: Integer;
104begin
105 I := 0;
106 while (I < Count) and (Compare(Items[I], Item) <> 0) do
107 Inc(I);
108 if I < Count then Result := I
109 else Result := -1;
110end;
111
112procedure TList<T>.Move(FromIndex, ToIndex: Integer);
113var
114 I: Integer;
115 Temp: T;
116begin
117 if (FromIndex >= 0) and (FromIndex < Count) and
118 (ToIndex >= 0) and (ToIndex < Count) then begin
119 if FromIndex > ToIndex then begin
120 Temp := Items[ToIndex];
121 for I := ToIndex to FromIndex - 1 do
122 Items[I] := Items[I + 1];
123 Items[FromIndex] := Temp;
124 end else
125 if FromIndex < ToIndex then begin
126 Temp := Items[FromIndex];
127 for I := FromIndex to ToIndex - 1 do
128 Items[I] := Items[I + 1];
129 Items[ToIndex] := Temp;
130 end;
131 end;
132end;
133
134function TList<T>.Remove(Item: T): Integer;
135begin
136 Result := IndexOf(Item);
137 if Result >= 0 then Delete(Result);
138end;
139
140procedure TList<T>.SetCount(const Value: Integer);
141begin
142 FCount := Value;
143 SetLength(FItems, Value);
144end;
145
146procedure TList<T>.SetItem(Index: Integer; const Value: T);
147begin
148 FItems[Index] := Value;
149end;
150
151end.
Note: See TracBrowser for help on using the repository browser.