sonic-forest
Version:
High-performance (binary) tree and sorted map implementation (AVL, Splay, Radix, Red-Black)
80 lines (79 loc) • 2.44 kB
JavaScript
"use strict";
Object.defineProperty(exports, "__esModule", { value: true });
exports.OrderedMapIterator = void 0;
const util_1 = require("./util");
class OrderedMapIterator {
constructor(node, header, container, iteratorType = 0) {
this._node = node;
this._header = header;
this.iteratorType = iteratorType;
if (this.iteratorType === 0) {
this.pre = function () {
if (this._node === this._header.l) {
(0, util_1.throwIteratorAccessError)();
}
this._node = this._node.prev();
return this;
};
this.next = function () {
if (this._node === this._header) {
(0, util_1.throwIteratorAccessError)();
}
this._node = this._node.next();
return this;
};
}
else {
this.pre = function () {
if (this._node === this._header.r) {
(0, util_1.throwIteratorAccessError)();
}
this._node = this._node.next();
return this;
};
this.next = function () {
if (this._node === this._header) {
(0, util_1.throwIteratorAccessError)();
}
this._node = this._node.prev();
return this;
};
}
this.container = container;
}
get index() {
let _node = this._node;
const root = this._header.p;
if (_node === this._header) {
if (root) {
return root._size - 1;
}
return 0;
}
let index = 0;
if (_node.l) {
index += _node.l._size;
}
while (_node !== root) {
const _parent = _node.p;
if (_node === _parent.r) {
index += 1;
if (_parent.l) {
index += _parent.l._size;
}
}
_node = _parent;
}
return index;
}
isAccessible() {
return this._node !== this._header;
}
copy() {
return new OrderedMapIterator(this._node, this._header, this.container, this.iteratorType);
}
equals(iter) {
return this._node === iter._node;
}
}
exports.OrderedMapIterator = OrderedMapIterator;