array_store_value.ts 3.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. import { IStoreValue } from "./istore_value";
  2. import { ArrayType } from "../../../typeSystem/array_type";
  3. import { IType } from "../../../typeSystem/itype";
  4. import { Type } from "../../../typeSystem/type";
  5. import { StoreValueAddress } from "./store_value_address";
  6. export class ArrayStoreValue implements IStoreValue {
  7. public type: IType;
  8. public id?: String;
  9. public isConst: boolean;
  10. public lines: number;
  11. public columns?: number;
  12. public values: StoreValueAddress[];
  13. constructor(type: ArrayType, values: StoreValueAddress[], lines: number, columns?: number, id?: String, isConst = false) {
  14. this.type = type;
  15. this.id = id;
  16. this.isConst = isConst
  17. this.values = values;
  18. this.lines = lines;
  19. this.columns = columns;
  20. }
  21. get (): StoreValueAddress[] {
  22. return this.values;
  23. }
  24. /**
  25. * @deprecated
  26. * Potential not necessary since array access is occuring directly in the StoreObject
  27. * @param line
  28. * @param column
  29. */
  30. getAt (line: number, column?: number): IStoreValue {
  31. console.log("Column ", column);
  32. if(this.isVector()) {
  33. if(column != null) {
  34. throw new Error(this.id + " is not a matrix!");
  35. }
  36. column = line;
  37. line = 0;
  38. } else if (column == null) {
  39. const values: StoreValueAddress[] = [];
  40. for(let col = 0; col < this.columns!; col += 1) {
  41. const index = line * this.lines + col;
  42. values.push(this.values[index]);
  43. }
  44. const array_type = this.type as ArrayType
  45. const vector_type = new ArrayType(array_type.innerType as Type, array_type.dimensions - 1);
  46. return new ArrayStoreValue(vector_type, values, this.columns!, 0, this.id, this.isConst);
  47. }
  48. const index = line * this.lines + column;
  49. console.log("Get at: ",index);
  50. return this.values[index];
  51. }
  52. setAt (value: IStoreValue, line:number, column?: number): void {
  53. let used_dims = 1;
  54. if(column != null) {
  55. used_dims += 1;
  56. }
  57. let actual_line = line;
  58. let actual_column = column;
  59. if(!(this.type as ArrayType).canAccept(value.type, used_dims)) {
  60. throw new Error("!!!Internal Error: Attempting to insert an invalid value inside array "+this.id);
  61. }
  62. if(this.isVector()) {
  63. if(column != null) {
  64. throw new Error(this.id + " is not a matrix!");
  65. }
  66. actual_column = actual_line;
  67. actual_line = 0;
  68. } else if (column == null) {
  69. if(!(value instanceof ArrayStoreValue)) {
  70. throw new Error("Attempting to insert a single value as a line of matrix " + this.id);
  71. }
  72. const actual_values = value.get()
  73. for(let i = 0; i < this.columns!; i += 1) {
  74. const pos = actual_line * this.columns! + i;
  75. const val = actual_values[i]
  76. this.values[pos] = new StoreValueAddress(value.type, val.get(), actual_line, i, this.id, this.isConst);
  77. }
  78. }
  79. const columns = this.columns == null ? 0 : this.columns;
  80. const pos = actual_line * columns + actual_column!;
  81. this.values[pos] = new StoreValueAddress(value.type, value.get(), line, column, this.id, this.isConst);
  82. }
  83. inStore () {
  84. return this.id != null;
  85. }
  86. isVector (): boolean {
  87. return (this.type as ArrayType).dimensions == 1;
  88. }
  89. }