-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathStack.pas
More file actions
67 lines (56 loc) · 1.15 KB
/
Stack.pas
File metadata and controls
67 lines (56 loc) · 1.15 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
{*
* Pascal Stack: Simple stack class to be used in Delphi (Pascal) with automatic memory growth
* Jonas Raoni Soares da Silva <http://raoni.org>
* https://github.com/jonasraoni/pascal-stack
*}
unit Stack;
interface
uses
SysUtils, Classes;
type
TStack = class
private
FList: PPointerList;
FCapacity, FCount: Cardinal;
procedure Grow;
public
destructor Destroy; override;
procedure Push(const Data: Pointer);
function Pop: Pointer;
end;
implementation
{ TStack }
destructor TStack.Destroy;
begin
FreeMem(FList);
inherited;
end;
procedure TStack.Grow;
begin
if FCapacity > 64 then
Inc(FCapacity, FCapacity div 4)
else
if FCapacity > 8 then
Inc(FCapacity, 16)
else
Inc(FCapacity, 4);
ReallocMem(FList, FCapacity * SizeOf(Pointer));
end;
function TStack.Pop: Pointer;
begin
if FCount > 0 then
begin
Dec(FCount);
Result := FList^[FCount];
end
else
Result := nil;
end;
procedure TStack.Push(const Data: Pointer);
begin
if FCapacity = FCount then
Grow;
FList^[FCount] := Data;
Inc(FCount);
end;
end.