2
0

Matrix.pas 1.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. unit Matrix;
  2. interface
  3. uses
  4. JS;
  5. type
  6. TMatrix = class
  7. public
  8. constructor Create (w, h: integer);
  9. procedure SetValue(x, y: integer; value: JSValue);
  10. function GetValue(x, y: integer): JSValue;
  11. procedure Show;
  12. function GetWidth: integer;
  13. function GetHeight: integer;
  14. // NOTE: no indexers yet?
  15. //property Indexer[const x,y:integer]:JSValue read GetValue write SetValue; default;
  16. private
  17. table: TJSArray;
  18. width: integer;
  19. height: integer;
  20. function IndexFor(x, y: integer): integer;
  21. end;
  22. implementation
  23. constructor TMatrix.Create (w, h: integer);
  24. begin
  25. width := w;
  26. height := h;
  27. table := TJSArray.new(width * height);
  28. end;
  29. procedure TMatrix.SetValue(x, y: integer; value: JSValue);
  30. begin
  31. table[IndexFor(x, y)] := value;
  32. end;
  33. function TMatrix.GetValue(x, y: integer): JSValue;
  34. begin
  35. result := table[IndexFor(x, y)];
  36. end;
  37. function TMatrix.IndexFor(x, y: integer): integer;
  38. begin
  39. result := x + y * height;
  40. end;
  41. procedure TMatrix.Show;
  42. var
  43. x, y: integer;
  44. begin
  45. for x := 0 to width - 1 do
  46. for y := 0 to height - 1 do
  47. begin
  48. writeln(x,',',y, ': ', GetValue(x, y));
  49. end;
  50. end;
  51. function TMatrix.GetWidth: integer;
  52. begin
  53. result := width;
  54. end;
  55. function TMatrix.GetHeight: integer;
  56. begin
  57. result := height;
  58. end;
  59. end.