-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathlsparray.pas
145 lines (103 loc) · 4.75 KB
/
lsparray.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
(*----------------------------------------------------------------------------*)
(* Array-Funktionen *)
(*----------------------------------------------------------------------------*)
(* Author: Joachim Pimiskern, 1994-2004 *)
(*----------------------------------------------------------------------------*)
unit lsparray;
{$O+,F+}
interface
uses
lspglobl;
function LspMakeArray(Size: pNode): pNode;
function LspAref(Feld,Index: pNode): pNode;
procedure LspSetAtIndex(Feld, Wert: pNode; Index: longint);
function LspGetAtIndex(Feld: pNode; Index: longint): pNode;
procedure LspSwapArrayVals(Feld: pNode; Index1, Index2: longint);
implementation
uses
Classes, Sysutils,
lsperr, lsppredi, lspmath, lspcreat;
(*----------------------------------------------------------------------------*)
(* Ein Array mit Size Knoten liefern. *)
(*----------------------------------------------------------------------------*)
function LspMakeArray(Size: pNode): pNode;
var s,i : longint;
empty: pNode;
begin
result := nil;
empty := nil;
if (not LspIntegerp(Size)) then
raise ELispException.Create('ErrIntegerExpected','');
s := LspGetIntegerVal(Size);
LspNew(result);
result^.Typ := cLspArray;
result^.Size := s;
result^.ArrayVal := TMemoryStream.Create;
for i := 0 to s - 1 do
result^.ArrayVal.Write(empty,sizeof(pNode));
end;
(*----------------------------------------------------------------------------*)
(* Auf den Inhalt des Feldes an einer bestimmten Stelle zugreifen *)
(*----------------------------------------------------------------------------*)
function LspAref(Feld,Index: pNode): pNode;
var i: longint;
begin
result := nil;
if (not LspArrayp(Feld)) then
raise ELispException.Create('ErrArrayExpected','First argument wrong');
if (not LspIntegerp(Index)) then
raise ELispException.Create('ErrIntegerExpected','Second argument wrong');
i := LspGetIntegerVal(Index);
if ((i < 0) or (i >= Feld^.Size)) then
raise ELispException.Create('ErrArrayIndexOutOfRange',IntToStr(i));
Feld^.ArrayVal.Seek(i * sizeof(pNode),0);
Feld^.ArrayVal.Read(Result, sizeof(pNode));
end;
(*----------------------------------------------------------------------------*)
(* Ein Element des Feldes mit einem Wert belegen *)
(*----------------------------------------------------------------------------*)
procedure LspSetAtIndex(Feld, Wert: pNode; Index: longint);
begin
if (not LspArrayp(Feld)) then
raise ELispException.Create('ErrArrayExpected','First argument');
if ((Index < 0) or (Index >= Feld^.Size)) then
raise ELispException.Create('ErrArrayIndexOutOfRange',IntToStr(Index));
Feld^.ArrayVal.Seek(Index * sizeof(pNode),0);
Feld^.ArrayVal.Write(Wert, sizeof(pNode));
end;
(*----------------------------------------------------------------------------*)
(* Den Wert des Feldes an einer bestimmten Stelle holen *)
(*----------------------------------------------------------------------------*)
function LspGetAtIndex(Feld: pNode; Index: longint): pNode;
begin
Result := nil;
if (not LspArrayp(Feld)) then
raise ELispException.Create('ErrArrayExpected','First argument');
if ((Index < 0) or (Index >= Feld^.Size)) then
raise ELispException.Create('ErrArrayIndexOutOfRange',IntToStr(Index));
Feld^.ArrayVal.Seek(Index * sizeof(pNode),0);
Feld^.ArrayVal.Read(Result, sizeof(pNode));
end;
(*----------------------------------------------------------------------------*)
(* Zwei Werte eines Feldes vertauschen *)
(*----------------------------------------------------------------------------*)
procedure LspSwapArrayVals(Feld: pNode; Index1, Index2: longint);
var Value1,Value2: pNode;
begin
if (not LspArrayp(Feld)) then
raise ELispException.Create('ErrArrayExpected','First argument');
if ((Index1 < 0) or (Index1 >= Feld^.Size)) then
raise ELispException.Create('ErrArrayIndexOutOfRange',IntToStr(Index1));
if ((Index2 < 0) or (Index2 >= Feld^.Size)) then
raise ELispException.Create('ErrArrayIndexOutOfRange',IntToStr(Index2));
Feld^.ArrayVal.Seek(Index1 * sizeof(pNode),0);
Feld^.ArrayVal.Read(Value1, sizeof(pNode));
Feld^.ArrayVal.Seek(Index2 * sizeof(pNode),0);
Feld^.ArrayVal.Read(Value2, sizeof(pNode));
Feld^.ArrayVal.Seek(Index1 * sizeof(pNode),0);
Feld^.ArrayVal.Write(Value2, sizeof(pNode));
Feld^.ArrayVal.Seek(Index2 * sizeof(pNode),0);
Feld^.ArrayVal.Write(Value1, sizeof(pNode));
end;
end.