js-sdsl.js 118 KB

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