Vector.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. "use strict";
  2. Object.defineProperty(exports, "t", {
  3. value: true
  4. });
  5. exports.default = exports.VectorIterator = void 0;
  6. var _Base = _interopRequireDefault(require("./Base"));
  7. var _RandomIterator = require("./Base/RandomIterator");
  8. function _interopRequireDefault(t) {
  9. return t && t.t ? t : {
  10. default: t
  11. };
  12. }
  13. class VectorIterator extends _RandomIterator.RandomIterator {
  14. copy() {
  15. return new VectorIterator(this.I, this.D, this.g, this.m, this.iteratorType);
  16. }
  17. }
  18. exports.VectorIterator = VectorIterator;
  19. class Vector extends _Base.default {
  20. constructor(t = [], e = true) {
  21. super();
  22. if (Array.isArray(t)) {
  23. this.H = e ? [ ...t ] : t;
  24. this.o = t.length;
  25. } else {
  26. this.H = [];
  27. t.forEach((t => this.pushBack(t)));
  28. }
  29. this.size = this.size.bind(this);
  30. this.getElementByPos = this.getElementByPos.bind(this);
  31. this.setElementByPos = this.setElementByPos.bind(this);
  32. }
  33. clear() {
  34. this.o = 0;
  35. this.H.length = 0;
  36. }
  37. begin() {
  38. return new VectorIterator(0, this.size, this.getElementByPos, this.setElementByPos);
  39. }
  40. end() {
  41. return new VectorIterator(this.o, this.size, this.getElementByPos, this.setElementByPos);
  42. }
  43. rBegin() {
  44. return new VectorIterator(this.o - 1, this.size, this.getElementByPos, this.setElementByPos, 1);
  45. }
  46. rEnd() {
  47. return new VectorIterator(-1, this.size, this.getElementByPos, this.setElementByPos, 1);
  48. }
  49. front() {
  50. return this.H[0];
  51. }
  52. back() {
  53. return this.H[this.o - 1];
  54. }
  55. forEach(t) {
  56. for (let e = 0; e < this.o; ++e) {
  57. t(this.H[e], e);
  58. }
  59. }
  60. getElementByPos(t) {
  61. if (t < 0 || t > this.o - 1) {
  62. throw new RangeError;
  63. }
  64. return this.H[t];
  65. }
  66. eraseElementByPos(t) {
  67. if (t < 0 || t > this.o - 1) {
  68. throw new RangeError;
  69. }
  70. this.H.splice(t, 1);
  71. this.o -= 1;
  72. }
  73. eraseElementByValue(t) {
  74. let e = 0;
  75. for (let r = 0; r < this.o; ++r) {
  76. if (this.H[r] !== t) {
  77. this.H[e++] = this.H[r];
  78. }
  79. }
  80. this.o = this.H.length = e;
  81. }
  82. eraseElementByIterator(t) {
  83. const e = t.I;
  84. t = t.next();
  85. this.eraseElementByPos(e);
  86. return t;
  87. }
  88. pushBack(t) {
  89. this.H.push(t);
  90. this.o += 1;
  91. }
  92. popBack() {
  93. if (!this.o) return;
  94. this.H.pop();
  95. this.o -= 1;
  96. }
  97. setElementByPos(t, e) {
  98. if (t < 0 || t > this.o - 1) {
  99. throw new RangeError;
  100. }
  101. this.H[t] = e;
  102. }
  103. insert(t, e, r = 1) {
  104. if (t < 0 || t > this.o) {
  105. throw new RangeError;
  106. }
  107. this.H.splice(t, 0, ...new Array(r).fill(e));
  108. this.o += r;
  109. }
  110. find(t) {
  111. for (let e = 0; e < this.o; ++e) {
  112. if (this.H[e] === t) {
  113. return new VectorIterator(e, this.size, this.getElementByPos, this.getElementByPos);
  114. }
  115. }
  116. return this.end();
  117. }
  118. reverse() {
  119. this.H.reverse();
  120. }
  121. unique() {
  122. let t = 1;
  123. for (let e = 1; e < this.o; ++e) {
  124. if (this.H[e] !== this.H[e - 1]) {
  125. this.H[t++] = this.H[e];
  126. }
  127. }
  128. this.o = this.H.length = t;
  129. }
  130. sort(t) {
  131. this.H.sort(t);
  132. }
  133. [Symbol.iterator]() {
  134. return function*() {
  135. return yield* this.H;
  136. }.bind(this)();
  137. }
  138. }
  139. var _default = Vector;
  140. exports.default = _default;