Vector.js 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. var __extends = this && this.t || function() {
  2. var extendStatics = function(t, r) {
  3. extendStatics = Object.setPrototypeOf || {
  4. __proto__: []
  5. } instanceof Array && function(t, r) {
  6. t.__proto__ = r;
  7. } || function(t, r) {
  8. for (var e in r) if (Object.prototype.hasOwnProperty.call(r, e)) t[e] = r[e];
  9. };
  10. return extendStatics(t, r);
  11. };
  12. return function(t, r) {
  13. if (typeof r !== "function" && r !== null) throw new TypeError("Class extends value " + String(r) + " is not a constructor or null");
  14. extendStatics(t, r);
  15. function __() {
  16. this.constructor = t;
  17. }
  18. t.prototype = r === null ? Object.create(r) : (__.prototype = r.prototype, new __);
  19. };
  20. }();
  21. var __generator = this && this.i || function(t, r) {
  22. var e = {
  23. label: 0,
  24. sent: function() {
  25. if (o[0] & 1) throw o[1];
  26. return o[1];
  27. },
  28. trys: [],
  29. ops: []
  30. }, n, i, o, s;
  31. return s = {
  32. next: verb(0),
  33. throw: verb(1),
  34. return: verb(2)
  35. }, typeof Symbol === "function" && (s[Symbol.iterator] = function() {
  36. return this;
  37. }), s;
  38. function verb(t) {
  39. return function(r) {
  40. return step([ t, r ]);
  41. };
  42. }
  43. function step(s) {
  44. if (n) throw new TypeError("Generator is already executing.");
  45. while (e) try {
  46. if (n = 1, i && (o = s[0] & 2 ? i["return"] : s[0] ? i["throw"] || ((o = i["return"]) && o.call(i),
  47. 0) : i.next) && !(o = o.call(i, s[1])).done) return o;
  48. if (i = 0, o) s = [ s[0] & 2, o.value ];
  49. switch (s[0]) {
  50. case 0:
  51. case 1:
  52. o = s;
  53. break;
  54. case 4:
  55. e.label++;
  56. return {
  57. value: s[1],
  58. done: false
  59. };
  60. case 5:
  61. e.label++;
  62. i = s[1];
  63. s = [ 0 ];
  64. continue;
  65. case 7:
  66. s = e.ops.pop();
  67. e.trys.pop();
  68. continue;
  69. default:
  70. if (!(o = e.trys, o = o.length > 0 && o[o.length - 1]) && (s[0] === 6 || s[0] === 2)) {
  71. e = 0;
  72. continue;
  73. }
  74. if (s[0] === 3 && (!o || s[1] > o[0] && s[1] < o[3])) {
  75. e.label = s[1];
  76. break;
  77. }
  78. if (s[0] === 6 && e.label < o[1]) {
  79. e.label = o[1];
  80. o = s;
  81. break;
  82. }
  83. if (o && e.label < o[2]) {
  84. e.label = o[2];
  85. e.ops.push(s);
  86. break;
  87. }
  88. if (o[2]) e.ops.pop();
  89. e.trys.pop();
  90. continue;
  91. }
  92. s = r.call(t, e);
  93. } catch (t) {
  94. s = [ 6, t ];
  95. i = 0;
  96. } finally {
  97. n = o = 0;
  98. }
  99. if (s[0] & 5) throw s[1];
  100. return {
  101. value: s[0] ? s[1] : void 0,
  102. done: true
  103. };
  104. }
  105. };
  106. var __read = this && this._ || function(t, r) {
  107. var e = typeof Symbol === "function" && t[Symbol.iterator];
  108. if (!e) return t;
  109. var n = e.call(t), i, o = [], s;
  110. try {
  111. while ((r === void 0 || r-- > 0) && !(i = n.next()).done) o.push(i.value);
  112. } catch (t) {
  113. s = {
  114. error: t
  115. };
  116. } finally {
  117. try {
  118. if (i && !i.done && (e = n["return"])) e.call(n);
  119. } finally {
  120. if (s) throw s.error;
  121. }
  122. }
  123. return o;
  124. };
  125. var __spreadArray = this && this.P || function(t, r, e) {
  126. if (e || arguments.length === 2) for (var n = 0, i = r.length, o; n < i; n++) {
  127. if (o || !(n in r)) {
  128. if (!o) o = Array.prototype.slice.call(r, 0, n);
  129. o[n] = r[n];
  130. }
  131. }
  132. return t.concat(o || Array.prototype.slice.call(r));
  133. };
  134. var __values = this && this.u || function(t) {
  135. var r = typeof Symbol === "function" && Symbol.iterator, e = r && t[r], n = 0;
  136. if (e) return e.call(t);
  137. if (t && typeof t.length === "number") return {
  138. next: function() {
  139. if (t && n >= t.length) t = void 0;
  140. return {
  141. value: t && t[n++],
  142. done: !t
  143. };
  144. }
  145. };
  146. throw new TypeError(r ? "Object is not iterable." : "Symbol.iterator is not defined.");
  147. };
  148. import SequentialContainer from "./Base";
  149. import { RandomIterator } from "./Base/RandomIterator";
  150. var VectorIterator = function(t) {
  151. __extends(VectorIterator, t);
  152. function VectorIterator() {
  153. return t !== null && t.apply(this, arguments) || this;
  154. }
  155. VectorIterator.prototype.copy = function() {
  156. return new VectorIterator(this.D, this.I, this.g, this.R, this.iteratorType);
  157. };
  158. return VectorIterator;
  159. }(RandomIterator);
  160. export { VectorIterator };
  161. var Vector = function(t) {
  162. __extends(Vector, t);
  163. function Vector(r, e) {
  164. if (r === void 0) {
  165. r = [];
  166. }
  167. if (e === void 0) {
  168. e = true;
  169. }
  170. var n = t.call(this) || this;
  171. if (Array.isArray(r)) {
  172. n.V = e ? __spreadArray([], __read(r), false) : r;
  173. n.o = r.length;
  174. } else {
  175. n.V = [];
  176. r.forEach((function(t) {
  177. return n.pushBack(t);
  178. }));
  179. }
  180. n.size = n.size.bind(n);
  181. n.getElementByPos = n.getElementByPos.bind(n);
  182. n.setElementByPos = n.setElementByPos.bind(n);
  183. return n;
  184. }
  185. Vector.prototype.clear = function() {
  186. this.o = 0;
  187. this.V.length = 0;
  188. };
  189. Vector.prototype.begin = function() {
  190. return new VectorIterator(0, this.size, this.getElementByPos, this.setElementByPos);
  191. };
  192. Vector.prototype.end = function() {
  193. return new VectorIterator(this.o, this.size, this.getElementByPos, this.setElementByPos);
  194. };
  195. Vector.prototype.rBegin = function() {
  196. return new VectorIterator(this.o - 1, this.size, this.getElementByPos, this.setElementByPos, 1);
  197. };
  198. Vector.prototype.rEnd = function() {
  199. return new VectorIterator(-1, this.size, this.getElementByPos, this.setElementByPos, 1);
  200. };
  201. Vector.prototype.front = function() {
  202. return this.V[0];
  203. };
  204. Vector.prototype.back = function() {
  205. return this.V[this.o - 1];
  206. };
  207. Vector.prototype.forEach = function(t) {
  208. for (var r = 0; r < this.o; ++r) {
  209. t(this.V[r], r);
  210. }
  211. };
  212. Vector.prototype.getElementByPos = function(t) {
  213. if (t < 0 || t > this.o - 1) {
  214. throw new RangeError;
  215. }
  216. return this.V[t];
  217. };
  218. Vector.prototype.eraseElementByPos = function(t) {
  219. if (t < 0 || t > this.o - 1) {
  220. throw new RangeError;
  221. }
  222. this.V.splice(t, 1);
  223. this.o -= 1;
  224. };
  225. Vector.prototype.eraseElementByValue = function(t) {
  226. var r = 0;
  227. for (var e = 0; e < this.o; ++e) {
  228. if (this.V[e] !== t) {
  229. this.V[r++] = this.V[e];
  230. }
  231. }
  232. this.o = this.V.length = r;
  233. };
  234. Vector.prototype.eraseElementByIterator = function(t) {
  235. var r = t.D;
  236. t = t.next();
  237. this.eraseElementByPos(r);
  238. return t;
  239. };
  240. Vector.prototype.pushBack = function(t) {
  241. this.V.push(t);
  242. this.o += 1;
  243. };
  244. Vector.prototype.popBack = function() {
  245. if (!this.o) return;
  246. this.V.pop();
  247. this.o -= 1;
  248. };
  249. Vector.prototype.setElementByPos = function(t, r) {
  250. if (t < 0 || t > this.o - 1) {
  251. throw new RangeError;
  252. }
  253. this.V[t] = r;
  254. };
  255. Vector.prototype.insert = function(t, r, e) {
  256. var n;
  257. if (e === void 0) {
  258. e = 1;
  259. }
  260. if (t < 0 || t > this.o) {
  261. throw new RangeError;
  262. }
  263. (n = this.V).splice.apply(n, __spreadArray([ t, 0 ], __read(new Array(e).fill(r)), false));
  264. this.o += e;
  265. };
  266. Vector.prototype.find = function(t) {
  267. for (var r = 0; r < this.o; ++r) {
  268. if (this.V[r] === t) {
  269. return new VectorIterator(r, this.size, this.getElementByPos, this.getElementByPos);
  270. }
  271. }
  272. return this.end();
  273. };
  274. Vector.prototype.reverse = function() {
  275. this.V.reverse();
  276. };
  277. Vector.prototype.unique = function() {
  278. var t = 1;
  279. for (var r = 1; r < this.o; ++r) {
  280. if (this.V[r] !== this.V[r - 1]) {
  281. this.V[t++] = this.V[r];
  282. }
  283. }
  284. this.o = this.V.length = t;
  285. };
  286. Vector.prototype.sort = function(t) {
  287. this.V.sort(t);
  288. };
  289. Vector.prototype[Symbol.iterator] = function() {
  290. return function() {
  291. return __generator(this, (function(t) {
  292. switch (t.label) {
  293. case 0:
  294. return [ 5, __values(this.V) ];
  295. case 1:
  296. return [ 2, t.sent() ];
  297. }
  298. }));
  299. }.bind(this)();
  300. };
  301. return Vector;
  302. }(SequentialContainer);
  303. export default Vector;