123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795 |
- "use strict";
- Object.defineProperty(exports, "t", {
- value: true
- });
- class TreeNode {
- constructor(t, e, s = 1) {
- this.i = undefined;
- this.h = undefined;
- this.o = undefined;
- this.u = t;
- this.l = e;
- this.p = s;
- }
- I() {
- let t = this;
- const e = t.o.o === t;
- if (e && t.p === 1) {
- t = t.h;
- } else if (t.i) {
- t = t.i;
- while (t.h) {
- t = t.h;
- }
- } else {
- if (e) {
- return t.o;
- }
- let s = t.o;
- while (s.i === t) {
- t = s;
- s = t.o;
- }
- t = s;
- }
- return t;
- }
- B() {
- let t = this;
- if (t.h) {
- t = t.h;
- while (t.i) {
- t = t.i;
- }
- return t;
- } else {
- let e = t.o;
- while (e.h === t) {
- t = e;
- e = t.o;
- }
- if (t.h !== e) {
- return e;
- } else return t;
- }
- }
- _() {
- const t = this.o;
- const e = this.h;
- const s = e.i;
- if (t.o === this) t.o = e; else if (t.i === this) t.i = e; else t.h = e;
- e.o = t;
- e.i = this;
- this.o = e;
- this.h = s;
- if (s) s.o = this;
- return e;
- }
- g() {
- const t = this.o;
- const e = this.i;
- const s = e.h;
- if (t.o === this) t.o = e; else if (t.i === this) t.i = e; else t.h = e;
- e.o = t;
- e.h = this;
- this.o = e;
- this.i = s;
- if (s) s.o = this;
- return e;
- }
- }
- class TreeNodeEnableIndex extends TreeNode {
- constructor() {
- super(...arguments);
- this.M = 1;
- }
- _() {
- const t = super._();
- this.O();
- t.O();
- return t;
- }
- g() {
- const t = super.g();
- this.O();
- t.O();
- return t;
- }
- O() {
- this.M = 1;
- if (this.i) {
- this.M += this.i.M;
- }
- if (this.h) {
- this.M += this.h.M;
- }
- }
- }
- class ContainerIterator {
- constructor(t = 0) {
- this.iteratorType = t;
- }
- equals(t) {
- return this.T === t.T;
- }
- }
- class Base {
- constructor() {
- this.m = 0;
- }
- get length() {
- return this.m;
- }
- size() {
- return this.m;
- }
- empty() {
- return this.m === 0;
- }
- }
- class Container extends Base {}
- function throwIteratorAccessError() {
- throw new RangeError("Iterator access denied!");
- }
- class TreeContainer extends Container {
- constructor(t = function(t, e) {
- if (t < e) return -1;
- if (t > e) return 1;
- return 0;
- }, e = false) {
- super();
- this.v = undefined;
- this.A = t;
- this.enableIndex = e;
- this.N = e ? TreeNodeEnableIndex : TreeNode;
- this.C = new this.N;
- }
- R(t, e) {
- let s = this.C;
- while (t) {
- const i = this.A(t.u, e);
- if (i < 0) {
- t = t.h;
- } else if (i > 0) {
- s = t;
- t = t.i;
- } else return t;
- }
- return s;
- }
- K(t, e) {
- let s = this.C;
- while (t) {
- const i = this.A(t.u, e);
- if (i <= 0) {
- t = t.h;
- } else {
- s = t;
- t = t.i;
- }
- }
- return s;
- }
- L(t, e) {
- let s = this.C;
- while (t) {
- const i = this.A(t.u, e);
- if (i < 0) {
- s = t;
- t = t.h;
- } else if (i > 0) {
- t = t.i;
- } else return t;
- }
- return s;
- }
- k(t, e) {
- let s = this.C;
- while (t) {
- const i = this.A(t.u, e);
- if (i < 0) {
- s = t;
- t = t.h;
- } else {
- t = t.i;
- }
- }
- return s;
- }
- P(t) {
- while (true) {
- const e = t.o;
- if (e === this.C) return;
- if (t.p === 1) {
- t.p = 0;
- return;
- }
- if (t === e.i) {
- const s = e.h;
- if (s.p === 1) {
- s.p = 0;
- e.p = 1;
- if (e === this.v) {
- this.v = e._();
- } else e._();
- } else {
- if (s.h && s.h.p === 1) {
- s.p = e.p;
- e.p = 0;
- s.h.p = 0;
- if (e === this.v) {
- this.v = e._();
- } else e._();
- return;
- } else if (s.i && s.i.p === 1) {
- s.p = 1;
- s.i.p = 0;
- s.g();
- } else {
- s.p = 1;
- t = e;
- }
- }
- } else {
- const s = e.i;
- if (s.p === 1) {
- s.p = 0;
- e.p = 1;
- if (e === this.v) {
- this.v = e.g();
- } else e.g();
- } else {
- if (s.i && s.i.p === 1) {
- s.p = e.p;
- e.p = 0;
- s.i.p = 0;
- if (e === this.v) {
- this.v = e.g();
- } else e.g();
- return;
- } else if (s.h && s.h.p === 1) {
- s.p = 1;
- s.h.p = 0;
- s._();
- } else {
- s.p = 1;
- t = e;
- }
- }
- }
- }
- }
- S(t) {
- if (this.m === 1) {
- this.clear();
- return;
- }
- let e = t;
- while (e.i || e.h) {
- if (e.h) {
- e = e.h;
- while (e.i) e = e.i;
- } else {
- e = e.i;
- }
- const s = t.u;
- t.u = e.u;
- e.u = s;
- const i = t.l;
- t.l = e.l;
- e.l = i;
- t = e;
- }
- if (this.C.i === e) {
- this.C.i = e.o;
- } else if (this.C.h === e) {
- this.C.h = e.o;
- }
- this.P(e);
- let s = e.o;
- if (e === s.i) {
- s.i = undefined;
- } else s.h = undefined;
- this.m -= 1;
- this.v.p = 0;
- if (this.enableIndex) {
- while (s !== this.C) {
- s.M -= 1;
- s = s.o;
- }
- }
- }
- U(t) {
- const e = typeof t === "number" ? t : undefined;
- const s = typeof t === "function" ? t : undefined;
- const i = typeof t === "undefined" ? [] : undefined;
- let r = 0;
- let n = this.v;
- const h = [];
- while (h.length || n) {
- if (n) {
- h.push(n);
- n = n.i;
- } else {
- n = h.pop();
- if (r === e) return n;
- i && i.push(n);
- s && s(n, r, this);
- r += 1;
- n = n.h;
- }
- }
- return i;
- }
- j(t) {
- while (true) {
- const e = t.o;
- if (e.p === 0) return;
- const s = e.o;
- if (e === s.i) {
- const i = s.h;
- if (i && i.p === 1) {
- i.p = e.p = 0;
- if (s === this.v) return;
- s.p = 1;
- t = s;
- continue;
- } else if (t === e.h) {
- t.p = 0;
- if (t.i) {
- t.i.o = e;
- }
- if (t.h) {
- t.h.o = s;
- }
- e.h = t.i;
- s.i = t.h;
- t.i = e;
- t.h = s;
- if (s === this.v) {
- this.v = t;
- this.C.o = t;
- } else {
- const e = s.o;
- if (e.i === s) {
- e.i = t;
- } else e.h = t;
- }
- t.o = s.o;
- e.o = t;
- s.o = t;
- s.p = 1;
- } else {
- e.p = 0;
- if (s === this.v) {
- this.v = s.g();
- } else s.g();
- s.p = 1;
- return;
- }
- } else {
- const i = s.i;
- if (i && i.p === 1) {
- i.p = e.p = 0;
- if (s === this.v) return;
- s.p = 1;
- t = s;
- continue;
- } else if (t === e.i) {
- t.p = 0;
- if (t.i) {
- t.i.o = s;
- }
- if (t.h) {
- t.h.o = e;
- }
- s.h = t.i;
- e.i = t.h;
- t.i = s;
- t.h = e;
- if (s === this.v) {
- this.v = t;
- this.C.o = t;
- } else {
- const e = s.o;
- if (e.i === s) {
- e.i = t;
- } else e.h = t;
- }
- t.o = s.o;
- e.o = t;
- s.o = t;
- s.p = 1;
- } else {
- e.p = 0;
- if (s === this.v) {
- this.v = s._();
- } else s._();
- s.p = 1;
- return;
- }
- }
- if (this.enableIndex) {
- e.O();
- s.O();
- t.O();
- }
- return;
- }
- }
- q(t, e, s) {
- if (this.v === undefined) {
- this.m += 1;
- this.v = new this.N(t, e, 0);
- this.v.o = this.C;
- this.C.o = this.C.i = this.C.h = this.v;
- return this.m;
- }
- let i;
- const r = this.C.i;
- const n = this.A(r.u, t);
- if (n === 0) {
- r.l = e;
- return this.m;
- } else if (n > 0) {
- r.i = new this.N(t, e);
- r.i.o = r;
- i = r.i;
- this.C.i = i;
- } else {
- const r = this.C.h;
- const n = this.A(r.u, t);
- if (n === 0) {
- r.l = e;
- return this.m;
- } else if (n < 0) {
- r.h = new this.N(t, e);
- r.h.o = r;
- i = r.h;
- this.C.h = i;
- } else {
- if (s !== undefined) {
- const r = s.T;
- if (r !== this.C) {
- const s = this.A(r.u, t);
- if (s === 0) {
- r.l = e;
- return this.m;
- } else if (s > 0) {
- const s = r.I();
- const n = this.A(s.u, t);
- if (n === 0) {
- s.l = e;
- return this.m;
- } else if (n < 0) {
- i = new this.N(t, e);
- if (s.h === undefined) {
- s.h = i;
- i.o = s;
- } else {
- r.i = i;
- i.o = r;
- }
- }
- }
- }
- }
- if (i === undefined) {
- i = this.v;
- while (true) {
- const s = this.A(i.u, t);
- if (s > 0) {
- if (i.i === undefined) {
- i.i = new this.N(t, e);
- i.i.o = i;
- i = i.i;
- break;
- }
- i = i.i;
- } else if (s < 0) {
- if (i.h === undefined) {
- i.h = new this.N(t, e);
- i.h.o = i;
- i = i.h;
- break;
- }
- i = i.h;
- } else {
- i.l = e;
- return this.m;
- }
- }
- }
- }
- }
- if (this.enableIndex) {
- let t = i.o;
- while (t !== this.C) {
- t.M += 1;
- t = t.o;
- }
- }
- this.j(i);
- this.m += 1;
- return this.m;
- }
- H(t, e) {
- while (t) {
- const s = this.A(t.u, e);
- if (s < 0) {
- t = t.h;
- } else if (s > 0) {
- t = t.i;
- } else return t;
- }
- return t || this.C;
- }
- clear() {
- this.m = 0;
- this.v = undefined;
- this.C.o = undefined;
- this.C.i = this.C.h = undefined;
- }
- updateKeyByIterator(t, e) {
- const s = t.T;
- if (s === this.C) {
- throwIteratorAccessError();
- }
- if (this.m === 1) {
- s.u = e;
- return true;
- }
- const i = s.B().u;
- if (s === this.C.i) {
- if (this.A(i, e) > 0) {
- s.u = e;
- return true;
- }
- return false;
- }
- const r = s.I().u;
- if (s === this.C.h) {
- if (this.A(r, e) < 0) {
- s.u = e;
- return true;
- }
- return false;
- }
- if (this.A(r, e) >= 0 || this.A(i, e) <= 0) return false;
- s.u = e;
- return true;
- }
- eraseElementByPos(t) {
- if (t < 0 || t > this.m - 1) {
- throw new RangeError;
- }
- const e = this.U(t);
- this.S(e);
- return this.m;
- }
- eraseElementByKey(t) {
- if (this.m === 0) return false;
- const e = this.H(this.v, t);
- if (e === this.C) return false;
- this.S(e);
- return true;
- }
- eraseElementByIterator(t) {
- const e = t.T;
- if (e === this.C) {
- throwIteratorAccessError();
- }
- const s = e.h === undefined;
- const i = t.iteratorType === 0;
- if (i) {
- if (s) t.next();
- } else {
- if (!s || e.i === undefined) t.next();
- }
- this.S(e);
- return t;
- }
- getHeight() {
- if (this.m === 0) return 0;
- function traversal(t) {
- if (!t) return 0;
- return Math.max(traversal(t.i), traversal(t.h)) + 1;
- }
- return traversal(this.v);
- }
- }
- class TreeIterator extends ContainerIterator {
- constructor(t, e, s) {
- super(s);
- this.T = t;
- this.C = e;
- if (this.iteratorType === 0) {
- this.pre = function() {
- if (this.T === this.C.i) {
- throwIteratorAccessError();
- }
- this.T = this.T.I();
- return this;
- };
- this.next = function() {
- if (this.T === this.C) {
- throwIteratorAccessError();
- }
- this.T = this.T.B();
- return this;
- };
- } else {
- this.pre = function() {
- if (this.T === this.C.h) {
- throwIteratorAccessError();
- }
- this.T = this.T.B();
- return this;
- };
- this.next = function() {
- if (this.T === this.C) {
- throwIteratorAccessError();
- }
- this.T = this.T.I();
- return this;
- };
- }
- }
- get index() {
- let t = this.T;
- const e = this.C.o;
- if (t === this.C) {
- if (e) {
- return e.M - 1;
- }
- return 0;
- }
- let s = 0;
- if (t.i) {
- s += t.i.M;
- }
- while (t !== e) {
- const e = t.o;
- if (t === e.h) {
- s += 1;
- if (e.i) {
- s += e.i.M;
- }
- }
- t = e;
- }
- return s;
- }
- isAccessible() {
- return this.T !== this.C;
- }
- }
- class OrderedMapIterator extends TreeIterator {
- constructor(t, e, s, i) {
- super(t, e, i);
- this.container = s;
- }
- get pointer() {
- if (this.T === this.C) {
- throwIteratorAccessError();
- }
- const t = this;
- return new Proxy([], {
- get(e, s) {
- if (s === "0") return t.T.u; else if (s === "1") return t.T.l;
- e[0] = t.T.u;
- e[1] = t.T.l;
- return e[s];
- },
- set(e, s, i) {
- if (s !== "1") {
- throw new TypeError("prop must be 1");
- }
- t.T.l = i;
- return true;
- }
- });
- }
- copy() {
- return new OrderedMapIterator(this.T, this.C, this.container, this.iteratorType);
- }
- }
- class OrderedMap extends TreeContainer {
- constructor(t = [], e, s) {
- super(e, s);
- const i = this;
- t.forEach((function(t) {
- i.setElement(t[0], t[1]);
- }));
- }
- begin() {
- return new OrderedMapIterator(this.C.i || this.C, this.C, this);
- }
- end() {
- return new OrderedMapIterator(this.C, this.C, this);
- }
- rBegin() {
- return new OrderedMapIterator(this.C.h || this.C, this.C, this, 1);
- }
- rEnd() {
- return new OrderedMapIterator(this.C, this.C, this, 1);
- }
- front() {
- if (this.m === 0) return;
- const t = this.C.i;
- return [ t.u, t.l ];
- }
- back() {
- if (this.m === 0) return;
- const t = this.C.h;
- return [ t.u, t.l ];
- }
- lowerBound(t) {
- const e = this.R(this.v, t);
- return new OrderedMapIterator(e, this.C, this);
- }
- upperBound(t) {
- const e = this.K(this.v, t);
- return new OrderedMapIterator(e, this.C, this);
- }
- reverseLowerBound(t) {
- const e = this.L(this.v, t);
- return new OrderedMapIterator(e, this.C, this);
- }
- reverseUpperBound(t) {
- const e = this.k(this.v, t);
- return new OrderedMapIterator(e, this.C, this);
- }
- forEach(t) {
- this.U((function(e, s, i) {
- t([ e.u, e.l ], s, i);
- }));
- }
- setElement(t, e, s) {
- return this.q(t, e, s);
- }
- getElementByPos(t) {
- if (t < 0 || t > this.m - 1) {
- throw new RangeError;
- }
- const e = this.U(t);
- return [ e.u, e.l ];
- }
- find(t) {
- const e = this.H(this.v, t);
- return new OrderedMapIterator(e, this.C, this);
- }
- getElementByKey(t) {
- const e = this.H(this.v, t);
- return e.l;
- }
- union(t) {
- const e = this;
- t.forEach((function(t) {
- e.setElement(t[0], t[1]);
- }));
- return this.m;
- }
- * [Symbol.iterator]() {
- const t = this.m;
- const e = this.U();
- for (let s = 0; s < t; ++s) {
- const t = e[s];
- yield [ t.u, t.l ];
- }
- }
- }
- exports.OrderedMap = OrderedMap;
- //# sourceMappingURL=index.js.map
|