IOTConnect-Web/node_modules/js-sdsl/dist/cjs/container/SequentialContainer/Deque.js.map
2024-05-09 09:49:52 +08:00

1 line
33 KiB
Plaintext

{"version":3,"sources":["container/SequentialContainer/Deque.js","../../src/container/SequentialContainer/Deque.ts"],"names":["Object","defineProperty","exports","value","default","_Base","_interopRequireDefault","require","_RandomIterator","obj","__esModule","DequeIterator","RandomIterator","constructor","node","container","iteratorType","super","this","copy","_node","Deque","SequentialContainer","_bucketSize","_first","_curFirst","_last","_curLast","_bucketNum","_map","_length","length","size","TypeError","Math","max","ceil","i","push","Array","needBucketNum","self","forEach","element","pushBack","_reAllocate","newMap","addBucketNum","_getElementIndex","pos","offset","offsetRemainder","curNodePointerIndex","curNodeBucketIndex","clear","begin","end","rBegin","rEnd","front","back","popBack","pushFront","popFront","getElementByPos","RangeError","setElementByPos","insert","num","arr","cut","eraseElementByPos","el","eraseElementByValue","eraseElementByIterator","iter","next","find","reverse","l","r","tmp","unique","index","pre","cur","sort","cmp","shrinkToFit","callback","Symbol","iterator","bind","_default"],"mappings":"AAAA;;AAEAA,OAAOC,eAAeC,SAAS,KAAc;IAC3CC,OAAO;;;AAETD,QAAQE,eAAe;;ACLvB,IAAAC,QAAAC,uBAAAC,QAAA;;AAEA,IAAAC,kBAAAD,QAAA;;AAAqF,SAAAD,uBAAAG;IAAA,OAAAA,KAAAA,EAAAC,IAAAD,IAAA;QAAAL,SAAAK;;AAAA;;AAIrF,MAAME,sBAAyBC,gBAAAA;IAE7BC,YAAYC,GAAcC,GAAqBC;QAC7CC,MAAMH,GAAME;QACZE,KAAKH,YAAYA;ADFjB;ICIFI;QACE,OAAO,IAAIR,cAAiBO,KAAKE,GAAOF,KAAKH,WAAWG,KAAKF;ADF7D;;;ACUJ,MAAMK,cAAiBC,MAAAA;IA6BrBT,YAAYE,IAA8B,IAAIQ,IAAe,KAAK;QAChEN;QA1BMC,KAAAM,IAAS;QAITN,KAAAO,IAAY;QAIZP,KAAAQ,IAAQ;QAIRR,KAAAS,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Deque.js","sourcesContent":["import SequentialContainer from './Base';\nimport { RandomIterator } from \"./Base/RandomIterator\";\nimport $checkWithinAccessParams from \"../../utils/checkParams.macro\";\nimport $getContainerSize from \"../../utils/getContainerSize.macro\";\nclass DequeIterator extends RandomIterator {\n constructor(node, container, iteratorType) {\n super(node, iteratorType);\n this.container = container;\n }\n copy() {\n return new DequeIterator(this._node, this.container, this.iteratorType);\n }\n}\nclass Deque extends SequentialContainer {\n constructor(container = [], _bucketSize = (1 << 12)) {\n super();\n /**\n * @internal\n */\n this._first = 0;\n /**\n * @internal\n */\n this._curFirst = 0;\n /**\n * @internal\n */\n this._last = 0;\n /**\n * @internal\n */\n this._curLast = 0;\n /**\n * @internal\n */\n this._bucketNum = 0;\n /**\n * @internal\n */\n this._map = [];\n const _length = (() => {\n if (typeof container.length === \"number\")\n return container.length;\n if (typeof container.size === \"number\")\n return container.size;\n if (typeof container.size === \"function\")\n return container.size();\n throw new TypeError(\"Cannot get the length or size of the container\");\n })();\n this._bucketSize = _bucketSize;\n this._bucketNum = Math.max(Math.ceil(_length / this._bucketSize), 1);\n for (let i = 0; i < this._bucketNum; ++i) {\n this._map.push(new Array(this._bucketSize));\n }\n const needBucketNum = Math.ceil(_length / this._bucketSize);\n this._first = this._last = (this._bucketNum >> 1) - (needBucketNum >> 1);\n this._curFirst = this._curLast = (this._bucketSize - _length % this._bucketSize) >> 1;\n const self = this;\n container.forEach(function (element) {\n self.pushBack(element);\n });\n }\n /**\n * @description Growth the Deque.\n * @internal\n */\n _reAllocate() {\n const newMap = [];\n const addBucketNum = Math.max(this._bucketNum >> 1, 1);\n for (let i = 0; i < addBucketNum; ++i) {\n newMap[i] = new Array(this._bucketSize);\n }\n for (let i = this._first; i < this._bucketNum; ++i) {\n newMap[newMap.length] = this._map[i];\n }\n for (let i = 0; i < this._last; ++i) {\n newMap[newMap.length] = this._map[i];\n }\n newMap[newMap.length] = [...this._map[this._last]];\n this._first = addBucketNum;\n this._last = newMap.length - 1;\n for (let i = 0; i < addBucketNum; ++i) {\n newMap[newMap.length] = new Array(this._bucketSize);\n }\n this._map = newMap;\n this._bucketNum = newMap.length;\n }\n /**\n * @description Get the bucket position of the element and the pointer position by index.\n * @param pos - The element's index.\n * @internal\n */\n _getElementIndex(pos) {\n const offset = this._curFirst + pos + 1;\n const offsetRemainder = offset % this._bucketSize;\n let curNodePointerIndex = offsetRemainder - 1;\n let curNodeBucketIndex = this._first + (offset - offsetRemainder) / this._bucketSize;\n if (offsetRemainder === 0)\n curNodeBucketIndex -= 1;\n curNodeBucketIndex %= this._bucketNum;\n if (curNodePointerIndex < 0)\n curNodePointerIndex += this._bucketSize;\n return { curNodeBucketIndex, curNodePointerIndex };\n }\n clear() {\n this._map = [new Array(this._bucketSize)];\n this._bucketNum = 1;\n this._first = this._last = this._length = 0;\n this._curFirst = this._curLast = this._bucketSize >> 1;\n }\n begin() {\n return new DequeIterator(0, this);\n }\n end() {\n return new DequeIterator(this._length, this);\n }\n rBegin() {\n return new DequeIterator(this._length - 1, this, 1 /* IteratorType.REVERSE */);\n }\n rEnd() {\n return new DequeIterator(-1, this, 1 /* IteratorType.REVERSE */);\n }\n front() {\n if (this._length === 0)\n return;\n return this._map[this._first][this._curFirst];\n }\n back() {\n if (this._length === 0)\n return;\n return this._map[this._last][this._curLast];\n }\n pushBack(element) {\n if (this._length) {\n if (this._curLast < this._bucketSize - 1) {\n this._curLast += 1;\n }\n else if (this._last < this._bucketNum - 1) {\n this._last += 1;\n this._curLast = 0;\n }\n else {\n this._last = 0;\n this._curLast = 0;\n }\n if (this._last === this._first &&\n this._curLast === this._curFirst)\n this._reAllocate();\n }\n this._length += 1;\n this._map[this._last][this._curLast] = element;\n return this._length;\n }\n popBack() {\n if (this._length === 0)\n return;\n const value = this._map[this._last][this._curLast];\n if (this._length !== 1) {\n if (this._curLast > 0) {\n this._curLast -= 1;\n }\n else if (this._last > 0) {\n this._last -= 1;\n this._curLast = this._bucketSize - 1;\n }\n else {\n this._last = this._bucketNum - 1;\n this._curLast = this._bucketSize - 1;\n }\n }\n this._length -= 1;\n return value;\n }\n /**\n * @description Push the element to the front.\n * @param element - The element you want to push.\n * @returns The size of queue after pushing.\n */\n pushFront(element) {\n if (this._length) {\n if (this._curFirst > 0) {\n this._curFirst -= 1;\n }\n else if (this._first > 0) {\n this._first -= 1;\n this._curFirst = this._bucketSize - 1;\n }\n else {\n this._first = this._bucketNum - 1;\n this._curFirst = this._bucketSize - 1;\n }\n if (this._first === this._last &&\n this._curFirst === this._curLast)\n this._reAllocate();\n }\n this._length += 1;\n this._map[this._first][this._curFirst] = element;\n return this._length;\n }\n /**\n * @description Remove the _first element.\n * @returns The element you popped.\n */\n popFront() {\n if (this._length === 0)\n return;\n const value = this._map[this._first][this._curFirst];\n if (this._length !== 1) {\n if (this._curFirst < this._bucketSize - 1) {\n this._curFirst += 1;\n }\n else if (this._first < this._bucketNum - 1) {\n this._first += 1;\n this._curFirst = 0;\n }\n else {\n this._first = 0;\n this._curFirst = 0;\n }\n }\n this._length -= 1;\n return value;\n }\n getElementByPos(pos) {\n if (pos < 0 || pos > this._length - 1) {\n throw new RangeError();\n }\n const { curNodeBucketIndex, curNodePointerIndex } = this._getElementIndex(pos);\n return this._map[curNodeBucketIndex][curNodePointerIndex];\n }\n setElementByPos(pos, element) {\n if (pos < 0 || pos > this._length - 1) {\n throw new RangeError();\n }\n const { curNodeBucketIndex, curNodePointerIndex } = this._getElementIndex(pos);\n this._map[curNodeBucketIndex][curNodePointerIndex] = element;\n }\n insert(pos, element, num = 1) {\n if (pos < 0 || pos > this._length) {\n throw new RangeError();\n }\n if (pos === 0) {\n while (num--)\n this.pushFront(element);\n }\n else if (pos === this._length) {\n while (num--)\n this.pushBack(element);\n }\n else {\n const arr = [];\n for (let i = pos; i < this._length; ++i) {\n arr.push(this.getElementByPos(i));\n }\n this.cut(pos - 1);\n for (let i = 0; i < num; ++i)\n this.pushBack(element);\n for (let i = 0; i < arr.length; ++i)\n this.pushBack(arr[i]);\n }\n return this._length;\n }\n /**\n * @description Remove all elements after the specified position (excluding the specified position).\n * @param pos - The previous position of the first removed element.\n * @returns The size of the container after cutting.\n * @example\n * deque.cut(1); // Then deque's size will be 2. deque -> [0, 1]\n */\n cut(pos) {\n if (pos < 0) {\n this.clear();\n return 0;\n }\n const { curNodeBucketIndex, curNodePointerIndex } = this._getElementIndex(pos);\n this._last = curNodeBucketIndex;\n this._curLast = curNodePointerIndex;\n this._length = pos + 1;\n return this._length;\n }\n eraseElementByPos(pos) {\n if (pos < 0 || pos > this._length - 1) {\n throw new RangeError();\n }\n if (pos === 0)\n this.popFront();\n else if (pos === this._length - 1)\n this.popBack();\n else {\n const arr = [];\n for (let i = pos + 1; i < this._length; ++i) {\n arr.push(this.getElementByPos(i));\n }\n this.cut(pos);\n this.popBack();\n const self = this;\n arr.forEach(function (el) {\n self.pushBack(el);\n });\n }\n return this._length;\n }\n eraseElementByValue(value) {\n if (this._length === 0)\n return 0;\n const arr = [];\n for (let i = 0; i < this._length; ++i) {\n const element = this.getElementByPos(i);\n if (element !== value)\n arr.push(element);\n }\n const _length = arr.length;\n for (let i = 0; i < _length; ++i)\n this.setElementByPos(i, arr[i]);\n return this.cut(_length - 1);\n }\n eraseElementByIterator(iter) {\n const _node = iter._node;\n this.eraseElementByPos(_node);\n iter = iter.next();\n return iter;\n }\n find(element) {\n for (let i = 0; i < this._length; ++i) {\n if (this.getElementByPos(i) === element) {\n return new DequeIterator(i, this);\n }\n }\n return this.end();\n }\n reverse() {\n let l = 0;\n let r = this._length - 1;\n while (l < r) {\n const tmp = this.getElementByPos(l);\n this.setElementByPos(l, this.getElementByPos(r));\n this.setElementByPos(r, tmp);\n l += 1;\n r -= 1;\n }\n }\n unique() {\n if (this._length <= 1) {\n return this._length;\n }\n let index = 1;\n let pre = this.getElementByPos(0);\n for (let i = 1; i < this._length; ++i) {\n const cur = this.getElementByPos(i);\n if (cur !== pre) {\n pre = cur;\n this.setElementByPos(index++, cur);\n }\n }\n while (this._length > index)\n this.popBack();\n return this._length;\n }\n sort(cmp) {\n const arr = [];\n for (let i = 0; i < this._length; ++i) {\n arr.push(this.getElementByPos(i));\n }\n arr.sort(cmp);\n for (let i = 0; i < this._length; ++i)\n this.setElementByPos(i, arr[i]);\n }\n /**\n * @description Remove as much useless space as possible.\n */\n shrinkToFit() {\n if (this._length === 0)\n return;\n const arr = [];\n this.forEach(function (el) {\n arr.push(el);\n });\n this._bucketNum = Math.max(Math.ceil(this._length / this._bucketSize), 1);\n this._length = this._first = this._last = this._curFirst = this._curLast = 0;\n this._map = [];\n for (let i = 0; i < this._bucketNum; ++i) {\n this._map.push(new Array(this._bucketSize));\n }\n for (let i = 0; i < arr.length; ++i)\n this.pushBack(arr[i]);\n }\n forEach(callback) {\n for (let i = 0; i < this._length; ++i) {\n callback(this.getElementByPos(i), i, this);\n }\n }\n [Symbol.iterator]() {\n return function* () {\n for (let i = 0; i < this._length; ++i) {\n yield this.getElementByPos(i);\n }\n }.bind(this)();\n }\n}\nexport default Deque;\n","import SequentialContainer from './Base';\nimport { IteratorType, initContainer } from '@/container/ContainerBase';\nimport { RandomIterator } from '@/container/SequentialContainer/Base/RandomIterator';\nimport $checkWithinAccessParams from '@/utils/checkParams.macro';\nimport $getContainerSize from '@/utils/getContainerSize.macro';\n\nclass DequeIterator<T> extends RandomIterator<T> {\n readonly container: Deque<T>;\n constructor(node: number, container: Deque<T>, iteratorType?: IteratorType) {\n super(node, iteratorType);\n this.container = container;\n }\n copy() {\n return new DequeIterator<T>(this._node, this.container, this.iteratorType);\n }\n // @ts-ignore\n equals(iter: DequeIterator<T>): boolean;\n}\n\nexport type { DequeIterator };\n\nclass Deque<T> extends SequentialContainer<T> {\n /**\n * @internal\n */\n private _first = 0;\n /**\n * @internal\n */\n private _curFirst = 0;\n /**\n * @internal\n */\n private _last = 0;\n /**\n * @internal\n */\n private _curLast = 0;\n /**\n * @internal\n */\n private _bucketNum = 0;\n /**\n * @internal\n */\n private readonly _bucketSize: number;\n /**\n * @internal\n */\n private _map: T[][] = [];\n constructor(container: initContainer<T> = [], _bucketSize = (1 << 12)) {\n super();\n const _length = $getContainerSize!(container);\n this._bucketSize = _bucketSize;\n this._bucketNum = Math.max(Math.ceil(_length / this._bucketSize), 1);\n for (let i = 0; i < this._bucketNum; ++i) {\n this._map.push(new Array(this._bucketSize));\n }\n const needBucketNum = Math.ceil(_length / this._bucketSize);\n this._first = this._last = (this._bucketNum >> 1) - (needBucketNum >> 1);\n this._curFirst = this._curLast = (this._bucketSize - _length % this._bucketSize) >> 1;\n const self = this;\n container.forEach(function (element) {\n self.pushBack(element);\n });\n }\n /**\n * @description Growth the Deque.\n * @internal\n */\n private _reAllocate() {\n const newMap = [];\n const addBucketNum = Math.max(this._bucketNum >> 1, 1);\n for (let i = 0; i < addBucketNum; ++i) {\n newMap[i] = new Array(this._bucketSize);\n }\n for (let i = this._first; i < this._bucketNum; ++i) {\n newMap[newMap.length] = this._map[i];\n }\n for (let i = 0; i < this._last; ++i) {\n newMap[newMap.length] = this._map[i];\n }\n newMap[newMap.length] = [...this._map[this._last]];\n this._first = addBucketNum;\n this._last = newMap.length - 1;\n for (let i = 0; i < addBucketNum; ++i) {\n newMap[newMap.length] = new Array(this._bucketSize);\n }\n this._map = newMap;\n this._bucketNum = newMap.length;\n }\n /**\n * @description Get the bucket position of the element and the pointer position by index.\n * @param pos - The element's index.\n * @internal\n */\n private _getElementIndex(pos: number) {\n const offset = this._curFirst + pos + 1;\n const offsetRemainder = offset % this._bucketSize;\n let curNodePointerIndex = offsetRemainder - 1;\n let curNodeBucketIndex = this._first + (offset - offsetRemainder) / this._bucketSize;\n if (offsetRemainder === 0) curNodeBucketIndex -= 1;\n curNodeBucketIndex %= this._bucketNum;\n if (curNodePointerIndex < 0) curNodePointerIndex += this._bucketSize;\n return { curNodeBucketIndex, curNodePointerIndex };\n }\n clear() {\n this._map = [new Array(this._bucketSize)];\n this._bucketNum = 1;\n this._first = this._last = this._length = 0;\n this._curFirst = this._curLast = this._bucketSize >> 1;\n }\n begin() {\n return new DequeIterator<T>(0, this);\n }\n end() {\n return new DequeIterator<T>(this._length, this);\n }\n rBegin() {\n return new DequeIterator<T>(this._length - 1, this, IteratorType.REVERSE);\n }\n rEnd() {\n return new DequeIterator<T>(-1, this, IteratorType.REVERSE);\n }\n front(): T | undefined {\n if (this._length === 0) return;\n return this._map[this._first][this._curFirst];\n }\n back(): T | undefined {\n if (this._length === 0) return;\n return this._map[this._last][this._curLast];\n }\n pushBack(element: T) {\n if (this._length) {\n if (this._curLast < this._bucketSize - 1) {\n this._curLast += 1;\n } else if (this._last < this._bucketNum - 1) {\n this._last += 1;\n this._curLast = 0;\n } else {\n this._last = 0;\n this._curLast = 0;\n }\n if (\n this._last === this._first &&\n this._curLast === this._curFirst\n ) this._reAllocate();\n }\n this._length += 1;\n this._map[this._last][this._curLast] = element;\n return this._length;\n }\n popBack() {\n if (this._length === 0) return;\n const value = this._map[this._last][this._curLast];\n if (this._length !== 1) {\n if (this._curLast > 0) {\n this._curLast -= 1;\n } else if (this._last > 0) {\n this._last -= 1;\n this._curLast = this._bucketSize - 1;\n } else {\n this._last = this._bucketNum - 1;\n this._curLast = this._bucketSize - 1;\n }\n }\n this._length -= 1;\n return value;\n }\n /**\n * @description Push the element to the front.\n * @param element - The element you want to push.\n * @returns The size of queue after pushing.\n */\n pushFront(element: T) {\n if (this._length) {\n if (this._curFirst > 0) {\n this._curFirst -= 1;\n } else if (this._first > 0) {\n this._first -= 1;\n this._curFirst = this._bucketSize - 1;\n } else {\n this._first = this._bucketNum - 1;\n this._curFirst = this._bucketSize - 1;\n }\n if (\n this._first === this._last &&\n this._curFirst === this._curLast\n ) this._reAllocate();\n }\n this._length += 1;\n this._map[this._first][this._curFirst] = element;\n return this._length;\n }\n /**\n * @description Remove the _first element.\n * @returns The element you popped.\n */\n popFront() {\n if (this._length === 0) return;\n const value = this._map[this._first][this._curFirst];\n if (this._length !== 1) {\n if (this._curFirst < this._bucketSize - 1) {\n this._curFirst += 1;\n } else if (this._first < this._bucketNum - 1) {\n this._first += 1;\n this._curFirst = 0;\n } else {\n this._first = 0;\n this._curFirst = 0;\n }\n }\n this._length -= 1;\n return value;\n }\n getElementByPos(pos: number): T {\n $checkWithinAccessParams!(pos, 0, this._length - 1);\n const {\n curNodeBucketIndex,\n curNodePointerIndex\n } = this._getElementIndex(pos);\n return this._map[curNodeBucketIndex][curNodePointerIndex]!;\n }\n setElementByPos(pos: number, element: T) {\n $checkWithinAccessParams!(pos, 0, this._length - 1);\n const {\n curNodeBucketIndex,\n curNodePointerIndex\n } = this._getElementIndex(pos);\n this._map[curNodeBucketIndex][curNodePointerIndex] = element;\n }\n insert(pos: number, element: T, num = 1) {\n $checkWithinAccessParams!(pos, 0, this._length);\n if (pos === 0) {\n while (num--) this.pushFront(element);\n } else if (pos === this._length) {\n while (num--) this.pushBack(element);\n } else {\n const arr: T[] = [];\n for (let i = pos; i < this._length; ++i) {\n arr.push(this.getElementByPos(i));\n }\n this.cut(pos - 1);\n for (let i = 0; i < num; ++i) this.pushBack(element);\n for (let i = 0; i < arr.length; ++i) this.pushBack(arr[i]);\n }\n return this._length;\n }\n /**\n * @description Remove all elements after the specified position (excluding the specified position).\n * @param pos - The previous position of the first removed element.\n * @returns The size of the container after cutting.\n * @example\n * deque.cut(1); // Then deque's size will be 2. deque -> [0, 1]\n */\n cut(pos: number) {\n if (pos < 0) {\n this.clear();\n return 0;\n }\n const {\n curNodeBucketIndex,\n curNodePointerIndex\n } = this._getElementIndex(pos);\n this._last = curNodeBucketIndex;\n this._curLast = curNodePointerIndex;\n this._length = pos + 1;\n return this._length;\n }\n eraseElementByPos(pos: number) {\n $checkWithinAccessParams!(pos, 0, this._length - 1);\n if (pos === 0) this.popFront();\n else if (pos === this._length - 1) this.popBack();\n else {\n const arr = [];\n for (let i = pos + 1; i < this._length; ++i) {\n arr.push(this.getElementByPos(i));\n }\n this.cut(pos);\n this.popBack();\n const self = this;\n arr.forEach(function (el) {\n self.pushBack(el);\n });\n }\n return this._length;\n }\n eraseElementByValue(value: T) {\n if (this._length === 0) return 0;\n const arr: T[] = [];\n for (let i = 0; i < this._length; ++i) {\n const element = this.getElementByPos(i);\n if (element !== value) arr.push(element);\n }\n const _length = arr.length;\n for (let i = 0; i < _length; ++i) this.setElementByPos(i, arr[i]);\n return this.cut(_length - 1);\n }\n eraseElementByIterator(iter: DequeIterator<T>) {\n const _node = iter._node;\n this.eraseElementByPos(_node);\n iter = iter.next();\n return iter;\n }\n find(element: T) {\n for (let i = 0; i < this._length; ++i) {\n if (this.getElementByPos(i) === element) {\n return new DequeIterator<T>(i, this);\n }\n }\n return this.end();\n }\n reverse() {\n let l = 0; let r = this._length - 1;\n while (l < r) {\n const tmp = this.getElementByPos(l);\n this.setElementByPos(l, this.getElementByPos(r));\n this.setElementByPos(r, tmp);\n l += 1;\n r -= 1;\n }\n }\n unique() {\n if (this._length <= 1) {\n return this._length;\n }\n let index = 1;\n let pre = this.getElementByPos(0);\n for (let i = 1; i < this._length; ++i) {\n const cur = this.getElementByPos(i);\n if (cur !== pre) {\n pre = cur;\n this.setElementByPos(index++, cur);\n }\n }\n while (this._length > index) this.popBack();\n return this._length;\n }\n sort(cmp?: (x: T, y: T) => number) {\n const arr: T[] = [];\n for (let i = 0; i < this._length; ++i) {\n arr.push(this.getElementByPos(i));\n }\n arr.sort(cmp);\n for (let i = 0; i < this._length; ++i) this.setElementByPos(i, arr[i]);\n }\n /**\n * @description Remove as much useless space as possible.\n */\n shrinkToFit() {\n if (this._length === 0) return;\n const arr: T[] = [];\n this.forEach(function (el) {\n arr.push(el);\n });\n this._bucketNum = Math.max(Math.ceil(this._length / this._bucketSize), 1);\n this._length = this._first = this._last = this._curFirst = this._curLast = 0;\n this._map = [];\n for (let i = 0; i < this._bucketNum; ++i) {\n this._map.push(new Array(this._bucketSize));\n }\n for (let i = 0; i < arr.length; ++i) this.pushBack(arr[i]);\n }\n forEach(callback: (element: T, index: number, deque: Deque<T>) => void) {\n for (let i = 0; i < this._length; ++i) {\n callback(this.getElementByPos(i), i, this);\n }\n }\n [Symbol.iterator]() {\n return function * (this: Deque<T>) {\n for (let i = 0; i < this._length; ++i) {\n yield this.getElementByPos(i);\n }\n }.bind(this)();\n }\n}\n\nexport default Deque;\n"]}