-
Notifications
You must be signed in to change notification settings - Fork 8
/
Matrix.pas
71 lines (59 loc) · 1.24 KB
/
Matrix.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
unit Matrix;
interface
uses
JS;
type
TMatrix = class
public
constructor Create (w, h: integer);
procedure SetValue(x, y: integer; value: JSValue);
function GetValue(x, y: integer): JSValue;
procedure Show;
function GetWidth: integer;
function GetHeight: integer;
// NOTE: no indexers yet?
//property Indexer[const x,y:integer]:JSValue read GetValue write SetValue; default;
private
table: TJSArray;
width: integer;
height: integer;
function IndexFor(x, y: integer): integer;
end;
implementation
constructor TMatrix.Create (w, h: integer);
begin
width := w;
height := h;
table := TJSArray.new(width * height);
end;
procedure TMatrix.SetValue(x, y: integer; value: JSValue);
begin
table[IndexFor(x, y)] := value;
end;
function TMatrix.GetValue(x, y: integer): JSValue;
begin
result := table[IndexFor(x, y)];
end;
function TMatrix.IndexFor(x, y: integer): integer;
begin
result := x + y * height;
end;
procedure TMatrix.Show;
var
x, y: integer;
begin
for x := 0 to width - 1 do
for y := 0 to height - 1 do
begin
writeln(x,',',y, ': ', GetValue(x, y));
end;
end;
function TMatrix.GetWidth: integer;
begin
result := width;
end;
function TMatrix.GetHeight: integer;
begin
result := height;
end;
end.