1 | Unit ObjectListTemplate;
|
---|
2 |
|
---|
3 | {#TemplateParameter ContainedClass}
|
---|
4 | {#TemplateParameter OutputClass}
|
---|
5 | {#TemplateParameter ContainedClassUnit}
|
---|
6 |
|
---|
7 | Interface
|
---|
8 |
|
---|
9 | uses
|
---|
10 | ContainedClassUnit,
|
---|
11 | Classes;
|
---|
12 |
|
---|
13 | type
|
---|
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 |
|
---|
35 | Implementation
|
---|
36 |
|
---|
37 | { ContainedClassList }
|
---|
38 |
|
---|
39 | function OutputClass.Add( item: ContainedClass ): integer;
|
---|
40 | begin
|
---|
41 | result := inherited Add( item );
|
---|
42 | end;
|
---|
43 |
|
---|
44 | function OutputClass.First: ContainedClass;
|
---|
45 | begin
|
---|
46 | result := inherited First;
|
---|
47 | end;
|
---|
48 |
|
---|
49 | function OutputClass.Get( index: integer ): ContainedClass;
|
---|
50 | begin
|
---|
51 | result := inherited Get( Index );
|
---|
52 | end;
|
---|
53 |
|
---|
54 | function OutputClass.IndexOf( item: ContainedClass ): integer;
|
---|
55 | begin
|
---|
56 | result := inherited IndexOf( item );
|
---|
57 | end;
|
---|
58 |
|
---|
59 | procedure OutputClass.Insert( Index: Integer; Item: ContainedClass );
|
---|
60 | begin
|
---|
61 | inherited Insert( Index, Item );
|
---|
62 | end;
|
---|
63 |
|
---|
64 | function OutputClass.Last: ContainedClass;
|
---|
65 | begin
|
---|
66 | result := inherited Last;
|
---|
67 | end;
|
---|
68 |
|
---|
69 | procedure OutputClass.Put( index: integer; Item: ContainedClass );
|
---|
70 | begin
|
---|
71 | inherited Put( index, Item );
|
---|
72 | end;
|
---|
73 |
|
---|
74 | function OutputClass.Remove( Item: ContainedClass ): Integer;
|
---|
75 | begin
|
---|
76 | result := inherited Remove( Item );
|
---|
77 | end;
|
---|
78 |
|
---|
79 | procedure QuickSort( SortList: PPointerList;
|
---|
80 | L, R: Integer;
|
---|
81 | CompareFunction: ObjectSortCompare );
|
---|
82 | var
|
---|
83 | I, J: Integer;
|
---|
84 | P, T: Pointer;
|
---|
85 | begin
|
---|
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;
|
---|
110 | end;
|
---|
111 |
|
---|
112 | procedure OutputClass.Sort( CompareFunction: ObjectSortCompare );
|
---|
113 | begin
|
---|
114 | if ( List <> nil ) and ( Count > 0 ) then
|
---|
115 | QuickSort( List, 0, Count - 1, CompareFunction );
|
---|
116 | end;
|
---|
117 |
|
---|
118 | Procedure OutputClass.DestroyContents;
|
---|
119 | var
|
---|
120 | Index: longint;
|
---|
121 | begin
|
---|
122 | for Index := 0 to Count - 1 do
|
---|
123 | ContainedClass( Items[ Index ] ).Destroy;
|
---|
124 | Clear;
|
---|
125 | end;
|
---|
126 |
|
---|
127 | procedure OutputClass.Assign( OtherList: OutputClass );
|
---|
128 | begin
|
---|
129 | Clear;
|
---|
130 | AddList( OtherList );
|
---|
131 | end;
|
---|
132 |
|
---|
133 | procedure AddList( OtherList: OutputClass );
|
---|
134 | var
|
---|
135 | Index: longint;
|
---|
136 | begin
|
---|
137 | SetCapacity( Capacity + OtherList.Capacity );
|
---|
138 | for Index := 0 to OtherList.Count - 1 do
|
---|
139 | inherited Add( OtherList[ Index ] );
|
---|
140 | end;
|
---|
141 |
|
---|
142 | Initialization
|
---|
143 | End.
|
---|
144 |
|
---|