source: trunk/Library/ListTemplate.pas@ 17

Last change on this file since 17 was 17, checked in by RBRi, 19 years ago

+ Library

  • Property svn:eol-style set to native
File size: 3.3 KB
Line 
1Unit ObjectListTemplate;
2
3{#TemplateParameter ContainedClass}
4{#TemplateParameter OutputClass}
5{#TemplateParameter ContainedClassUnit}
6
7Interface
8
9uses
10 ContainedClassUnit,
11 Classes;
12
13type
14 // To use sort, you must pass in a compar method as below
15 ObjectSortCompare = function ( Item1, Item2: ContainedClass ): Integer;
16
17 OutputClass = class( TList )
18 protected
19 function Get( index: integer ): ContainedClass;
20 procedure Put( index: integer; Item: ContainedClass );
21 public
22 function Add( item: ContainedClass ): integer;
23 function First: ContainedClass;
24 function IndexOf( item: ContainedClass ): integer;
25 procedure Insert( Index: Integer; Item: ContainedClass );
26 function Last: ContainedClass;
27 function Remove( Item: ContainedClass ): Integer;
28 procedure Sort( CompareFunction: ObjectSortCompare );
29 property Items[ index: integer ]: ContainedClass read Get write Put; default;
30 procedure DestroyContents;
31 procedure Assign( OtherList: OutputClass );
32 procedure AddList( OtherList: OutputClass );
33 end;
34
35Implementation
36
37{ ContainedClassList }
38
39function OutputClass.Add( item: ContainedClass ): integer;
40begin
41 result := inherited Add( item );
42end;
43
44function OutputClass.First: ContainedClass;
45begin
46 result := inherited First;
47end;
48
49function OutputClass.Get( index: integer ): ContainedClass;
50begin
51 result := inherited Get( Index );
52end;
53
54function OutputClass.IndexOf( item: ContainedClass ): integer;
55begin
56 result := inherited IndexOf( item );
57end;
58
59procedure OutputClass.Insert( Index: Integer; Item: ContainedClass );
60begin
61 inherited Insert( Index, Item );
62end;
63
64function OutputClass.Last: ContainedClass;
65begin
66 result := inherited Last;
67end;
68
69procedure OutputClass.Put( index: integer; Item: ContainedClass );
70begin
71 inherited Put( index, Item );
72end;
73
74function OutputClass.Remove( Item: ContainedClass ): Integer;
75begin
76 result := inherited Remove( Item );
77end;
78
79procedure QuickSort( SortList: PPointerList;
80 L, R: Integer;
81 CompareFunction: ObjectSortCompare );
82var
83 I, J: Integer;
84 P, T: Pointer;
85begin
86 repeat
87 I := L;
88 J := R;
89 P := SortList^[(L + R) shr 1];
90 repeat
91 while CompareFunction( ContainedClass( SortList^[I] ),
92 ContainedClass( P ) ) < 0 do
93 Inc(I);
94 while CompareFunction( ContainedClass( SortList^[J] ),
95 ContainedClass( P ) ) > 0 do
96 Dec(J);
97 if I <= J then
98 begin
99 T := SortList^[I];
100 SortList^[I] := SortList^[J];
101 SortList^[J] := T;
102 Inc(I);
103 Dec(J);
104 end;
105 until I > J;
106 if L < J then
107 QuickSort( SortList, L, J, CompareFunction );
108 L := I;
109 until I >= R;
110end;
111
112procedure OutputClass.Sort( CompareFunction: ObjectSortCompare );
113begin
114 if ( List <> nil ) and ( Count > 0 ) then
115 QuickSort( List, 0, Count - 1, CompareFunction );
116end;
117
118Procedure OutputClass.DestroyContents;
119var
120 Index: longint;
121begin
122 for Index := 0 to Count - 1 do
123 ContainedClass( Items[ Index ] ).Destroy;
124 Clear;
125end;
126
127procedure OutputClass.Assign( OtherList: OutputClass );
128begin
129 Clear;
130 AddList( OtherList );
131end;
132
133procedure AddList( OtherList: OutputClass );
134var
135 Index: longint;
136begin
137 SetCapacity( Capacity + OtherList.Capacity );
138 for Index := 0 to OtherList.Count - 1 do
139 inherited Add( OtherList[ Index ] );
140end;
141
142Initialization
143End.
144
Note: See TracBrowser for help on using the repository browser.