1 |
// Upgraded to Delphi 2009: Sebastian Zierer
|
2 |
|
3 |
(* ***** BEGIN LICENSE BLOCK *****
|
4 |
* Version: MPL 1.1
|
5 |
*
|
6 |
* The contents of this file are subject to the Mozilla Public License Version
|
7 |
* 1.1 (the "License"); you may not use this file except in compliance with
|
8 |
* the License. You may obtain a copy of the License at
|
9 |
* http://www.mozilla.org/MPL/
|
10 |
*
|
11 |
* Software distributed under the License is distributed on an "AS IS" basis,
|
12 |
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
|
13 |
* for the specific language governing rights and limitations under the
|
14 |
* License.
|
15 |
*
|
16 |
* The Original Code is TurboPower SysTools
|
17 |
*
|
18 |
* The Initial Developer of the Original Code is
|
19 |
* TurboPower Software
|
20 |
*
|
21 |
* Portions created by the Initial Developer are Copyright (C) 1996-2002
|
22 |
* the Initial Developer. All Rights Reserved.
|
23 |
*
|
24 |
* Contributor(s):
|
25 |
*
|
26 |
* ***** END LICENSE BLOCK ***** *)
|
27 |
|
28 |
{*********************************************************}
|
29 |
{* SysTools: StDQue.pas 4.04 *}
|
30 |
{*********************************************************}
|
31 |
{* SysTools: DEQue class *}
|
32 |
{*********************************************************}
|
33 |
|
34 |
{$I StDefine.inc}
|
35 |
|
36 |
{Notes:
|
37 |
This class is derived from TStList and allows all of
|
38 |
the inherited list methods to be used.
|
39 |
|
40 |
The "head" of the queue is element 0 in the list. The "tail" of the
|
41 |
queue is the last element in the list.
|
42 |
|
43 |
The dequeue can be used as a LIFO stack by calling PushTail and
|
44 |
PopTail, or as a FIFO queue by calling PushTail and PopHead.
|
45 |
}
|
46 |
|
47 |
unit StDQue;
|
48 |
|
49 |
interface
|
50 |
|
51 |
uses
|
52 |
Windows,
|
53 |
STConst, StBase, StList;
|
54 |
|
55 |
type
|
56 |
TStDQue = class(TStList)
|
57 |
public
|
58 |
procedure PushTail(Data : Pointer);
|
59 |
{-Add element at tail of queue}
|
60 |
procedure PopTail;
|
61 |
{-Delete element at tail of queue, destroys its data}
|
62 |
procedure PeekTail(var Data : Pointer);
|
63 |
{-Return data at tail of queue}
|
64 |
|
65 |
procedure PushHead(Data : Pointer);
|
66 |
{-Add element at head of queue}
|
67 |
procedure PopHead;
|
68 |
{-Delete element at head of queue, destroys its data}
|
69 |
procedure PeekHead(var Data : Pointer);
|
70 |
{-Return data at head of queue}
|
71 |
end;
|
72 |
|
73 |
{======================================================================}
|
74 |
|
75 |
implementation
|
76 |
|
77 |
|
78 |
|
79 |
procedure TStDQue.PeekHead(var Data : Pointer);
|
80 |
begin
|
81 |
{$IFDEF ThreadSafe}
|
82 |
EnterCS;
|
83 |
try
|
84 |
{$ENDIF}
|
85 |
if Count = 0 then
|
86 |
Data := nil
|
87 |
else
|
88 |
Data := Head.Data;
|
89 |
{$IFDEF ThreadSafe}
|
90 |
finally
|
91 |
LeaveCS;
|
92 |
end;
|
93 |
{$ENDIF}
|
94 |
end;
|
95 |
|
96 |
procedure TStDQue.PeekTail(var Data : Pointer);
|
97 |
begin
|
98 |
{$IFDEF ThreadSafe}
|
99 |
EnterCS;
|
100 |
try
|
101 |
{$ENDIF}
|
102 |
if Count = 0 then
|
103 |
Data := nil
|
104 |
else
|
105 |
Data := Tail.Data;
|
106 |
{$IFDEF ThreadSafe}
|
107 |
finally
|
108 |
LeaveCS;
|
109 |
end;
|
110 |
{$ENDIF}
|
111 |
end;
|
112 |
|
113 |
procedure TStDQue.PopHead;
|
114 |
begin
|
115 |
{$IFDEF ThreadSafe}
|
116 |
EnterCS;
|
117 |
try
|
118 |
{$ENDIF}
|
119 |
if Count > 0 then
|
120 |
Delete(Head);
|
121 |
{$IFDEF ThreadSafe}
|
122 |
finally
|
123 |
LeaveCS;
|
124 |
end;
|
125 |
{$ENDIF}
|
126 |
end;
|
127 |
|
128 |
procedure TStDQue.PopTail;
|
129 |
begin
|
130 |
{$IFDEF ThreadSafe}
|
131 |
EnterCS;
|
132 |
try
|
133 |
{$ENDIF}
|
134 |
if Count > 0 then
|
135 |
Delete(Tail);
|
136 |
{$IFDEF ThreadSafe}
|
137 |
finally
|
138 |
LeaveCS;
|
139 |
end;
|
140 |
{$ENDIF}
|
141 |
end;
|
142 |
|
143 |
procedure TStDQue.PushHead(Data : Pointer);
|
144 |
begin
|
145 |
{$IFDEF ThreadSafe}
|
146 |
EnterCS;
|
147 |
try
|
148 |
{$ENDIF}
|
149 |
Insert(Data);
|
150 |
{$IFDEF ThreadSafe}
|
151 |
finally
|
152 |
LeaveCS;
|
153 |
end;
|
154 |
{$ENDIF}
|
155 |
end;
|
156 |
|
157 |
procedure TStDQue.PushTail(Data : Pointer);
|
158 |
begin
|
159 |
{$IFDEF ThreadSafe}
|
160 |
EnterCS;
|
161 |
try
|
162 |
{$ENDIF}
|
163 |
Append(Data);
|
164 |
{$IFDEF ThreadSafe}
|
165 |
finally
|
166 |
LeaveCS;
|
167 |
end;
|
168 |
{$ENDIF}
|
169 |
end;
|
170 |
|
171 |
|
172 |
end.
|