-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbasedata.pas
192 lines (161 loc) · 4.31 KB
/
basedata.pas
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
unit basedata;
{ Erik Donovan Johnson erikquiet@gmail.com
Free to use and modify in any way you want.
Example code. No warranty. Use at your own risk. }
{ data stack used by Behavior Tree. could be used for
any stack, and fleshed out with more classes }
interface
uses
Classes, SysUtils;
type
{ data class stores integer or single as implemented in subclasses }
TBehaviorData = class
end;
TBehaviorInt = class( TBehaviorData )
constructor create( ivalue : integer );
protected
value : integer;
end;
TBehaviorSingle = class( TBehaviorData )
constructor create( ivalue : single );
protected
value : single;
end;
TBehaviorDataStack = class { for storing data }
destructor destroy; override;
procedure Push(item : tobject);
function Pop : tobject;
function IsEmpty : boolean;
procedure pushint( i : integer );
function popint : integer;
function peekint( var i : integer ) : boolean; { look at what would have popped without popping }
procedure pushsingle( i : single );
function popsingle : single;
function peeksingle( var s: single ) : boolean; { look at what would have popped without popping }
function peekitem( var it : tobject ) : boolean;
private
stack : array of tobject;
end;
implementation
constructor TBehaviorInt.create( ivalue : integer );
begin
value := ivalue;
end;
constructor TBehaviorSingle.create( ivalue : single );
begin
value := iValue;
end;
//-------------------------------
destructor TBehaviorDataStack.destroy;
var i : integer;
item : TObject;
begin
for i := 0 to length( stack ) - 1 do
begin
item := stack[i];
if Item is TBehaviorData then
Item.Free;
end;
setlength( stack, 0 );
inherited;
end;
procedure TBehaviorDataStack.Push(item : tobject);
var l : integer;
begin
l := length( stack );
setlength(stack, l + 1 );
stack[l] := item;
end;
function TBehaviorDataStack.Pop : tobject;
var l : integer;
begin
result := nil;
l := length( stack );
if l > 0 then
begin
dec( l );
result := stack[l];
setlength( stack, l );
end;
end;
function TBehaviorDataStack.IsEmpty : boolean;
begin
result := length( stack ) = 0;
end;
procedure TBehaviorDataStack.pushint( i : integer );
begin
push( TBehaviorInt.create( i ));
end;
function TBehaviorDataStack.popint : integer;
var item : tobject;
begin
result := 0;
item := tobject( pop );
assert( assigned( item ));
assert( item is TBehaviorInt ); { enforced types to be safer }
result := TBehaviorInt( item ).value;
item.free;
end;
function TBehaviorDataStack.peekint( var i : integer ) : boolean; { look at what would have popped without popping }
var l : integer;
item : tobject;
begin
i := 0;
l := length( stack );
result := l > 0;
if result then
begin
dec( l );
item := stack[l];
result := item is TBehaviorInt;
if result then
i := TBehaviorInt( item ).value;
end;
end;
procedure TBehaviorDataStack.pushsingle( i : single );
begin
push( TBehaviorSingle.create( i ));
end;
function TBehaviorDataStack.popsingle : single;
var item : tobject;
begin
result := 0;
item := tobject( pop );
assert( assigned( item ));
assert( item is TBehaviorSingle ); { enforced types to be safer }
result := TBehaviorSingle( item ).value;
item.free;
end;
function TBehaviorDataStack.peeksingle( var s : single ) : boolean; { look at what would have popped without popping }
var l : integer;
item : tobject;
begin
s := 0;
l := length( stack );
result := l > 0;
if result then
begin
dec( l );
item := stack[l];
result := item is TBehaviorSingle;
if result then
s := TBehaviorSingle( item ).value;
end;
end;
function TBehaviorDataStack.peekitem( var it : tobject ) : boolean;
var l : integer;
item : tobject;
begin
it := nil;
l := length( stack );
result := l > 0;
if result then
begin
dec( l );
item := stack[l];
result := not ( it is TBehaviorData );
if result then
it := item;
end;
end;
end.