371b452878ea6d72b7a34d18027efbffd8ac730a32888e0c318f9ef8cd364d2211f40786e8e169a4f546761c034e115c5c15db04d4251da0b0e8276cf7fcb0 106 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154
  1. /*!
  2. * js-sdsl v4.3.0
  3. * https://github.com/js-sdsl/js-sdsl
  4. * (c) 2021-present ZLY201 <zilongyao1366@gmail.com>
  5. * MIT license
  6. */
  7. (function (global, factory) {
  8. typeof exports === 'object' && typeof module !== 'undefined' ? factory(exports) :
  9. typeof define === 'function' && define.amd ? define(['exports'], factory) :
  10. (global = typeof globalThis !== 'undefined' ? globalThis : global || self, factory(global.sdsl = {}));
  11. })(this, (function (exports) { 'use strict';
  12. /******************************************************************************
  13. Copyright (c) Microsoft Corporation.
  14. Permission to use, copy, modify, and/or distribute this software for any
  15. purpose with or without fee is hereby granted.
  16. THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
  17. REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  18. AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
  19. INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  20. LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
  21. OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  22. PERFORMANCE OF THIS SOFTWARE.
  23. ***************************************************************************** */
  24. /* global Reflect, Promise */
  25. var extendStatics = function (d, b) {
  26. extendStatics = Object.setPrototypeOf || {
  27. __proto__: []
  28. } instanceof Array && function (d, b) {
  29. d.__proto__ = b;
  30. } || function (d, b) {
  31. for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p];
  32. };
  33. return extendStatics(d, b);
  34. };
  35. function __extends(d, b) {
  36. if (typeof b !== "function" && b !== null) throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
  37. extendStatics(d, b);
  38. function __() {
  39. this.constructor = d;
  40. }
  41. d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
  42. }
  43. function __generator(thisArg, body) {
  44. var _ = {
  45. label: 0,
  46. sent: function () {
  47. if (t[0] & 1) throw t[1];
  48. return t[1];
  49. },
  50. trys: [],
  51. ops: []
  52. },
  53. f,
  54. y,
  55. t,
  56. g;
  57. return g = {
  58. next: verb(0),
  59. "throw": verb(1),
  60. "return": verb(2)
  61. }, typeof Symbol === "function" && (g[Symbol.iterator] = function () {
  62. return this;
  63. }), g;
  64. function verb(n) {
  65. return function (v) {
  66. return step([n, v]);
  67. };
  68. }
  69. function step(op) {
  70. if (f) throw new TypeError("Generator is already executing.");
  71. while (g && (g = 0, op[0] && (_ = 0)), _) try {
  72. if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
  73. if (y = 0, t) op = [op[0] & 2, t.value];
  74. switch (op[0]) {
  75. case 0:
  76. case 1:
  77. t = op;
  78. break;
  79. case 4:
  80. _.label++;
  81. return {
  82. value: op[1],
  83. done: false
  84. };
  85. case 5:
  86. _.label++;
  87. y = op[1];
  88. op = [0];
  89. continue;
  90. case 7:
  91. op = _.ops.pop();
  92. _.trys.pop();
  93. continue;
  94. default:
  95. if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) {
  96. _ = 0;
  97. continue;
  98. }
  99. if (op[0] === 3 && (!t || op[1] > t[0] && op[1] < t[3])) {
  100. _.label = op[1];
  101. break;
  102. }
  103. if (op[0] === 6 && _.label < t[1]) {
  104. _.label = t[1];
  105. t = op;
  106. break;
  107. }
  108. if (t && _.label < t[2]) {
  109. _.label = t[2];
  110. _.ops.push(op);
  111. break;
  112. }
  113. if (t[2]) _.ops.pop();
  114. _.trys.pop();
  115. continue;
  116. }
  117. op = body.call(thisArg, _);
  118. } catch (e) {
  119. op = [6, e];
  120. y = 0;
  121. } finally {
  122. f = t = 0;
  123. }
  124. if (op[0] & 5) throw op[1];
  125. return {
  126. value: op[0] ? op[1] : void 0,
  127. done: true
  128. };
  129. }
  130. }
  131. function __values(o) {
  132. var s = typeof Symbol === "function" && Symbol.iterator,
  133. m = s && o[s],
  134. i = 0;
  135. if (m) return m.call(o);
  136. if (o && typeof o.length === "number") return {
  137. next: function () {
  138. if (o && i >= o.length) o = void 0;
  139. return {
  140. value: o && o[i++],
  141. done: !o
  142. };
  143. }
  144. };
  145. throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
  146. }
  147. function __read(o, n) {
  148. var m = typeof Symbol === "function" && o[Symbol.iterator];
  149. if (!m) return o;
  150. var i = m.call(o),
  151. r,
  152. ar = [],
  153. e;
  154. try {
  155. while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
  156. } catch (error) {
  157. e = {
  158. error: error
  159. };
  160. } finally {
  161. try {
  162. if (r && !r.done && (m = i["return"])) m.call(i);
  163. } finally {
  164. if (e) throw e.error;
  165. }
  166. }
  167. return ar;
  168. }
  169. function __spreadArray(to, from, pack) {
  170. if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
  171. if (ar || !(i in from)) {
  172. if (!ar) ar = Array.prototype.slice.call(from, 0, i);
  173. ar[i] = from[i];
  174. }
  175. }
  176. return to.concat(ar || Array.prototype.slice.call(from));
  177. }
  178. var ContainerIterator = /** @class */function () {
  179. /**
  180. * @internal
  181. */
  182. function ContainerIterator(iteratorType) {
  183. if (iteratorType === void 0) {
  184. iteratorType = 0 /* IteratorType.NORMAL */;
  185. }
  186. this.iteratorType = iteratorType;
  187. }
  188. /**
  189. * @param iter - The other iterator you want to compare.
  190. * @returns Whether this equals to obj.
  191. * @example
  192. * container.find(1).equals(container.end());
  193. */
  194. ContainerIterator.prototype.equals = function (iter) {
  195. return this._node === iter._node;
  196. };
  197. return ContainerIterator;
  198. }();
  199. var Base = /** @class */function () {
  200. function Base() {
  201. /**
  202. * @description Container's size.
  203. * @internal
  204. */
  205. this._length = 0;
  206. }
  207. Object.defineProperty(Base.prototype, "length", {
  208. /**
  209. * @returns The size of the container.
  210. * @example
  211. * const container = new Vector([1, 2]);
  212. * console.log(container.length); // 2
  213. */
  214. get: function () {
  215. return this._length;
  216. },
  217. enumerable: false,
  218. configurable: true
  219. });
  220. /**
  221. * @returns The size of the container.
  222. * @example
  223. * const container = new Vector([1, 2]);
  224. * console.log(container.size()); // 2
  225. */
  226. Base.prototype.size = function () {
  227. return this._length;
  228. };
  229. /**
  230. * @returns Whether the container is empty.
  231. * @example
  232. * container.clear();
  233. * console.log(container.empty()); // true
  234. */
  235. Base.prototype.empty = function () {
  236. return this._length === 0;
  237. };
  238. return Base;
  239. }();
  240. var Container = /** @class */function (_super) {
  241. __extends(Container, _super);
  242. function Container() {
  243. return _super !== null && _super.apply(this, arguments) || this;
  244. }
  245. return Container;
  246. }(Base);
  247. var Stack = /** @class */function (_super) {
  248. __extends(Stack, _super);
  249. function Stack(container) {
  250. if (container === void 0) {
  251. container = [];
  252. }
  253. var _this = _super.call(this) || this;
  254. /**
  255. * @internal
  256. */
  257. _this._stack = [];
  258. var self = _this;
  259. container.forEach(function (el) {
  260. self.push(el);
  261. });
  262. return _this;
  263. }
  264. Stack.prototype.clear = function () {
  265. this._length = 0;
  266. this._stack = [];
  267. };
  268. /**
  269. * @description Insert element to stack's end.
  270. * @description The element you want to push to the back.
  271. * @returns The container length after erasing.
  272. */
  273. Stack.prototype.push = function (element) {
  274. this._stack.push(element);
  275. this._length += 1;
  276. return this._length;
  277. };
  278. /**
  279. * @description Removes the end element.
  280. * @returns The element you popped.
  281. */
  282. Stack.prototype.pop = function () {
  283. if (this._length === 0) return;
  284. this._length -= 1;
  285. return this._stack.pop();
  286. };
  287. /**
  288. * @description Accesses the end element.
  289. * @returns The last element.
  290. */
  291. Stack.prototype.top = function () {
  292. return this._stack[this._length - 1];
  293. };
  294. return Stack;
  295. }(Base);
  296. var Queue = /** @class */function (_super) {
  297. __extends(Queue, _super);
  298. function Queue(container) {
  299. if (container === void 0) {
  300. container = [];
  301. }
  302. var _this = _super.call(this) || this;
  303. /**
  304. * @internal
  305. */
  306. _this._first = 0;
  307. /**
  308. * @internal
  309. */
  310. _this._queue = [];
  311. var self = _this;
  312. container.forEach(function (el) {
  313. self.push(el);
  314. });
  315. return _this;
  316. }
  317. Queue.prototype.clear = function () {
  318. this._queue = [];
  319. this._length = this._first = 0;
  320. };
  321. /**
  322. * @description Inserts element to queue's end.
  323. * @param element - The element you want to push to the front.
  324. * @returns The container length after pushing.
  325. */
  326. Queue.prototype.push = function (element) {
  327. var capacity = this._queue.length;
  328. if (this._first / capacity > 0.5 /* QUEUE_CONSTANT.ALLOCATE_SIGMA */ && this._first + this._length >= capacity && capacity > 4096 /* QUEUE_CONSTANT.MIN_ALLOCATE_SIZE */) {
  329. var length_1 = this._length;
  330. for (var i = 0; i < length_1; ++i) {
  331. this._queue[i] = this._queue[this._first + i];
  332. }
  333. this._first = 0;
  334. this._queue[this._length] = element;
  335. } else this._queue[this._first + this._length] = element;
  336. return ++this._length;
  337. };
  338. /**
  339. * @description Removes the first element.
  340. * @returns The element you popped.
  341. */
  342. Queue.prototype.pop = function () {
  343. if (this._length === 0) return;
  344. var el = this._queue[this._first++];
  345. this._length -= 1;
  346. return el;
  347. };
  348. /**
  349. * @description Access the first element.
  350. * @returns The first element.
  351. */
  352. Queue.prototype.front = function () {
  353. if (this._length === 0) return;
  354. return this._queue[this._first];
  355. };
  356. return Queue;
  357. }(Base);
  358. var PriorityQueue = /** @class */function (_super) {
  359. __extends(PriorityQueue, _super);
  360. /**
  361. * @description PriorityQueue's constructor.
  362. * @param container - Initialize container, must have a forEach function.
  363. * @param cmp - Compare function.
  364. * @param copy - When the container is an array, you can choose to directly operate on the original object of
  365. * the array or perform a shallow copy. The default is shallow copy.
  366. * @example
  367. * new PriorityQueue();
  368. * new PriorityQueue([1, 2, 3]);
  369. * new PriorityQueue([1, 2, 3], (x, y) => x - y);
  370. * new PriorityQueue([1, 2, 3], (x, y) => x - y, false);
  371. */
  372. function PriorityQueue(container, cmp, copy) {
  373. if (container === void 0) {
  374. container = [];
  375. }
  376. if (cmp === void 0) {
  377. cmp = function (x, y) {
  378. if (x > y) return -1;
  379. if (x < y) return 1;
  380. return 0;
  381. };
  382. }
  383. if (copy === void 0) {
  384. copy = true;
  385. }
  386. var _this = _super.call(this) || this;
  387. _this._cmp = cmp;
  388. if (Array.isArray(container)) {
  389. _this._priorityQueue = copy ? __spreadArray([], __read(container), false) : container;
  390. } else {
  391. _this._priorityQueue = [];
  392. var self_1 = _this;
  393. container.forEach(function (el) {
  394. self_1._priorityQueue.push(el);
  395. });
  396. }
  397. _this._length = _this._priorityQueue.length;
  398. var halfLength = _this._length >> 1;
  399. for (var parent_1 = _this._length - 1 >> 1; parent_1 >= 0; --parent_1) {
  400. _this._pushDown(parent_1, halfLength);
  401. }
  402. return _this;
  403. }
  404. /**
  405. * @internal
  406. */
  407. PriorityQueue.prototype._pushUp = function (pos) {
  408. var item = this._priorityQueue[pos];
  409. while (pos > 0) {
  410. var parent_2 = pos - 1 >> 1;
  411. var parentItem = this._priorityQueue[parent_2];
  412. if (this._cmp(parentItem, item) <= 0) break;
  413. this._priorityQueue[pos] = parentItem;
  414. pos = parent_2;
  415. }
  416. this._priorityQueue[pos] = item;
  417. };
  418. /**
  419. * @internal
  420. */
  421. PriorityQueue.prototype._pushDown = function (pos, halfLength) {
  422. var item = this._priorityQueue[pos];
  423. while (pos < halfLength) {
  424. var left = pos << 1 | 1;
  425. var right = left + 1;
  426. var minItem = this._priorityQueue[left];
  427. if (right < this._length && this._cmp(minItem, this._priorityQueue[right]) > 0) {
  428. left = right;
  429. minItem = this._priorityQueue[right];
  430. }
  431. if (this._cmp(minItem, item) >= 0) break;
  432. this._priorityQueue[pos] = minItem;
  433. pos = left;
  434. }
  435. this._priorityQueue[pos] = item;
  436. };
  437. PriorityQueue.prototype.clear = function () {
  438. this._length = 0;
  439. this._priorityQueue.length = 0;
  440. };
  441. /**
  442. * @description Push element into a container in order.
  443. * @param item - The element you want to push.
  444. * @returns The size of heap after pushing.
  445. * @example
  446. * queue.push(1);
  447. */
  448. PriorityQueue.prototype.push = function (item) {
  449. this._priorityQueue.push(item);
  450. this._pushUp(this._length);
  451. this._length += 1;
  452. };
  453. /**
  454. * @description Removes the top element.
  455. * @returns The element you popped.
  456. * @example
  457. * queue.pop();
  458. */
  459. PriorityQueue.prototype.pop = function () {
  460. if (this._length === 0) return;
  461. var value = this._priorityQueue[0];
  462. var last = this._priorityQueue.pop();
  463. this._length -= 1;
  464. if (this._length) {
  465. this._priorityQueue[0] = last;
  466. this._pushDown(0, this._length >> 1);
  467. }
  468. return value;
  469. };
  470. /**
  471. * @description Accesses the top element.
  472. * @example
  473. * const top = queue.top();
  474. */
  475. PriorityQueue.prototype.top = function () {
  476. return this._priorityQueue[0];
  477. };
  478. /**
  479. * @description Check if element is in heap.
  480. * @param item - The item want to find.
  481. * @returns Whether element is in heap.
  482. * @example
  483. * const que = new PriorityQueue([], (x, y) => x.id - y.id);
  484. * const obj = { id: 1 };
  485. * que.push(obj);
  486. * console.log(que.find(obj)); // true
  487. */
  488. PriorityQueue.prototype.find = function (item) {
  489. return this._priorityQueue.indexOf(item) >= 0;
  490. };
  491. /**
  492. * @description Remove specified item from heap.
  493. * @param item - The item want to remove.
  494. * @returns Whether remove success.
  495. * @example
  496. * const que = new PriorityQueue([], (x, y) => x.id - y.id);
  497. * const obj = { id: 1 };
  498. * que.push(obj);
  499. * que.remove(obj);
  500. */
  501. PriorityQueue.prototype.remove = function (item) {
  502. var index = this._priorityQueue.indexOf(item);
  503. if (index < 0) return false;
  504. if (index === 0) {
  505. this.pop();
  506. } else if (index === this._length - 1) {
  507. this._priorityQueue.pop();
  508. this._length -= 1;
  509. } else {
  510. this._priorityQueue.splice(index, 1, this._priorityQueue.pop());
  511. this._length -= 1;
  512. this._pushUp(index);
  513. this._pushDown(index, this._length >> 1);
  514. }
  515. return true;
  516. };
  517. /**
  518. * @description Update item and it's pos in the heap.
  519. * @param item - The item want to update.
  520. * @returns Whether update success.
  521. * @example
  522. * const que = new PriorityQueue([], (x, y) => x.id - y.id);
  523. * const obj = { id: 1 };
  524. * que.push(obj);
  525. * obj.id = 2;
  526. * que.updateItem(obj);
  527. */
  528. PriorityQueue.prototype.updateItem = function (item) {
  529. var index = this._priorityQueue.indexOf(item);
  530. if (index < 0) return false;
  531. this._pushUp(index);
  532. this._pushDown(index, this._length >> 1);
  533. return true;
  534. };
  535. /**
  536. * @returns Return a copy array of heap.
  537. * @example
  538. * const arr = queue.toArray();
  539. */
  540. PriorityQueue.prototype.toArray = function () {
  541. return __spreadArray([], __read(this._priorityQueue), false);
  542. };
  543. return PriorityQueue;
  544. }(Base);
  545. var SequentialContainer = /** @class */function (_super) {
  546. __extends(SequentialContainer, _super);
  547. function SequentialContainer() {
  548. return _super !== null && _super.apply(this, arguments) || this;
  549. }
  550. return SequentialContainer;
  551. }(Container);
  552. /**
  553. * @description Throw an iterator access error.
  554. * @internal
  555. */
  556. function throwIteratorAccessError() {
  557. throw new RangeError('Iterator access denied!');
  558. }
  559. var RandomIterator = /** @class */function (_super) {
  560. __extends(RandomIterator, _super);
  561. /**
  562. * @internal
  563. */
  564. function RandomIterator(index, iteratorType) {
  565. var _this = _super.call(this, iteratorType) || this;
  566. _this._node = index;
  567. if (_this.iteratorType === 0 /* IteratorType.NORMAL */) {
  568. _this.pre = function () {
  569. if (this._node === 0) {
  570. throwIteratorAccessError();
  571. }
  572. this._node -= 1;
  573. return this;
  574. };
  575. _this.next = function () {
  576. if (this._node === this.container.size()) {
  577. throwIteratorAccessError();
  578. }
  579. this._node += 1;
  580. return this;
  581. };
  582. } else {
  583. _this.pre = function () {
  584. if (this._node === this.container.size() - 1) {
  585. throwIteratorAccessError();
  586. }
  587. this._node += 1;
  588. return this;
  589. };
  590. _this.next = function () {
  591. if (this._node === -1) {
  592. throwIteratorAccessError();
  593. }
  594. this._node -= 1;
  595. return this;
  596. };
  597. }
  598. return _this;
  599. }
  600. Object.defineProperty(RandomIterator.prototype, "pointer", {
  601. get: function () {
  602. return this.container.getElementByPos(this._node);
  603. },
  604. set: function (newValue) {
  605. this.container.setElementByPos(this._node, newValue);
  606. },
  607. enumerable: false,
  608. configurable: true
  609. });
  610. return RandomIterator;
  611. }(ContainerIterator);
  612. var VectorIterator = /** @class */function (_super) {
  613. __extends(VectorIterator, _super);
  614. function VectorIterator(node, container, iteratorType) {
  615. var _this = _super.call(this, node, iteratorType) || this;
  616. _this.container = container;
  617. return _this;
  618. }
  619. VectorIterator.prototype.copy = function () {
  620. return new VectorIterator(this._node, this.container, this.iteratorType);
  621. };
  622. return VectorIterator;
  623. }(RandomIterator);
  624. var Vector = /** @class */function (_super) {
  625. __extends(Vector, _super);
  626. /**
  627. * @param container - Initialize container, must have a forEach function.
  628. * @param copy - When the container is an array, you can choose to directly operate on the original object of
  629. * the array or perform a shallow copy. The default is shallow copy.
  630. */
  631. function Vector(container, copy) {
  632. if (container === void 0) {
  633. container = [];
  634. }
  635. if (copy === void 0) {
  636. copy = true;
  637. }
  638. var _this = _super.call(this) || this;
  639. if (Array.isArray(container)) {
  640. _this._vector = copy ? __spreadArray([], __read(container), false) : container;
  641. _this._length = container.length;
  642. } else {
  643. _this._vector = [];
  644. var self_1 = _this;
  645. container.forEach(function (el) {
  646. self_1.pushBack(el);
  647. });
  648. }
  649. return _this;
  650. }
  651. Vector.prototype.clear = function () {
  652. this._length = 0;
  653. this._vector.length = 0;
  654. };
  655. Vector.prototype.begin = function () {
  656. return new VectorIterator(0, this);
  657. };
  658. Vector.prototype.end = function () {
  659. return new VectorIterator(this._length, this);
  660. };
  661. Vector.prototype.rBegin = function () {
  662. return new VectorIterator(this._length - 1, this, 1 /* IteratorType.REVERSE */);
  663. };
  664. Vector.prototype.rEnd = function () {
  665. return new VectorIterator(-1, this, 1 /* IteratorType.REVERSE */);
  666. };
  667. Vector.prototype.front = function () {
  668. return this._vector[0];
  669. };
  670. Vector.prototype.back = function () {
  671. return this._vector[this._length - 1];
  672. };
  673. Vector.prototype.getElementByPos = function (pos) {
  674. if (pos < 0 || pos > this._length - 1) {
  675. throw new RangeError();
  676. }
  677. return this._vector[pos];
  678. };
  679. Vector.prototype.eraseElementByPos = function (pos) {
  680. if (pos < 0 || pos > this._length - 1) {
  681. throw new RangeError();
  682. }
  683. this._vector.splice(pos, 1);
  684. this._length -= 1;
  685. return this._length;
  686. };
  687. Vector.prototype.eraseElementByValue = function (value) {
  688. var index = 0;
  689. for (var i = 0; i < this._length; ++i) {
  690. if (this._vector[i] !== value) {
  691. this._vector[index++] = this._vector[i];
  692. }
  693. }
  694. this._length = this._vector.length = index;
  695. return this._length;
  696. };
  697. Vector.prototype.eraseElementByIterator = function (iter) {
  698. var _node = iter._node;
  699. iter = iter.next();
  700. this.eraseElementByPos(_node);
  701. return iter;
  702. };
  703. Vector.prototype.pushBack = function (element) {
  704. this._vector.push(element);
  705. this._length += 1;
  706. return this._length;
  707. };
  708. Vector.prototype.popBack = function () {
  709. if (this._length === 0) return;
  710. this._length -= 1;
  711. return this._vector.pop();
  712. };
  713. Vector.prototype.setElementByPos = function (pos, element) {
  714. if (pos < 0 || pos > this._length - 1) {
  715. throw new RangeError();
  716. }
  717. this._vector[pos] = element;
  718. };
  719. Vector.prototype.insert = function (pos, element, num) {
  720. var _a;
  721. if (num === void 0) {
  722. num = 1;
  723. }
  724. if (pos < 0 || pos > this._length) {
  725. throw new RangeError();
  726. }
  727. (_a = this._vector).splice.apply(_a, __spreadArray([pos, 0], __read(new Array(num).fill(element)), false));
  728. this._length += num;
  729. return this._length;
  730. };
  731. Vector.prototype.find = function (element) {
  732. for (var i = 0; i < this._length; ++i) {
  733. if (this._vector[i] === element) {
  734. return new VectorIterator(i, this);
  735. }
  736. }
  737. return this.end();
  738. };
  739. Vector.prototype.reverse = function () {
  740. this._vector.reverse();
  741. };
  742. Vector.prototype.unique = function () {
  743. var index = 1;
  744. for (var i = 1; i < this._length; ++i) {
  745. if (this._vector[i] !== this._vector[i - 1]) {
  746. this._vector[index++] = this._vector[i];
  747. }
  748. }
  749. this._length = this._vector.length = index;
  750. return this._length;
  751. };
  752. Vector.prototype.sort = function (cmp) {
  753. this._vector.sort(cmp);
  754. };
  755. Vector.prototype.forEach = function (callback) {
  756. for (var i = 0; i < this._length; ++i) {
  757. callback(this._vector[i], i, this);
  758. }
  759. };
  760. Vector.prototype[Symbol.iterator] = function () {
  761. return function () {
  762. return __generator(this, function (_a) {
  763. switch (_a.label) {
  764. case 0:
  765. return [5 /*yield**/, __values(this._vector)];
  766. case 1:
  767. _a.sent();
  768. return [2 /*return*/];
  769. }
  770. });
  771. }.bind(this)();
  772. };
  773. return Vector;
  774. }(SequentialContainer);
  775. var LinkListIterator = /** @class */function (_super) {
  776. __extends(LinkListIterator, _super);
  777. /**
  778. * @internal
  779. */
  780. function LinkListIterator(_node, _header, container, iteratorType) {
  781. var _this = _super.call(this, iteratorType) || this;
  782. _this._node = _node;
  783. _this._header = _header;
  784. _this.container = container;
  785. if (_this.iteratorType === 0 /* IteratorType.NORMAL */) {
  786. _this.pre = function () {
  787. if (this._node._pre === this._header) {
  788. throwIteratorAccessError();
  789. }
  790. this._node = this._node._pre;
  791. return this;
  792. };
  793. _this.next = function () {
  794. if (this._node === this._header) {
  795. throwIteratorAccessError();
  796. }
  797. this._node = this._node._next;
  798. return this;
  799. };
  800. } else {
  801. _this.pre = function () {
  802. if (this._node._next === this._header) {
  803. throwIteratorAccessError();
  804. }
  805. this._node = this._node._next;
  806. return this;
  807. };
  808. _this.next = function () {
  809. if (this._node === this._header) {
  810. throwIteratorAccessError();
  811. }
  812. this._node = this._node._pre;
  813. return this;
  814. };
  815. }
  816. return _this;
  817. }
  818. Object.defineProperty(LinkListIterator.prototype, "pointer", {
  819. get: function () {
  820. if (this._node === this._header) {
  821. throwIteratorAccessError();
  822. }
  823. return this._node._value;
  824. },
  825. set: function (newValue) {
  826. if (this._node === this._header) {
  827. throwIteratorAccessError();
  828. }
  829. this._node._value = newValue;
  830. },
  831. enumerable: false,
  832. configurable: true
  833. });
  834. LinkListIterator.prototype.copy = function () {
  835. return new LinkListIterator(this._node, this._header, this.container, this.iteratorType);
  836. };
  837. return LinkListIterator;
  838. }(ContainerIterator);
  839. var LinkList = /** @class */function (_super) {
  840. __extends(LinkList, _super);
  841. function LinkList(container) {
  842. if (container === void 0) {
  843. container = [];
  844. }
  845. var _this = _super.call(this) || this;
  846. _this._header = {};
  847. _this._head = _this._tail = _this._header._pre = _this._header._next = _this._header;
  848. var self = _this;
  849. container.forEach(function (el) {
  850. self.pushBack(el);
  851. });
  852. return _this;
  853. }
  854. /**
  855. * @internal
  856. */
  857. LinkList.prototype._eraseNode = function (node) {
  858. var _pre = node._pre,
  859. _next = node._next;
  860. _pre._next = _next;
  861. _next._pre = _pre;
  862. if (node === this._head) {
  863. this._head = _next;
  864. }
  865. if (node === this._tail) {
  866. this._tail = _pre;
  867. }
  868. this._length -= 1;
  869. };
  870. /**
  871. * @internal
  872. */
  873. LinkList.prototype._insertNode = function (value, pre) {
  874. var next = pre._next;
  875. var node = {
  876. _value: value,
  877. _pre: pre,
  878. _next: next
  879. };
  880. pre._next = node;
  881. next._pre = node;
  882. if (pre === this._header) {
  883. this._head = node;
  884. }
  885. if (next === this._header) {
  886. this._tail = node;
  887. }
  888. this._length += 1;
  889. };
  890. LinkList.prototype.clear = function () {
  891. this._length = 0;
  892. this._head = this._tail = this._header._pre = this._header._next = this._header;
  893. };
  894. LinkList.prototype.begin = function () {
  895. return new LinkListIterator(this._head, this._header, this);
  896. };
  897. LinkList.prototype.end = function () {
  898. return new LinkListIterator(this._header, this._header, this);
  899. };
  900. LinkList.prototype.rBegin = function () {
  901. return new LinkListIterator(this._tail, this._header, this, 1 /* IteratorType.REVERSE */);
  902. };
  903. LinkList.prototype.rEnd = function () {
  904. return new LinkListIterator(this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  905. };
  906. LinkList.prototype.front = function () {
  907. return this._head._value;
  908. };
  909. LinkList.prototype.back = function () {
  910. return this._tail._value;
  911. };
  912. LinkList.prototype.getElementByPos = function (pos) {
  913. if (pos < 0 || pos > this._length - 1) {
  914. throw new RangeError();
  915. }
  916. var curNode = this._head;
  917. while (pos--) {
  918. curNode = curNode._next;
  919. }
  920. return curNode._value;
  921. };
  922. LinkList.prototype.eraseElementByPos = function (pos) {
  923. if (pos < 0 || pos > this._length - 1) {
  924. throw new RangeError();
  925. }
  926. var curNode = this._head;
  927. while (pos--) {
  928. curNode = curNode._next;
  929. }
  930. this._eraseNode(curNode);
  931. return this._length;
  932. };
  933. LinkList.prototype.eraseElementByValue = function (_value) {
  934. var curNode = this._head;
  935. while (curNode !== this._header) {
  936. if (curNode._value === _value) {
  937. this._eraseNode(curNode);
  938. }
  939. curNode = curNode._next;
  940. }
  941. return this._length;
  942. };
  943. LinkList.prototype.eraseElementByIterator = function (iter) {
  944. var node = iter._node;
  945. if (node === this._header) {
  946. throwIteratorAccessError();
  947. }
  948. iter = iter.next();
  949. this._eraseNode(node);
  950. return iter;
  951. };
  952. LinkList.prototype.pushBack = function (element) {
  953. this._insertNode(element, this._tail);
  954. return this._length;
  955. };
  956. LinkList.prototype.popBack = function () {
  957. if (this._length === 0) return;
  958. var value = this._tail._value;
  959. this._eraseNode(this._tail);
  960. return value;
  961. };
  962. /**
  963. * @description Push an element to the front.
  964. * @param element - The element you want to push.
  965. * @returns The size of queue after pushing.
  966. */
  967. LinkList.prototype.pushFront = function (element) {
  968. this._insertNode(element, this._header);
  969. return this._length;
  970. };
  971. /**
  972. * @description Removes the first element.
  973. * @returns The element you popped.
  974. */
  975. LinkList.prototype.popFront = function () {
  976. if (this._length === 0) return;
  977. var value = this._head._value;
  978. this._eraseNode(this._head);
  979. return value;
  980. };
  981. LinkList.prototype.setElementByPos = function (pos, element) {
  982. if (pos < 0 || pos > this._length - 1) {
  983. throw new RangeError();
  984. }
  985. var curNode = this._head;
  986. while (pos--) {
  987. curNode = curNode._next;
  988. }
  989. curNode._value = element;
  990. };
  991. LinkList.prototype.insert = function (pos, element, num) {
  992. if (num === void 0) {
  993. num = 1;
  994. }
  995. if (pos < 0 || pos > this._length) {
  996. throw new RangeError();
  997. }
  998. if (num <= 0) return this._length;
  999. if (pos === 0) {
  1000. while (num--) this.pushFront(element);
  1001. } else if (pos === this._length) {
  1002. while (num--) this.pushBack(element);
  1003. } else {
  1004. var curNode = this._head;
  1005. for (var i = 1; i < pos; ++i) {
  1006. curNode = curNode._next;
  1007. }
  1008. var next = curNode._next;
  1009. this._length += num;
  1010. while (num--) {
  1011. curNode._next = {
  1012. _value: element,
  1013. _pre: curNode
  1014. };
  1015. curNode._next._pre = curNode;
  1016. curNode = curNode._next;
  1017. }
  1018. curNode._next = next;
  1019. next._pre = curNode;
  1020. }
  1021. return this._length;
  1022. };
  1023. LinkList.prototype.find = function (element) {
  1024. var curNode = this._head;
  1025. while (curNode !== this._header) {
  1026. if (curNode._value === element) {
  1027. return new LinkListIterator(curNode, this._header, this);
  1028. }
  1029. curNode = curNode._next;
  1030. }
  1031. return this.end();
  1032. };
  1033. LinkList.prototype.reverse = function () {
  1034. if (this._length <= 1) return;
  1035. var pHead = this._head;
  1036. var pTail = this._tail;
  1037. var cnt = 0;
  1038. while (cnt << 1 < this._length) {
  1039. var tmp = pHead._value;
  1040. pHead._value = pTail._value;
  1041. pTail._value = tmp;
  1042. pHead = pHead._next;
  1043. pTail = pTail._pre;
  1044. cnt += 1;
  1045. }
  1046. };
  1047. LinkList.prototype.unique = function () {
  1048. if (this._length <= 1) {
  1049. return this._length;
  1050. }
  1051. var curNode = this._head;
  1052. while (curNode !== this._header) {
  1053. var tmpNode = curNode;
  1054. while (tmpNode._next !== this._header && tmpNode._value === tmpNode._next._value) {
  1055. tmpNode = tmpNode._next;
  1056. this._length -= 1;
  1057. }
  1058. curNode._next = tmpNode._next;
  1059. curNode._next._pre = curNode;
  1060. curNode = curNode._next;
  1061. }
  1062. return this._length;
  1063. };
  1064. LinkList.prototype.sort = function (cmp) {
  1065. if (this._length <= 1) return;
  1066. var arr = [];
  1067. this.forEach(function (el) {
  1068. arr.push(el);
  1069. });
  1070. arr.sort(cmp);
  1071. var curNode = this._head;
  1072. arr.forEach(function (element) {
  1073. curNode._value = element;
  1074. curNode = curNode._next;
  1075. });
  1076. };
  1077. /**
  1078. * @description Merges two sorted lists.
  1079. * @param list - The other list you want to merge (must be sorted).
  1080. * @returns The size of list after merging.
  1081. * @example
  1082. * const linkA = new LinkList([1, 3, 5]);
  1083. * const linkB = new LinkList([2, 4, 6]);
  1084. * linkA.merge(linkB); // [1, 2, 3, 4, 5];
  1085. */
  1086. LinkList.prototype.merge = function (list) {
  1087. var self = this;
  1088. if (this._length === 0) {
  1089. list.forEach(function (el) {
  1090. self.pushBack(el);
  1091. });
  1092. } else {
  1093. var curNode_1 = this._head;
  1094. list.forEach(function (el) {
  1095. while (curNode_1 !== self._header && curNode_1._value <= el) {
  1096. curNode_1 = curNode_1._next;
  1097. }
  1098. self._insertNode(el, curNode_1._pre);
  1099. });
  1100. }
  1101. return this._length;
  1102. };
  1103. LinkList.prototype.forEach = function (callback) {
  1104. var curNode = this._head;
  1105. var index = 0;
  1106. while (curNode !== this._header) {
  1107. callback(curNode._value, index++, this);
  1108. curNode = curNode._next;
  1109. }
  1110. };
  1111. LinkList.prototype[Symbol.iterator] = function () {
  1112. return function () {
  1113. var curNode;
  1114. return __generator(this, function (_a) {
  1115. switch (_a.label) {
  1116. case 0:
  1117. if (this._length === 0) return [2 /*return*/];
  1118. curNode = this._head;
  1119. _a.label = 1;
  1120. case 1:
  1121. if (!(curNode !== this._header)) return [3 /*break*/, 3];
  1122. return [4 /*yield*/, curNode._value];
  1123. case 2:
  1124. _a.sent();
  1125. curNode = curNode._next;
  1126. return [3 /*break*/, 1];
  1127. case 3:
  1128. return [2 /*return*/];
  1129. }
  1130. });
  1131. }.bind(this)();
  1132. };
  1133. return LinkList;
  1134. }(SequentialContainer);
  1135. var DequeIterator = /** @class */function (_super) {
  1136. __extends(DequeIterator, _super);
  1137. function DequeIterator(node, container, iteratorType) {
  1138. var _this = _super.call(this, node, iteratorType) || this;
  1139. _this.container = container;
  1140. return _this;
  1141. }
  1142. DequeIterator.prototype.copy = function () {
  1143. return new DequeIterator(this._node, this.container, this.iteratorType);
  1144. };
  1145. return DequeIterator;
  1146. }(RandomIterator);
  1147. var Deque = /** @class */function (_super) {
  1148. __extends(Deque, _super);
  1149. function Deque(container, _bucketSize) {
  1150. if (container === void 0) {
  1151. container = [];
  1152. }
  1153. if (_bucketSize === void 0) {
  1154. _bucketSize = 1 << 12;
  1155. }
  1156. var _this = _super.call(this) || this;
  1157. /**
  1158. * @internal
  1159. */
  1160. _this._first = 0;
  1161. /**
  1162. * @internal
  1163. */
  1164. _this._curFirst = 0;
  1165. /**
  1166. * @internal
  1167. */
  1168. _this._last = 0;
  1169. /**
  1170. * @internal
  1171. */
  1172. _this._curLast = 0;
  1173. /**
  1174. * @internal
  1175. */
  1176. _this._bucketNum = 0;
  1177. /**
  1178. * @internal
  1179. */
  1180. _this._map = [];
  1181. var _length = function () {
  1182. if (typeof container.length === "number") return container.length;
  1183. if (typeof container.size === "number") return container.size;
  1184. if (typeof container.size === "function") return container.size();
  1185. throw new TypeError("Cannot get the length or size of the container");
  1186. }();
  1187. _this._bucketSize = _bucketSize;
  1188. _this._bucketNum = Math.max(Math.ceil(_length / _this._bucketSize), 1);
  1189. for (var i = 0; i < _this._bucketNum; ++i) {
  1190. _this._map.push(new Array(_this._bucketSize));
  1191. }
  1192. var needBucketNum = Math.ceil(_length / _this._bucketSize);
  1193. _this._first = _this._last = (_this._bucketNum >> 1) - (needBucketNum >> 1);
  1194. _this._curFirst = _this._curLast = _this._bucketSize - _length % _this._bucketSize >> 1;
  1195. var self = _this;
  1196. container.forEach(function (element) {
  1197. self.pushBack(element);
  1198. });
  1199. return _this;
  1200. }
  1201. /**
  1202. * @description Growth the Deque.
  1203. * @internal
  1204. */
  1205. Deque.prototype._reAllocate = function () {
  1206. var newMap = [];
  1207. var addBucketNum = Math.max(this._bucketNum >> 1, 1);
  1208. for (var i = 0; i < addBucketNum; ++i) {
  1209. newMap[i] = new Array(this._bucketSize);
  1210. }
  1211. for (var i = this._first; i < this._bucketNum; ++i) {
  1212. newMap[newMap.length] = this._map[i];
  1213. }
  1214. for (var i = 0; i < this._last; ++i) {
  1215. newMap[newMap.length] = this._map[i];
  1216. }
  1217. newMap[newMap.length] = __spreadArray([], __read(this._map[this._last]), false);
  1218. this._first = addBucketNum;
  1219. this._last = newMap.length - 1;
  1220. for (var i = 0; i < addBucketNum; ++i) {
  1221. newMap[newMap.length] = new Array(this._bucketSize);
  1222. }
  1223. this._map = newMap;
  1224. this._bucketNum = newMap.length;
  1225. };
  1226. /**
  1227. * @description Get the bucket position of the element and the pointer position by index.
  1228. * @param pos - The element's index.
  1229. * @internal
  1230. */
  1231. Deque.prototype._getElementIndex = function (pos) {
  1232. var offset = this._curFirst + pos + 1;
  1233. var offsetRemainder = offset % this._bucketSize;
  1234. var curNodePointerIndex = offsetRemainder - 1;
  1235. var curNodeBucketIndex = this._first + (offset - offsetRemainder) / this._bucketSize;
  1236. if (offsetRemainder === 0) curNodeBucketIndex -= 1;
  1237. curNodeBucketIndex %= this._bucketNum;
  1238. if (curNodePointerIndex < 0) curNodePointerIndex += this._bucketSize;
  1239. return {
  1240. curNodeBucketIndex: curNodeBucketIndex,
  1241. curNodePointerIndex: curNodePointerIndex
  1242. };
  1243. };
  1244. Deque.prototype.clear = function () {
  1245. this._map = [new Array(this._bucketSize)];
  1246. this._bucketNum = 1;
  1247. this._first = this._last = this._length = 0;
  1248. this._curFirst = this._curLast = this._bucketSize >> 1;
  1249. };
  1250. Deque.prototype.begin = function () {
  1251. return new DequeIterator(0, this);
  1252. };
  1253. Deque.prototype.end = function () {
  1254. return new DequeIterator(this._length, this);
  1255. };
  1256. Deque.prototype.rBegin = function () {
  1257. return new DequeIterator(this._length - 1, this, 1 /* IteratorType.REVERSE */);
  1258. };
  1259. Deque.prototype.rEnd = function () {
  1260. return new DequeIterator(-1, this, 1 /* IteratorType.REVERSE */);
  1261. };
  1262. Deque.prototype.front = function () {
  1263. if (this._length === 0) return;
  1264. return this._map[this._first][this._curFirst];
  1265. };
  1266. Deque.prototype.back = function () {
  1267. if (this._length === 0) return;
  1268. return this._map[this._last][this._curLast];
  1269. };
  1270. Deque.prototype.pushBack = function (element) {
  1271. if (this._length) {
  1272. if (this._curLast < this._bucketSize - 1) {
  1273. this._curLast += 1;
  1274. } else if (this._last < this._bucketNum - 1) {
  1275. this._last += 1;
  1276. this._curLast = 0;
  1277. } else {
  1278. this._last = 0;
  1279. this._curLast = 0;
  1280. }
  1281. if (this._last === this._first && this._curLast === this._curFirst) this._reAllocate();
  1282. }
  1283. this._length += 1;
  1284. this._map[this._last][this._curLast] = element;
  1285. return this._length;
  1286. };
  1287. Deque.prototype.popBack = function () {
  1288. if (this._length === 0) return;
  1289. var value = this._map[this._last][this._curLast];
  1290. if (this._length !== 1) {
  1291. if (this._curLast > 0) {
  1292. this._curLast -= 1;
  1293. } else if (this._last > 0) {
  1294. this._last -= 1;
  1295. this._curLast = this._bucketSize - 1;
  1296. } else {
  1297. this._last = this._bucketNum - 1;
  1298. this._curLast = this._bucketSize - 1;
  1299. }
  1300. }
  1301. this._length -= 1;
  1302. return value;
  1303. };
  1304. /**
  1305. * @description Push the element to the front.
  1306. * @param element - The element you want to push.
  1307. * @returns The size of queue after pushing.
  1308. */
  1309. Deque.prototype.pushFront = function (element) {
  1310. if (this._length) {
  1311. if (this._curFirst > 0) {
  1312. this._curFirst -= 1;
  1313. } else if (this._first > 0) {
  1314. this._first -= 1;
  1315. this._curFirst = this._bucketSize - 1;
  1316. } else {
  1317. this._first = this._bucketNum - 1;
  1318. this._curFirst = this._bucketSize - 1;
  1319. }
  1320. if (this._first === this._last && this._curFirst === this._curLast) this._reAllocate();
  1321. }
  1322. this._length += 1;
  1323. this._map[this._first][this._curFirst] = element;
  1324. return this._length;
  1325. };
  1326. /**
  1327. * @description Remove the _first element.
  1328. * @returns The element you popped.
  1329. */
  1330. Deque.prototype.popFront = function () {
  1331. if (this._length === 0) return;
  1332. var value = this._map[this._first][this._curFirst];
  1333. if (this._length !== 1) {
  1334. if (this._curFirst < this._bucketSize - 1) {
  1335. this._curFirst += 1;
  1336. } else if (this._first < this._bucketNum - 1) {
  1337. this._first += 1;
  1338. this._curFirst = 0;
  1339. } else {
  1340. this._first = 0;
  1341. this._curFirst = 0;
  1342. }
  1343. }
  1344. this._length -= 1;
  1345. return value;
  1346. };
  1347. Deque.prototype.getElementByPos = function (pos) {
  1348. if (pos < 0 || pos > this._length - 1) {
  1349. throw new RangeError();
  1350. }
  1351. var _a = this._getElementIndex(pos),
  1352. curNodeBucketIndex = _a.curNodeBucketIndex,
  1353. curNodePointerIndex = _a.curNodePointerIndex;
  1354. return this._map[curNodeBucketIndex][curNodePointerIndex];
  1355. };
  1356. Deque.prototype.setElementByPos = function (pos, element) {
  1357. if (pos < 0 || pos > this._length - 1) {
  1358. throw new RangeError();
  1359. }
  1360. var _a = this._getElementIndex(pos),
  1361. curNodeBucketIndex = _a.curNodeBucketIndex,
  1362. curNodePointerIndex = _a.curNodePointerIndex;
  1363. this._map[curNodeBucketIndex][curNodePointerIndex] = element;
  1364. };
  1365. Deque.prototype.insert = function (pos, element, num) {
  1366. if (num === void 0) {
  1367. num = 1;
  1368. }
  1369. if (pos < 0 || pos > this._length) {
  1370. throw new RangeError();
  1371. }
  1372. if (pos === 0) {
  1373. while (num--) this.pushFront(element);
  1374. } else if (pos === this._length) {
  1375. while (num--) this.pushBack(element);
  1376. } else {
  1377. var arr = [];
  1378. for (var i = pos; i < this._length; ++i) {
  1379. arr.push(this.getElementByPos(i));
  1380. }
  1381. this.cut(pos - 1);
  1382. for (var i = 0; i < num; ++i) this.pushBack(element);
  1383. for (var i = 0; i < arr.length; ++i) this.pushBack(arr[i]);
  1384. }
  1385. return this._length;
  1386. };
  1387. /**
  1388. * @description Remove all elements after the specified position (excluding the specified position).
  1389. * @param pos - The previous position of the first removed element.
  1390. * @returns The size of the container after cutting.
  1391. * @example
  1392. * deque.cut(1); // Then deque's size will be 2. deque -> [0, 1]
  1393. */
  1394. Deque.prototype.cut = function (pos) {
  1395. if (pos < 0) {
  1396. this.clear();
  1397. return 0;
  1398. }
  1399. var _a = this._getElementIndex(pos),
  1400. curNodeBucketIndex = _a.curNodeBucketIndex,
  1401. curNodePointerIndex = _a.curNodePointerIndex;
  1402. this._last = curNodeBucketIndex;
  1403. this._curLast = curNodePointerIndex;
  1404. this._length = pos + 1;
  1405. return this._length;
  1406. };
  1407. Deque.prototype.eraseElementByPos = function (pos) {
  1408. if (pos < 0 || pos > this._length - 1) {
  1409. throw new RangeError();
  1410. }
  1411. if (pos === 0) this.popFront();else if (pos === this._length - 1) this.popBack();else {
  1412. var arr = [];
  1413. for (var i = pos + 1; i < this._length; ++i) {
  1414. arr.push(this.getElementByPos(i));
  1415. }
  1416. this.cut(pos);
  1417. this.popBack();
  1418. var self_1 = this;
  1419. arr.forEach(function (el) {
  1420. self_1.pushBack(el);
  1421. });
  1422. }
  1423. return this._length;
  1424. };
  1425. Deque.prototype.eraseElementByValue = function (value) {
  1426. if (this._length === 0) return 0;
  1427. var arr = [];
  1428. for (var i = 0; i < this._length; ++i) {
  1429. var element = this.getElementByPos(i);
  1430. if (element !== value) arr.push(element);
  1431. }
  1432. var _length = arr.length;
  1433. for (var i = 0; i < _length; ++i) this.setElementByPos(i, arr[i]);
  1434. return this.cut(_length - 1);
  1435. };
  1436. Deque.prototype.eraseElementByIterator = function (iter) {
  1437. var _node = iter._node;
  1438. this.eraseElementByPos(_node);
  1439. iter = iter.next();
  1440. return iter;
  1441. };
  1442. Deque.prototype.find = function (element) {
  1443. for (var i = 0; i < this._length; ++i) {
  1444. if (this.getElementByPos(i) === element) {
  1445. return new DequeIterator(i, this);
  1446. }
  1447. }
  1448. return this.end();
  1449. };
  1450. Deque.prototype.reverse = function () {
  1451. var l = 0;
  1452. var r = this._length - 1;
  1453. while (l < r) {
  1454. var tmp = this.getElementByPos(l);
  1455. this.setElementByPos(l, this.getElementByPos(r));
  1456. this.setElementByPos(r, tmp);
  1457. l += 1;
  1458. r -= 1;
  1459. }
  1460. };
  1461. Deque.prototype.unique = function () {
  1462. if (this._length <= 1) {
  1463. return this._length;
  1464. }
  1465. var index = 1;
  1466. var pre = this.getElementByPos(0);
  1467. for (var i = 1; i < this._length; ++i) {
  1468. var cur = this.getElementByPos(i);
  1469. if (cur !== pre) {
  1470. pre = cur;
  1471. this.setElementByPos(index++, cur);
  1472. }
  1473. }
  1474. while (this._length > index) this.popBack();
  1475. return this._length;
  1476. };
  1477. Deque.prototype.sort = function (cmp) {
  1478. var arr = [];
  1479. for (var i = 0; i < this._length; ++i) {
  1480. arr.push(this.getElementByPos(i));
  1481. }
  1482. arr.sort(cmp);
  1483. for (var i = 0; i < this._length; ++i) this.setElementByPos(i, arr[i]);
  1484. };
  1485. /**
  1486. * @description Remove as much useless space as possible.
  1487. */
  1488. Deque.prototype.shrinkToFit = function () {
  1489. if (this._length === 0) return;
  1490. var arr = [];
  1491. this.forEach(function (el) {
  1492. arr.push(el);
  1493. });
  1494. this._bucketNum = Math.max(Math.ceil(this._length / this._bucketSize), 1);
  1495. this._length = this._first = this._last = this._curFirst = this._curLast = 0;
  1496. this._map = [];
  1497. for (var i = 0; i < this._bucketNum; ++i) {
  1498. this._map.push(new Array(this._bucketSize));
  1499. }
  1500. for (var i = 0; i < arr.length; ++i) this.pushBack(arr[i]);
  1501. };
  1502. Deque.prototype.forEach = function (callback) {
  1503. for (var i = 0; i < this._length; ++i) {
  1504. callback(this.getElementByPos(i), i, this);
  1505. }
  1506. };
  1507. Deque.prototype[Symbol.iterator] = function () {
  1508. return function () {
  1509. var i;
  1510. return __generator(this, function (_a) {
  1511. switch (_a.label) {
  1512. case 0:
  1513. i = 0;
  1514. _a.label = 1;
  1515. case 1:
  1516. if (!(i < this._length)) return [3 /*break*/, 4];
  1517. return [4 /*yield*/, this.getElementByPos(i)];
  1518. case 2:
  1519. _a.sent();
  1520. _a.label = 3;
  1521. case 3:
  1522. ++i;
  1523. return [3 /*break*/, 1];
  1524. case 4:
  1525. return [2 /*return*/];
  1526. }
  1527. });
  1528. }.bind(this)();
  1529. };
  1530. return Deque;
  1531. }(SequentialContainer);
  1532. var TreeNode = /** @class */function () {
  1533. function TreeNode(key, value) {
  1534. this._color = 1 /* TreeNodeColor.RED */;
  1535. this._key = undefined;
  1536. this._value = undefined;
  1537. this._left = undefined;
  1538. this._right = undefined;
  1539. this._parent = undefined;
  1540. this._key = key;
  1541. this._value = value;
  1542. }
  1543. /**
  1544. * @description Get the pre node.
  1545. * @returns TreeNode about the pre node.
  1546. */
  1547. TreeNode.prototype._pre = function () {
  1548. var preNode = this;
  1549. if (preNode._color === 1 /* TreeNodeColor.RED */ && preNode._parent._parent === preNode) {
  1550. preNode = preNode._right;
  1551. } else if (preNode._left) {
  1552. preNode = preNode._left;
  1553. while (preNode._right) {
  1554. preNode = preNode._right;
  1555. }
  1556. } else {
  1557. var pre = preNode._parent;
  1558. while (pre._left === preNode) {
  1559. preNode = pre;
  1560. pre = preNode._parent;
  1561. }
  1562. preNode = pre;
  1563. }
  1564. return preNode;
  1565. };
  1566. /**
  1567. * @description Get the next node.
  1568. * @returns TreeNode about the next node.
  1569. */
  1570. TreeNode.prototype._next = function () {
  1571. var nextNode = this;
  1572. if (nextNode._right) {
  1573. nextNode = nextNode._right;
  1574. while (nextNode._left) {
  1575. nextNode = nextNode._left;
  1576. }
  1577. return nextNode;
  1578. } else {
  1579. var pre = nextNode._parent;
  1580. while (pre._right === nextNode) {
  1581. nextNode = pre;
  1582. pre = nextNode._parent;
  1583. }
  1584. if (nextNode._right !== pre) {
  1585. return pre;
  1586. } else return nextNode;
  1587. }
  1588. };
  1589. /**
  1590. * @description Rotate left.
  1591. * @returns TreeNode about moved to original position after rotation.
  1592. */
  1593. TreeNode.prototype._rotateLeft = function () {
  1594. var PP = this._parent;
  1595. var V = this._right;
  1596. var R = V._left;
  1597. if (PP._parent === this) PP._parent = V;else if (PP._left === this) PP._left = V;else PP._right = V;
  1598. V._parent = PP;
  1599. V._left = this;
  1600. this._parent = V;
  1601. this._right = R;
  1602. if (R) R._parent = this;
  1603. return V;
  1604. };
  1605. /**
  1606. * @description Rotate right.
  1607. * @returns TreeNode about moved to original position after rotation.
  1608. */
  1609. TreeNode.prototype._rotateRight = function () {
  1610. var PP = this._parent;
  1611. var F = this._left;
  1612. var K = F._right;
  1613. if (PP._parent === this) PP._parent = F;else if (PP._left === this) PP._left = F;else PP._right = F;
  1614. F._parent = PP;
  1615. F._right = this;
  1616. this._parent = F;
  1617. this._left = K;
  1618. if (K) K._parent = this;
  1619. return F;
  1620. };
  1621. return TreeNode;
  1622. }();
  1623. var TreeNodeEnableIndex = /** @class */function (_super) {
  1624. __extends(TreeNodeEnableIndex, _super);
  1625. function TreeNodeEnableIndex() {
  1626. var _this = _super !== null && _super.apply(this, arguments) || this;
  1627. _this._subTreeSize = 1;
  1628. return _this;
  1629. }
  1630. /**
  1631. * @description Rotate left and do recount.
  1632. * @returns TreeNode about moved to original position after rotation.
  1633. */
  1634. TreeNodeEnableIndex.prototype._rotateLeft = function () {
  1635. var parent = _super.prototype._rotateLeft.call(this);
  1636. this._recount();
  1637. parent._recount();
  1638. return parent;
  1639. };
  1640. /**
  1641. * @description Rotate right and do recount.
  1642. * @returns TreeNode about moved to original position after rotation.
  1643. */
  1644. TreeNodeEnableIndex.prototype._rotateRight = function () {
  1645. var parent = _super.prototype._rotateRight.call(this);
  1646. this._recount();
  1647. parent._recount();
  1648. return parent;
  1649. };
  1650. TreeNodeEnableIndex.prototype._recount = function () {
  1651. this._subTreeSize = 1;
  1652. if (this._left) {
  1653. this._subTreeSize += this._left._subTreeSize;
  1654. }
  1655. if (this._right) {
  1656. this._subTreeSize += this._right._subTreeSize;
  1657. }
  1658. };
  1659. return TreeNodeEnableIndex;
  1660. }(TreeNode);
  1661. var TreeContainer = /** @class */function (_super) {
  1662. __extends(TreeContainer, _super);
  1663. /**
  1664. * @internal
  1665. */
  1666. function TreeContainer(cmp, enableIndex) {
  1667. if (cmp === void 0) {
  1668. cmp = function (x, y) {
  1669. if (x < y) return -1;
  1670. if (x > y) return 1;
  1671. return 0;
  1672. };
  1673. }
  1674. if (enableIndex === void 0) {
  1675. enableIndex = false;
  1676. }
  1677. var _this = _super.call(this) || this;
  1678. /**
  1679. * @internal
  1680. */
  1681. _this._root = undefined;
  1682. _this._cmp = cmp;
  1683. if (enableIndex) {
  1684. _this._TreeNodeClass = TreeNodeEnableIndex;
  1685. _this._set = function (key, value, hint) {
  1686. var curNode = this._preSet(key, value, hint);
  1687. if (curNode) {
  1688. var p = curNode._parent;
  1689. while (p !== this._header) {
  1690. p._subTreeSize += 1;
  1691. p = p._parent;
  1692. }
  1693. var nodeList = this._insertNodeSelfBalance(curNode);
  1694. if (nodeList) {
  1695. var _a = nodeList,
  1696. parentNode = _a.parentNode,
  1697. grandParent = _a.grandParent,
  1698. curNode_1 = _a.curNode;
  1699. parentNode._recount();
  1700. grandParent._recount();
  1701. curNode_1._recount();
  1702. }
  1703. }
  1704. return this._length;
  1705. };
  1706. _this._eraseNode = function (curNode) {
  1707. var p = this._preEraseNode(curNode);
  1708. while (p !== this._header) {
  1709. p._subTreeSize -= 1;
  1710. p = p._parent;
  1711. }
  1712. };
  1713. } else {
  1714. _this._TreeNodeClass = TreeNode;
  1715. _this._set = function (key, value, hint) {
  1716. var curNode = this._preSet(key, value, hint);
  1717. if (curNode) this._insertNodeSelfBalance(curNode);
  1718. return this._length;
  1719. };
  1720. _this._eraseNode = _this._preEraseNode;
  1721. }
  1722. _this._header = new _this._TreeNodeClass();
  1723. return _this;
  1724. }
  1725. /**
  1726. * @internal
  1727. */
  1728. TreeContainer.prototype._lowerBound = function (curNode, key) {
  1729. var resNode = this._header;
  1730. while (curNode) {
  1731. var cmpResult = this._cmp(curNode._key, key);
  1732. if (cmpResult < 0) {
  1733. curNode = curNode._right;
  1734. } else if (cmpResult > 0) {
  1735. resNode = curNode;
  1736. curNode = curNode._left;
  1737. } else return curNode;
  1738. }
  1739. return resNode;
  1740. };
  1741. /**
  1742. * @internal
  1743. */
  1744. TreeContainer.prototype._upperBound = function (curNode, key) {
  1745. var resNode = this._header;
  1746. while (curNode) {
  1747. var cmpResult = this._cmp(curNode._key, key);
  1748. if (cmpResult <= 0) {
  1749. curNode = curNode._right;
  1750. } else {
  1751. resNode = curNode;
  1752. curNode = curNode._left;
  1753. }
  1754. }
  1755. return resNode;
  1756. };
  1757. /**
  1758. * @internal
  1759. */
  1760. TreeContainer.prototype._reverseLowerBound = function (curNode, key) {
  1761. var resNode = this._header;
  1762. while (curNode) {
  1763. var cmpResult = this._cmp(curNode._key, key);
  1764. if (cmpResult < 0) {
  1765. resNode = curNode;
  1766. curNode = curNode._right;
  1767. } else if (cmpResult > 0) {
  1768. curNode = curNode._left;
  1769. } else return curNode;
  1770. }
  1771. return resNode;
  1772. };
  1773. /**
  1774. * @internal
  1775. */
  1776. TreeContainer.prototype._reverseUpperBound = function (curNode, key) {
  1777. var resNode = this._header;
  1778. while (curNode) {
  1779. var cmpResult = this._cmp(curNode._key, key);
  1780. if (cmpResult < 0) {
  1781. resNode = curNode;
  1782. curNode = curNode._right;
  1783. } else {
  1784. curNode = curNode._left;
  1785. }
  1786. }
  1787. return resNode;
  1788. };
  1789. /**
  1790. * @internal
  1791. */
  1792. TreeContainer.prototype._eraseNodeSelfBalance = function (curNode) {
  1793. while (true) {
  1794. var parentNode = curNode._parent;
  1795. if (parentNode === this._header) return;
  1796. if (curNode._color === 1 /* TreeNodeColor.RED */) {
  1797. curNode._color = 0 /* TreeNodeColor.BLACK */;
  1798. return;
  1799. }
  1800. if (curNode === parentNode._left) {
  1801. var brother = parentNode._right;
  1802. if (brother._color === 1 /* TreeNodeColor.RED */) {
  1803. brother._color = 0 /* TreeNodeColor.BLACK */;
  1804. parentNode._color = 1 /* TreeNodeColor.RED */;
  1805. if (parentNode === this._root) {
  1806. this._root = parentNode._rotateLeft();
  1807. } else parentNode._rotateLeft();
  1808. } else {
  1809. if (brother._right && brother._right._color === 1 /* TreeNodeColor.RED */) {
  1810. brother._color = parentNode._color;
  1811. parentNode._color = 0 /* TreeNodeColor.BLACK */;
  1812. brother._right._color = 0 /* TreeNodeColor.BLACK */;
  1813. if (parentNode === this._root) {
  1814. this._root = parentNode._rotateLeft();
  1815. } else parentNode._rotateLeft();
  1816. return;
  1817. } else if (brother._left && brother._left._color === 1 /* TreeNodeColor.RED */) {
  1818. brother._color = 1 /* TreeNodeColor.RED */;
  1819. brother._left._color = 0 /* TreeNodeColor.BLACK */;
  1820. brother._rotateRight();
  1821. } else {
  1822. brother._color = 1 /* TreeNodeColor.RED */;
  1823. curNode = parentNode;
  1824. }
  1825. }
  1826. } else {
  1827. var brother = parentNode._left;
  1828. if (brother._color === 1 /* TreeNodeColor.RED */) {
  1829. brother._color = 0 /* TreeNodeColor.BLACK */;
  1830. parentNode._color = 1 /* TreeNodeColor.RED */;
  1831. if (parentNode === this._root) {
  1832. this._root = parentNode._rotateRight();
  1833. } else parentNode._rotateRight();
  1834. } else {
  1835. if (brother._left && brother._left._color === 1 /* TreeNodeColor.RED */) {
  1836. brother._color = parentNode._color;
  1837. parentNode._color = 0 /* TreeNodeColor.BLACK */;
  1838. brother._left._color = 0 /* TreeNodeColor.BLACK */;
  1839. if (parentNode === this._root) {
  1840. this._root = parentNode._rotateRight();
  1841. } else parentNode._rotateRight();
  1842. return;
  1843. } else if (brother._right && brother._right._color === 1 /* TreeNodeColor.RED */) {
  1844. brother._color = 1 /* TreeNodeColor.RED */;
  1845. brother._right._color = 0 /* TreeNodeColor.BLACK */;
  1846. brother._rotateLeft();
  1847. } else {
  1848. brother._color = 1 /* TreeNodeColor.RED */;
  1849. curNode = parentNode;
  1850. }
  1851. }
  1852. }
  1853. }
  1854. };
  1855. /**
  1856. * @internal
  1857. */
  1858. TreeContainer.prototype._preEraseNode = function (curNode) {
  1859. var _a, _b;
  1860. if (this._length === 1) {
  1861. this.clear();
  1862. return this._header;
  1863. }
  1864. var swapNode = curNode;
  1865. while (swapNode._left || swapNode._right) {
  1866. if (swapNode._right) {
  1867. swapNode = swapNode._right;
  1868. while (swapNode._left) swapNode = swapNode._left;
  1869. } else {
  1870. swapNode = swapNode._left;
  1871. }
  1872. _a = __read([swapNode._key, curNode._key], 2), curNode._key = _a[0], swapNode._key = _a[1];
  1873. _b = __read([swapNode._value, curNode._value], 2), curNode._value = _b[0], swapNode._value = _b[1];
  1874. curNode = swapNode;
  1875. }
  1876. if (this._header._left === swapNode) {
  1877. this._header._left = swapNode._parent;
  1878. } else if (this._header._right === swapNode) {
  1879. this._header._right = swapNode._parent;
  1880. }
  1881. this._eraseNodeSelfBalance(swapNode);
  1882. var _parent = swapNode._parent;
  1883. if (swapNode === _parent._left) {
  1884. _parent._left = undefined;
  1885. } else _parent._right = undefined;
  1886. this._length -= 1;
  1887. this._root._color = 0 /* TreeNodeColor.BLACK */;
  1888. return _parent;
  1889. };
  1890. /**
  1891. * @internal
  1892. */
  1893. TreeContainer.prototype._inOrderTraversal = function (curNode, callback) {
  1894. if (curNode === undefined) return false;
  1895. var ifReturn = this._inOrderTraversal(curNode._left, callback);
  1896. if (ifReturn) return true;
  1897. if (callback(curNode)) return true;
  1898. return this._inOrderTraversal(curNode._right, callback);
  1899. };
  1900. /**
  1901. * @internal
  1902. */
  1903. TreeContainer.prototype._insertNodeSelfBalance = function (curNode) {
  1904. while (true) {
  1905. var parentNode = curNode._parent;
  1906. if (parentNode._color === 0 /* TreeNodeColor.BLACK */) return;
  1907. var grandParent = parentNode._parent;
  1908. if (parentNode === grandParent._left) {
  1909. var uncle = grandParent._right;
  1910. if (uncle && uncle._color === 1 /* TreeNodeColor.RED */) {
  1911. uncle._color = parentNode._color = 0 /* TreeNodeColor.BLACK */;
  1912. if (grandParent === this._root) return;
  1913. grandParent._color = 1 /* TreeNodeColor.RED */;
  1914. curNode = grandParent;
  1915. continue;
  1916. } else if (curNode === parentNode._right) {
  1917. curNode._color = 0 /* TreeNodeColor.BLACK */;
  1918. if (curNode._left) curNode._left._parent = parentNode;
  1919. if (curNode._right) curNode._right._parent = grandParent;
  1920. parentNode._right = curNode._left;
  1921. grandParent._left = curNode._right;
  1922. curNode._left = parentNode;
  1923. curNode._right = grandParent;
  1924. if (grandParent === this._root) {
  1925. this._root = curNode;
  1926. this._header._parent = curNode;
  1927. } else {
  1928. var GP = grandParent._parent;
  1929. if (GP._left === grandParent) {
  1930. GP._left = curNode;
  1931. } else GP._right = curNode;
  1932. }
  1933. curNode._parent = grandParent._parent;
  1934. parentNode._parent = curNode;
  1935. grandParent._parent = curNode;
  1936. grandParent._color = 1 /* TreeNodeColor.RED */;
  1937. return {
  1938. parentNode: parentNode,
  1939. grandParent: grandParent,
  1940. curNode: curNode
  1941. };
  1942. } else {
  1943. parentNode._color = 0 /* TreeNodeColor.BLACK */;
  1944. if (grandParent === this._root) {
  1945. this._root = grandParent._rotateRight();
  1946. } else grandParent._rotateRight();
  1947. grandParent._color = 1 /* TreeNodeColor.RED */;
  1948. }
  1949. } else {
  1950. var uncle = grandParent._left;
  1951. if (uncle && uncle._color === 1 /* TreeNodeColor.RED */) {
  1952. uncle._color = parentNode._color = 0 /* TreeNodeColor.BLACK */;
  1953. if (grandParent === this._root) return;
  1954. grandParent._color = 1 /* TreeNodeColor.RED */;
  1955. curNode = grandParent;
  1956. continue;
  1957. } else if (curNode === parentNode._left) {
  1958. curNode._color = 0 /* TreeNodeColor.BLACK */;
  1959. if (curNode._left) curNode._left._parent = grandParent;
  1960. if (curNode._right) curNode._right._parent = parentNode;
  1961. grandParent._right = curNode._left;
  1962. parentNode._left = curNode._right;
  1963. curNode._left = grandParent;
  1964. curNode._right = parentNode;
  1965. if (grandParent === this._root) {
  1966. this._root = curNode;
  1967. this._header._parent = curNode;
  1968. } else {
  1969. var GP = grandParent._parent;
  1970. if (GP._left === grandParent) {
  1971. GP._left = curNode;
  1972. } else GP._right = curNode;
  1973. }
  1974. curNode._parent = grandParent._parent;
  1975. parentNode._parent = curNode;
  1976. grandParent._parent = curNode;
  1977. grandParent._color = 1 /* TreeNodeColor.RED */;
  1978. return {
  1979. parentNode: parentNode,
  1980. grandParent: grandParent,
  1981. curNode: curNode
  1982. };
  1983. } else {
  1984. parentNode._color = 0 /* TreeNodeColor.BLACK */;
  1985. if (grandParent === this._root) {
  1986. this._root = grandParent._rotateLeft();
  1987. } else grandParent._rotateLeft();
  1988. grandParent._color = 1 /* TreeNodeColor.RED */;
  1989. }
  1990. }
  1991. return;
  1992. }
  1993. };
  1994. /**
  1995. * @internal
  1996. */
  1997. TreeContainer.prototype._preSet = function (key, value, hint) {
  1998. if (this._root === undefined) {
  1999. this._length += 1;
  2000. this._root = new this._TreeNodeClass(key, value);
  2001. this._root._color = 0 /* TreeNodeColor.BLACK */;
  2002. this._root._parent = this._header;
  2003. this._header._parent = this._root;
  2004. this._header._left = this._root;
  2005. this._header._right = this._root;
  2006. return;
  2007. }
  2008. var curNode;
  2009. var minNode = this._header._left;
  2010. var compareToMin = this._cmp(minNode._key, key);
  2011. if (compareToMin === 0) {
  2012. minNode._value = value;
  2013. return;
  2014. } else if (compareToMin > 0) {
  2015. minNode._left = new this._TreeNodeClass(key, value);
  2016. minNode._left._parent = minNode;
  2017. curNode = minNode._left;
  2018. this._header._left = curNode;
  2019. } else {
  2020. var maxNode = this._header._right;
  2021. var compareToMax = this._cmp(maxNode._key, key);
  2022. if (compareToMax === 0) {
  2023. maxNode._value = value;
  2024. return;
  2025. } else if (compareToMax < 0) {
  2026. maxNode._right = new this._TreeNodeClass(key, value);
  2027. maxNode._right._parent = maxNode;
  2028. curNode = maxNode._right;
  2029. this._header._right = curNode;
  2030. } else {
  2031. if (hint !== undefined) {
  2032. var iterNode = hint._node;
  2033. if (iterNode !== this._header) {
  2034. var iterCmpRes = this._cmp(iterNode._key, key);
  2035. if (iterCmpRes === 0) {
  2036. iterNode._value = value;
  2037. return;
  2038. } else /* istanbul ignore else */if (iterCmpRes > 0) {
  2039. var preNode = iterNode._pre();
  2040. var preCmpRes = this._cmp(preNode._key, key);
  2041. if (preCmpRes === 0) {
  2042. preNode._value = value;
  2043. return;
  2044. } else if (preCmpRes < 0) {
  2045. curNode = new this._TreeNodeClass(key, value);
  2046. if (preNode._right === undefined) {
  2047. preNode._right = curNode;
  2048. curNode._parent = preNode;
  2049. } else {
  2050. iterNode._left = curNode;
  2051. curNode._parent = iterNode;
  2052. }
  2053. }
  2054. }
  2055. }
  2056. }
  2057. if (curNode === undefined) {
  2058. curNode = this._root;
  2059. while (true) {
  2060. var cmpResult = this._cmp(curNode._key, key);
  2061. if (cmpResult > 0) {
  2062. if (curNode._left === undefined) {
  2063. curNode._left = new this._TreeNodeClass(key, value);
  2064. curNode._left._parent = curNode;
  2065. curNode = curNode._left;
  2066. break;
  2067. }
  2068. curNode = curNode._left;
  2069. } else if (cmpResult < 0) {
  2070. if (curNode._right === undefined) {
  2071. curNode._right = new this._TreeNodeClass(key, value);
  2072. curNode._right._parent = curNode;
  2073. curNode = curNode._right;
  2074. break;
  2075. }
  2076. curNode = curNode._right;
  2077. } else {
  2078. curNode._value = value;
  2079. return;
  2080. }
  2081. }
  2082. }
  2083. }
  2084. }
  2085. this._length += 1;
  2086. return curNode;
  2087. };
  2088. /**
  2089. * @internal
  2090. */
  2091. TreeContainer.prototype._findElementNode = function (curNode, key) {
  2092. while (curNode) {
  2093. var cmpResult = this._cmp(curNode._key, key);
  2094. if (cmpResult < 0) {
  2095. curNode = curNode._right;
  2096. } else if (cmpResult > 0) {
  2097. curNode = curNode._left;
  2098. } else return curNode;
  2099. }
  2100. return curNode || this._header;
  2101. };
  2102. TreeContainer.prototype.clear = function () {
  2103. this._length = 0;
  2104. this._root = undefined;
  2105. this._header._parent = undefined;
  2106. this._header._left = this._header._right = undefined;
  2107. };
  2108. /**
  2109. * @description Update node's key by iterator.
  2110. * @param iter - The iterator you want to change.
  2111. * @param key - The key you want to update.
  2112. * @returns Whether the modification is successful.
  2113. * @example
  2114. * const st = new orderedSet([1, 2, 5]);
  2115. * const iter = st.find(2);
  2116. * st.updateKeyByIterator(iter, 3); // then st will become [1, 3, 5]
  2117. */
  2118. TreeContainer.prototype.updateKeyByIterator = function (iter, key) {
  2119. var node = iter._node;
  2120. if (node === this._header) {
  2121. throwIteratorAccessError();
  2122. }
  2123. if (this._length === 1) {
  2124. node._key = key;
  2125. return true;
  2126. }
  2127. if (node === this._header._left) {
  2128. if (this._cmp(node._next()._key, key) > 0) {
  2129. node._key = key;
  2130. return true;
  2131. }
  2132. return false;
  2133. }
  2134. if (node === this._header._right) {
  2135. if (this._cmp(node._pre()._key, key) < 0) {
  2136. node._key = key;
  2137. return true;
  2138. }
  2139. return false;
  2140. }
  2141. var preKey = node._pre()._key;
  2142. if (this._cmp(preKey, key) >= 0) return false;
  2143. var nextKey = node._next()._key;
  2144. if (this._cmp(nextKey, key) <= 0) return false;
  2145. node._key = key;
  2146. return true;
  2147. };
  2148. TreeContainer.prototype.eraseElementByPos = function (pos) {
  2149. if (pos < 0 || pos > this._length - 1) {
  2150. throw new RangeError();
  2151. }
  2152. var index = 0;
  2153. var self = this;
  2154. this._inOrderTraversal(this._root, function (curNode) {
  2155. if (pos === index) {
  2156. self._eraseNode(curNode);
  2157. return true;
  2158. }
  2159. index += 1;
  2160. return false;
  2161. });
  2162. return this._length;
  2163. };
  2164. /**
  2165. * @description Remove the element of the specified key.
  2166. * @param key - The key you want to remove.
  2167. * @returns Whether erase successfully.
  2168. */
  2169. TreeContainer.prototype.eraseElementByKey = function (key) {
  2170. if (this._length === 0) return false;
  2171. var curNode = this._findElementNode(this._root, key);
  2172. if (curNode === this._header) return false;
  2173. this._eraseNode(curNode);
  2174. return true;
  2175. };
  2176. TreeContainer.prototype.eraseElementByIterator = function (iter) {
  2177. var node = iter._node;
  2178. if (node === this._header) {
  2179. throwIteratorAccessError();
  2180. }
  2181. var hasNoRight = node._right === undefined;
  2182. var isNormal = iter.iteratorType === 0 /* IteratorType.NORMAL */;
  2183. // For the normal iterator, the `next` node will be swapped to `this` node when has right.
  2184. if (isNormal) {
  2185. // So we should move it to next when it's right is null.
  2186. if (hasNoRight) iter.next();
  2187. } else {
  2188. // For the reverse iterator, only when it doesn't have right and has left the `next` node will be swapped.
  2189. // So when it has right, or it is a leaf node we should move it to `next`.
  2190. if (!hasNoRight || node._left === undefined) iter.next();
  2191. }
  2192. this._eraseNode(node);
  2193. return iter;
  2194. };
  2195. TreeContainer.prototype.forEach = function (callback) {
  2196. var e_1, _a;
  2197. var index = 0;
  2198. try {
  2199. for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
  2200. var element = _c.value;
  2201. callback(element, index++, this);
  2202. }
  2203. } catch (e_1_1) {
  2204. e_1 = {
  2205. error: e_1_1
  2206. };
  2207. } finally {
  2208. try {
  2209. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  2210. } finally {
  2211. if (e_1) throw e_1.error;
  2212. }
  2213. }
  2214. };
  2215. TreeContainer.prototype.getElementByPos = function (pos) {
  2216. var e_2, _a;
  2217. if (pos < 0 || pos > this._length - 1) {
  2218. throw new RangeError();
  2219. }
  2220. var res;
  2221. var index = 0;
  2222. try {
  2223. for (var _b = __values(this), _c = _b.next(); !_c.done; _c = _b.next()) {
  2224. var element = _c.value;
  2225. if (index === pos) {
  2226. res = element;
  2227. break;
  2228. }
  2229. index += 1;
  2230. }
  2231. } catch (e_2_1) {
  2232. e_2 = {
  2233. error: e_2_1
  2234. };
  2235. } finally {
  2236. try {
  2237. if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
  2238. } finally {
  2239. if (e_2) throw e_2.error;
  2240. }
  2241. }
  2242. return res;
  2243. };
  2244. /**
  2245. * @description Get the height of the tree.
  2246. * @returns Number about the height of the RB-tree.
  2247. */
  2248. TreeContainer.prototype.getHeight = function () {
  2249. if (this._length === 0) return 0;
  2250. var traversal = function (curNode) {
  2251. if (!curNode) return 0;
  2252. return Math.max(traversal(curNode._left), traversal(curNode._right)) + 1;
  2253. };
  2254. return traversal(this._root);
  2255. };
  2256. return TreeContainer;
  2257. }(Container);
  2258. var TreeIterator = /** @class */function (_super) {
  2259. __extends(TreeIterator, _super);
  2260. /**
  2261. * @internal
  2262. */
  2263. function TreeIterator(node, header, iteratorType) {
  2264. var _this = _super.call(this, iteratorType) || this;
  2265. _this._node = node;
  2266. _this._header = header;
  2267. if (_this.iteratorType === 0 /* IteratorType.NORMAL */) {
  2268. _this.pre = function () {
  2269. if (this._node === this._header._left) {
  2270. throwIteratorAccessError();
  2271. }
  2272. this._node = this._node._pre();
  2273. return this;
  2274. };
  2275. _this.next = function () {
  2276. if (this._node === this._header) {
  2277. throwIteratorAccessError();
  2278. }
  2279. this._node = this._node._next();
  2280. return this;
  2281. };
  2282. } else {
  2283. _this.pre = function () {
  2284. if (this._node === this._header._right) {
  2285. throwIteratorAccessError();
  2286. }
  2287. this._node = this._node._next();
  2288. return this;
  2289. };
  2290. _this.next = function () {
  2291. if (this._node === this._header) {
  2292. throwIteratorAccessError();
  2293. }
  2294. this._node = this._node._pre();
  2295. return this;
  2296. };
  2297. }
  2298. return _this;
  2299. }
  2300. Object.defineProperty(TreeIterator.prototype, "index", {
  2301. /**
  2302. * @description Get the sequential index of the iterator in the tree container.<br/>
  2303. * <strong>Note:</strong>
  2304. * This function only takes effect when the specified tree container `enableIndex = true`.
  2305. * @returns The index subscript of the node in the tree.
  2306. * @example
  2307. * const st = new OrderedSet([1, 2, 3], true);
  2308. * console.log(st.begin().next().index); // 1
  2309. */
  2310. get: function () {
  2311. var _node = this._node;
  2312. var root = this._header._parent;
  2313. if (_node === this._header) {
  2314. if (root) {
  2315. return root._subTreeSize - 1;
  2316. }
  2317. return 0;
  2318. }
  2319. var index = 0;
  2320. if (_node._left) {
  2321. index += _node._left._subTreeSize;
  2322. }
  2323. while (_node !== root) {
  2324. var _parent = _node._parent;
  2325. if (_node === _parent._right) {
  2326. index += 1;
  2327. if (_parent._left) {
  2328. index += _parent._left._subTreeSize;
  2329. }
  2330. }
  2331. _node = _parent;
  2332. }
  2333. return index;
  2334. },
  2335. enumerable: false,
  2336. configurable: true
  2337. });
  2338. return TreeIterator;
  2339. }(ContainerIterator);
  2340. var OrderedSetIterator = /** @class */function (_super) {
  2341. __extends(OrderedSetIterator, _super);
  2342. function OrderedSetIterator(node, header, container, iteratorType) {
  2343. var _this = _super.call(this, node, header, iteratorType) || this;
  2344. _this.container = container;
  2345. return _this;
  2346. }
  2347. Object.defineProperty(OrderedSetIterator.prototype, "pointer", {
  2348. get: function () {
  2349. if (this._node === this._header) {
  2350. throwIteratorAccessError();
  2351. }
  2352. return this._node._key;
  2353. },
  2354. enumerable: false,
  2355. configurable: true
  2356. });
  2357. OrderedSetIterator.prototype.copy = function () {
  2358. return new OrderedSetIterator(this._node, this._header, this.container, this.iteratorType);
  2359. };
  2360. return OrderedSetIterator;
  2361. }(TreeIterator);
  2362. var OrderedSet = /** @class */function (_super) {
  2363. __extends(OrderedSet, _super);
  2364. /**
  2365. * @param container - The initialization container.
  2366. * @param cmp - The compare function.
  2367. * @param enableIndex - Whether to enable iterator indexing function.
  2368. * @example
  2369. * new OrderedSet();
  2370. * new OrderedSet([0, 1, 2]);
  2371. * new OrderedSet([0, 1, 2], (x, y) => x - y);
  2372. * new OrderedSet([0, 1, 2], (x, y) => x - y, true);
  2373. */
  2374. function OrderedSet(container, cmp, enableIndex) {
  2375. if (container === void 0) {
  2376. container = [];
  2377. }
  2378. var _this = _super.call(this, cmp, enableIndex) || this;
  2379. var self = _this;
  2380. container.forEach(function (el) {
  2381. self.insert(el);
  2382. });
  2383. return _this;
  2384. }
  2385. /**
  2386. * @internal
  2387. */
  2388. OrderedSet.prototype._iterationFunc = function (curNode) {
  2389. return __generator(this, function (_a) {
  2390. switch (_a.label) {
  2391. case 0:
  2392. if (curNode === undefined) return [2 /*return*/];
  2393. return [5 /*yield**/, __values(this._iterationFunc(curNode._left))];
  2394. case 1:
  2395. _a.sent();
  2396. return [4 /*yield*/, curNode._key];
  2397. case 2:
  2398. _a.sent();
  2399. return [5 /*yield**/, __values(this._iterationFunc(curNode._right))];
  2400. case 3:
  2401. _a.sent();
  2402. return [2 /*return*/];
  2403. }
  2404. });
  2405. };
  2406. OrderedSet.prototype.begin = function () {
  2407. return new OrderedSetIterator(this._header._left || this._header, this._header, this);
  2408. };
  2409. OrderedSet.prototype.end = function () {
  2410. return new OrderedSetIterator(this._header, this._header, this);
  2411. };
  2412. OrderedSet.prototype.rBegin = function () {
  2413. return new OrderedSetIterator(this._header._right || this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  2414. };
  2415. OrderedSet.prototype.rEnd = function () {
  2416. return new OrderedSetIterator(this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  2417. };
  2418. OrderedSet.prototype.front = function () {
  2419. return this._header._left ? this._header._left._key : undefined;
  2420. };
  2421. OrderedSet.prototype.back = function () {
  2422. return this._header._right ? this._header._right._key : undefined;
  2423. };
  2424. /**
  2425. * @description Insert element to set.
  2426. * @param key - The key want to insert.
  2427. * @param hint - You can give an iterator hint to improve insertion efficiency.
  2428. * @return The size of container after setting.
  2429. * @example
  2430. * const st = new OrderedSet([2, 4, 5]);
  2431. * const iter = st.begin();
  2432. * st.insert(1);
  2433. * st.insert(3, iter); // give a hint will be faster.
  2434. */
  2435. OrderedSet.prototype.insert = function (key, hint) {
  2436. return this._set(key, undefined, hint);
  2437. };
  2438. OrderedSet.prototype.find = function (element) {
  2439. var resNode = this._findElementNode(this._root, element);
  2440. return new OrderedSetIterator(resNode, this._header, this);
  2441. };
  2442. OrderedSet.prototype.lowerBound = function (key) {
  2443. var resNode = this._lowerBound(this._root, key);
  2444. return new OrderedSetIterator(resNode, this._header, this);
  2445. };
  2446. OrderedSet.prototype.upperBound = function (key) {
  2447. var resNode = this._upperBound(this._root, key);
  2448. return new OrderedSetIterator(resNode, this._header, this);
  2449. };
  2450. OrderedSet.prototype.reverseLowerBound = function (key) {
  2451. var resNode = this._reverseLowerBound(this._root, key);
  2452. return new OrderedSetIterator(resNode, this._header, this);
  2453. };
  2454. OrderedSet.prototype.reverseUpperBound = function (key) {
  2455. var resNode = this._reverseUpperBound(this._root, key);
  2456. return new OrderedSetIterator(resNode, this._header, this);
  2457. };
  2458. OrderedSet.prototype.union = function (other) {
  2459. var self = this;
  2460. other.forEach(function (el) {
  2461. self.insert(el);
  2462. });
  2463. return this._length;
  2464. };
  2465. OrderedSet.prototype[Symbol.iterator] = function () {
  2466. return this._iterationFunc(this._root);
  2467. };
  2468. return OrderedSet;
  2469. }(TreeContainer);
  2470. var OrderedMapIterator = /** @class */function (_super) {
  2471. __extends(OrderedMapIterator, _super);
  2472. function OrderedMapIterator(node, header, container, iteratorType) {
  2473. var _this = _super.call(this, node, header, iteratorType) || this;
  2474. _this.container = container;
  2475. return _this;
  2476. }
  2477. Object.defineProperty(OrderedMapIterator.prototype, "pointer", {
  2478. get: function () {
  2479. if (this._node === this._header) {
  2480. throwIteratorAccessError();
  2481. }
  2482. var self = this;
  2483. return new Proxy([], {
  2484. get: function (_, props) {
  2485. if (props === '0') return self._node._key;else if (props === '1') return self._node._value;
  2486. },
  2487. set: function (_, props, newValue) {
  2488. if (props !== '1') {
  2489. throw new TypeError('props must be 1');
  2490. }
  2491. self._node._value = newValue;
  2492. return true;
  2493. }
  2494. });
  2495. },
  2496. enumerable: false,
  2497. configurable: true
  2498. });
  2499. OrderedMapIterator.prototype.copy = function () {
  2500. return new OrderedMapIterator(this._node, this._header, this.container, this.iteratorType);
  2501. };
  2502. return OrderedMapIterator;
  2503. }(TreeIterator);
  2504. var OrderedMap = /** @class */function (_super) {
  2505. __extends(OrderedMap, _super);
  2506. /**
  2507. * @param container - The initialization container.
  2508. * @param cmp - The compare function.
  2509. * @param enableIndex - Whether to enable iterator indexing function.
  2510. * @example
  2511. * new OrderedMap();
  2512. * new OrderedMap([[0, 1], [2, 1]]);
  2513. * new OrderedMap([[0, 1], [2, 1]], (x, y) => x - y);
  2514. * new OrderedMap([[0, 1], [2, 1]], (x, y) => x - y, true);
  2515. */
  2516. function OrderedMap(container, cmp, enableIndex) {
  2517. if (container === void 0) {
  2518. container = [];
  2519. }
  2520. var _this = _super.call(this, cmp, enableIndex) || this;
  2521. var self = _this;
  2522. container.forEach(function (el) {
  2523. self.setElement(el[0], el[1]);
  2524. });
  2525. return _this;
  2526. }
  2527. /**
  2528. * @internal
  2529. */
  2530. OrderedMap.prototype._iterationFunc = function (curNode) {
  2531. return __generator(this, function (_a) {
  2532. switch (_a.label) {
  2533. case 0:
  2534. if (curNode === undefined) return [2 /*return*/];
  2535. return [5 /*yield**/, __values(this._iterationFunc(curNode._left))];
  2536. case 1:
  2537. _a.sent();
  2538. return [4 /*yield*/, [curNode._key, curNode._value]];
  2539. case 2:
  2540. _a.sent();
  2541. return [5 /*yield**/, __values(this._iterationFunc(curNode._right))];
  2542. case 3:
  2543. _a.sent();
  2544. return [2 /*return*/];
  2545. }
  2546. });
  2547. };
  2548. OrderedMap.prototype.begin = function () {
  2549. return new OrderedMapIterator(this._header._left || this._header, this._header, this);
  2550. };
  2551. OrderedMap.prototype.end = function () {
  2552. return new OrderedMapIterator(this._header, this._header, this);
  2553. };
  2554. OrderedMap.prototype.rBegin = function () {
  2555. return new OrderedMapIterator(this._header._right || this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  2556. };
  2557. OrderedMap.prototype.rEnd = function () {
  2558. return new OrderedMapIterator(this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  2559. };
  2560. OrderedMap.prototype.front = function () {
  2561. if (this._length === 0) return;
  2562. var minNode = this._header._left;
  2563. return [minNode._key, minNode._value];
  2564. };
  2565. OrderedMap.prototype.back = function () {
  2566. if (this._length === 0) return;
  2567. var maxNode = this._header._right;
  2568. return [maxNode._key, maxNode._value];
  2569. };
  2570. OrderedMap.prototype.lowerBound = function (key) {
  2571. var resNode = this._lowerBound(this._root, key);
  2572. return new OrderedMapIterator(resNode, this._header, this);
  2573. };
  2574. OrderedMap.prototype.upperBound = function (key) {
  2575. var resNode = this._upperBound(this._root, key);
  2576. return new OrderedMapIterator(resNode, this._header, this);
  2577. };
  2578. OrderedMap.prototype.reverseLowerBound = function (key) {
  2579. var resNode = this._reverseLowerBound(this._root, key);
  2580. return new OrderedMapIterator(resNode, this._header, this);
  2581. };
  2582. OrderedMap.prototype.reverseUpperBound = function (key) {
  2583. var resNode = this._reverseUpperBound(this._root, key);
  2584. return new OrderedMapIterator(resNode, this._header, this);
  2585. };
  2586. /**
  2587. * @description Insert a key-value pair or set value by the given key.
  2588. * @param key - The key want to insert.
  2589. * @param value - The value want to set.
  2590. * @param hint - You can give an iterator hint to improve insertion efficiency.
  2591. * @return The size of container after setting.
  2592. * @example
  2593. * const mp = new OrderedMap([[2, 0], [4, 0], [5, 0]]);
  2594. * const iter = mp.begin();
  2595. * mp.setElement(1, 0);
  2596. * mp.setElement(3, 0, iter); // give a hint will be faster.
  2597. */
  2598. OrderedMap.prototype.setElement = function (key, value, hint) {
  2599. return this._set(key, value, hint);
  2600. };
  2601. OrderedMap.prototype.find = function (key) {
  2602. var curNode = this._findElementNode(this._root, key);
  2603. return new OrderedMapIterator(curNode, this._header, this);
  2604. };
  2605. /**
  2606. * @description Get the value of the element of the specified key.
  2607. * @param key - The specified key you want to get.
  2608. * @example
  2609. * const val = container.getElementByKey(1);
  2610. */
  2611. OrderedMap.prototype.getElementByKey = function (key) {
  2612. var curNode = this._findElementNode(this._root, key);
  2613. return curNode._value;
  2614. };
  2615. OrderedMap.prototype.union = function (other) {
  2616. var self = this;
  2617. other.forEach(function (el) {
  2618. self.setElement(el[0], el[1]);
  2619. });
  2620. return this._length;
  2621. };
  2622. OrderedMap.prototype[Symbol.iterator] = function () {
  2623. return this._iterationFunc(this._root);
  2624. };
  2625. return OrderedMap;
  2626. }(TreeContainer);
  2627. /**
  2628. * @description Determine whether the type of key is `object`.
  2629. * @param key - The key want to check.
  2630. * @returns Whether the type of key is `object`.
  2631. * @internal
  2632. */
  2633. function checkObject(key) {
  2634. var t = typeof key;
  2635. return t === 'object' && key !== null || t === 'function';
  2636. }
  2637. var HashContainerIterator = /** @class */function (_super) {
  2638. __extends(HashContainerIterator, _super);
  2639. /**
  2640. * @internal
  2641. */
  2642. function HashContainerIterator(node, header, iteratorType) {
  2643. var _this = _super.call(this, iteratorType) || this;
  2644. _this._node = node;
  2645. _this._header = header;
  2646. if (_this.iteratorType === 0 /* IteratorType.NORMAL */) {
  2647. _this.pre = function () {
  2648. if (this._node._pre === this._header) {
  2649. throwIteratorAccessError();
  2650. }
  2651. this._node = this._node._pre;
  2652. return this;
  2653. };
  2654. _this.next = function () {
  2655. if (this._node === this._header) {
  2656. throwIteratorAccessError();
  2657. }
  2658. this._node = this._node._next;
  2659. return this;
  2660. };
  2661. } else {
  2662. _this.pre = function () {
  2663. if (this._node._next === this._header) {
  2664. throwIteratorAccessError();
  2665. }
  2666. this._node = this._node._next;
  2667. return this;
  2668. };
  2669. _this.next = function () {
  2670. if (this._node === this._header) {
  2671. throwIteratorAccessError();
  2672. }
  2673. this._node = this._node._pre;
  2674. return this;
  2675. };
  2676. }
  2677. return _this;
  2678. }
  2679. return HashContainerIterator;
  2680. }(ContainerIterator);
  2681. var HashContainer = /** @class */function (_super) {
  2682. __extends(HashContainer, _super);
  2683. /**
  2684. * @internal
  2685. */
  2686. function HashContainer() {
  2687. var _this = _super.call(this) || this;
  2688. /**
  2689. * @internal
  2690. */
  2691. _this._objMap = [];
  2692. /**
  2693. * @internal
  2694. */
  2695. _this._originMap = {};
  2696. /**
  2697. * @description Unique symbol used to tag object.
  2698. */
  2699. _this.HASH_TAG = Symbol('@@HASH_TAG');
  2700. Object.setPrototypeOf(_this._originMap, null);
  2701. _this._header = {};
  2702. _this._header._pre = _this._header._next = _this._head = _this._tail = _this._header;
  2703. return _this;
  2704. }
  2705. /**
  2706. * @internal
  2707. */
  2708. HashContainer.prototype._eraseNode = function (node) {
  2709. var _pre = node._pre,
  2710. _next = node._next;
  2711. _pre._next = _next;
  2712. _next._pre = _pre;
  2713. if (node === this._head) {
  2714. this._head = _next;
  2715. }
  2716. if (node === this._tail) {
  2717. this._tail = _pre;
  2718. }
  2719. this._length -= 1;
  2720. };
  2721. /**
  2722. * @internal
  2723. */
  2724. HashContainer.prototype._set = function (key, value, isObject) {
  2725. if (isObject === undefined) isObject = checkObject(key);
  2726. var newTail;
  2727. if (isObject) {
  2728. var index = key[this.HASH_TAG];
  2729. if (index !== undefined) {
  2730. this._objMap[index]._value = value;
  2731. return this._length;
  2732. }
  2733. Object.defineProperty(key, this.HASH_TAG, {
  2734. value: this._objMap.length,
  2735. configurable: true
  2736. });
  2737. newTail = {
  2738. _key: key,
  2739. _value: value,
  2740. _pre: this._tail,
  2741. _next: this._header
  2742. };
  2743. this._objMap.push(newTail);
  2744. } else {
  2745. var node = this._originMap[key];
  2746. if (node) {
  2747. node._value = value;
  2748. return this._length;
  2749. }
  2750. newTail = {
  2751. _key: key,
  2752. _value: value,
  2753. _pre: this._tail,
  2754. _next: this._header
  2755. };
  2756. this._originMap[key] = newTail;
  2757. }
  2758. if (this._length === 0) {
  2759. this._head = newTail;
  2760. this._header._next = newTail;
  2761. } else {
  2762. this._tail._next = newTail;
  2763. }
  2764. this._tail = newTail;
  2765. this._header._pre = newTail;
  2766. return ++this._length;
  2767. };
  2768. /**
  2769. * @internal
  2770. */
  2771. HashContainer.prototype._findElementNode = function (key, isObject) {
  2772. if (isObject === undefined) isObject = checkObject(key);
  2773. if (isObject) {
  2774. var index = key[this.HASH_TAG];
  2775. if (index === undefined) return this._header;
  2776. return this._objMap[index];
  2777. } else {
  2778. return this._originMap[key] || this._header;
  2779. }
  2780. };
  2781. HashContainer.prototype.clear = function () {
  2782. var HASH_TAG = this.HASH_TAG;
  2783. this._objMap.forEach(function (el) {
  2784. delete el._key[HASH_TAG];
  2785. });
  2786. this._objMap = [];
  2787. this._originMap = {};
  2788. Object.setPrototypeOf(this._originMap, null);
  2789. this._length = 0;
  2790. this._head = this._tail = this._header._pre = this._header._next = this._header;
  2791. };
  2792. /**
  2793. * @description Remove the element of the specified key.
  2794. * @param key - The key you want to remove.
  2795. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.<br/>
  2796. * If a `undefined` value is passed in, the type will be automatically judged.
  2797. * @returns Whether erase successfully.
  2798. */
  2799. HashContainer.prototype.eraseElementByKey = function (key, isObject) {
  2800. var node;
  2801. if (isObject === undefined) isObject = checkObject(key);
  2802. if (isObject) {
  2803. var index = key[this.HASH_TAG];
  2804. if (index === undefined) return false;
  2805. delete key[this.HASH_TAG];
  2806. node = this._objMap[index];
  2807. delete this._objMap[index];
  2808. } else {
  2809. node = this._originMap[key];
  2810. if (node === undefined) return false;
  2811. delete this._originMap[key];
  2812. }
  2813. this._eraseNode(node);
  2814. return true;
  2815. };
  2816. HashContainer.prototype.eraseElementByIterator = function (iter) {
  2817. var node = iter._node;
  2818. if (node === this._header) {
  2819. throwIteratorAccessError();
  2820. }
  2821. this._eraseNode(node);
  2822. return iter.next();
  2823. };
  2824. HashContainer.prototype.eraseElementByPos = function (pos) {
  2825. if (pos < 0 || pos > this._length - 1) {
  2826. throw new RangeError();
  2827. }
  2828. var node = this._head;
  2829. while (pos--) {
  2830. node = node._next;
  2831. }
  2832. this._eraseNode(node);
  2833. return this._length;
  2834. };
  2835. return HashContainer;
  2836. }(Container);
  2837. var HashSetIterator = /** @class */function (_super) {
  2838. __extends(HashSetIterator, _super);
  2839. function HashSetIterator(node, header, container, iteratorType) {
  2840. var _this = _super.call(this, node, header, iteratorType) || this;
  2841. _this.container = container;
  2842. return _this;
  2843. }
  2844. Object.defineProperty(HashSetIterator.prototype, "pointer", {
  2845. get: function () {
  2846. if (this._node === this._header) {
  2847. throwIteratorAccessError();
  2848. }
  2849. return this._node._key;
  2850. },
  2851. enumerable: false,
  2852. configurable: true
  2853. });
  2854. HashSetIterator.prototype.copy = function () {
  2855. return new HashSetIterator(this._node, this._header, this.container, this.iteratorType);
  2856. };
  2857. return HashSetIterator;
  2858. }(HashContainerIterator);
  2859. var HashSet = /** @class */function (_super) {
  2860. __extends(HashSet, _super);
  2861. function HashSet(container) {
  2862. if (container === void 0) {
  2863. container = [];
  2864. }
  2865. var _this = _super.call(this) || this;
  2866. var self = _this;
  2867. container.forEach(function (el) {
  2868. self.insert(el);
  2869. });
  2870. return _this;
  2871. }
  2872. HashSet.prototype.begin = function () {
  2873. return new HashSetIterator(this._head, this._header, this);
  2874. };
  2875. HashSet.prototype.end = function () {
  2876. return new HashSetIterator(this._header, this._header, this);
  2877. };
  2878. HashSet.prototype.rBegin = function () {
  2879. return new HashSetIterator(this._tail, this._header, this, 1 /* IteratorType.REVERSE */);
  2880. };
  2881. HashSet.prototype.rEnd = function () {
  2882. return new HashSetIterator(this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  2883. };
  2884. HashSet.prototype.front = function () {
  2885. return this._head._key;
  2886. };
  2887. HashSet.prototype.back = function () {
  2888. return this._tail._key;
  2889. };
  2890. /**
  2891. * @description Insert element to set.
  2892. * @param key - The key want to insert.
  2893. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.<br/>
  2894. * If a `undefined` value is passed in, the type will be automatically judged.
  2895. * @returns The size of container after inserting.
  2896. */
  2897. HashSet.prototype.insert = function (key, isObject) {
  2898. return this._set(key, undefined, isObject);
  2899. };
  2900. HashSet.prototype.getElementByPos = function (pos) {
  2901. if (pos < 0 || pos > this._length - 1) {
  2902. throw new RangeError();
  2903. }
  2904. var node = this._head;
  2905. while (pos--) {
  2906. node = node._next;
  2907. }
  2908. return node._key;
  2909. };
  2910. /**
  2911. * @description Check key if exist in container.
  2912. * @param key - The element you want to search.
  2913. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.<br/>
  2914. * If a `undefined` value is passed in, the type will be automatically judged.
  2915. * @returns An iterator pointing to the element if found, or super end if not found.
  2916. */
  2917. HashSet.prototype.find = function (key, isObject) {
  2918. var node = this._findElementNode(key, isObject);
  2919. return new HashSetIterator(node, this._header, this);
  2920. };
  2921. HashSet.prototype.forEach = function (callback) {
  2922. var index = 0;
  2923. var node = this._head;
  2924. while (node !== this._header) {
  2925. callback(node._key, index++, this);
  2926. node = node._next;
  2927. }
  2928. };
  2929. HashSet.prototype[Symbol.iterator] = function () {
  2930. return function () {
  2931. var node;
  2932. return __generator(this, function (_a) {
  2933. switch (_a.label) {
  2934. case 0:
  2935. node = this._head;
  2936. _a.label = 1;
  2937. case 1:
  2938. if (!(node !== this._header)) return [3 /*break*/, 3];
  2939. return [4 /*yield*/, node._key];
  2940. case 2:
  2941. _a.sent();
  2942. node = node._next;
  2943. return [3 /*break*/, 1];
  2944. case 3:
  2945. return [2 /*return*/];
  2946. }
  2947. });
  2948. }.bind(this)();
  2949. };
  2950. return HashSet;
  2951. }(HashContainer);
  2952. var HashMapIterator = /** @class */function (_super) {
  2953. __extends(HashMapIterator, _super);
  2954. function HashMapIterator(node, header, container, iteratorType) {
  2955. var _this = _super.call(this, node, header, iteratorType) || this;
  2956. _this.container = container;
  2957. return _this;
  2958. }
  2959. Object.defineProperty(HashMapIterator.prototype, "pointer", {
  2960. get: function () {
  2961. if (this._node === this._header) {
  2962. throwIteratorAccessError();
  2963. }
  2964. var self = this;
  2965. return new Proxy([], {
  2966. get: function (_, props) {
  2967. if (props === '0') return self._node._key;else if (props === '1') return self._node._value;
  2968. },
  2969. set: function (_, props, newValue) {
  2970. if (props !== '1') {
  2971. throw new TypeError('props must be 1');
  2972. }
  2973. self._node._value = newValue;
  2974. return true;
  2975. }
  2976. });
  2977. },
  2978. enumerable: false,
  2979. configurable: true
  2980. });
  2981. HashMapIterator.prototype.copy = function () {
  2982. return new HashMapIterator(this._node, this._header, this.container, this.iteratorType);
  2983. };
  2984. return HashMapIterator;
  2985. }(HashContainerIterator);
  2986. var HashMap = /** @class */function (_super) {
  2987. __extends(HashMap, _super);
  2988. function HashMap(container) {
  2989. if (container === void 0) {
  2990. container = [];
  2991. }
  2992. var _this = _super.call(this) || this;
  2993. var self = _this;
  2994. container.forEach(function (el) {
  2995. self.setElement(el[0], el[1]);
  2996. });
  2997. return _this;
  2998. }
  2999. HashMap.prototype.begin = function () {
  3000. return new HashMapIterator(this._head, this._header, this);
  3001. };
  3002. HashMap.prototype.end = function () {
  3003. return new HashMapIterator(this._header, this._header, this);
  3004. };
  3005. HashMap.prototype.rBegin = function () {
  3006. return new HashMapIterator(this._tail, this._header, this, 1 /* IteratorType.REVERSE */);
  3007. };
  3008. HashMap.prototype.rEnd = function () {
  3009. return new HashMapIterator(this._header, this._header, this, 1 /* IteratorType.REVERSE */);
  3010. };
  3011. HashMap.prototype.front = function () {
  3012. if (this._length === 0) return;
  3013. return [this._head._key, this._head._value];
  3014. };
  3015. HashMap.prototype.back = function () {
  3016. if (this._length === 0) return;
  3017. return [this._tail._key, this._tail._value];
  3018. };
  3019. /**
  3020. * @description Insert a key-value pair or set value by the given key.
  3021. * @param key - The key want to insert.
  3022. * @param value - The value want to set.
  3023. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.<br/>
  3024. * If a `undefined` value is passed in, the type will be automatically judged.
  3025. * @returns The size of container after setting.
  3026. */
  3027. HashMap.prototype.setElement = function (key, value, isObject) {
  3028. return this._set(key, value, isObject);
  3029. };
  3030. /**
  3031. * @description Get the value of the element of the specified key.
  3032. * @param key - The key want to search.
  3033. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.<br/>
  3034. * If a `undefined` value is passed in, the type will be automatically judged.
  3035. * @example
  3036. * const val = container.getElementByKey(1);
  3037. */
  3038. HashMap.prototype.getElementByKey = function (key, isObject) {
  3039. if (isObject === undefined) isObject = checkObject(key);
  3040. if (isObject) {
  3041. var index = key[this.HASH_TAG];
  3042. return index !== undefined ? this._objMap[index]._value : undefined;
  3043. }
  3044. var node = this._originMap[key];
  3045. return node ? node._value : undefined;
  3046. };
  3047. HashMap.prototype.getElementByPos = function (pos) {
  3048. if (pos < 0 || pos > this._length - 1) {
  3049. throw new RangeError();
  3050. }
  3051. var node = this._head;
  3052. while (pos--) {
  3053. node = node._next;
  3054. }
  3055. return [node._key, node._value];
  3056. };
  3057. /**
  3058. * @description Check key if exist in container.
  3059. * @param key - The element you want to search.
  3060. * @param isObject - Tell us if the type of inserted key is `object` to improve efficiency.<br/>
  3061. * If a `undefined` value is passed in, the type will be automatically judged.
  3062. * @returns An iterator pointing to the element if found, or super end if not found.
  3063. */
  3064. HashMap.prototype.find = function (key, isObject) {
  3065. var node = this._findElementNode(key, isObject);
  3066. return new HashMapIterator(node, this._header, this);
  3067. };
  3068. HashMap.prototype.forEach = function (callback) {
  3069. var index = 0;
  3070. var node = this._head;
  3071. while (node !== this._header) {
  3072. callback([node._key, node._value], index++, this);
  3073. node = node._next;
  3074. }
  3075. };
  3076. HashMap.prototype[Symbol.iterator] = function () {
  3077. return function () {
  3078. var node;
  3079. return __generator(this, function (_a) {
  3080. switch (_a.label) {
  3081. case 0:
  3082. node = this._head;
  3083. _a.label = 1;
  3084. case 1:
  3085. if (!(node !== this._header)) return [3 /*break*/, 3];
  3086. return [4 /*yield*/, [node._key, node._value]];
  3087. case 2:
  3088. _a.sent();
  3089. node = node._next;
  3090. return [3 /*break*/, 1];
  3091. case 3:
  3092. return [2 /*return*/];
  3093. }
  3094. });
  3095. }.bind(this)();
  3096. };
  3097. return HashMap;
  3098. }(HashContainer);
  3099. exports.Deque = Deque;
  3100. exports.HashMap = HashMap;
  3101. exports.HashSet = HashSet;
  3102. exports.LinkList = LinkList;
  3103. exports.OrderedMap = OrderedMap;
  3104. exports.OrderedSet = OrderedSet;
  3105. exports.PriorityQueue = PriorityQueue;
  3106. exports.Queue = Queue;
  3107. exports.Stack = Stack;
  3108. exports.Vector = Vector;
  3109. Object.defineProperty(exports, '__esModule', { value: true });
  3110. }));