1 {(*}
2 (*------------------------------------------------------------------------------
3  Delphi Code formatter source code
4 
5 The Original Code is IntList.pas, released April 2000.
6 The Initial Developer of the Original Code is Anthony Steele.
7 Portions created by Anthony Steele are Copyright (C) 1999-2008 Anthony Steele.
8 All Rights Reserved.
9 Contributor(s): Anthony Steele.
10 
11 The contents of this file are subject to the Mozilla Public License Version 1.1
12 (the "License"). you may not use this file except in compliance with the License.
13 You may obtain a copy of the License at http://www.mozilla.org/NPL/
14 
15 Software distributed under the License is distributed on an "AS IS" basis,
16 WITHOUT WARRANTY OF ANY KIND, either express or implied.
17 See the License for the specific language governing rights and limitations
18 under the License.
19 
20 Alternatively, the contents of this file may be used under the terms of
21 the GNU General Public License Version 2 or later (the "GPL")
22 See http://www.gnu.org/licenses/gpl.html
23 ------------------------------------------------------------------------------*)
24 {*)}
25 
26 unit IntList;
27 
28 { AFS 2 April 2K
29   A old concept, and a generic class
30   implemented here because I needed it in the LineBreaker
31 
32   * Change by JuMa to use TIntegerList from LazUtils as a base class. *
33 
34   This class could be extended in mnay ways (e.g. math ops on the list like sum, min, max, avg etc)
35   Fns to add without duplicates, Sort, add another int list (and intersection, difference) etc
36   But I don't need any of that right now.
37 }
38 
39 
40 {$I JcfGlobal.inc}
41 
42 interface
43 
44 uses Classes, SysUtils, IntegerList;
45 
46 type
47 
48   TIntList = class(TIntegerList)
49   public
50     procedure ChangeValue(const liIndex, liDelta: integer);
IndexOfMaxnull51     function IndexOfMax: integer;
52     { to use it as a stack }
Topnull53     function Top: integer;
Popnull54     function Pop: integer;
55   end;
56 
57 implementation
58 
59 { TIntList }
60 
61 procedure TIntList.ChangeValue(const liIndex, liDelta: integer);
62 begin
63   { can fall out of bounds, easiest to ignore it here }
64   if (liIndex < 0) or (liIndex >= Count) then
65     exit;
66 
67   Items[liIndex] := Items[liIndex] + liDelta;
68 end;
69 
TIntList.IndexOfMaxnull70 function TIntList.IndexOfMax: integer;
71 var
72   liItem, liMax: integer;
73   liLoop: integer;
74 begin
75   Result := -1;
76   liMax  := Low(integer);
77 
78   for liLoop := 0 to Count - 1 do
79   begin
80     liItem := Items[liLoop];
81     { the >= is a hack kinda but for the purposes of the linebreaker,
82       in case of a tie, take the last item }
83 
84     if liItem >= liMax then
85     begin
86       liMax  := liItem;
87       Result := liLoop;
88     end;
89   end;
90 end;
91 
Popnull92 function TIntList.Pop: integer;
93 begin
94   if Count > 0 then
95   begin
96     Result := Items[Count - 1];
97     Delete(Count - 1);
98   end
99   else
100     Result := 0;
101 end;
102 
Topnull103 function TIntList.Top: integer;
104 begin
105   if Count > 0 then
106     Result := Items[Count - 1]
107   else
108     Result := 0;
109 end;
110 
111 end.
112