index.js 97 KB

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