UNPKG

6.97 kBSource Map (JSON)View Raw
1{"version":3,"file":"list.cjs","sources":["../list.js"],"sourcesContent":["import { id } from './function.js'\nimport * as error from './error.js'\n\nexport class ListNode {\n constructor () {\n /**\n * @type {this|null}\n */\n this.next = null\n /**\n * @type {this|null}\n */\n this.prev = null\n }\n}\n\n/**\n * @template {ListNode} N\n */\nexport class List {\n constructor () {\n /**\n * @type {N | null}\n */\n this.start = null\n /**\n * @type {N | null}\n */\n this.end = null\n this.len = 0\n }\n}\n\n/**\n * @note The queue implementation is experimental and unfinished.\n * Don't use this in production yet.\n *\n * @template {ListNode} N\n *\n * @return {List<N>}\n */\nexport const create = () => new List()\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} queue\n */\nexport const isEmpty = queue => queue.start === null\n\n/**\n * Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.\n *\n * @template {ListNode} N\n *\n * @param {List<N>} queue\n * @param {N} node\n */\nexport const remove = (queue, node) => {\n const prev = node.prev\n const next = node.next\n if (prev) {\n prev.next = next\n } else {\n queue.start = next\n }\n if (next) {\n next.prev = prev\n } else {\n queue.end = prev\n }\n queue.len--\n return node\n}\n\n/**\n * @deprecated @todo remove in next major release\n */\nexport const removeNode = remove\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} queue\n * @param {N| null} left\n * @param {N| null} right\n * @param {N} node\n */\nexport const insertBetween = (queue, left, right, node) => {\n /* c8 ignore start */\n if (left != null && left.next !== right) {\n throw error.unexpectedCase()\n }\n /* c8 ignore stop */\n if (left) {\n left.next = node\n } else {\n queue.start = node\n }\n if (right) {\n right.prev = node\n } else {\n queue.end = node\n }\n node.prev = left\n node.next = right\n queue.len++\n}\n\n/**\n * Remove a single node from the queue. Only works with Queues that operate on Doubly-linked lists of nodes.\n *\n * @template {ListNode} N\n *\n * @param {List<N>} queue\n * @param {N} node\n * @param {N} newNode\n */\nexport const replace = (queue, node, newNode) => {\n insertBetween(queue, node, node.next, newNode)\n remove(queue, node)\n}\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} queue\n * @param {N} n\n */\nexport const pushEnd = (queue, n) =>\n insertBetween(queue, queue.end, null, n)\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} queue\n * @param {N} n\n */\nexport const pushFront = (queue, n) =>\n insertBetween(queue, null, queue.start, n)\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} list\n * @return {N| null}\n */\nexport const popFront = list =>\n list.start ? removeNode(list, list.start) : null\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} list\n * @return {N| null}\n */\nexport const popEnd = list =>\n list.end ? removeNode(list, list.end) : null\n\n/**\n * @template {ListNode} N\n * @template M\n *\n * @param {List<N>} list\n * @param {function(N):M} f\n * @return {Array<M>}\n */\nexport const map = (list, f) => {\n /**\n * @type {Array<M>}\n */\n const arr = []\n let n = list.start\n while (n) {\n arr.push(f(n))\n n = n.next\n }\n return arr\n}\n\n/**\n * @template {ListNode} N\n *\n * @param {List<N>} list\n */\nexport const toArray = list => map(list, id)\n\n/**\n * @template {ListNode} N\n * @template M\n *\n * @param {List<N>} list\n * @param {function(N):M} f\n */\nexport const forEach = (list, f) => {\n let n = list.start\n while (n) {\n f(n)\n n = n.next\n }\n}\n"],"names":["error.unexpectedCase","id"],"mappings":";;;;;;;;;;AAGO,MAAM,QAAQ,CAAC;AACtB,EAAE,WAAW,CAAC,GAAG;AACjB;AACA;AACA;AACA,IAAI,IAAI,CAAC,IAAI,GAAG,KAAI;AACpB;AACA;AACA;AACA,IAAI,IAAI,CAAC,IAAI,GAAG,KAAI;AACpB,GAAG;AACH,CAAC;AACD;AACA;AACA;AACA;AACO,MAAM,IAAI,CAAC;AAClB,EAAE,WAAW,CAAC,GAAG;AACjB;AACA;AACA;AACA,IAAI,IAAI,CAAC,KAAK,GAAG,KAAI;AACrB;AACA;AACA;AACA,IAAI,IAAI,CAAC,GAAG,GAAG,KAAI;AACnB,IAAI,IAAI,CAAC,GAAG,GAAG,EAAC;AAChB,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