1
0

storeObjectArray.ts 4.2 KB

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