1
0

storeObjectArray.ts 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. import { StoreObject } from './storeObject';
  2. import { ArrayType } from '../../typeSystem/array_type';
  3. import { ArrayStoreValue } from './value/array_store_value';
  4. import { Location } from "../../memory/location";
  5. import { IStoreValue } from './value/istore_value';
  6. import { StoreValue } from './value/store_value';
  7. export class StoreObjectArray extends StoreObject {
  8. constructor (type: ArrayType, private _lines:number, private _columns:number, private loc_addresses: number[], readOnly = false) {
  9. super(type, -1, readOnly);
  10. }
  11. get lines () {
  12. return this._lines;
  13. }
  14. get columns () {
  15. return this._columns;
  16. }
  17. isCompatible (another: IStoreValue): boolean {
  18. if(another instanceof ArrayStoreValue) {
  19. const cols = another.columns == null ? 0 : another.columns;
  20. if(this.lines === another.lines && this.columns === cols) {
  21. return super.isCompatible(another);
  22. }
  23. }
  24. return false;
  25. }
  26. get isVector () {
  27. return (this.type as ArrayType).dimensions === 1;
  28. }
  29. /**@override
  30. * Returns the list of values stored by this array.
  31. * All returned values are compatible with @prop{StoreObject.type}
  32. */
  33. get value () {
  34. const values = [];
  35. for(let i = 0; i < this.addresses.length; i += 1) {
  36. const address = Location.find(this.addresses[i]);
  37. if (address != null) {
  38. values.push(address.value);
  39. } else {
  40. throw new Error("!!!Critical Error: variable "+this.id+" does not have a valid address. Loc-Address "+ this.locAddress);
  41. }
  42. }
  43. return values;
  44. }
  45. destroy () {
  46. let bool = true;
  47. for(let i = 0; i < this.addresses.length; i += 1) {
  48. bool = bool && Location.deallocate(this.addresses[i]);
  49. }
  50. return bool;
  51. }
  52. /**
  53. * @override
  54. *
  55. * Returns the address that contains a list of all addresses of the values present in the array
  56. */
  57. get locAddress (): number {
  58. throw new Error("!!!Internal Error: Cannot invoke locAddress on StoreObjectArray");
  59. }
  60. protected get addresses (): number[] {
  61. return this.loc_addresses;
  62. }
  63. getAt (line: number, column?: number): any {
  64. if(this.isVector) {
  65. if(column != null) {
  66. throw new Error(this.id + " is not a matrix!");
  67. }
  68. column = line;
  69. line = 0;
  70. } else if (column == null) {
  71. // this is a whole line...
  72. const values = [];
  73. for(let col = 0; col < this.columns; col += 1) {
  74. const index = this.getIndexOf(line, col);
  75. const address = Location.find(this.addresses[index])!;
  76. values.push(address.value);
  77. }
  78. return values;
  79. }
  80. const index = this.getIndexOf(line, column);
  81. const address = Location.find(this.addresses[index])!;
  82. return address.value;
  83. }
  84. setAt (value: StoreValue, line:number, column?: number): void {
  85. let used_dims = 1;
  86. if(column != null) {
  87. used_dims += 1;
  88. }
  89. if(!(this.type as ArrayType).canAccept(value.type, used_dims)) {
  90. throw new Error("!!!Internal Error: Attempting to insert an invalid value inside array "+this.id);
  91. }
  92. if(this.isVector) {
  93. if(column != null) {
  94. throw new Error(this.id + " is not a matrix!");
  95. }
  96. column = line;
  97. line = 0;
  98. } else if (column == null) {
  99. throw new Error("!!!Internal Error: Attempting to insert a line into matrix "+ this.id );
  100. }
  101. const pos = this.getIndexOf(line, column);
  102. Location.updateAddress(this.addresses[pos], value.get());
  103. }
  104. protected getIndexOf (line: number, column: number): number {
  105. return line * this.columns + column;
  106. }
  107. getLocAddressOf (line: number, column?: number): number | number[] {
  108. if(this.isVector) {
  109. if(column != null) {
  110. throw new Error(this.id + " is not a matrix!");
  111. }
  112. column = line;
  113. line = 0;
  114. } else if (column == null) {
  115. // this is a whole line...
  116. const values: number[] = [];
  117. for(let col = 0; col < this.columns; col += 1) {
  118. const index = this.getIndexOf(line, col);
  119. values.push(this.addresses[index]);
  120. }
  121. return values;
  122. }
  123. const index = this.getIndexOf(line, column);
  124. return this.addresses[index];
  125. }
  126. }